PageThumbProtocolHandler.h (5125B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef PageThumbProtocolHandler_h___ 7 #define PageThumbProtocolHandler_h___ 8 9 #include "mozilla/Result.h" 10 #include "mozilla/MozPromise.h" 11 #include "mozilla/net/RemoteStreamGetter.h" 12 #include "SubstitutingProtocolHandler.h" 13 #include "nsIInputStream.h" 14 #include "nsWeakReference.h" 15 16 namespace mozilla { 17 namespace net { 18 19 class RemoteStreamGetter; 20 21 class PageThumbProtocolHandler final : public nsISubstitutingProtocolHandler, 22 public SubstitutingProtocolHandler, 23 public nsSupportsWeakReference { 24 public: 25 NS_DECL_ISUPPORTS_INHERITED 26 NS_FORWARD_NSIPROTOCOLHANDLER(SubstitutingProtocolHandler::) 27 NS_FORWARD_NSISUBSTITUTINGPROTOCOLHANDLER(SubstitutingProtocolHandler::) 28 29 static already_AddRefed<PageThumbProtocolHandler> GetSingleton(); 30 31 /** 32 * To be called in the parent process to obtain an input stream for the 33 * given thumbnail. 34 * 35 * @param aChildURI a moz-page-thumb URI sent from the child. 36 * @param aTerminateSender out param set to true when the params are invalid 37 * and indicate the child should be terminated. If |aChildURI| is 38 * not a moz-page-thumb URI, the child is in an invalid state and 39 * should be terminated. This outparam will be set synchronously. 40 * 41 * @return RemoteStreamPromise 42 * The RemoteStreamPromise will resolve with an RemoteStreamInfo on 43 * success, and reject with an nsresult on failure. 44 */ 45 RefPtr<RemoteStreamPromise> NewStream(nsIURI* aChildURI, 46 bool* aTerminateSender); 47 48 protected: 49 ~PageThumbProtocolHandler() = default; 50 51 private: 52 explicit PageThumbProtocolHandler(); 53 54 [[nodiscard]] bool ResolveSpecialCases(const nsACString& aHost, 55 const nsACString& aPath, 56 const nsACString& aPathname, 57 nsACString& aResult) override; 58 59 /** 60 * On entry to this function, *aRetVal is expected to be non-null and already 61 * addrefed. This function may release the object stored in *aRetVal on entry 62 * and write a new pointer to an already addrefed channel to *aRetVal. 63 * 64 * @param aURI the moz-page-thumb URI. 65 * @param aLoadInfo the loadinfo for the request. 66 * @param aRetVal in/out channel param referring to the channel that 67 * might need to be substituted with a remote channel. 68 * @return NS_OK if channel has been substituted successfully or no 69 * substitution at all. Otherwise, returns an error. This function 70 * will return NS_ERROR_NO_INTERFACE if the URI resolves to a 71 * non file:// URI. 72 */ 73 [[nodiscard]] virtual nsresult SubstituteChannel( 74 nsIURI* aURI, nsILoadInfo* aLoadInfo, nsIChannel** aRetVal) override; 75 76 /** 77 * This replaces the provided channel with a channel that will proxy the load 78 * to the parent process. 79 * 80 * @param aURI the moz-page-thumb URI. 81 * @param aLoadInfo the loadinfo for the request. 82 * @param aRetVal in/out channel param referring to the channel that 83 * might need to be substituted with a remote channel. 84 * @return NS_OK if the replacement channel was created successfully. 85 * Otherwise, returns an error. 86 */ 87 Result<Ok, nsresult> SubstituteRemoteChannel(nsIURI* aURI, 88 nsILoadInfo* aLoadInfo, 89 nsIChannel** aRetVal); 90 91 /* 92 * Extracts the URL from the query string in the given moz-page-thumb URI 93 * and queries PageThumbsStorageService using the extracted URL to obtain 94 * the local file path of the screenshot. This should only be called from 95 * the parent because PageThumbsStorageService relies on the path of the 96 * profile directory, which is unavailable in the child. 97 * 98 * @param aPath the path of the moz-page-thumb URI. 99 * @param aHost the host of the moz-page-thumb URI. 100 * @param aThumbnailPath in/out string param referring to the thumbnail path. 101 * @return NS_OK if the thumbnail path was obtained successfully. Otherwise 102 * returns an error. 103 */ 104 nsresult GetThumbnailPath(const nsACString& aPath, const nsACString& aHost, 105 nsString& aThumbnailPath); 106 107 // To allow parent IPDL actors to invoke methods on this handler when 108 // handling moz-page-thumb requests from the child. 109 static StaticRefPtr<PageThumbProtocolHandler> sSingleton; 110 111 // Gets a SimpleChannel that wraps the provided channel. 112 static void NewSimpleChannel(nsIURI* aURI, nsILoadInfo* aLoadinfo, 113 RemoteStreamGetter* aStreamGetter, 114 nsIChannel** aRetVal); 115 }; 116 117 } // namespace net 118 } // namespace mozilla 119 120 #endif /* PageThumbProtocolHandler_h___ */