Chromium Code Reviews| Index: content/browser/appcache/appcache_url_loader_factory.cc |
| diff --git a/content/browser/appcache/appcache_url_loader_factory.cc b/content/browser/appcache/appcache_url_loader_factory.cc |
| index ce57fbdff08f2cc4c6bddbdfa806cb7b1d089f05..d544690e231c4eaa6f4bba0bc5bd92dd8a86b9c8 100644 |
| --- a/content/browser/appcache/appcache_url_loader_factory.cc |
| +++ b/content/browser/appcache/appcache_url_loader_factory.cc |
| @@ -6,13 +6,17 @@ |
| #include "base/bind.h" |
| #include "base/logging.h" |
| -#include "content/browser/appcache/appcache_entry.h" |
| -#include "content/browser/appcache/appcache_policy.h" |
| +#include "content/browser/appcache/appcache_backend_impl.h" |
| +#include "content/browser/appcache/appcache_host.h" |
| +#include "content/browser/appcache/appcache_navigation_handle_core.h" |
| #include "content/browser/appcache/appcache_request.h" |
| +#include "content/browser/appcache/appcache_request_handler.h" |
| #include "content/browser/appcache/appcache_storage.h" |
| +#include "content/browser/appcache/appcache_url_loader_job.h" |
| +#include "content/browser/appcache/appcache_url_loader_request.h" |
| #include "content/browser/appcache/chrome_appcache_service.h" |
| +#include "content/browser/loader/url_loader_request_handler.h" |
| #include "content/browser/url_loader_factory_getter.h" |
| -#include "content/common/network_service.mojom.h" |
| #include "content/common/resource_request.h" |
| #include "content/common/url_loader.mojom.h" |
| #include "content/common/url_loader_factory.mojom.h" |
| @@ -20,49 +24,122 @@ |
| #include "mojo/public/cpp/bindings/binding.h" |
| #include "mojo/public/cpp/bindings/binding_set.h" |
| #include "mojo/public/cpp/bindings/interface_ptr.h" |
| +#include "mojo/public/cpp/system/data_pipe.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/http/http_response_headers.h" |
| namespace content { |
| -namespace { |
| - |
| -// Handles AppCache URL loads for the network service. |
| -class AppCacheURLLoader : public AppCacheStorage::Delegate, |
| - public mojom::URLLoader { |
| +// This class implements the URLLoader mojom which provides functionality to |
| +// service URL loads from the AppCache. It is also invoked for navigation |
|
jam
2017/06/01 15:00:46
I'm finding it hard to read that this class is bot
ananta
2017/06/01 17:28:01
This class holds on the AppCacheRequestHandler cla
ananta
2017/06/01 17:37:16
During navigation the class calls the callback wit
michaeln
2017/06/02 01:17:59
I had some trouble reading thru too and have some
|
| +// requests to check if they can be serviced from the AppCache. If yes then |
| +// it creates an instance of the AppCacheURLLoaderFactory class and passes it |
| +// to the LoaderFactoryCallback callback. If not it invokes the |
| +// LoaderFactoryCallback with a null factory pointer which passes the request |
| +// to the next handler. |
| +class AppCacheURLLoader : public mojom::URLLoader, |
| + public AppCacheURLLoaderJob::Delegate { |
| public: |
| - AppCacheURLLoader(const ResourceRequest& request, |
| - mojom::URLLoaderRequest url_loader_request, |
| - int32_t routing_id, |
| - int32_t request_id, |
| - mojom::URLLoaderClientPtr client_info, |
| - ChromeAppCacheService* appcache_service, |
| - URLLoaderFactoryGetter* factory_getter) |
| - : request_(request), |
| - routing_id_(routing_id), |
| - request_id_(request_id), |
| - client_info_(std::move(client_info)), |
| - appcache_service_(appcache_service), |
| - factory_getter_(factory_getter), |
| - binding_(this, std::move(url_loader_request)) { |
| - binding_.set_connection_error_handler(base::Bind( |
| - &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); |
| + // Creates an instance of the AppCacheURLLoader class which checks if the |
| + // navigation |request| can be served via AppCache. If yes it creates an |
| + // instance of the AppCacheURLLoaderFactory class and passes it to the |
| + // |callback|. If not it invokes the callback with a null factory which |
| + // indicates that the next handler should be invoked. |
| + static AppCacheURLLoader* CreateForNavigationRequest( |
| + const ResourceRequest& request, |
| + ChromeAppCacheService* appcache_service, |
| + LoaderFactoryCallback callback, |
| + URLLoaderFactoryGetter* factory_getter) { |
| + AppCacheURLLoader* loader = new AppCacheURLLoader( |
| + request, appcache_service, std::move(callback), factory_getter); |
| + return loader; |
| + } |
| + |
| + // Creates an instance of the AppCacheURLLoader class which is used to handle |
| + // URL requests from the client. We expect this function to be called for |
| + // handling subresource requests. |
| + static AppCacheURLLoader* CreateForURLLoads( |
| + const ResourceRequest& request, |
| + mojom::URLLoaderRequest url_loader_request, |
| + int32_t routing_id, |
| + int32_t request_id, |
| + mojom::URLLoaderClientPtr client_info, |
| + ChromeAppCacheService* appcache_service, |
| + URLLoaderFactoryGetter* factory_getter) { |
| + AppCacheURLLoader* loader = new AppCacheURLLoader( |
| + request, std::move(url_loader_request), routing_id, request_id, |
| + std::move(client_info), appcache_service, factory_getter); |
| + return loader; |
| } |
| ~AppCacheURLLoader() override {} |
| - void Start() { |
| + // Starts the process of checking if the request can be served from the |
| + // AppCache. |
| + void Start(AppCacheHost* host) { |
| + DCHECK(host); |
| // If the origin does not exist in the AppCache usage map, then we can |
| - // safely call the network service here. |
| - if (appcache_service_->storage()->usage_map()->find( |
| + // safely call the network service or pass it to the next handler. |
| + if (appcache_service_->storage()->IsInitialized() && |
| + appcache_service_->storage()->usage_map()->find( |
| request_.url.GetOrigin()) == |
| - appcache_service_->storage()->usage_map()->end()) { |
| - factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| - mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| - mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| + appcache_service_->storage()->usage_map()->end()) { |
| + SendNetworkResponse(); |
| return; |
| } |
| - appcache_service_->storage()->FindResponseForMainRequest(request_.url, |
| - GURL(), this); |
| + if (IsResourceTypeFrame(request_.resource_type)) { |
| + handler_ = host->CreateRequestHandler( |
| + AppCacheURLLoaderRequest::Create(request_), request_.resource_type, |
| + request_.should_reset_appcache); |
| + |
| + AppCacheJob* job = handler_->MaybeLoadResource(nullptr); |
| + if (!job) { |
| + SendNetworkResponse(); |
| + return; |
| + } |
| + |
| + job_.reset(job->AsURLLoaderJob()); |
| + job_->set_delegate(this); |
| + } else { |
| + // We should not be hitting this yet. We have to plumb the AppCache |
| + // response through to the renderer before we receive requests for sub |
| + // resources. |
| + DCHECK(false); |
| + } |
| + } |
| + |
| + // Binds the client end point with this instance. |
| + void BindEndpoints(const ResourceRequest& request, |
| + mojom::URLLoaderRequest url_loader_request, |
| + int32_t routing_id, |
| + int32_t request_id, |
| + mojom::URLLoaderClientPtrInfo client_info) { |
| + request_ = request; |
| + |
| + binding_.reset(new mojo::Binding<mojom::URLLoader>( |
| + this, std::move(url_loader_request))); |
| + binding_->set_connection_error_handler(base::Bind( |
| + &AppCacheURLLoader::OnConnectionError, base::Unretained(this))); |
| + |
| + routing_id_ = routing_id; |
| + request_id_ = request_id; |
| + |
| + client_info_.Bind(std::move(client_info)); |
| + |
| + // We pass the cached AppCache response and the data to the client for |
| + // navigation requests. |
| + if (IsNavigationRequest()) { |
| + DCHECK(cached_response_info_.get()); |
| + HandleAppCacheResponseInternal(cached_response_info_.get()); |
| + cached_response_info_ = nullptr; |
| + |
| + if (cached_buffer_.get()) { |
| + HandleAppCacheDataInternal(cached_buffer_.get(), cached_buffer_size_); |
| + cached_buffer_ = nullptr; |
| + cached_buffer_size_ = 0; |
| + } |
| + } |
| } |
| // mojom::URLLoader implementation: |
| @@ -73,39 +150,150 @@ class AppCacheURLLoader : public AppCacheStorage::Delegate, |
| DCHECK(false); |
| } |
| - private: |
| - // AppCacheStorage::Delegate methods. |
| - void OnMainResponseFound(const GURL& url, |
| - const AppCacheEntry& entry, |
| - const GURL& fallback_url, |
| - const AppCacheEntry& fallback_entry, |
| - int64_t cache_id, |
| - int64_t group_id, |
| - const GURL& manifest_url) override { |
| - AppCachePolicy* policy = appcache_service_->appcache_policy(); |
| - bool was_blocked_by_policy = |
| - !manifest_url.is_empty() && policy && |
| - !policy->CanLoadAppCache(manifest_url, |
| - request_.first_party_for_cookies); |
| - |
| - if (was_blocked_by_policy || !entry.has_response_id() || |
| - cache_id == kAppCacheNoCacheId) { |
| - factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| - mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| - mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| + // AppCacheURLLoaderJob::Delegate overrides. |
| + void SendNetworkResponse() override { |
| + // For a navigation request we invoke the LoaderFactoryCallback with null to |
| + // ensure that the next handler gets a stab at the request. |
| + if (IsNavigationRequest()) { |
| + std::move(callback_).Run(nullptr); |
| + base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| } else { |
| - DLOG(WARNING) << "AppCache found for url " << url |
| - << " Returning AppCache factory\n"; |
| // TODO(ananta) |
| - // Provide the plumbing to initiate AppCache requests here. |
| + // We probably need to chain to the next factory instead of always |
|
jam
2017/06/01 15:00:46
can you explain this some more to me? Isn't this g
ananta
2017/06/01 17:28:01
Yes. I was wondering about chain of factories with
|
| + // passing the request to the network factory. |
| factory_getter_->GetNetworkFactory()->get()->CreateLoaderAndStart( |
| mojo::MakeRequest(&network_loader_request_), routing_id_, request_id_, |
| mojom::kURLLoadOptionSendSSLInfo, request_, std::move(client_info_)); |
| } |
| } |
| + void SendErrorResponse() override { |
| + DLOG(WARNING) << "Sending error response for URL: " << request_.url; |
| + } |
| + |
| + void HandleAppCacheResponseStart(AppCacheResponseInfo* response_info) |
| + override { |
| + DCHECK(response_info); |
| + DCHECK(response_info->http_response_info()); |
| + |
| + // For a navigation request, we create an instance of the |
| + // AppCacheURLLoaderFactory class which implements the URLLoaderFactory |
| + // interface and pass it to the LoaderFactoryCallback. |
| + if (IsNavigationRequest()) { |
| + // Cache the response here to be passed to the client when it connects to |
| + // us. |
| + cached_response_info_ = response_info; |
| + mojom::URLLoaderFactoryPtr appcache_factory; |
| + AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
| + mojo::MakeRequest(&appcache_factory), appcache_service_.get(), |
| + factory_getter_.get(), this, 0); |
| + std::move(callback_).Run(appcache_factory.get()); |
| + return; |
| + } |
| + |
| + HandleAppCacheResponseInternal(response_info); |
| + } |
| + |
| + void HandleAppCacheData(net::IOBuffer* buffer, int buffer_size) override { |
| + DLOG(WARNING) << "Received AppCache data for URL: " << request_.url; |
| + |
| + // Cache the buffer and the size. We will use it later when the client |
| + // connects to us via URLLoaderFactory::CreateLoaderAndStart(). |
| + |
| + // If the cached AppCacheResponseInfo member is null, it means that the |
| + // client has already connected to us. It is safe to forward the buffer |
| + // directly to the client. |
| + if (IsNavigationRequest() && cached_response_info_.get()) { |
| + cached_buffer_ = buffer; |
| + cached_buffer_size_ = buffer_size; |
| + return; |
| + } |
| + |
| + HandleAppCacheDataInternal(buffer, buffer_size); |
| + } |
| + |
| + private: |
| + // This ctor is used for checking if navigation requests can be served from |
| + // the AppCache. |
| + AppCacheURLLoader(const ResourceRequest& request, |
| + ChromeAppCacheService* appcache_service, |
| + LoaderFactoryCallback callback, |
| + URLLoaderFactoryGetter* factory_getter) |
| + : appcache_service_(appcache_service), |
| + factory_getter_(factory_getter), |
| + request_(request), |
| + routing_id_(-1), |
| + request_id_(-1), |
| + callback_(std::move(callback)), |
| + cached_buffer_size_(0), |
| + navigation_request_(true) {} |
| + |
| + // This ctor is used for handling URL load requests. |
| + AppCacheURLLoader(const ResourceRequest& request, |
| + mojom::URLLoaderRequest url_loader_request, |
| + int32_t routing_id, |
| + int32_t request_id, |
| + mojom::URLLoaderClientPtr client_info, |
| + ChromeAppCacheService* appcache_service, |
| + URLLoaderFactoryGetter* factory_getter) |
| + : appcache_service_(appcache_service), |
| + factory_getter_(factory_getter), |
| + cached_buffer_size_(0), |
| + navigation_request_(false) { |
| + BindEndpoints(request, std::move(url_loader_request), routing_id, |
| + request_id, client_info.PassInterface()); |
| + } |
| + |
| void OnConnectionError() { delete this; } |
| + bool IsNavigationRequest() const { return navigation_request_; } |
|
jam
2017/06/01 15:00:46
nit: no need for this helper method to look up a m
ananta
2017/06/01 17:28:01
Done.
|
| + |
| + void HandleAppCacheResponseInternal(AppCacheResponseInfo* response_info) { |
| + DLOG(WARNING) << "Received AppCache response for URL: " << request_.url; |
| + |
| + const net::HttpResponseInfo* http_info = |
| + response_info->http_response_info(); |
| + |
| + ResourceResponseHead response_head; |
| + response_head.headers = new net::HttpResponseHeaders(""); |
| + |
| + std::string name; |
| + std::string value; |
| + |
| + for (size_t it = 0; |
| + http_info->headers->EnumerateHeaderLines(&it, &name, &value);) { |
| + response_head.headers->AddHeader(name + ": " + value); |
| + } |
| + |
| + // TODO(ananta) |
| + // Copy more fields. |
| + http_info->headers->GetMimeType(&response_head.mime_type); |
| + http_info->headers->GetCharset(&response_head.charset); |
| + |
| + response_head.request_time = http_info->request_time; |
| + response_head.response_time = http_info->response_time; |
| + response_head.content_length = response_info->response_data_size(); |
| + |
| + client_info_->OnReceiveResponse(response_head, http_info->ssl_info, |
| + mojom::DownloadedTempFilePtr()); |
| + } |
| + |
| + void HandleAppCacheDataInternal(net::IOBuffer* buffer, int buffer_size) { |
| + uint32_t bytes_written = static_cast<uint32_t>(buffer_size); |
| + mojo::WriteDataRaw(data_pipe_.producer_handle.get(), buffer->data(), |
| + &bytes_written, MOJO_WRITE_DATA_FLAG_NONE); |
| + client_info_->OnStartLoadingResponseBody( |
| + std::move(data_pipe_.consumer_handle)); |
| + } |
| + |
| + // Used to query AppCacheStorage to see if a request can be served out of the |
| + /// AppCache. |
| + scoped_refptr<ChromeAppCacheService> appcache_service_; |
| + |
| + // Used to retrieve the network service factory to pass requests to the |
| + // network service. |
| + scoped_refptr<URLLoaderFactoryGetter> factory_getter_; |
| + |
| // The current request. |
| ResourceRequest request_; |
| @@ -123,27 +311,91 @@ class AppCacheURLLoader : public AppCacheStorage::Delegate, |
| // about the URL load |
| mojom::URLLoaderClientPtr client_info_; |
| - // Used to query AppCacheStorage to see if a request can be served out of the |
| - /// AppCache. |
| - scoped_refptr<ChromeAppCacheService> appcache_service_; |
| + // Binds the URLLoaderClient with us. |
| + std::unique_ptr<mojo::Binding<mojom::URLLoader>> binding_; |
| - // Used to retrieve the network service factory to pass requests to the |
| - // network service. |
| - scoped_refptr<URLLoaderFactoryGetter> factory_getter_; |
| + // The handler for the AppCache request. |
| + std::unique_ptr<AppCacheRequestHandler> handler_; |
| - // Binds the URLLoaderClient with us. |
| - mojo::Binding<mojom::URLLoader> binding_; |
| + // The job which gets invoked by the handler to deliver the responses to the |
| + // client. |
| + std::unique_ptr<AppCacheURLLoaderJob> job_; |
| + |
| + // The data pipe used to transfer AppCache data to the client. |
| + mojo::DataPipe data_pipe_; |
| + |
| + // Callback to be invoked with the AppCache factory or null to fall through |
| + // to the next handler in the chain. |
| + LoaderFactoryCallback callback_; |
| + |
| + // Cached AppCache response to be used later when we are initialized with |
| + // the connection end point. |
| + scoped_refptr<AppCacheResponseInfo> cached_response_info_; |
| + |
| + // Cached IOBuffer to be used later when we are initialized with the |
| + // connection end point. |
| + scoped_refptr<net::IOBuffer> cached_buffer_; |
| + |
| + // Cached buffer size to be used later when we are initialized with the |
| + // connection end point. |
| + int cached_buffer_size_; |
| + |
| + // Set to true for navigation requests. |
| + bool navigation_request_; |
| DISALLOW_COPY_AND_ASSIGN(AppCacheURLLoader); |
| }; |
| -} // namespace |
| +// This class is instantiated for navigation requests. It initiates checks on |
| +// whether the request can be serviced via the AppCache. |
| +// It uses the AppCacheURLLoader class defined above for this. If the request |
| +// cannot be serviced via AppCache, the AppCacheURLLoader class calls |
| +// the LoaderFactoryCallback with a null factory which ensures that the request |
| +// goes to the next handler in the chain. |
| +// If the request can be serviced with AppCache the AppCacheURLLoader class |
| +// creates an instance of the AppCacheURLLoaderFactory class and passes it to |
| +// the LoaderFactoryCallback function. |
| +class AppCacheNavigationRequestHandler : public URLLoaderRequestHandler { |
| + public: |
| + explicit AppCacheNavigationRequestHandler( |
|
jam
2017/06/01 15:00:46
nit: explicit not needed
ananta
2017/06/01 17:28:01
Done.
|
| + AppCacheNavigationHandleCore* appcache_handle_core, |
| + URLLoaderFactoryGetter* url_loader_factory_getter) |
| + : appcache_core_(appcache_handle_core), |
| + factory_getter_(url_loader_factory_getter) {} |
| + |
| + void MaybeCreateLoaderFactory(const ResourceRequest& resource_request, |
| + ResourceContext* resource_context, |
| + LoaderFactoryCallback callback) override { |
| + // The AppCacheURLLoader instance will be deleted when the URL load request |
| + // completes. It can complete in the following ways: |
| + // 1. The request is serviced via AppCache. In this case it is deleted when |
| + // the connection is dropped by the client. |
| + // 2. The request is serviced by the next handler. |
| + AppCacheURLLoader* loader = AppCacheURLLoader::CreateForNavigationRequest( |
| + resource_request, appcache_core_->GetAppCacheService(), |
| + std::move(callback), factory_getter_.get()); |
| + loader->Start(appcache_core_->host()); |
| + } |
| + |
| + private: |
| + AppCacheNavigationHandleCore* appcache_core_; |
| + // Used to retrieve the network service factory to pass requests to the |
| + // network service. |
| + scoped_refptr<URLLoaderFactoryGetter> factory_getter_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AppCacheNavigationRequestHandler); |
| +}; |
| // Implements the URLLoaderFactory mojom for AppCache requests. |
| AppCacheURLLoaderFactory::AppCacheURLLoaderFactory( |
| ChromeAppCacheService* appcache_service, |
| - URLLoaderFactoryGetter* factory_getter) |
| - : appcache_service_(appcache_service), factory_getter_(factory_getter) {} |
| + URLLoaderFactoryGetter* factory_getter, |
| + AppCacheURLLoader* navigation_url_loader, |
| + int process_id) |
| + : appcache_service_(appcache_service), |
| + factory_getter_(factory_getter), |
| + navigation_url_loader_(navigation_url_loader), |
| + process_id_(process_id) {} |
| AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
| @@ -151,14 +403,28 @@ AppCacheURLLoaderFactory::~AppCacheURLLoaderFactory() {} |
| void AppCacheURLLoaderFactory::CreateURLLoaderFactory( |
| mojom::URLLoaderFactoryRequest request, |
| ChromeAppCacheService* appcache_service, |
| - URLLoaderFactoryGetter* factory_getter) { |
| + URLLoaderFactoryGetter* factory_getter, |
| + AppCacheURLLoader* navigation_url_loader, |
| + int process_id) { |
| std::unique_ptr<AppCacheURLLoaderFactory> factory_instance( |
| - new AppCacheURLLoaderFactory(appcache_service, factory_getter)); |
| + new AppCacheURLLoaderFactory(appcache_service, factory_getter, |
| + navigation_url_loader, process_id)); |
| AppCacheURLLoaderFactory* raw_factory = factory_instance.get(); |
| raw_factory->loader_factory_bindings_.AddBinding(std::move(factory_instance), |
| std::move(request)); |
| } |
| +// static |
| +std::unique_ptr<URLLoaderRequestHandler> |
| +AppCacheURLLoaderFactory::CreateRequestHandler( |
| + AppCacheNavigationHandleCore* appcache_handle_core, |
| + URLLoaderFactoryGetter* url_loader_factory_getter) { |
| + std::unique_ptr<URLLoaderRequestHandler> handler( |
| + new AppCacheNavigationRequestHandler(appcache_handle_core, |
| + url_loader_factory_getter)); |
| + return handler; |
| +} |
| + |
| void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
| mojom::URLLoaderRequest url_loader_request, |
| int32_t routing_id, |
| @@ -168,11 +434,25 @@ void AppCacheURLLoaderFactory::CreateLoaderAndStart( |
| mojom::URLLoaderClientPtr client) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + AppCacheHost* host = nullptr; |
| + // If we have an ongoing navigation, bind the endpoints here. |
| + if (navigation_url_loader_) { |
| + navigation_url_loader_->BindEndpoints( |
| + request, std::move(url_loader_request), routing_id, request_id, |
| + client.PassInterface()); |
| + return; |
| + } else { |
| + AppCacheBackendImpl* backend = appcache_service_->GetBackend(process_id_); |
| + DCHECK(backend); |
| + host = backend->GetHost(request.appcache_host_id); |
| + DCHECK(host); |
| + } |
| + |
| // This will get deleted when the connection is dropped by the client. |
| - AppCacheURLLoader* loader = new AppCacheURLLoader( |
| + AppCacheURLLoader* loader = AppCacheURLLoader::CreateForURLLoads( |
| request, std::move(url_loader_request), routing_id, request_id, |
| std::move(client), appcache_service_.get(), factory_getter_.get()); |
| - loader->Start(); |
| + loader->Start(host); |
| } |
| void AppCacheURLLoaderFactory::SyncLoad(int32_t routing_id, |