OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/appcache/appcache_url_loader_factory.h" | 5 #include "content/browser/appcache/appcache_url_loader_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/browser/appcache/appcache_entry.h" | 9 #include "content/browser/appcache/appcache_request_handler.h" |
10 #include "content/browser/appcache/appcache_policy.h" | 10 #include "content/browser/appcache/appcache_url_loader_job.h" |
11 #include "content/browser/appcache/appcache_request.h" | 11 #include "content/browser/appcache/appcache_url_loader_request.h" |
12 #include "content/browser/appcache/appcache_storage.h" | |
13 #include "content/browser/appcache/chrome_appcache_service.h" | |
14 #include "content/browser/url_loader_factory_getter.h" | 12 #include "content/browser/url_loader_factory_getter.h" |
15 #include "content/common/network_service.mojom.h" | |
16 #include "content/common/resource_request.h" | 13 #include "content/common/resource_request.h" |
17 #include "content/common/url_loader.mojom.h" | |
18 #include "content/common/url_loader_factory.mojom.h" | 14 #include "content/common/url_loader_factory.mojom.h" |
19 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
20 #include "mojo/public/cpp/bindings/associated_binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | |
22 #include "mojo/public/cpp/bindings/binding_set.h" | 17 #include "mojo/public/cpp/bindings/binding_set.h" |
18 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
23 | 19 |
24 namespace content { | 20 namespace content { |
25 | 21 |
26 namespace { | |
27 | |
28 // Handles AppCache URL loads for the network service. | |
29 class AppCacheURLLoader : public AppCacheStorage::Delegate, | |
30 public mojom::URLLoader { | |
31 public: | |
32 AppCacheURLLoader(const ResourceRequest& request, | |
33 mojom::URLLoaderAssociatedRequest url_loader_request, | |
34 int32_t routing_id, | |
35 int32_t request_id, | |
36 mojom::URLLoaderClientPtr client_info, | |
37 ChromeAppCacheService* appcache_service, | |
38 URLLoaderFactoryGetter* factory_getter) | |
39 : request_(request), | |
40 routing_id_(routing_id), | |
41 request_id_(request_id), | |
42 client_info_(std::move(client_info)), | |
43 appcache_service_(appcache_service), | |
44 factory_getter_(factory_getter), | |
45 binding_(this, std::move(url_loader_request)) { | |
46 binding_.set_connection_error_handler(base::Bind( | |
47 &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); | |
48 } | |
49 | |
50 ~AppCacheURLLoader() override {} | |
51 | |
52 void Start() { | |
53 // If the origin does not exist in the AppCache usage map, then we can | |
54 // safely call the network service here. | |
55 if (appcache_service_->storage()->usage_map()->find( | |
56 request_.url.GetOrigin()) == | |
57 appcache_service_->storage()->usage_map()->end()) { | |
58 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | |
59 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | |
60 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | |
61 return; | |
62 } | |
63 | |
64 appcache_service_->storage()->FindResponseForMainRequest(request_.url, | |
65 GURL(), this); | |
66 } | |
67 | |
68 // mojom::URLLoader implementation: | |
69 void FollowRedirect() override { network_loader_request_->FollowRedirect(); } | |
70 | |
71 void SetPriority(net::RequestPriority priority, | |
72 int32_t intra_priority_value) override { | |
73 DCHECK(false); | |
74 } | |
75 | |
76 private: | |
77 // AppCacheStorage::Delegate methods. | |
78 void OnMainResponseFound(const GURL& url, | |
79 const AppCacheEntry& entry, | |
80 const GURL& fallback_url, | |
81 const AppCacheEntry& fallback_entry, | |
82 int64_t cache_id, | |
83 int64_t group_id, | |
84 const GURL& manifest_url) override { | |
85 AppCachePolicy* policy = appcache_service_->appcache_policy(); | |
86 bool was_blocked_by_policy = | |
87 !manifest_url.is_empty() && policy && | |
88 !policy->CanLoadAppCache(manifest_url, | |
89 request_.first_party_for_cookies); | |
90 | |
91 if (was_blocked_by_policy || !entry.has_response_id() || | |
92 cache_id == kAppCacheNoCacheId) { | |
93 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | |
94 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | |
95 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | |
96 } else { | |
97 DLOG(WARNING) << "AppCache found for url " << url | |
98 << " Returning AppCache factory\n"; | |
99 // TODO(ananta) | |
100 // Provide the plumbing to initiate AppCache requests here. | |
101 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | |
102 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | |
103 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | |
104 } | |
105 } | |
106 | |
107 void OnConnectionError() { delete this; } | |
108 | |
109 // The current request. | |
110 ResourceRequest request_; | |
111 | |
112 // URLLoader proxy for the network service. | |
113 mojom::URLLoaderAssociatedPtr network_loader_request_; | |
114 | |
115 // Routing id of the request. This is 0 for navigation requests. For | |
116 // subresource requests it is non zero. | |
117 int routing_id_; | |
118 | |
119 // Request id. | |
120 int request_id_; | |
121 | |
122 // The URLLoaderClient pointer. We call this interface with notifications | |
123 // about the URL load | |
124 mojom::URLLoaderClientPtr client_info_; | |
125 | |
126 // Used to query AppCacheStorage to see if a request can be served out of the | |
127 /// AppCache. | |
128 scoped_refptr<ChromeAppCacheService> appcache_service_; | |
129 | |
130 // Used to retrieve the network service factory to pass requests to the | |
131 // network service. | |
132 scoped_refptr<URLLoaderFactoryGetter> factory_getter_; | |
133 | |
134 // Binds the URLLoaderClient with us. | |
135 mojo::AssociatedBinding<mojom::URLLoader> binding_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); | |
138 }; | |
139 | |
140 } // namespace | |
141 | |
142 // Implements the URLLoaderFactory mojom for AppCache requests. | 22 // Implements the URLLoaderFactory mojom for AppCache requests. |
143 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( | 23 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( |
144 ChromeAppCacheService* appcache_service, | 24 mojom::URLLoaderFactoryRequest request, |
145 URLLoaderFactoryGetter* factory_getter) | 25 URLLoaderFactoryGetter* factory_getter) |
146 : appcache_service_(appcache_service), factory_getter_(factory_getter) {} | 26 : binding_(this, std::move(request)), factory_getter_(factory_getter) { |
27 binding_.set_connection_error_handler(base::Bind( | |
28 &AppCacheURLLoaderFactory::OnConnectionError, base::Unretained(this))); | |
29 } | |
147 | 30 |
148 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} | 31 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
149 | 32 |
150 // static | 33 // static |
151 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( | 34 mojom::URLLoaderFactoryPtr AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
kinuko
2017/06/08 10:31:21
Is this still used somewhere?
ananta
2017/06/08 13:48:56
Not at the moment. Will be used in some capacity w
| |
152 mojom::URLLoaderFactoryRequest request, | |
153 ChromeAppCacheService* appcache_service, | |
154 URLLoaderFactoryGetter* factory_getter) { | 35 URLLoaderFactoryGetter* factory_getter) { |
155 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( | 36 mojom::URLLoaderFactoryPtr loader_factory; |
156 new AppCacheURLLoaderFactory(appcache_service, factory_getter)); | 37 mojom::URLLoaderFactoryRequest request = mojo::MakeRequest(&loader_factory); |
157 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); | 38 |
158 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), | 39 // This instance will get deleted when the client drops the connection. |
159 std::move(request)); | 40 // Please see OnConnectionError() for details. |
41 new AppCacheURLLoaderFactory(std::move(request), factory_getter); | |
42 return loader_factory; | |
160 } | 43 } |
161 | 44 |
162 void AppCacheURLLoaderFactory::CreateLoaderAndStart( | 45 void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
163 mojom::URLLoaderAssociatedRequest url_loader_request, | 46 mojom::URLLoaderAssociatedRequest url_loader_request, |
164 int32_t routing_id, | 47 int32_t routing_id, |
165 int32_t request_id, | 48 int32_t request_id, |
166 uint32_t options, | 49 uint32_t options, |
167 const ResourceRequest& request, | 50 const ResourceRequest& request, |
168 mojom::URLLoaderClientPtr client) { | 51 mojom::URLLoaderClientPtr client) { |
169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
170 | 53 NOTREACHED() << "Currently not implemented"; |
171 // This will get deleted when the connection is dropped by the client. | |
172 AppCacheURLLoader* loader = new AppCacheURLLoader( | |
173 request, std::move(url_loader_request), routing_id, request_id, | |
174 std::move(client), appcache_service_.get(), factory_getter_.get()); | |
175 loader->Start(); | |
176 } | 54 } |
177 | 55 |
178 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, | 56 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, |
179 int32_t request_id, | 57 int32_t request_id, |
180 const ResourceRequest& request, | 58 const ResourceRequest& request, |
181 SyncLoadCallback callback) { | 59 SyncLoadCallback callback) { |
182 NOTREACHED(); | 60 NOTREACHED(); |
183 } | 61 } |
184 | 62 |
63 void AppCacheURLLoaderFactory::OnConnectionError() { | |
64 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
65 } | |
66 | |
185 } // namespace content | 67 } // namespace content |
OLD | NEW |