| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/loader/navigation_url_loader_impl_core.h" | 5 #include "content/browser/loader/navigation_url_loader_impl_core.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/browser/frame_host/navigation_request_info.h" | 10 #include "content/browser/frame_host/navigation_request_info.h" |
| 11 #include "content/browser/loader/navigation_resource_handler.h" | 11 #include "content/browser/loader/navigation_resource_handler.h" |
| 12 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 12 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/common/navigation_params.h" | 14 #include "content/common/navigation_params.h" |
| 15 #include "content/common/service_worker/service_worker_types.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/stream_handle.h" | 17 #include "content/public/browser/stream_handle.h" |
| 16 #include "content/public/common/resource_response.h" | 18 #include "content/public/common/resource_response.h" |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "net/url_request/redirect_info.h" | 20 #include "net/url_request/redirect_info.h" |
| 19 | 21 |
| 20 namespace content { | 22 namespace content { |
| 21 | 23 |
| 22 NavigationURLLoaderImplCore::NavigationURLLoaderImplCore( | 24 NavigationURLLoaderImplCore::NavigationURLLoaderImplCore( |
| 23 const base::WeakPtr<NavigationURLLoaderImpl>& loader) | 25 const base::WeakPtr<NavigationURLLoaderImpl>& loader, |
| 26 ServiceWorkerContext* service_worker_context) |
| 24 : loader_(loader), | 27 : loader_(loader), |
| 28 service_worker_context_( |
| 29 static_cast<ServiceWorkerContextWrapper*>(service_worker_context)), |
| 25 resource_handler_(nullptr) { | 30 resource_handler_(nullptr) { |
| 26 // This object is created on the UI thread but otherwise is accessed and | 31 // This object is created on the UI thread but otherwise is accessed and |
| 27 // deleted on the IO thread. | 32 // deleted on the IO thread. |
| 28 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 33 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 29 } | 34 } |
| 30 | 35 |
| 31 NavigationURLLoaderImplCore::~NavigationURLLoaderImplCore() { | 36 NavigationURLLoaderImplCore::~NavigationURLLoaderImplCore() { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 33 | 38 |
| 34 if (resource_handler_) | 39 if (resource_handler_) |
| 35 resource_handler_->Cancel(); | 40 resource_handler_->Cancel(); |
| 36 } | 41 } |
| 37 | 42 |
| 38 void NavigationURLLoaderImplCore::Start( | 43 void NavigationURLLoaderImplCore::Start( |
| 39 ResourceContext* resource_context, | 44 ResourceContext* resource_context, |
| 40 int frame_tree_node_id, | 45 int frame_tree_node_id, |
| 41 scoped_ptr<NavigationRequestInfo> request_info) { | 46 scoped_ptr<NavigationRequestInfo> request_info) { |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 47 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 43 | 48 |
| 44 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
| 45 BrowserThread::UI, FROM_HERE, | 50 BrowserThread::UI, FROM_HERE, |
| 46 base::Bind(&NavigationURLLoaderImpl::NotifyRequestStarted, loader_, | 51 base::Bind(&NavigationURLLoaderImpl::NotifyRequestStarted, loader_, |
| 47 base::TimeTicks::Now())); | 52 base::TimeTicks::Now())); |
| 48 | 53 |
| 49 ResourceDispatcherHostImpl::Get()->BeginNavigationRequest( | 54 ResourceDispatcherHostImpl::Get()->BeginNavigationRequest( |
| 50 resource_context, frame_tree_node_id, | 55 resource_context, frame_tree_node_id, *request_info, this, |
| 51 *request_info, this); | 56 service_worker_context_.get()); |
| 52 } | 57 } |
| 53 | 58 |
| 54 void NavigationURLLoaderImplCore::FollowRedirect() { | 59 void NavigationURLLoaderImplCore::FollowRedirect() { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 56 | 61 |
| 57 if (resource_handler_) | 62 if (resource_handler_) |
| 58 resource_handler_->FollowRedirect(); | 63 resource_handler_->FollowRedirect(); |
| 59 } | 64 } |
| 60 | 65 |
| 66 void NavigationURLLoaderImplCore::SetServiceWorkerProviderHost( |
| 67 scoped_ptr<ServiceWorkerProviderHost> service_worker_provider_host) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 69 service_worker_provider_host_ = service_worker_provider_host.Pass(); |
| 70 } |
| 61 | 71 |
| 62 void NavigationURLLoaderImplCore::NotifyRequestRedirected( | 72 void NavigationURLLoaderImplCore::NotifyRequestRedirected( |
| 63 const net::RedirectInfo& redirect_info, | 73 const net::RedirectInfo& redirect_info, |
| 64 ResourceResponse* response) { | 74 ResourceResponse* response) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 66 | 76 |
| 67 // Make a copy of the ResourceResponse before it is passed to another thread. | 77 // Make a copy of the ResourceResponse before it is passed to another thread. |
| 68 // | 78 // |
| 69 // TODO(davidben): This copy could be avoided if ResourceResponse weren't | 79 // TODO(davidben): This copy could be avoided if ResourceResponse weren't |
| 70 // reference counted and the loader stack passed unique ownership of the | 80 // reference counted and the loader stack passed unique ownership of the |
| 71 // response. https://crbug.com/416050 | 81 // response. https://crbug.com/416050 |
| 72 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
| 73 BrowserThread::UI, FROM_HERE, | 83 BrowserThread::UI, FROM_HERE, |
| 74 base::Bind(&NavigationURLLoaderImpl::NotifyRequestRedirected, loader_, | 84 base::Bind(&NavigationURLLoaderImpl::NotifyRequestRedirected, loader_, |
| 75 redirect_info, response->DeepCopy())); | 85 redirect_info, response->DeepCopy())); |
| 76 } | 86 } |
| 77 | 87 |
| 78 void NavigationURLLoaderImplCore::NotifyResponseStarted( | 88 void NavigationURLLoaderImplCore::NotifyResponseStarted( |
| 79 ResourceResponse* response, | 89 ResourceResponse* response, |
| 80 scoped_ptr<StreamHandle> body) { | 90 scoped_ptr<StreamHandle> body) { |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 92 service_worker_context_->RegisterNavigationProviderHost( |
| 93 body->GetURL(), service_worker_provider_host_.Pass()); |
| 82 | 94 |
| 83 // If, by the time the task reaches the UI thread, |loader_| has already been | 95 // If, by the time the task reaches the UI thread, |loader_| has already been |
| 84 // destroyed, NotifyResponseStarted will not run. |body| will be destructed | 96 // destroyed, NotifyResponseStarted will not run. |body| will be destructed |
| 85 // and the request released at that point. | 97 // and the request released at that point. |
| 86 | 98 |
| 87 // Make a copy of the ResourceResponse before it is passed to another thread. | 99 // Make a copy of the ResourceResponse before it is passed to another thread. |
| 88 // | 100 // |
| 89 // TODO(davidben): This copy could be avoided if ResourceResponse weren't | 101 // TODO(davidben): This copy could be avoided if ResourceResponse weren't |
| 90 // reference counted and the loader stack passed unique ownership of the | 102 // reference counted and the loader stack passed unique ownership of the |
| 91 // response. https://crbug.com/416050 | 103 // response. https://crbug.com/416050 |
| 92 BrowserThread::PostTask( | 104 BrowserThread::PostTask( |
| 93 BrowserThread::UI, FROM_HERE, | 105 BrowserThread::UI, FROM_HERE, |
| 94 base::Bind(&NavigationURLLoaderImpl::NotifyResponseStarted, loader_, | 106 base::Bind(&NavigationURLLoaderImpl::NotifyResponseStarted, loader_, |
| 95 response->DeepCopy(), base::Passed(&body))); | 107 response->DeepCopy(), base::Passed(&body))); |
| 96 } | 108 } |
| 97 | 109 |
| 98 void NavigationURLLoaderImplCore::NotifyRequestFailed(bool in_cache, | 110 void NavigationURLLoaderImplCore::NotifyRequestFailed(bool in_cache, |
| 99 int net_error) { | 111 int net_error) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 101 | 113 |
| 102 BrowserThread::PostTask( | 114 BrowserThread::PostTask( |
| 103 BrowserThread::UI, FROM_HERE, | 115 BrowserThread::UI, FROM_HERE, |
| 104 base::Bind(&NavigationURLLoaderImpl::NotifyRequestFailed, loader_, | 116 base::Bind(&NavigationURLLoaderImpl::NotifyRequestFailed, loader_, |
| 105 in_cache, net_error)); | 117 in_cache, net_error)); |
| 106 } | 118 } |
| 107 | 119 |
| 108 } // namespace content | 120 } // namespace content |
| OLD | NEW |