Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/browser/renderer_host/offline_resource_throttle.cc

Issue 810583002: Teach CrOS's OfflineResourceThrottle about ServiceWorkers so it know longer presents its offline in… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 (!completion_callback_.IsCancelled())
72 appcache_completion_callback_.Cancel(); 74 completion_callback_.Cancel();
73 } 75 }
74 76
75 void OfflineResourceThrottle::WillStartRequest(bool* defer) { 77 void OfflineResourceThrottle::WillStartRequest(bool* defer) {
76 if (!ShouldShowOfflinePage(request_->url())) 78 if (!ShouldShowOfflinePage(request_->url()))
77 return; 79 return;
78 80
79 DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url(); 81 DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url();
80 82
81 const GURL* url = &(request_->url()); 83 const GURL* url = &(request_->url());
82 const GURL* first_party = &(request_->first_party_for_cookies()); 84 const GURL* first_party = &(request_->first_party_for_cookies());
83 85
84 // Anticipate a client-side HSTS based redirect from HTTP to HTTPS, and 86 // 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. 87 // ask the appcache about the HTTPS url instead of the HTTP url.
86 GURL redirect_url; 88 GURL redirect_url;
87 if (request_->GetHSTSRedirect(&redirect_url)) { 89 if (request_->GetHSTSRedirect(&redirect_url)) {
88 if (url->GetOrigin() == first_party->GetOrigin()) 90 if (url->GetOrigin() == first_party->GetOrigin())
89 first_party = &redirect_url; 91 first_party = &redirect_url;
90 url = &redirect_url; 92 url = &redirect_url;
91 } 93 }
92 94
93 DCHECK(appcache_completion_callback_.IsCancelled()); 95 DCHECK(completion_callback_.IsCancelled());
94 96
95 appcache_completion_callback_.Reset( 97 completion_callback_.Reset(
96 base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete, 98 base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete,
97 AsWeakPtr())); 99 AsWeakPtr()));
100
101 pending_callbacks_ = 1;
98 appcache_service_->CanHandleMainResourceOffline( 102 appcache_service_->CanHandleMainResourceOffline(
99 *url, *first_party, 103 *url, *first_party,
100 appcache_completion_callback_.callback()); 104 completion_callback_.callback());
101 105
106 content::ServiceWorkerContext* service_worker_context =
107 content::ServiceWorkerContext::GetServiceWorkerContext(request_);
108 if (service_worker_context) {
109 ++pending_callbacks_;
110 service_worker_context->CanHandleMainResourceOffline(
111 *url, *first_party,
112 completion_callback_.callback());
113 }
102 *defer = true; 114 *defer = true;
103 } 115 }
104 116
105 const char* OfflineResourceThrottle::GetNameForLogging() const { 117 const char* OfflineResourceThrottle::GetNameForLogging() const {
106 return "OfflineResourceThrottle"; 118 return "OfflineResourceThrottle";
107 } 119 }
108 120
109 void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) { 121 void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
111 123
(...skipping 10 matching lines...) Expand all
122 url.SchemeIs(url::kHttpsScheme)); 134 url.SchemeIs(url::kHttpsScheme));
123 } 135 }
124 136
125 bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const { 137 bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const {
126 // If the network is disconnected while loading other resources, we'll simply 138 // If the network is disconnected while loading other resources, we'll simply
127 // show broken link/images. 139 // show broken link/images.
128 return IsRemote(url) && net::NetworkChangeNotifier::IsOffline(); 140 return IsRemote(url) && net::NetworkChangeNotifier::IsOffline();
129 } 141 }
130 142
131 void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) { 143 void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) {
132 appcache_completion_callback_.Cancel(); 144 --pending_callbacks_;
133 145
134 if (rv == net::OK) { 146 if (rv == net::OK) {
147 completion_callback_.Cancel();
135 controller()->Resume(); 148 controller()->Resume();
136 } else { 149 } else if (!pending_callbacks_) {
150 completion_callback_.Cancel();
137 const content::ResourceRequestInfo* info = 151 const content::ResourceRequestInfo* info =
138 content::ResourceRequestInfo::ForRequest(request_); 152 content::ResourceRequestInfo::ForRequest(request_);
139 BrowserThread::PostTask( 153 BrowserThread::PostTask(
140 BrowserThread::UI, 154 BrowserThread::UI,
141 FROM_HERE, 155 FROM_HERE,
142 base::Bind( 156 base::Bind(
143 &ShowOfflinePage, 157 &ShowOfflinePage,
144 info->GetChildID(), 158 info->GetChildID(),
145 info->GetRouteID(), 159 info->GetRouteID(),
146 request_->url(), 160 request_->url(),
147 base::Bind( 161 base::Bind(
148 &OfflineResourceThrottle::OnBlockingPageComplete, 162 &OfflineResourceThrottle::OnBlockingPageComplete,
149 AsWeakPtr()))); 163 AsWeakPtr())));
150 } 164 }
151 } 165 }
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/offline_resource_throttle.h ('k') | content/browser/service_worker/service_worker_context_core.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698