Chromium Code Reviews| Index: content/browser/loader/resource_dispatcher_host_impl.cc |
| diff --git a/content/browser/loader/resource_dispatcher_host_impl.cc b/content/browser/loader/resource_dispatcher_host_impl.cc |
| index 4de8a0874c8d0f7434c35b0f0506527284926508..0f47073264ffb02057e2bd453f297d8accf1a91f 100644 |
| --- a/content/browser/loader/resource_dispatcher_host_impl.cc |
| +++ b/content/browser/loader/resource_dispatcher_host_impl.cc |
| @@ -538,6 +538,93 @@ ResourceDispatcherHost* ResourceDispatcherHost::Get() { |
| return g_resource_dispatcher_host; |
| } |
| +// This is a helper class consisting of mojo-related functionalities used in |
| +// ResourceDispatcherHostImpl. |
|
mmenke
2016/05/24 20:01:35
This is confusing...Why is the filter calling into
yhirano
2016/05/25 12:47:06
Done.
|
| +class ResourceDispatcherHostImpl::MojoHelper final { |
| + public: |
| + explicit MojoHelper(ResourceDispatcherHostImpl* owner) : owner_(owner) {} |
| + |
| + // Sends |message| via mojo. Returns true if it sends the message. |
| + bool Send(const IPC::Message& message, ResourceMessageFilter* filter) { |
| + if (IPC_MESSAGE_ID_CLASS(message.type()) != ResourceMsgStart) |
| + return false; |
| + |
| + base::PickleIterator iter(message); |
| + int request_id = -1; |
| + bool ok = iter.ReadInt(&request_id); |
| + DCHECK(ok); |
| + ResourceLoader* loader = owner_->GetLoader(filter->child_id(), request_id); |
| + if (!loader || !loader->client()) |
| + return false; |
| + |
| + DCHECK(!filter_); |
| + filter_ = filter; |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(MojoHelper, message) |
| + IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) |
| + IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete, OnRequestComplete) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + |
| + filter_ = nullptr; |
| + return handled; |
| + } |
| + |
| + // Adds |loader| as an uninitiated loader. |
| + void AddUninitiatedURLLoader(int child_id, |
| + std::unique_ptr<mojom::URLLoader> loader) { |
| + mojom::URLLoader* raw = loader.get(); |
| + uninitiated_url_loaders_.insert( |
| + std::make_pair(raw, std::make_pair(child_id, std::move(loader)))); |
| + } |
| + |
| + // Takes and returns the uninitiated loader whose address equals to |loader|. |
| + std::unique_ptr<mojom::URLLoader> TakeUninitiatedURLLoader( |
| + mojom::URLLoader* loader) { |
| + auto it = uninitiated_url_loaders_.find(loader); |
| + if (it == uninitiated_url_loaders_.end()) |
| + return nullptr; |
| + std::unique_ptr<mojom::URLLoader> result = std::move(it->second.second); |
| + uninitiated_url_loaders_.erase(it); |
| + return result; |
| + } |
| + |
| + // Cancels all uninitiated loaders for |child_id|. |
| + void CancelUninitiatedLoaders(int child_id) { |
| + auto it = uninitiated_url_loaders_.begin(); |
| + while (it != uninitiated_url_loaders_.end()) { |
| + if (it->second.first == child_id) { |
| + it = uninitiated_url_loaders_.erase(it); |
| + } else { |
| + ++it; |
| + } |
| + } |
| + } |
| + |
| + private: |
| + void OnReceivedResponse(int request_id, const ResourceResponseHead& head) { |
| + int child_id = filter_->child_id(); |
| + ResourceLoader* loader = owner_->GetLoader(child_id, request_id); |
| + loader->client()->OnReceiveResponse(head); |
| + } |
| + |
| + void OnRequestComplete(int request_id, |
| + const ResourceRequestCompletionStatus& status) { |
| + int child_id = filter_->child_id(); |
| + ResourceLoader* loader = owner_->GetLoader(child_id, request_id); |
| + mojom::URLLoaderClient* client = loader->client(); |
| + |
| + client->OnComplete(status); |
| + } |
| + |
| + ResourceDispatcherHostImpl* owner_; |
| + ResourceMessageFilter* filter_ = nullptr; |
| + std::map<mojom::URLLoader*, std::pair<int, std::unique_ptr<mojom::URLLoader>>> |
| + uninitiated_url_loaders_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MojoHelper); |
| +}; |
| + |
| ResourceDispatcherHostImpl::ResourceDispatcherHostImpl() |
| : save_file_manager_(new SaveFileManager()), |
| request_id_(-1), |
| @@ -551,7 +638,8 @@ ResourceDispatcherHostImpl::ResourceDispatcherHostImpl() |
| filter_(NULL), |
| delegate_(NULL), |
| allow_cross_origin_auth_prompt_(false), |
| - cert_store_for_testing_(nullptr) { |
| + cert_store_for_testing_(nullptr), |
| + mojo_helper_(new MojoHelper(this)) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| DCHECK(!g_resource_dispatcher_host); |
| g_resource_dispatcher_host = this; |
| @@ -584,6 +672,7 @@ ResourceDispatcherHostImpl::ResourceDispatcherHostImpl() |
| ResourceDispatcherHostImpl::~ResourceDispatcherHostImpl() { |
| DCHECK(outstanding_requests_stats_map_.empty()); |
| DCHECK(g_resource_dispatcher_host); |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| g_resource_dispatcher_host = NULL; |
| } |
| @@ -1178,6 +1267,7 @@ void ResourceDispatcherHostImpl::OnShutdown() { |
| bool ResourceDispatcherHostImpl::OnMessageReceived( |
| const IPC::Message& message, |
| ResourceMessageFilter* filter) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| filter_ = filter; |
| bool handled = true; |
| IPC_BEGIN_MESSAGE_MAP(ResourceDispatcherHostImpl, message) |
| @@ -1219,6 +1309,16 @@ void ResourceDispatcherHostImpl::OnRequestResource( |
| int routing_id, |
| int request_id, |
| const ResourceRequest& request_data) { |
| + OnRequestResourceInternal(routing_id, request_id, request_data, nullptr, |
| + nullptr); |
| +} |
| + |
| +void ResourceDispatcherHostImpl::OnRequestResourceInternal( |
| + int routing_id, |
| + int request_id, |
| + const ResourceRequest& request_data, |
| + std::unique_ptr<mojom::URLLoader> url_loader, |
| + mojom::URLLoaderClientPtr url_loader_client) { |
| // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed. |
| tracked_objects::ScopedTracker tracking_profile( |
| FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| @@ -1239,7 +1339,8 @@ void ResourceDispatcherHostImpl::OnRequestResource( |
| request_data.render_frame_id, |
| request_data.url)); |
| } |
| - BeginRequest(request_id, request_data, NULL, routing_id); |
| + BeginRequest(request_id, request_data, NULL, routing_id, |
| + std::move(url_loader), std::move(url_loader_client)); |
| } |
| // Begins a resource request with the given params on behalf of the specified |
| @@ -1369,7 +1470,9 @@ void ResourceDispatcherHostImpl::BeginRequest( |
| int request_id, |
| const ResourceRequest& request_data, |
| IPC::Message* sync_result, // only valid for sync |
| - int route_id) { |
| + int route_id, |
| + std::unique_ptr<mojom::URLLoader> url_loader, |
| + mojom::URLLoaderClientPtr url_loader_client) { |
| int process_type = filter_->process_type(); |
| int child_id = filter_->child_id(); |
| @@ -1628,10 +1731,11 @@ void ResourceDispatcherHostImpl::BeginRequest( |
| std::unique_ptr<ResourceHandler> handler(CreateResourceHandler( |
| new_request.get(), request_data, sync_result, route_id, process_type, |
| - child_id, resource_context)); |
| + child_id, resource_context, url_loader_client != nullptr)); |
| if (handler) |
| - BeginRequestInternal(std::move(new_request), std::move(handler)); |
| + BeginRequestInternal(std::move(new_request), std::move(handler), |
| + std::move(url_loader), std::move(url_loader_client)); |
| } |
| std::unique_ptr<ResourceHandler> |
| @@ -1642,7 +1746,8 @@ ResourceDispatcherHostImpl::CreateResourceHandler( |
| int route_id, |
| int process_type, |
| int child_id, |
| - ResourceContext* resource_context) { |
| + ResourceContext* resource_context, |
| + bool using_mojo) { |
| // TODO(pkasting): Remove ScopedTracker below once crbug.com/456331 is fixed. |
| tracked_objects::ScopedTracker tracking_profile( |
| FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| @@ -1650,6 +1755,7 @@ ResourceDispatcherHostImpl::CreateResourceHandler( |
| // Construct the IPC resource handler. |
| std::unique_ptr<ResourceHandler> handler; |
| if (sync_result) { |
| + DCHECK(!using_mojo); |
| // download_to_file is not supported for synchronous requests. |
| if (request_data.download_to_file) { |
| bad_message::ReceivedBadMessage(filter_, bad_message::RDH_BAD_DOWNLOAD); |
| @@ -1658,7 +1764,7 @@ ResourceDispatcherHostImpl::CreateResourceHandler( |
| handler.reset(new SyncResourceHandler(request, sync_result, this)); |
| } else { |
| - handler.reset(new AsyncResourceHandler(request, this)); |
| + handler.reset(new AsyncResourceHandler(request, this, using_mojo)); |
|
mmenke
2016/05/24 20:06:07
Why doesn't the AsyncResourceHandler take ownershi
mmenke
2016/05/25 04:39:03
Actually, can we just make a MojoResourceHandler,
yhirano
2016/05/25 12:47:06
Done.
|
| // The RedirectToFileResourceHandler depends on being next in the chain. |
| if (request_data.download_to_file) { |
| @@ -2064,6 +2170,11 @@ void ResourceDispatcherHostImpl::CancelRequestsForRoute( |
| CancelBlockedRequestsForRoute(route_id); |
| } |
| } |
| + |
| + // Uninitiated URLLoader has no routing ID, so it should be deleted only when |
| + // cancel_all_routes is specified. |
| + if (cancel_all_routes) |
| + mojo_helper_->CancelUninitiatedLoaders(child_id); |
| } |
| // Cancels the request and removes it from the list. |
| @@ -2351,6 +2462,47 @@ void ResourceDispatcherHostImpl::EnableStaleWhileRevalidateForTesting() { |
| async_revalidation_manager_.reset(new AsyncRevalidationManager); |
| } |
| +void ResourceDispatcherHostImpl::OnRequestResourceWithMojo( |
| + int routing_id, |
| + int request_id, |
| + const ResourceRequest& request, |
| + std::unique_ptr<mojom::URLLoader> url_loader, |
| + mojom::URLLoaderClientPtr url_loader_client, |
| + ResourceMessageFilter* filter) { |
| + filter_ = filter; |
| + OnRequestResourceInternal(routing_id, request_id, request, |
| + std::move(url_loader), |
| + std::move(url_loader_client)); |
| + filter_ = nullptr; |
| +} |
| + |
| +bool ResourceDispatcherHostImpl::SendWithMojoIfPossible( |
| + const IPC::Message& message, |
| + ResourceMessageFilter* filter) { |
| + return mojo_helper_->Send(message, filter); |
| +} |
| + |
| +void ResourceDispatcherHostImpl::OnStartLoadingResponseBodyWithMojo( |
| + const GlobalRequestID& id, |
| + mojo::ScopedDataPipeConsumerHandle response_body) { |
| + ResourceLoader* loader = GetLoader(id); |
| + if (!loader) |
| + return; |
| + |
| + loader->client()->OnStartLoadingResponseBody(std::move(response_body)); |
| +} |
| + |
| +void ResourceDispatcherHostImpl::AddUninitiatedURLLoader( |
| + int child_id, |
| + std::unique_ptr<mojom::URLLoader> loader) { |
| + mojo_helper_->AddUninitiatedURLLoader(child_id, std::move(loader)); |
| +} |
| + |
| +std::unique_ptr<mojom::URLLoader> |
| +ResourceDispatcherHostImpl::TakeUninitiatedURLLoader(mojom::URLLoader* loader) { |
| + return mojo_helper_->TakeUninitiatedURLLoader(loader); |
| +} |
| + |
| // static |
| int ResourceDispatcherHostImpl::CalculateApproximateMemoryCost( |
| net::URLRequest* request) { |
| @@ -2369,7 +2521,9 @@ int ResourceDispatcherHostImpl::CalculateApproximateMemoryCost( |
| void ResourceDispatcherHostImpl::BeginRequestInternal( |
| std::unique_ptr<net::URLRequest> request, |
| - std::unique_ptr<ResourceHandler> handler) { |
| + std::unique_ptr<ResourceHandler> handler, |
| + std::unique_ptr<mojom::URLLoader> url_loader, |
| + mojom::URLLoaderClientPtr url_loader_client) { |
| DCHECK(!request->is_pending()); |
| ResourceRequestInfoImpl* info = |
| ResourceRequestInfoImpl::ForRequest(request.get()); |
| @@ -2406,7 +2560,8 @@ void ResourceDispatcherHostImpl::BeginRequestInternal( |
| } |
| std::unique_ptr<ResourceLoader> loader(new ResourceLoader( |
| - std::move(request), std::move(handler), GetCertStore(), this)); |
| + std::move(request), std::move(handler), GetCertStore(), |
| + std::move(url_loader), std::move(url_loader_client), this)); |
| GlobalFrameRoutingId id(info->GetChildID(), info->GetRenderFrameID()); |
| BlockedLoadersMap::const_iterator iter = blocked_loaders_map_.find(id); |