Chromium Code Reviews| 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; | |
|
michaeln
2017/05/24 00:59:32
I don't think we should add this, i'd vote to take
ananta
2017/05/24 02:14:03
This code is going to change once kinuko lands her
| |
| 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, |
| 43 AppCacheHost* host) | |
| 39 : request_(request), | 44 : request_(request), |
| 40 routing_id_(routing_id), | 45 routing_id_(routing_id), |
| 41 request_id_(request_id), | 46 request_id_(request_id), |
| 42 client_info_(std::move(client_info)), | 47 client_info_(std::move(client_info)), |
| 43 appcache_service_(appcache_service), | 48 appcache_service_(appcache_service), |
| 44 factory_getter_(factory_getter), | 49 factory_getter_(factory_getter), |
| 50 host_(host), | |
| 45 binding_(this, std::move(url_loader_request)) { | 51 binding_(this, std::move(url_loader_request)) { |
| 46 binding_.set_connection_error_handler(base::Bind( | 52 binding_.set_connection_error_handler(base::Bind( |
| 47 &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); | 53 &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); |
| 48 } | 54 } |
| 49 | 55 |
| 50 ~AppCacheURLLoader() override {} | 56 ~AppCacheURLLoader() override {} |
| 51 | 57 |
| 52 void Start() { | 58 void Start() { |
| 53 // If the origin does not exist in the AppCache usage map, then we can | 59 // If the origin does not exist in the AppCache usage map, then we can |
| 54 // safely call the network service here. | 60 // safely call the network service here. |
| 55 if (appcache_service_->storage()->usage_map()->find( | 61 if (appcache_service_->storage()->usage_map()->find( |
|
michaeln
2017/05/24 00:59:32
we need to test IsInitTaskComplete() for this chec
ananta
2017/05/24 02:14:03
Added a virtual function IsInitialized() in AppCac
| |
| 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 appcache_service_->storage()->FindResponseForMainRequest(request_.url, |
|
michaeln
2017/05/24 00:59:32
Since this class is only used the main resource, m
ananta
2017/05/24 02:14:03
This class should be handling main and sub resourc
| |
| 65 GURL(), this); | 71 GURL(), this); |
| 66 } | 72 } |
| 67 | 73 |
| 68 // mojom::URLLoader implementation: | 74 // mojom::URLLoader implementation: |
| 69 void FollowRedirect() override { network_loader_request_->FollowRedirect(); } | 75 void FollowRedirect() override { network_loader_request_->FollowRedirect(); } |
| 70 | 76 |
| 71 void SetPriority(net::RequestPriority priority, | 77 void SetPriority(net::RequestPriority priority, |
| 72 int32_t intra_priority_value) override { | 78 int32_t intra_priority_value) override { |
| 73 DCHECK(false); | 79 DCHECK(false); |
| 74 } | 80 } |
| 75 | 81 |
| 76 private: | 82 private: |
| 77 // AppCacheStorage::Delegate methods. | 83 // AppCacheStorage::Delegate methods. |
| 78 void OnMainResponseFound(const GURL& url, | 84 void OnMainResponseFound(const GURL& url, |
|
michaeln
2017/05/24 00:59:32
When we talk a couple weeks ago, we talked about r
ananta
2017/05/24 02:14:03
Yes. The plan is to do that. I was going to call A
| |
| 79 const AppCacheEntry& entry, | 85 const AppCacheEntry& entry, |
| 80 const GURL& fallback_url, | 86 const GURL& fallback_url, |
| 81 const AppCacheEntry& fallback_entry, | 87 const AppCacheEntry& fallback_entry, |
| 82 int64_t cache_id, | 88 int64_t cache_id, |
| 83 int64_t group_id, | 89 int64_t group_id, |
| 84 const GURL& manifest_url) override { | 90 const GURL& manifest_url) override { |
| 85 AppCachePolicy* policy = appcache_service_->appcache_policy(); | 91 AppCachePolicy* policy = appcache_service_->appcache_policy(); |
| 86 bool was_blocked_by_policy = | 92 bool was_blocked_by_policy = |
| 87 !manifest_url.is_empty() && policy && | 93 !manifest_url.is_empty() && policy && |
| 88 !policy->CanLoadAppCache(manifest_url, | 94 !policy->CanLoadAppCache(manifest_url, |
| 89 request_.first_party_for_cookies); | 95 request_.first_party_for_cookies); |
| 90 | 96 |
| 91 if (was_blocked_by_policy || !entry.has_response_id() || | 97 if (was_blocked_by_policy || !entry.has_response_id() || |
| 92 cache_id == kAppCacheNoCacheId) { | 98 cache_id == kAppCacheNoCacheId) { |
| 93 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | 99 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| 94 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | 100 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| 95 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | 101 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| 96 } else { | 102 } else { |
| 97 DLOG(WARNING) << "AppCache found for url " << url | 103 DLOG(WARNING) << "AppCache found for url " << url |
| 98 << " Returning AppCache factory\n"; | 104 << " host id is " << host_->host_id() |
| 105 << " Passing request to default factory\n"; | |
| 99 // TODO(ananta) | 106 // TODO(ananta) |
| 100 // Provide the plumbing to initiate AppCache requests here. | 107 // Provide the plumbing to initiate AppCache requests here. |
| 101 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( | 108 factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| 102 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, | 109 mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| 103 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); | 110 mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| 104 } | 111 } |
| 105 } | 112 } |
| 106 | 113 |
| 107 void OnConnectionError() { delete this; } | 114 void OnConnectionError() { delete this; } |
| 108 | 115 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 124 mojom::URLLoaderClientPtr client_info_; | 131 mojom::URLLoaderClientPtr client_info_; |
| 125 | 132 |
| 126 // Used to query AppCacheStorage to see if a request can be served out of the | 133 // Used to query AppCacheStorage to see if a request can be served out of the |
| 127 /// AppCache. | 134 /// AppCache. |
| 128 scoped_refptr<ChromeAppCacheService> appcache_service_; | 135 scoped_refptr<ChromeAppCacheService> appcache_service_; |
| 129 | 136 |
| 130 // Used to retrieve the network service factory to pass requests to the | 137 // Used to retrieve the network service factory to pass requests to the |
| 131 // network service. | 138 // network service. |
| 132 scoped_refptr<URLLoaderFactoryGetter> factory_getter_; | 139 scoped_refptr<URLLoaderFactoryGetter> factory_getter_; |
| 133 | 140 |
| 141 // We don't own the AppCacheHost pointer. This is safe as the host outlives | |
|
michaeln
2017/05/24 00:59:32
what guarantees that?
ananta
2017/05/24 02:14:03
Changed to take the host in the Start() function.
| |
| 142 // us. | |
| 143 AppCacheHost* host_; | |
| 144 | |
| 134 // Binds the URLLoaderClient with us. | 145 // Binds the URLLoaderClient with us. |
| 135 mojo::AssociatedBinding<mojom::URLLoader> binding_; | 146 mojo::AssociatedBinding<mojom::URLLoader> binding_; |
| 136 | 147 |
| 137 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); | 148 DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); |
| 138 }; | 149 }; |
| 139 | 150 |
| 140 } // namespace | 151 } // namespace |
| 141 | 152 |
| 142 // Implements the URLLoaderFactory mojom for AppCache requests. | 153 // Implements the URLLoaderFactory mojom for AppCache requests. |
| 143 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( | 154 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( |
| 144 ChromeAppCacheService* appcache_service, | 155 ChromeAppCacheService* appcache_service, |
| 145 URLLoaderFactoryGetter* factory_getter) | 156 URLLoaderFactoryGetter* factory_getter, |
| 146 : appcache_service_(appcache_service), factory_getter_(factory_getter) {} | 157 int renderer_process_id) |
| 158 : appcache_service_(appcache_service), | |
| 159 factory_getter_(factory_getter), | |
| 160 renderer_process_id_(renderer_process_id) {} | |
| 147 | 161 |
| 148 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} | 162 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
| 149 | 163 |
| 150 // static | 164 // static |
| 151 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( | 165 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
| 152 mojom::URLLoaderFactoryRequest request, | 166 mojom::URLLoaderFactoryRequest request, |
| 153 ChromeAppCacheService* appcache_service, | 167 ChromeAppCacheService* appcache_service, |
| 154 URLLoaderFactoryGetter* factory_getter) { | 168 URLLoaderFactoryGetter* factory_getter, |
| 169 int renderer_process_id) { | |
| 155 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( | 170 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( |
| 156 new AppCacheURLLoaderFactory(appcache_service, factory_getter)); | 171 new AppCacheURLLoaderFactory(appcache_service, factory_getter, |
| 172 renderer_process_id)); | |
| 157 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); | 173 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); |
| 158 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), | 174 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), |
| 159 std::move(request)); | 175 std::move(request)); |
| 160 } | 176 } |
| 161 | 177 |
| 178 // static | |
| 179 void AppCacheURLLoaderFactory::SetAppCacheHost(AppCacheHost* host) { | |
| 180 DCHECK(!g_host); | |
| 181 g_host = host; | |
| 182 } | |
| 183 | |
| 162 void AppCacheURLLoaderFactory::CreateLoaderAndStart( | 184 void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
| 163 mojom::URLLoaderAssociatedRequest url_loader_request, | 185 mojom::URLLoaderAssociatedRequest url_loader_request, |
| 164 int32_t routing_id, | 186 int32_t routing_id, |
| 165 int32_t request_id, | 187 int32_t request_id, |
| 166 uint32_t options, | 188 uint32_t options, |
| 167 const ResourceRequest& request, | 189 const ResourceRequest& request, |
| 168 mojom::URLLoaderClientPtr client) { | 190 mojom::URLLoaderClientPtr client) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 | 192 |
| 193 AppCacheHost* host = nullptr; | |
| 194 if (renderer_process_id_ != -1) { | |
| 195 AppCacheBackendImpl* backend = | |
| 196 appcache_service_->GetBackend(renderer_process_id_); | |
| 197 DCHECK(backend); | |
| 198 host = backend->GetHost(request.appcache_host_id); | |
| 199 DCHECK(host); | |
| 200 } else { | |
| 201 DCHECK(g_host); | |
| 202 host = g_host; | |
| 203 g_host = nullptr; | |
| 204 } | |
| 205 | |
| 171 // This will get deleted when the connection is dropped by the client. | 206 // This will get deleted when the connection is dropped by the client. |
| 172 AppCacheURLLoader* loader = new AppCacheURLLoader( | 207 AppCacheURLLoader* loader = new AppCacheURLLoader( |
| 173 request, std::move(url_loader_request), routing_id, request_id, | 208 request, std::move(url_loader_request), routing_id, request_id, |
| 174 std::move(client), appcache_service_.get(), factory_getter_.get()); | 209 std::move(client), appcache_service_.get(), factory_getter_.get(), host); |
| 175 loader->Start(); | 210 loader->Start(); |
| 176 } | 211 } |
| 177 | 212 |
| 178 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, | 213 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, |
| 179 int32_t request_id, | 214 int32_t request_id, |
| 180 const ResourceRequest& request, | 215 const ResourceRequest& request, |
| 181 SyncLoadCallback callback) { | 216 SyncLoadCallback callback) { |
| 182 NOTREACHED(); | 217 NOTREACHED(); |
| 183 } | 218 } |
| 184 | 219 |
| 185 } // namespace content | 220 } // namespace content |
| OLD | NEW |