1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/renderer_host/offline_resource_throttle.h"
#include <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "chrome/browser/chromeos/offline/offline_load_page.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_controller.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "webkit/browser/appcache/appcache_service.h"
using content::BrowserThread;
using content::RenderViewHost;
using content::WebContents;
namespace {
void ShowOfflinePage(
int render_process_id,
int render_view_id,
const GURL& url,
const chromeos::OfflineLoadPage::CompletionCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Check again on UI thread and proceed if it's connected.
if (!net::NetworkChangeNotifier::IsOffline()) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE, base::Bind(callback, true));
} else {
RenderViewHost* render_view_host =
RenderViewHost::FromID(render_process_id, render_view_id);
WebContents* web_contents = render_view_host ?
WebContents::FromRenderViewHost(render_view_host) : NULL;
// There is a chance that the tab closed after we decided to show
// the offline page on the IO thread and before we actually show the
// offline page here on the UI thread.
if (web_contents)
(new chromeos::OfflineLoadPage(web_contents, url, callback))->Show();
}
}
} // namespace
OfflineResourceThrottle::OfflineResourceThrottle(
net::URLRequest* request,
appcache::AppCacheService* appcache_service)
: request_(request),
appcache_service_(appcache_service) {
DCHECK(appcache_service);
}
OfflineResourceThrottle::~OfflineResourceThrottle() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!appcache_completion_callback_.IsCancelled())
appcache_completion_callback_.Cancel();
}
void OfflineResourceThrottle::WillStartRequest(bool* defer) {
if (!ShouldShowOfflinePage(request_->url()))
return;
DVLOG(1) << "WillStartRequest: this=" << this << ", url=" << request_->url();
const GURL* url = &(request_->url());
const GURL* first_party = &(request_->first_party_for_cookies());
// Anticipate a client-side HSTS based redirect from HTTP to HTTPS, and
// ask the appcache about the HTTPS url instead of the HTTP url.
GURL redirect_url;
if (request_->GetHSTSRedirect(&redirect_url)) {
if (url->GetOrigin() == first_party->GetOrigin())
first_party = &redirect_url;
url = &redirect_url;
}
DCHECK(appcache_completion_callback_.IsCancelled());
appcache_completion_callback_.Reset(
base::Bind(&OfflineResourceThrottle::OnCanHandleOfflineComplete,
AsWeakPtr()));
appcache_service_->CanHandleMainResourceOffline(
*url, *first_party,
appcache_completion_callback_.callback());
*defer = true;
}
const char* OfflineResourceThrottle::GetNameForLogging() const {
return "OfflineResourceThrottle";
}
void OfflineResourceThrottle::OnBlockingPageComplete(bool proceed) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (proceed) {
controller()->Resume();
} else {
controller()->Cancel();
}
}
bool OfflineResourceThrottle::IsRemote(const GURL& url) const {
return !net::IsLocalhost(url.host()) && (url.SchemeIs(content::kFtpScheme) ||
url.SchemeIs(content::kHttpScheme) ||
url.SchemeIs(content::kHttpsScheme));
}
bool OfflineResourceThrottle::ShouldShowOfflinePage(const GURL& url) const {
// If the network is disconnected while loading other resources, we'll simply
// show broken link/images.
return IsRemote(url) && net::NetworkChangeNotifier::IsOffline();
}
void OfflineResourceThrottle::OnCanHandleOfflineComplete(int rv) {
appcache_completion_callback_.Cancel();
if (rv == net::OK) {
controller()->Resume();
} else {
const content::ResourceRequestInfo* info =
content::ResourceRequestInfo::ForRequest(request_);
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(
&ShowOfflinePage,
info->GetChildID(),
info->GetRouteID(),
request_->url(),
base::Bind(
&OfflineResourceThrottle::OnBlockingPageComplete,
AsWeakPtr())));
}
}
|