1 /*
2  * Copyright (c) 2009, Google Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_APPCACHE_APPLICATION_CACHE_HOST_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_APPCACHE_APPLICATION_CACHE_HOST_H_
33 
34 #include <memory>
35 
36 #include "base/gtest_prod_util.h"
37 #include "base/macros.h"
38 #include "mojo/public/cpp/bindings/pending_remote.h"
39 #include "third_party/blink/public/common/browser_interface_broker_proxy.h"
40 #include "third_party/blink/public/mojom/appcache/appcache.mojom-blink.h"
41 #include "third_party/blink/public/mojom/appcache/appcache_info.mojom-blink-forward.h"
42 #include "third_party/blink/public/mojom/devtools/console_message.mojom-blink-forward.h"
43 #include "third_party/blink/renderer/core/core_export.h"
44 #include "third_party/blink/renderer/platform/context_lifecycle_notifier.h"
45 #include "third_party/blink/renderer/platform/heap/handle.h"
46 #include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
47 #include "third_party/blink/renderer/platform/mojo/heap_mojo_receiver.h"
48 #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
49 #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
50 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
51 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
52 #include "third_party/blink/renderer/platform/wtf/vector.h"
53 
54 namespace blink {
55 
56 class CORE_EXPORT ApplicationCacheHost
57     : public GarbageCollected<ApplicationCacheHost>,
58       public mojom::blink::AppCacheFrontend {
59  public:
60   ApplicationCacheHost(
61       const BrowserInterfaceBrokerProxy& interface_broker_proxy,
62       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
63       ContextLifecycleNotifier* notifier);
64   ~ApplicationCacheHost() override;
65   virtual void Detach();
66 
67   struct CacheInfo {
68     STACK_ALLOCATED();
69 
70    public:
CacheInfoCacheInfo71     CacheInfo(const KURL& manifest,
72               double creation_time,
73               double update_time,
74               int64_t response_sizes,
75               int64_t padding_sizes)
76         : manifest_(manifest),
77           creation_time_(creation_time),
78           update_time_(update_time),
79           response_sizes_(response_sizes),
80           padding_sizes_(padding_sizes) {}
81     CacheInfo() = default;
82     KURL manifest_;
83     double creation_time_ = 0;
84     double update_time_ = 0;
85     int64_t response_sizes_ = 0;
86     int64_t padding_sizes_ = 0;
87   };
88 
89   mojom::blink::AppCacheStatus GetStatus() const;
90   void Abort();
91 
92   void FillResourceList(Vector<mojom::blink::AppCacheResourceInfo>*);
93   CacheInfo ApplicationCacheInfo();
94 
95   const base::UnguessableToken& GetHostID() const;
96   void SetHostID(const base::UnguessableToken& host_id);
97 
98   void SelectCacheForWorker(int64_t app_cache_id,
99                             base::OnceClosure completion_callback);
100 
101   // mojom::blink::AppCacheFrontend
102   void CacheSelected(mojom::blink::AppCacheInfoPtr info) override;
103   void EventRaised(mojom::blink::AppCacheEventID event_id) override;
104   void ProgressEventRaised(const KURL& url,
105                            int32_t num_total,
106                            int32_t num_complete) override;
107   void ErrorEventRaised(mojom::blink::AppCacheErrorDetailsPtr details) override;
LogMessage(mojom::blink::ConsoleMessageLevel log_level,const String & message)108   void LogMessage(mojom::blink::ConsoleMessageLevel log_level,
109                   const String& message) override {}
SetSubresourceFactory(mojo::PendingRemote<network::mojom::blink::URLLoaderFactory> url_loader_factory)110   void SetSubresourceFactory(
111       mojo::PendingRemote<network::mojom::blink::URLLoaderFactory>
112           url_loader_factory) override {}
113 
114   virtual void Trace(Visitor*) const;
115 
116  protected:
117   HeapMojoRemote<mojom::blink::AppCacheHost,
118                  HeapMojoWrapperMode::kForceWithoutContextObserver>
119       backend_host_;
120   mojom::blink::AppCacheStatus status_ =
121       mojom::blink::AppCacheStatus::APPCACHE_STATUS_UNCACHED;
122 
123   // Non-empty |host_id_| must be set before calling this function.
124   bool BindBackend();
125 
126  private:
NotifyApplicationCache(mojom::AppCacheEventID,int progress_total,int progress_done,mojom::AppCacheErrorReason,const String & error_url,int error_status,const String & error_message)127   virtual void NotifyApplicationCache(mojom::AppCacheEventID,
128                                       int progress_total,
129                                       int progress_done,
130                                       mojom::AppCacheErrorReason,
131                                       const String& error_url,
132                                       int error_status,
133                                       const String& error_message) {}
134 
135   void GetAssociatedCacheInfo(CacheInfo* info);
136 
137   HeapMojoReceiver<mojom::blink::AppCacheFrontend,
138                    ApplicationCacheHost,
139                    HeapMojoWrapperMode::kForceWithoutContextObserver>
140       receiver_;
141   HeapMojoRemote<mojom::blink::AppCacheBackend,
142                  HeapMojoWrapperMode::kForceWithoutContextObserver>
143       backend_remote_;
144 
145   base::UnguessableToken host_id_;
146   mojom::blink::AppCacheInfo cache_info_;
147   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
148 
149   const BrowserInterfaceBrokerProxy& interface_broker_proxy_;
150 
151   // Invoked when CacheSelected() is called.
152   base::OnceClosure select_cache_for_worker_completion_callback_;
153 
154   FRIEND_TEST_ALL_PREFIXES(DocumentTest, SandboxDisablesAppCache);
155 
156   DISALLOW_COPY_AND_ASSIGN(ApplicationCacheHost);
157 };
158 
159 }  // namespace blink
160 
161 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_APPCACHE_APPLICATION_CACHE_HOST_H_
162