Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/renderer_host/offline_resource_throttle.h" | 5 #include "chrome/browser/renderer_host/offline_resource_throttle.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "chrome/browser/chromeos/offline/offline_load_page.h" | 14 #include "chrome/browser/chromeos/offline/offline_load_page.h" |
| 15 #include "content/public/browser/appcache_service.h" | 15 #include "content/public/browser/appcache_service.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/render_view_host.h" | 17 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/resource_controller.h" | 18 #include "content/public/browser/resource_controller.h" |
| 19 #include "content/public/browser/resource_request_info.h" | 19 #include "content/public/browser/resource_request_info.h" |
| 20 #include "content/public/browser/service_worker_context.h" | |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 23 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 24 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 25 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 26 #include "url/url_constants.h" | 27 #include "url/url_constants.h" |
| 27 | 28 |
| 28 using content::BrowserThread; | 29 using content::BrowserThread; |
| 29 using content::RenderViewHost; | 30 using content::RenderViewHost; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 54 (new chromeos::OfflineLoadPage(web_contents, url, callback))->Show(); | 55 (new chromeos::OfflineLoadPage(web_contents, url, callback))->Show(); |
| 55 } | 56 } |
| 56 } | 57 } |
| 57 | 58 |
| 58 } // namespace | 59 } // namespace |
| 59 | 60 |
| 60 OfflineResourceThrottle::OfflineResourceThrottle( | 61 OfflineResourceThrottle::OfflineResourceThrottle( |
| 61 net::URLRequest* request, | 62 net::URLRequest* request, |
| 62 content::AppCacheService* appcache_service) | 63 content::AppCacheService* appcache_service) |
| 63 : request_(request), | 64 : request_(request), |
| 64 appcache_service_(appcache_service) { | 65 appcache_service_(appcache_service), |
| 66 pending_callbacks_(0) { | |
| 65 DCHECK(appcache_service); | 67 DCHECK(appcache_service); |
| 66 } | 68 } |
| 67 | 69 |
| 68 OfflineResourceThrottle::~OfflineResourceThrottle() { | 70 OfflineResourceThrottle::~OfflineResourceThrottle() { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 70 | 72 |
| 71 if (!appcache_completion_callback_.IsCancelled()) | 73 if (!appcache_completion_callback_.IsCancelled()) |
| 72 appcache_completion_callback_.Cancel(); | 74 appcache_completion_callback_.Cancel(); |
| 75 if (!service_worker_completion_callback_.IsCancelled()) | |
| 76 service_worker_completion_callback_.Cancel(); | |
| 73 } | 77 } |
| 74 | 78 |
| 75 void OfflineResourceThrottle::WillStartRequest(bool* defer) { | 79 void OfflineResourceThrottle::WillStartRequest(bool* defer) { |
| 76 if (!ShouldShowOfflinePage(request_->url())) | 80 if (!ShouldShowOfflinePage(request_->url())) |
| 77 return; | 81 return; |
| 78 | 82 |
| 79 DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url(); | 83 DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url(); |
| 80 | 84 |
| 81 const GURL* url = &(request_->url()); | 85 const GURL* url = &(request_->url()); |
| 82 const GURL* first_party = &(request_->first_party_for_cookies()); | 86 const GURL* first_party = &(request_->first_party_for_cookies()); |
| 83 | 87 |
| 84 // Anticipate a client-side HSTS based redirect from HTTP to HTTPS, and | 88 // Anticipate a client-side HSTS based redirect from HTTP to HTTPS, and |
| 85 // ask the appcache about the HTTPS url instead of the HTTP url. | 89 // ask the appcache about the HTTPS url instead of the HTTP url. |
| 86 GURL redirect_url; | 90 GURL redirect_url; |
| 87 if (request_->GetHSTSRedirect(&redirect_url)) { | 91 if (request_->GetHSTSRedirect(&redirect_url)) { |
| 88 if (url->GetOrigin() == first_party->GetOrigin()) | 92 if (url->GetOrigin() == first_party->GetOrigin()) |
| 89 first_party = &redirect_url; | 93 first_party = &redirect_url; |
| 90 url = &redirect_url; | 94 url = &redirect_url; |
| 91 } | 95 } |
| 92 | 96 |
| 97 pending_callbacks_ = 1; | |
| 98 | |
| 93 DCHECK(appcache_completion_callback_.IsCancelled()); | 99 DCHECK(appcache_completion_callback_.IsCancelled()); |
| 94 | |
| 95 appcache_completion_callback_.Reset( | 100 appcache_completion_callback_.Reset( |
| 96 base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete, | 101 base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete, |
| 97 AsWeakPtr())); | 102 AsWeakPtr())); |
| 98 appcache_service_->CanHandleMainResourceOffline( | 103 appcache_service_->CanHandleMainResourceOffline( |
| 99 *url, *first_party, | 104 *url, *first_party, |
| 100 appcache_completion_callback_.callback()); | 105 appcache_completion_callback_.callback()); |
| 101 | 106 |
| 107 DCHECK(service_worker_completion_callback_.IsCancelled()); | |
| 108 content::ServiceWorkerContext* service_worker_context = | |
| 109 content::ServiceWorkerContext::GetServiceWorkerContext(request_); | |
| 110 if (service_worker_context) { | |
| 111 pending_callbacks_ = 2; | |
| 112 service_worker_completion_callback_.Reset( | |
| 113 base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete, | |
| 114 AsWeakPtr())); | |
| 115 service_worker_context->CanHandleMainResourceOffline( | |
| 116 *url, *first_party, | |
| 117 service_worker_completion_callback_.callback()); | |
| 118 } | |
| 102 *defer = true; | 119 *defer = true; |
| 103 } | 120 } |
| 104 | 121 |
| 105 const char* OfflineResourceThrottle::GetNameForLogging() const { | 122 const char* OfflineResourceThrottle::GetNameForLogging() const { |
| 106 return "OfflineResourceThrottle"; | 123 return "OfflineResourceThrottle"; |
| 107 } | 124 } |
| 108 | 125 |
| 109 void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) { | 126 void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 111 | 128 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 122 url.SchemeIs(url::kHttpsScheme)); | 139 url.SchemeIs(url::kHttpsScheme)); |
| 123 } | 140 } |
| 124 | 141 |
| 125 bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const { | 142 bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const { |
| 126 // If the network is disconnected while loading other resources, we'll simply | 143 // If the network is disconnected while loading other resources, we'll simply |
| 127 // show broken link/images. | 144 // show broken link/images. |
| 128 return IsRemote(url) && net::NetworkChangeNotifier::IsOffline(); | 145 return IsRemote(url) && net::NetworkChangeNotifier::IsOffline(); |
| 129 } | 146 } |
| 130 | 147 |
| 131 void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) { | 148 void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) { |
| 132 appcache_completion_callback_.Cancel(); | 149 --pending_callbacks_; |
| 133 | 150 |
| 134 if (rv == net::OK) { | 151 if (rv == net::OK) { |
| 152 appcache_completion_callback_.Cancel(); | |
| 153 service_worker_completion_callback_.Cancel(); | |
| 135 controller()->Resume(); | 154 controller()->Resume(); |
| 136 } else { | 155 } else if (!pending_callbacks_) { |
| 156 appcache_completion_callback_.Cancel(); | |
| 157 service_worker_completion_callback_.Cancel(); | |
|
falken
2014/12/16 08:16:49
Would it improve readability to set pending_callba
michaeln
2015/01/09 01:47:36
Done, kinda
| |
| 137 const content::ResourceRequestInfo* info = | 158 const content::ResourceRequestInfo* info = |
| 138 content::ResourceRequestInfo::ForRequest(request_); | 159 content::ResourceRequestInfo::ForRequest(request_); |
| 139 BrowserThread::PostTask( | 160 BrowserThread::PostTask( |
| 140 BrowserThread::UI, | 161 BrowserThread::UI, |
| 141 FROM_HERE, | 162 FROM_HERE, |
| 142 base::Bind( | 163 base::Bind( |
| 143 &ShowOfflinePage, | 164 &ShowOfflinePage, |
| 144 info->GetChildID(), | 165 info->GetChildID(), |
| 145 info->GetRouteID(), | 166 info->GetRouteID(), |
| 146 request_->url(), | 167 request_->url(), |
| 147 base::Bind( | 168 base::Bind( |
| 148 &OfflineResourceThrottle::OnBlockingPageComplete, | 169 &OfflineResourceThrottle::OnBlockingPageComplete, |
| 149 AsWeakPtr()))); | 170 AsWeakPtr()))); |
| 150 } | 171 } |
| 151 } | 172 } |
| OLD | NEW |