OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/appcache/appcache_url_loader_factory.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "content/browser/appcache/appcache_entry.h" |
| 10 #include "content/browser/appcache/appcache_policy.h" |
| 11 #include "content/browser/appcache/appcache_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" |
| 15 #include "content/common/network_service.mojom.h" |
| 16 #include "content/common/resource_request.h" |
| 17 #include "content/common/url_loader.mojom.h" |
| 18 #include "content/common/url_loader_factory.mojom.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 22 #include "mojo/public/cpp/bindings/binding_set.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 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. |
| 143 AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( |
| 144 ChromeAppCacheService* appcache_service, |
| 145 URLLoaderFactoryGetter* factory_getter) |
| 146 : appcache_service_(appcache_service), factory_getter_(factory_getter) {} |
| 147 |
| 148 AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
| 149 |
| 150 // static |
| 151 void AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
| 152 mojom::URLLoaderFactoryRequest request, |
| 153 ChromeAppCacheService* appcache_service, |
| 154 URLLoaderFactoryGetter* factory_getter) { |
| 155 std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( |
| 156 new AppCacheURLLoaderFactory(appcache_service, factory_getter)); |
| 157 AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); |
| 158 raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), |
| 159 std::move(request)); |
| 160 } |
| 161 |
| 162 void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
| 163 mojom::URLLoaderAssociatedRequest url_loader_request, |
| 164 int32_t routing_id, |
| 165 int32_t request_id, |
| 166 uint32_t options, |
| 167 const ResourceRequest& request, |
| 168 mojom::URLLoaderClientPtr client) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 |
| 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 } |
| 177 |
| 178 void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, |
| 179 int32_t request_id, |
| 180 const ResourceRequest& request, |
| 181 SyncLoadCallback callback) { |
| 182 NOTREACHED(); |
| 183 } |
| 184 |
| 185 } // namespace content |
OLD | NEW |