Files
firefox/toolkit/components/thumbnails/PageThumbsProtocol.cpp
T
Gabriele Svelto f03cdb2b2d Bug 1600545 - Remove useless inclusions of header files generated from IDL files in modules/, netwerk/, parser/, security/, startupcache/, storage/, toolkit/, tools/, uriloader/, widget/, xpcom/ and xpfe/ r=Ehsan
The inclusions were removed with the following very crude script and the
resulting breakage was fixed up by hand. The manual fixups did either
revert the changes done by the script, replace a generic header with a more
specific one or replace a header with a forward declaration.

find . -name "*.idl" | grep -v web-platform | grep -v third_party | while read path; do
    interfaces=$(grep "^\(class\|interface\).*:.*" "$path" | cut -d' ' -f2)
    if [ -n "$interfaces" ]; then
        if [[ "$interfaces" == *$'\n'* ]]; then
          regexp="\("
          for i in $interfaces; do regexp="$regexp$i\|"; done
          regexp="${regexp%%\\\|}\)"
        else
          regexp="$interfaces"
        fi
        interface=$(basename "$path")
        rg -l "#include.*${interface%%.idl}.h" . | while read path2; do
            hits=$(grep -v "#include.*${interface%%.idl}.h" "$path2" | grep -c "$regexp" )
            if [ $hits -eq 0 ]; then
                echo "Removing ${interface} from ${path2}"
                grep -v "#include.*${interface%%.idl}.h" "$path2" > "$path2".tmp
                mv -f "$path2".tmp "$path2"
            fi
        done
    fi
done

Differential Revision: https://phabricator.services.mozilla.com/D55444
2019-12-06 09:17:57 +00:00

159 lines
4.8 KiB
C++

//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Implementation of moz-page-thumb protocol. This accesses and displays
* screenshots for URLs that are stored in the profile directory.
*/
#include "nsIPageThumbsStorageService.h"
#include "PageThumbsProtocol.h"
#include "nsIURI.h"
#include "nsIFile.h"
#include "nsIChannel.h"
#include "nsComponentManagerUtils.h"
#include "nsNetUtil.h"
#include "mozilla/dom/URLSearchParams.h"
#include "nsStandardURL.h"
using mozilla::dom::URLParams;
using mozilla::net::nsStandardURL;
NS_IMPL_ISUPPORTS(PageThumbsProtocol, nsIProtocolHandler);
// PageThumbsProtocol::GetScheme
NS_IMETHODIMP
PageThumbsProtocol::GetScheme(nsACString& aScheme) {
aScheme.AssignLiteral("moz-page-thumb");
return NS_OK;
}
// PageThumbsProtocol::GetDefaultPort
NS_IMETHODIMP
PageThumbsProtocol::GetDefaultPort(int32_t* aDefaultPort) {
*aDefaultPort = -1;
return NS_OK;
}
// PageThumbsProtocol::GetProtocolFlags
NS_IMETHODIMP
PageThumbsProtocol::GetProtocolFlags(uint32_t* aProtocolFlags) {
*aProtocolFlags = (URI_DANGEROUS_TO_LOAD | URI_IS_LOCAL_RESOURCE |
URI_NORELATIVE | URI_NOAUTH);
return NS_OK;
}
// PageThumbsProtocol::NewChannel
NS_IMETHODIMP
PageThumbsProtocol::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
nsIChannel** _retval) {
// Get the file path for the URL
nsCOMPtr<nsIFile> filePath;
nsresult rv = GetFilePathForURL(aURI, getter_AddRefs(filePath));
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Get a file URI from the local file path
nsCOMPtr<nsIURI> fileURI;
rv = NS_NewFileURI(getter_AddRefs(fileURI), filePath);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Create a new channel with the file URI created
nsCOMPtr<nsIChannel> channel;
nsCOMPtr<nsIIOService> ios = do_GetIOService();
rv = ios->NewChannelFromURIWithLoadInfo(fileURI, aLoadInfo,
getter_AddRefs(channel));
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
channel->SetOriginalURI(aURI);
channel.forget(_retval);
return NS_OK;
}
// PageThumbsProtocol::AllowPort
NS_IMETHODIMP
PageThumbsProtocol::AllowPort(int32_t aPort, const char* aScheme,
bool* _retval) {
*_retval = false;
return NS_OK;
}
// PageThumbsProtocol::ParseProtocolURL
//
// Extracts the URL from the query parameter. The URI is passed in in the
// form: 'moz-page-thumb://thumbnail/?url=http%3A%2F%2Fwww.mozilla.org%2F'.
nsresult PageThumbsProtocol::ParseProtocolURL(nsIURI* aURI,
nsString& aParsedURL) {
nsAutoCString spec;
aURI->GetSpec(spec);
// Check that we have the correct host
nsAutoCString host;
host = Substring(spec, spec.FindChar(':') + 3, 9);
if (!host.EqualsLiteral("thumbnail")) {
return NS_ERROR_NOT_AVAILABLE;
}
// Get the path out of the URI
nsAutoCString path;
nsresult rv = aURI->GetPathQueryRef(path);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Since this is a protocol URI and it doesn't parse nicely, we split on where
// the start of the query is and parse it from there
int32_t queryBegins = path.FindChar('?');
if (queryBegins <= 0) {
return NS_ERROR_MALFORMED_URI;
}
URLParams::Extract(Substring(path, queryBegins + 1), NS_LITERAL_STRING("url"),
aParsedURL);
// If there's no URL as part of the query params, there will be no thumbnail
if (aParsedURL.IsVoid()) {
return NS_ERROR_NOT_AVAILABLE;
}
return NS_OK;
}
// PageThumbsProtocol::GetFilePathForURL
//
// Returns the thumbnail's file path for a given URL
nsresult PageThumbsProtocol::GetFilePathForURL(nsIURI* aURI,
nsIFile** _retval) {
nsresult rv;
// Use PageThumbsStorageService to get the local file path of the screenshot
// for the given URL
nsAutoString filePathForURL;
nsCOMPtr<nsIPageThumbsStorageService> pageThumbsStorage =
do_GetService("@mozilla.org/thumbnails/pagethumbs-service;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Parse the protocol URL to extract the thumbnail's URL
nsAutoString parsedURL;
rv = ParseProtocolURL(aURI, parsedURL);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
rv = pageThumbsStorage->GetFilePathForURL(parsedURL, filePathForURL);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Find the local file containing the screenshot
nsCOMPtr<nsIFile> filePath = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
rv = filePath->InitWithPath(filePathForURL);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
filePath.forget(_retval);
return NS_OK;
}