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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
// Copyright 2014 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/net/chrome_extensions_network_delegate.h"
#include <stdint.h>
#include "base/macros.h"
#include "net/base/net_errors.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/proxy/proxy_api.h"
#include "chrome/browser/extensions/event_router_forwarder.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_request_info.h"
#include "extensions/browser/api/web_request/web_request_api.h"
#include "extensions/browser/info_map.h"
#include "extensions/browser/process_manager.h"
#include "net/url_request/url_request.h"
using content::BrowserThread;
using content::ResourceRequestInfo;
using extensions::ExtensionWebRequestEventRouter;
namespace {
enum RequestStatus { REQUEST_STARTED, REQUEST_DONE };
// Notifies the extensions::ProcessManager that a request has started or stopped
// for a particular RenderFrame.
void NotifyEPMRequestStatus(RequestStatus status,
void* profile_id,
uint64_t request_id,
int process_id,
int render_frame_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!g_browser_process->profile_manager()->IsValidProfile(profile_id))
return;
Profile* profile = reinterpret_cast<Profile*>(profile_id);
extensions::ProcessManager* process_manager =
extensions::ProcessManager::Get(profile);
DCHECK(process_manager);
// Will be NULL if the request was not issued on behalf of a renderer (e.g. a
// system-level request).
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(process_id, render_frame_id);
if (render_frame_host) {
if (status == REQUEST_STARTED) {
process_manager->OnNetworkRequestStarted(render_frame_host, request_id);
} else if (status == REQUEST_DONE) {
process_manager->OnNetworkRequestDone(render_frame_host, request_id);
} else {
NOTREACHED();
}
}
}
void ForwardRequestStatus(
RequestStatus status, net::URLRequest* request, void* profile_id) {
const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request);
if (!info)
return;
if (status == REQUEST_STARTED && request->url_chain().size() > 1) {
// It's a redirect, this request has already been counted.
return;
}
int process_id, render_frame_id;
if (info->GetAssociatedRenderFrame(&process_id, &render_frame_id)) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&NotifyEPMRequestStatus, status, profile_id,
request->identifier(), process_id, render_frame_id));
}
}
class ChromeExtensionsNetworkDelegateImpl
: public ChromeExtensionsNetworkDelegate {
public:
explicit ChromeExtensionsNetworkDelegateImpl(
extensions::EventRouterForwarder* event_router);
~ChromeExtensionsNetworkDelegateImpl() override;
private:
// ChromeExtensionsNetworkDelegate implementation.
void ForwardProxyErrors(net::URLRequest* request) override;
void ForwardStartRequestStatus(net::URLRequest* request) override;
void ForwardDoneRequestStatus(net::URLRequest* request) override;
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeSendHeaders(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnSendHeaders(net::URLRequest* request,
const net::HttpRequestHeaders& headers) override;
int OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;
void OnResponseStarted(net::URLRequest* request) override;
void OnCompleted(net::URLRequest* request, bool started) override;
void OnURLRequestDestroyed(net::URLRequest* request) override;
void OnPACScriptError(int line_number, const base::string16& error) override;
net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) override;
scoped_refptr<extensions::EventRouterForwarder> event_router_;
DISALLOW_COPY_AND_ASSIGN(ChromeExtensionsNetworkDelegateImpl);
};
ChromeExtensionsNetworkDelegateImpl::ChromeExtensionsNetworkDelegateImpl(
extensions::EventRouterForwarder* event_router) {
DCHECK(event_router);
event_router_ = event_router;
}
ChromeExtensionsNetworkDelegateImpl::~ChromeExtensionsNetworkDelegateImpl() {}
void ChromeExtensionsNetworkDelegateImpl::ForwardProxyErrors(
net::URLRequest* request) {
if (request->status().status() == net::URLRequestStatus::FAILED) {
switch (request->status().error()) {
case net::ERR_PROXY_AUTH_UNSUPPORTED:
case net::ERR_PROXY_CONNECTION_FAILED:
case net::ERR_TUNNEL_CONNECTION_FAILED:
extensions::ProxyEventRouter::GetInstance()->OnProxyError(
event_router_.get(), profile_, request->status().error());
}
}
}
void ChromeExtensionsNetworkDelegateImpl::ForwardStartRequestStatus(
net::URLRequest* request) {
ForwardRequestStatus(REQUEST_STARTED, request, profile_);
}
void ChromeExtensionsNetworkDelegateImpl::ForwardDoneRequestStatus(
net::URLRequest* request) {
ForwardRequestStatus(REQUEST_DONE, request, profile_);
}
int ChromeExtensionsNetworkDelegateImpl::OnBeforeURLRequest(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRequest(
profile_, extension_info_map_.get(), request, callback, new_url);
}
int ChromeExtensionsNetworkDelegateImpl::OnBeforeSendHeaders(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
return ExtensionWebRequestEventRouter::GetInstance()->OnBeforeSendHeaders(
profile_, extension_info_map_.get(), request, callback, headers);
}
void ChromeExtensionsNetworkDelegateImpl::OnSendHeaders(
net::URLRequest* request,
const net::HttpRequestHeaders& headers) {
ExtensionWebRequestEventRouter::GetInstance()->OnSendHeaders(
profile_, extension_info_map_.get(), request, headers);
}
int ChromeExtensionsNetworkDelegateImpl::OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) {
return ExtensionWebRequestEventRouter::GetInstance()->OnHeadersReceived(
profile_,
extension_info_map_.get(),
request,
callback,
original_response_headers,
override_response_headers,
allowed_unsafe_redirect_url);
}
void ChromeExtensionsNetworkDelegateImpl::OnBeforeRedirect(
net::URLRequest* request,
const GURL& new_location) {
ExtensionWebRequestEventRouter::GetInstance()->OnBeforeRedirect(
profile_, extension_info_map_.get(), request, new_location);
}
void ChromeExtensionsNetworkDelegateImpl::OnResponseStarted(
net::URLRequest* request) {
ExtensionWebRequestEventRouter::GetInstance()->OnResponseStarted(
profile_, extension_info_map_.get(), request);
ForwardProxyErrors(request);
}
void ChromeExtensionsNetworkDelegateImpl::OnCompleted(
net::URLRequest* request,
bool started) {
if (request->status().status() == net::URLRequestStatus::SUCCESS) {
bool is_redirect = request->response_headers() &&
net::HttpResponseHeaders::IsRedirectResponseCode(
request->response_headers()->response_code());
if (!is_redirect) {
ExtensionWebRequestEventRouter::GetInstance()->OnCompleted(
profile_, extension_info_map_.get(), request);
}
return;
}
if (request->status().status() == net::URLRequestStatus::FAILED ||
request->status().status() == net::URLRequestStatus::CANCELED) {
ExtensionWebRequestEventRouter::GetInstance()->OnErrorOccurred(
profile_, extension_info_map_.get(), request, started);
return;
}
NOTREACHED();
}
void ChromeExtensionsNetworkDelegateImpl::OnURLRequestDestroyed(
net::URLRequest* request) {
ExtensionWebRequestEventRouter::GetInstance()->OnURLRequestDestroyed(
profile_, request);
}
void ChromeExtensionsNetworkDelegateImpl::OnPACScriptError(
int line_number,
const base::string16& error) {
extensions::ProxyEventRouter::GetInstance()->OnPACScriptError(
event_router_.get(), profile_, line_number, error);
}
net::NetworkDelegate::AuthRequiredResponse
ChromeExtensionsNetworkDelegateImpl::OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) {
return ExtensionWebRequestEventRouter::GetInstance()->OnAuthRequired(
profile_, extension_info_map_.get(), request, auth_info, callback,
credentials);
}
} // namespace
#endif // defined(ENABLE_EXTENSIONS)
// static
ChromeExtensionsNetworkDelegate* ChromeExtensionsNetworkDelegate::Create(
extensions::EventRouterForwarder* event_router) {
#if defined(ENABLE_EXTENSIONS)
return new ChromeExtensionsNetworkDelegateImpl(event_router);
#else
return new ChromeExtensionsNetworkDelegate();
#endif
}
ChromeExtensionsNetworkDelegate::ChromeExtensionsNetworkDelegate()
: profile_(NULL) {
}
ChromeExtensionsNetworkDelegate::~ChromeExtensionsNetworkDelegate() {}
void ChromeExtensionsNetworkDelegate::set_extension_info_map(
extensions::InfoMap* extension_info_map) {
#if defined(ENABLE_EXTENSIONS)
extension_info_map_ = extension_info_map;
#endif
}
void ChromeExtensionsNetworkDelegate::ForwardProxyErrors(
net::URLRequest* request) {
}
void ChromeExtensionsNetworkDelegate::ForwardStartRequestStatus(
net::URLRequest* request) {
}
void ChromeExtensionsNetworkDelegate::ForwardDoneRequestStatus(
net::URLRequest* request) {
}
int ChromeExtensionsNetworkDelegate::OnBeforeURLRequest(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
return net::OK;
}
int ChromeExtensionsNetworkDelegate::OnBeforeSendHeaders(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
return net::OK;
}
void ChromeExtensionsNetworkDelegate::OnSendHeaders(
net::URLRequest* request,
const net::HttpRequestHeaders& headers) {
}
int ChromeExtensionsNetworkDelegate::OnHeadersReceived(
net::URLRequest* request,
const net::CompletionCallback& callback,
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) {
return net::OK;
}
void ChromeExtensionsNetworkDelegate::OnBeforeRedirect(
net::URLRequest* request,
const GURL& new_location) {
}
void ChromeExtensionsNetworkDelegate::OnResponseStarted(
net::URLRequest* request) {
}
void ChromeExtensionsNetworkDelegate::OnCompleted(
net::URLRequest* request,
bool started) {
}
void ChromeExtensionsNetworkDelegate::OnURLRequestDestroyed(
net::URLRequest* request) {
}
void ChromeExtensionsNetworkDelegate::OnPACScriptError(
int line_number,
const base::string16& error) {
}
net::NetworkDelegate::AuthRequiredResponse
ChromeExtensionsNetworkDelegate::OnAuthRequired(
net::URLRequest* request,
const net::AuthChallengeInfo& auth_info,
const AuthCallback& callback,
net::AuthCredentials* credentials) {
return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
}
|