| 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_backend_impl.h" |
| 9 #include "content/browser/appcache/appcache_entry.h" | 10 #include "content/browser/appcache/appcache_entry.h" |
| 11 #include "content/browser/appcache/appcache_host.h" |
| 10 #include "content/browser/appcache/appcache_policy.h" | 12 #include "content/browser/appcache/appcache_policy.h" |
| 11 #include "content/browser/appcache/appcache_request.h" | 13 #include "content/browser/appcache/appcache_request.h" |
| 12 #include "content/browser/appcache/appcache_storage.h" | 14 #include "content/browser/appcache/appcache_storage.h" |
| 13 #include "content/browser/appcache/chrome_appcache_service.h" | 15 #include "content/browser/appcache/chrome_appcache_service.h" |
| 14 #include "content/browser/url_loader_factory_getter.h" | 16 #include "content/browser/url_loader_factory_getter.h" |
| 15 #include "content/common/network_service.mojom.h" | 17 #include "content/common/network_service.mojom.h" |
| 16 #include "content/common/resource_request.h" | 18 #include "content/common/resource_request.h" |
| 17 #include "content/common/url_loader.mojom.h" | 19 #include "content/common/url_loader.mojom.h" |
| 18 #include "content/common/url_loader_factory.mojom.h" | 20 #include "content/common/url_loader_factory.mojom.h" |
| 19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 20 #include "mojo/public/cpp/bindings/associated_binding.h" | 22 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 23 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 22 #include "mojo/public/cpp/bindings/binding_set.h" | 24 #include "mojo/public/cpp/bindings/binding_set.h" |
| 23 | 25 |
| 24 namespace content { | 26 namespace content { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 30 AppCacheHost* g_host = nullptr; |
| 31 |
| 28 // Handles AppCache URL loads for the network service. | 32 // Handles AppCache URL loads for the network service. |
| 29 class AppCacheURLLoader : public AppCacheStorage::Delegate, | 33 class AppCacheURLLoader : public AppCacheStorage::Delegate, |
| 30 public mojom::URLLoader { | 34 public mojom::URLLoader { |
| 31 public: | 35 public: |
| 32 AppCacheURLLoader(const ResourceRequest& request, | 36 AppCacheURLLoader(const ResourceRequest& request, |
| 33 mojom::URLLoaderAssociatedRequest url_loader_request, | 37 mojom::URLLoaderAssociatedRequest url_loader_request, |
| 34 int32_t routing_id, | 38 int32_t routing_id, |
| 35 int32_t request_id, | 39 int32_t request_id, |
| 36 mojom::URLLoaderClientPtr client_info, | 40 mojom::URLLoaderClientPtr client_info, |
| 37 ChromeAppCacheService* appcache_service, | 41 ChromeAppCacheService* appcache_service, |
| 38 URLLoaderFactoryGetter* factory_getter) | 42 URLLoaderFactoryGetter* factory_getter) |
| 39 : request_(request), | 43 : request_(request), |
| 40 routing_id_(routing_id), | 44 routing_id_(routing_id), |
| 41 request_id_(request_id), | 45 request_id_(request_id), |
| 42 client_info_(std::move(client_info)), | 46 client_info_(std::move(client_info)), |
| 43 appcache_service_(appcache_service), | 47 appcache_service_(appcache_service), |
| 44 factory_getter_(factory_getter), | 48 factory_getter_(factory_getter), |
| 45 binding_(this, std::move(url_loader_request)) { | 49 binding_(this, std::move(url_loader_request)) { |
| 46 binding_.set_connection_error_handler(base::Bind( | 50 binding_.set_connection_error_handler(base::Bind( |
| 47 &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); | 51 &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); |
| 48 } | 52 } |
| 49 | 53 |
| 50 ~AppCacheURLLoader() override {} | 54 ~AppCacheURLLoader() override {} |
| 51 | 55 |
| 52 void Start() { | 56 void Start(AppCacheHost* host) { |
| 57 DCHECK(host); |
| 53 // If the origin does not exist in the AppCache usage map, then we can | 58 // If the origin does not exist in the AppCache usage map, then we can |
| 54 // safely call the network service here. | 59 // safely call the network service here. |
| 55 if (appcache_service_->storage()->usage_map()->find( | 60 if (appcache_service_->storage()->IsInitialized() && |
| 61 appcache_service_->storage()->usage_map()->find( |
| 56 request_.url.GetOrigin()) == | 62 request_.url.GetOrigin()) == |
| 57 appcache_service_->storage()->usage_map()->end()) { | 63 appcache_service_->storage()->usage_map()->end()) { |
| 58 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | 64 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| 59 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | 65 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| 60 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | 66 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| 61 return; | 67 return; |
| 62 } | 68 } |
| 63 | 69 |
| 64 appcache_service_->storage()->FindResponseForMainRequest(request_.url, | 70 switch (request_.resource_type) { |
| 65 GURL(), this); | 71 case RESOURCE_TYPE_MAIN_FRAME: |
| 72 case RESOURCE_TYPE_SUB_FRAME: |
| 73 appcache_service_->storage()->FindResponseForMainRequest(request_.url, |
| 74 GURL(), this); |
| 75 break; |
| 76 |
| 77 default: |
| 78 // We should not be hitting this yet. We have to plumb the AppCache |
| 79 // response through to the renderer before we receive requests for sub |
| 80 // resources. |
| 81 DCHECK(false); |
| 82 break; |
| 83 } |
| 66 } | 84 } |
| 67 | 85 |
| 68 // mojom::URLLoader implementation: | 86 // mojom::URLLoader implementation: |
| 69 void FollowRedirect() override { network_loader_request_->FollowRedirect(); } | 87 void FollowRedirect() override { network_loader_request_->FollowRedirect(); } |
| 70 | 88 |
| 71 void SetPriority(net::RequestPriority priority, | 89 void SetPriority(net::RequestPriority priority, |
| 72 int32_t intra_priority_value) override { | 90 int32_t intra_priority_value) override { |
| 73 DCHECK(false); | 91 DCHECK(false); |
| 74 } | 92 } |
| 75 | 93 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 88 !policy->CanLoadAppCache(manifest_url, | 106 !policy->CanLoadAppCache(manifest_url, |
| 89 request_.first_party_for_cookies); | 107 request_.first_party_for_cookies); |
| 90 | 108 |
| 91 if (was_blocked_by_policy || !entry.has_response_id() || | 109 if (was_blocked_by_policy || !entry.has_response_id() || |
| 92 cache_id == kAppCacheNoCacheId) { | 110 cache_id == kAppCacheNoCacheId) { |
| 93 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | 111 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| 94 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | 112 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| 95 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | 113 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| 96 } else { | 114 } else { |
| 97 DLOG(WARNING) << "AppCache found for url " << url | 115 DLOG(WARNING) << "AppCache found for url " << url |
| 98 << " Returning AppCache factory\n"; | 116 << " Passing request to default factory\n"; |
| 99 // TODO(ananta) | 117 // TODO(ananta) |
| 100 // Provide the plumbing to initiate AppCache requests here. | 118 // Provide the plumbing to initiate AppCache requests here. |
| 101 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | 119 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| 102 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | 120 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| 103 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | 121 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| 104 } | 122 } |
| 105 } | 123 } |
| 106 | 124 |
| 107 void OnConnectionError() { delete this; } | 125 void OnConnectionError() { delete this; } |
| 108 | 126 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 135 mojo::AssociatedBinding<mojom::URLLoader> binding_; | 153 mojo::AssociatedBinding<mojom::URLLoader> binding_; |
| 136 | 154 |
| 137 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); | 155 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); |
| 138 }; | 156 }; |
| 139 | 157 |
| 140 } // namespace | 158 } // namespace |
| 141 | 159 |
| 142 // Implements the URLLoaderFactory mojom for AppCache requests. | 160 // Implements the URLLoaderFactory mojom for AppCache requests. |
| 143 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( | 161 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( |
| 144 ChromeAppCacheService* appcache_service, | 162 ChromeAppCacheService* appcache_service, |
| 145 URLLoaderFactoryGetter* factory_getter) | 163 URLLoaderFactoryGetter* factory_getter, |
| 146 : appcache_service_(appcache_service), factory_getter_(factory_getter) {} | 164 int renderer_process_id) |
| 165 : appcache_service_(appcache_service), |
| 166 factory_getter_(factory_getter), |
| 167 renderer_process_id_(renderer_process_id) {} |
| 147 | 168 |
| 148 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} | 169 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
| 149 | 170 |
| 150 // static | 171 // static |
| 151 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( | 172 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
| 152 mojom::URLLoaderFactoryRequest request, | 173 mojom::URLLoaderFactoryRequest request, |
| 153 ChromeAppCacheService* appcache_service, | 174 ChromeAppCacheService* appcache_service, |
| 154 URLLoaderFactoryGetter* factory_getter) { | 175 URLLoaderFactoryGetter* factory_getter, |
| 176 int renderer_process_id) { |
| 155 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( | 177 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( |
| 156 new AppCacheURLLoaderFactory(appcache_service, factory_getter)); | 178 new AppCacheURLLoaderFactory(appcache_service, factory_getter, |
| 179 renderer_process_id)); |
| 157 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); | 180 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); |
| 158 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), | 181 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), |
| 159 std::move(request)); | 182 std::move(request)); |
| 160 } | 183 } |
| 161 | 184 |
| 185 // static |
| 186 void AppCacheURLLoaderFactory::SetAppCacheHost(AppCacheHost* host) { |
| 187 DCHECK(!g_host); |
| 188 g_host = host; |
| 189 } |
| 190 |
| 162 void AppCacheURLLoaderFactory::CreateLoaderAndStart( | 191 void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
| 163 mojom::URLLoaderAssociatedRequest url_loader_request, | 192 mojom::URLLoaderAssociatedRequest url_loader_request, |
| 164 int32_t routing_id, | 193 int32_t routing_id, |
| 165 int32_t request_id, | 194 int32_t request_id, |
| 166 uint32_t options, | 195 uint32_t options, |
| 167 const ResourceRequest& request, | 196 const ResourceRequest& request, |
| 168 mojom::URLLoaderClientPtr client) { | 197 mojom::URLLoaderClientPtr client) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 198 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 | 199 |
| 200 AppCacheHost* host = nullptr; |
| 201 if (renderer_process_id_ != -1) { |
| 202 AppCacheBackendImpl* backend = |
| 203 appcache_service_->GetBackend(renderer_process_id_); |
| 204 DCHECK(backend); |
| 205 host = backend->GetHost(request.appcache_host_id); |
| 206 DCHECK(host); |
| 207 } else { |
| 208 DCHECK(g_host); |
| 209 host = g_host; |
| 210 g_host = nullptr; |
| 211 } |
| 212 |
| 171 // This will get deleted when the connection is dropped by the client. | 213 // This will get deleted when the connection is dropped by the client. |
| 172 AppCacheURLLoader* loader = new AppCacheURLLoader( | 214 AppCacheURLLoader* loader = new AppCacheURLLoader( |
| 173 request, std::move(url_loader_request), routing_id, request_id, | 215 request, std::move(url_loader_request), routing_id, request_id, |
| 174 std::move(client), appcache_service_.get(), factory_getter_.get()); | 216 std::move(client), appcache_service_.get(), factory_getter_.get()); |
| 175 loader->Start(); | 217 loader->Start(host); |
| 176 } | 218 } |
| 177 | 219 |
| 178 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, | 220 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, |
| 179 int32_t request_id, | 221 int32_t request_id, |
| 180 const ResourceRequest& request, | 222 const ResourceRequest& request, |
| 181 SyncLoadCallback callback) { | 223 SyncLoadCallback callback) { |
| 182 NOTREACHED(); | 224 NOTREACHED(); |
| 183 } | 225 } |
| 184 | 226 |
| 185 } // namespace content | 227 } // namespace content |
| OLD | NEW |