MozNewTabWallpaperProtocolHandler.h (3485B)
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 MozNewTabWallpaperProtocolHandler_h___ 7 #define MozNewTabWallpaperProtocolHandler_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 MozNewTabWallpaperProtocolHandler final 22 : public nsISubstitutingProtocolHandler, 23 public SubstitutingProtocolHandler, 24 public nsSupportsWeakReference { 25 public: 26 NS_DECL_ISUPPORTS_INHERITED 27 NS_FORWARD_NSIPROTOCOLHANDLER(SubstitutingProtocolHandler::) 28 NS_FORWARD_NSISUBSTITUTINGPROTOCOLHANDLER(SubstitutingProtocolHandler::) 29 30 static already_AddRefed<MozNewTabWallpaperProtocolHandler> GetSingleton(); 31 32 /** 33 * Obtains an input stream for a user-uploaded New Tab wallpaper. 34 * 35 * @param aChildURI moz-newtab-wallpaper URI from child process 36 * @param aTerminateSender set to true if URI is invalid (terminates child) 37 * @return RemoteStreamPromise resolving to RemoteStreamInfo on success or 38 * nsresult on failure 39 */ 40 RefPtr<RemoteStreamPromise> NewStream(nsIURI* aChildURI, 41 bool* aTerminateSender); 42 43 protected: 44 ~MozNewTabWallpaperProtocolHandler() = default; 45 46 private: 47 explicit MozNewTabWallpaperProtocolHandler(); 48 49 [[nodiscard]] bool ResolveSpecialCases(const nsACString& aHost, 50 const nsACString& aPath, 51 const nsACString& aPathname, 52 nsACString& aResult) override; 53 54 /** 55 * Substitutes the channel with a remote channel in child process. 56 * 57 * @param aURI the moz-newtab-wallpaper URI 58 * @param aLoadInfo the loadinfo for the request 59 * @param aRetVal in/out channel param for the substituted channel 60 * @return NS_OK on success or NS_ERROR_NO_INTERFACE if URI doesn't 61 * resolve to file:// 62 */ 63 [[nodiscard]] virtual nsresult SubstituteChannel( 64 nsIURI* aURI, nsILoadInfo* aLoadInfo, nsIChannel** aRetVal) override; 65 66 /** 67 * Replaces the channel with one that proxies the load to parent process. 68 * 69 * @param aURI the moz-newtab-wallpaper URI 70 * @param aLoadInfo the loadinfo for the request 71 * @param aRetVal in/out channel param for the substituted remote channel 72 * @return NS_OK if successful, otherwise an error 73 */ 74 Result<Ok, nsresult> SubstituteRemoteChannel(nsIURI* aURI, 75 nsILoadInfo* aLoadInfo, 76 nsIChannel** aRetVal); 77 78 // To allow parent IPDL actors to invoke methods on this handler when 79 // handling moz-newtab-wallpaper requests from the child. 80 static StaticRefPtr<MozNewTabWallpaperProtocolHandler> sSingleton; 81 82 // Gets a SimpleChannel that wraps the provided channel. 83 static void NewSimpleChannel(nsIURI* aURI, nsILoadInfo* aLoadinfo, 84 RemoteStreamGetter* aStreamGetter, 85 nsIChannel** aRetVal); 86 }; 87 88 } // namespace net 89 } // namespace mozilla 90 91 #endif /* MozNewTabWallpaperProtocolHandler_h___ */