1 /*
2  * Copyright (C) 2012 Motorola Mobility Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_PUBLIC_URL_MANAGER_H_
27 #define THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_PUBLIC_URL_MANAGER_H_
28 
29 #include "mojo/public/cpp/bindings/associated_remote.h"
30 #include "mojo/public/cpp/bindings/pending_receiver.h"
31 #include "services/network/public/mojom/url_loader_factory.mojom-blink-forward.h"
32 #include "third_party/blink/public/mojom/blob/blob_url_store.mojom-blink.h"
33 #include "third_party/blink/renderer/core/core_export.h"
34 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
35 #include "third_party/blink/renderer/platform/heap/handle.h"
36 #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"
37 #include "third_party/blink/renderer/platform/wtf/hash_map.h"
38 #include "third_party/blink/renderer/platform/wtf/hash_set.h"
39 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
40 
41 namespace blink {
42 
43 class KURL;
44 class ExecutionContext;
45 class URLRegistry;
46 class URLRegistrable;
47 
48 class CORE_EXPORT PublicURLManager final
49     : public GarbageCollected<PublicURLManager>,
50       public ExecutionContextLifecycleObserver {
51  public:
52   explicit PublicURLManager(ExecutionContext*);
53 
54   // Generates a new Blob URL and registers the URLRegistrable to the
55   // corresponding URLRegistry with the Blob URL. Returns the serialization
56   // of the Blob URL.
57   String RegisterURL(URLRegistrable*);
58   // Revokes the given URL.
59   void Revoke(const KURL&);
60   // When mojo Blob URLs are enabled this resolves the provided URL to a
61   // factory capable of creating loaders for the specific URL.
62   void Resolve(const KURL&,
63                mojo::PendingReceiver<network::mojom::blink::URLLoaderFactory>);
64   // When mojo Blob URLs are enabled this resolves the provided URL to a mojom
65   // BlobURLToken. This token can be used by the browser process to securely
66   // lookup what blob a URL used to refer to, even after the URL is revoked.
67   // If the URL fails to resolve the request will simply be disconnected.
68   void Resolve(const KURL&, mojo::PendingReceiver<mojom::blink::BlobURLToken>);
69 
70   // ExecutionContextLifecycleObserver interface.
71   void ContextDestroyed() override;
72 
73   void Trace(Visitor*) const override;
74 
SetURLStoreForTesting(HeapMojoAssociatedRemote<mojom::blink::BlobURLStore> url_store)75   void SetURLStoreForTesting(
76       HeapMojoAssociatedRemote<mojom::blink::BlobURLStore> url_store) {
77     url_store_ = std::move(url_store);
78   }
79 
80  private:
81   typedef String URLString;
82   // Map from URLs to the URLRegistry they are registered with.
83   typedef HashMap<URLString, URLRegistry*> URLToRegistryMap;
84   URLToRegistryMap url_to_registry_;
85   HashSet<URLString> mojo_urls_;
86 
87   bool is_stopped_;
88 
89   HeapMojoAssociatedRemote<mojom::blink::BlobURLStore> url_store_;
90 };
91 
92 }  // namespace blink
93 
94 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FILEAPI_PUBLIC_URL_MANAGER_H_
95