no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / file / uri / BlobURLProtocolHandler.h
blobc34fcfbd4cfb8cfd3f1d851d257891e6c0d12e2a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_BlobURLProtocolHandler_h
8 #define mozilla_dom_BlobURLProtocolHandler_h
10 #include "mozilla/Attributes.h"
11 #include "nsIProtocolHandler.h"
12 #include "nsIURI.h"
13 #include "nsCOMPtr.h"
14 #include "nsTArray.h"
15 #include "nsWeakReference.h"
16 #include <functional>
18 #define BLOBURI_SCHEME "blob"
20 class nsIPrincipal;
22 namespace mozilla {
23 class BlobURLsReporter;
24 class OriginAttributes;
25 template <class T>
26 class Maybe;
28 namespace dom {
30 class BlobImpl;
31 class BlobURLRegistrationData;
32 class ContentParent;
33 class MediaSource;
35 class BlobURLProtocolHandler final : public nsIProtocolHandler,
36 public nsSupportsWeakReference {
37 public:
38 NS_DECL_ISUPPORTS
39 NS_DECL_NSIPROTOCOLHANDLER
41 BlobURLProtocolHandler();
43 static nsresult CreateNewURI(const nsACString& aSpec, const char* aCharset,
44 nsIURI* aBaseURI, nsIURI** result);
46 // Methods for managing uri->object mapping
47 // AddDataEntry creates the URI with the given scheme and returns it in aUri
48 static nsresult AddDataEntry(BlobImpl*, nsIPrincipal*,
49 const nsCString& aPartitionKey,
50 nsACString& aUri);
51 static nsresult AddDataEntry(MediaSource*, nsIPrincipal*,
52 const nsCString& aPartitionKey,
53 nsACString& aUri);
54 // IPC only
55 static void AddDataEntry(const nsACString& aURI, nsIPrincipal* aPrincipal,
56 const nsCString& aPartitionKey, BlobImpl* aBlobImpl);
58 // These methods revoke a blobURL. Because some operations could still be in
59 // progress, the revoking consists in marking the blobURL as revoked and in
60 // removing it after RELEASING_TIMER milliseconds.
61 static void RemoveDataEntry(const nsACString& aUri,
62 bool aBroadcastToOTherProcesses = true);
63 // Returns true if the entry was allowed to be removed.
64 static bool RemoveDataEntry(const nsACString& aUri, nsIPrincipal* aPrincipal,
65 const nsCString& aPartitionKey);
67 static void RemoveDataEntries();
69 static bool HasDataEntry(const nsACString& aUri);
71 static bool GetDataEntry(const nsACString& aUri, BlobImpl** aBlobImpl,
72 nsIPrincipal* aLoadingPrincipal,
73 nsIPrincipal* aTriggeringPrincipal,
74 const OriginAttributes& aOriginAttributes,
75 uint64_t aInnerWindowId,
76 const nsCString& aPartitionKey,
77 bool aAlsoIfRevoked = false);
79 static void Traverse(const nsACString& aUri,
80 nsCycleCollectionTraversalCallback& aCallback);
82 // Main-thread only method to invoke a helper function that gets called for
83 // every known and recently revoked Blob URL. The helper function should
84 // return true to keep going or false to stop enumerating (presumably because
85 // of an unexpected XPCOM or IPC error). This method returns false if already
86 // shutdown or if the helper method returns false, true otherwise.
87 static bool ForEachBlobURL(
88 std::function<bool(BlobImpl*, nsIPrincipal*, const nsCString&,
89 const nsACString&, bool aRevoked)>&& aCb);
91 // This method returns false if aURI is not a known BlobURL. Otherwise it
92 // returns true.
94 // When true is returned, the aPrincipal out param is meaningful. It gets
95 // set to the principal that a channel loaded from the blob would get if
96 // the blob is not already revoked and to a NullPrincipal if the blob is
97 // revoked.
99 // This means that for a revoked blob URL this method may either return
100 // false or return true and hand out a NullPrincipal in aPrincipal,
101 // depending on whether the "remove it from the hashtable" timer has
102 // fired. See RemoveDataEntry().
103 static bool GetBlobURLPrincipal(nsIURI* aURI, nsIPrincipal** aPrincipal);
105 // Check if metadata about Blob URLs created with this principal should be
106 // broadcast into every content process. This is currently the case for
107 // extension blob URLs and system principal blob URLs, as they can be loaded
108 // by system code and content scripts respectively.
109 static bool IsBlobURLBroadcastPrincipal(nsIPrincipal* aPrincipal);
111 private:
112 ~BlobURLProtocolHandler();
114 static void Init();
116 // If principal is not null, its origin will be used to generate the URI.
117 static nsresult GenerateURIString(nsIPrincipal* aPrincipal, nsACString& aUri);
120 bool IsBlobURI(nsIURI* aUri);
121 bool IsMediaSourceURI(nsIURI* aUri);
123 // Return true if inner scheme of blobURL is http or https, false otherwise.
124 bool BlobURLSchemeIsHTTPOrHTTPS(const nsACString& aUri);
126 } // namespace dom
127 } // namespace mozilla
129 extern nsresult NS_GetBlobForBlobURI(nsIURI* aURI,
130 mozilla::dom::BlobImpl** aBlob);
132 extern nsresult NS_GetBlobForBlobURISpec(const nsACString& aSpec,
133 mozilla::dom::BlobImpl** aBlob,
134 bool aAlsoIfRevoked = false);
136 extern nsresult NS_SetChannelContentRangeForBlobURI(nsIChannel* aChannel,
137 nsIURI* aURI,
138 nsACString& aRangeHeader);
140 extern nsresult NS_GetSourceForMediaSourceURI(
141 nsIURI* aURI, mozilla::dom::MediaSource** aSource);
143 #endif /* mozilla_dom_BlobURLProtocolHandler_h */