OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/net/chrome_url_request_context.h" | 5 #include "chrome/browser/net/chrome_url_request_context.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/privacy_blacklist/blacklist.h" | 10 #include "chrome/browser/privacy_blacklist/blacklist.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "net/http/http_util.h" | 26 #include "net/http/http_util.h" |
27 #include "net/proxy/proxy_config_service_fixed.h" | 27 #include "net/proxy/proxy_config_service_fixed.h" |
28 #include "net/proxy/proxy_service.h" | 28 #include "net/proxy/proxy_service.h" |
29 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
30 #include "webkit/glue/webkit_glue.h" | 30 #include "webkit/glue/webkit_glue.h" |
31 | 31 |
32 #if defined(OS_LINUX) | 32 #if defined(OS_LINUX) |
33 #include "net/ocsp/nss_ocsp.h" | 33 #include "net/ocsp/nss_ocsp.h" |
34 #endif | 34 #endif |
35 | 35 |
36 // TODO(eroman): Fix the definition ordering to match-up with the declaration | 36 namespace { |
37 // ordering (this was done to help keep the diffs simple during | |
38 // refactor). | |
39 | |
40 // TODO(eroman): The ChromeURLRequestContext's Blacklist* is shared with the | 37 // TODO(eroman): The ChromeURLRequestContext's Blacklist* is shared with the |
41 // Profile... This is a problem since the Profile dies before | 38 // Profile... This is a problem since the Profile dies before |
42 // the IO thread, so we may end up accessing a deleted variable. | 39 // the IO thread, so we may end up accessing a deleted variable. |
| 40 // http://crbug.com/26733. |
43 | 41 |
44 static void CheckCurrentlyOnIOThread() { | 42 // ---------------------------------------------------------------------------- |
| 43 // Helper methods to check current thread |
| 44 // ---------------------------------------------------------------------------- |
| 45 |
| 46 void CheckCurrentlyOnIOThread() { |
45 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | 47 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
46 } | 48 } |
47 | 49 |
48 static void CheckCurrentlyOnMainThread() { | 50 void CheckCurrentlyOnMainThread() { |
49 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 51 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
50 } | 52 } |
51 | 53 |
52 net::ProxyConfig* CreateProxyConfig(const CommandLine& command_line) { | 54 // ---------------------------------------------------------------------------- |
53 // Scan for all "enable" type proxy switches. | 55 // Helper methods to initialize proxy |
54 static const char* proxy_switches[] = { | 56 // ---------------------------------------------------------------------------- |
55 switches::kProxyServer, | |
56 switches::kProxyPacUrl, | |
57 switches::kProxyAutoDetect, | |
58 switches::kProxyBypassList | |
59 }; | |
60 | 57 |
61 bool found_enable_proxy_switch = false; | 58 net::ProxyConfigService* CreateProxyConfigService( |
62 for (size_t i = 0; i < arraysize(proxy_switches); i++) { | |
63 if (command_line.HasSwitch(proxy_switches[i])) { | |
64 found_enable_proxy_switch = true; | |
65 break; | |
66 } | |
67 } | |
68 | |
69 if (!found_enable_proxy_switch && | |
70 !command_line.HasSwitch(switches::kNoProxyServer)) { | |
71 return NULL; | |
72 } | |
73 | |
74 net::ProxyConfig* proxy_config = new net::ProxyConfig(); | |
75 if (command_line.HasSwitch(switches::kNoProxyServer)) { | |
76 // Ignore (and warn about) all the other proxy config switches we get if | |
77 // the --no-proxy-server command line argument is present. | |
78 if (found_enable_proxy_switch) { | |
79 LOG(WARNING) << "Additional command line proxy switches found when --" | |
80 << switches::kNoProxyServer << " was specified."; | |
81 } | |
82 return proxy_config; | |
83 } | |
84 | |
85 if (command_line.HasSwitch(switches::kProxyServer)) { | |
86 const std::wstring& proxy_server = | |
87 command_line.GetSwitchValue(switches::kProxyServer); | |
88 proxy_config->proxy_rules.ParseFromString(WideToASCII(proxy_server)); | |
89 } | |
90 | |
91 if (command_line.HasSwitch(switches::kProxyPacUrl)) { | |
92 proxy_config->pac_url = | |
93 GURL(WideToASCII(command_line.GetSwitchValue( | |
94 switches::kProxyPacUrl))); | |
95 } | |
96 | |
97 if (command_line.HasSwitch(switches::kProxyAutoDetect)) { | |
98 proxy_config->auto_detect = true; | |
99 } | |
100 | |
101 if (command_line.HasSwitch(switches::kProxyBypassList)) { | |
102 proxy_config->ParseNoProxyList( | |
103 WideToASCII(command_line.GetSwitchValue( | |
104 switches::kProxyBypassList))); | |
105 } | |
106 | |
107 return proxy_config; | |
108 } | |
109 | |
110 static net::ProxyConfigService* CreateProxyConfigService( | |
111 const CommandLine& command_line) { | 59 const CommandLine& command_line) { |
112 // The linux gconf-based proxy settings getter relies on being initialized | 60 // The linux gconf-based proxy settings getter relies on being initialized |
113 // from the UI thread. | 61 // from the UI thread. |
114 CheckCurrentlyOnMainThread(); | 62 CheckCurrentlyOnMainThread(); |
115 | 63 |
116 scoped_ptr<net::ProxyConfig> proxy_config_from_cmd_line( | 64 scoped_ptr<net::ProxyConfig> proxy_config_from_cmd_line( |
117 CreateProxyConfig(command_line)); | 65 CreateProxyConfig(command_line)); |
118 | 66 |
119 if (!proxy_config_from_cmd_line.get()) { | 67 if (!proxy_config_from_cmd_line.get()) { |
120 // Use system settings. | 68 // Use system settings. |
121 // TODO(port): the IO and FILE message loops are only used by Linux. Can | 69 // TODO(port): the IO and FILE message loops are only used by Linux. Can |
122 // that code be moved to chrome/browser instead of being in net, so that it | 70 // that code be moved to chrome/browser instead of being in net, so that it |
123 // can use ChromeThread instead of raw MessageLoop pointers? See bug 25354. | 71 // can use ChromeThread instead of raw MessageLoop pointers? See bug 25354. |
124 return net::ProxyService::CreateSystemProxyConfigService( | 72 return net::ProxyService::CreateSystemProxyConfigService( |
125 g_browser_process->io_thread()->message_loop(), | 73 g_browser_process->io_thread()->message_loop(), |
126 g_browser_process->file_thread()->message_loop()); | 74 g_browser_process->file_thread()->message_loop()); |
127 } | 75 } |
128 | 76 |
129 // Otherwise use the fixed settings from the command line. | 77 // Otherwise use the fixed settings from the command line. |
130 return new net::ProxyConfigServiceFixed(*proxy_config_from_cmd_line.get()); | 78 return new net::ProxyConfigServiceFixed(*proxy_config_from_cmd_line.get()); |
131 } | 79 } |
132 | 80 |
133 // Create a proxy service according to the options on command line. | 81 // Create a proxy service according to the options on command line. |
134 static net::ProxyService* CreateProxyService( | 82 net::ProxyService* CreateProxyService( |
135 URLRequestContext* context, | 83 URLRequestContext* context, |
136 net::ProxyConfigService* proxy_config_service, | 84 net::ProxyConfigService* proxy_config_service, |
137 const CommandLine& command_line, | 85 const CommandLine& command_line, |
138 MessageLoop* io_loop) { | 86 MessageLoop* io_loop) { |
139 CheckCurrentlyOnIOThread(); | 87 CheckCurrentlyOnIOThread(); |
140 | 88 |
141 bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver); | 89 bool use_v8 = !command_line.HasSwitch(switches::kWinHttpProxyResolver); |
142 if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) { | 90 if (use_v8 && command_line.HasSwitch(switches::kSingleProcess)) { |
143 // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h | 91 // See the note about V8 multithreading in net/proxy/proxy_resolver_v8.h |
144 // to understand why we have this limitation. | 92 // to understand why we have this limitation. |
145 LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode."; | 93 LOG(ERROR) << "Cannot use V8 Proxy resolver in single process mode."; |
146 use_v8 = false; // Fallback to non-v8 implementation. | 94 use_v8 = false; // Fallback to non-v8 implementation. |
147 } | 95 } |
148 | 96 |
149 return net::ProxyService::Create( | 97 return net::ProxyService::Create( |
150 proxy_config_service, | 98 proxy_config_service, |
151 use_v8, | 99 use_v8, |
152 context, | 100 context, |
153 io_loop); | 101 io_loop); |
154 } | 102 } |
155 | 103 |
156 // Lazily create a ChromeURLRequestContext using our factory. | 104 // ---------------------------------------------------------------------------- |
157 URLRequestContext* ChromeURLRequestContextGetter::GetURLRequestContext() { | 105 // Helper factories |
158 CheckCurrentlyOnIOThread(); | 106 // ---------------------------------------------------------------------------- |
159 | |
160 if (!url_request_context_) { | |
161 DCHECK(factory_.get()); | |
162 url_request_context_ = factory_->Create(); | |
163 factory_.reset(); | |
164 } | |
165 | |
166 return url_request_context_; | |
167 } | |
168 | 107 |
169 // Factory that creates the main ChromeURLRequestContext. | 108 // Factory that creates the main ChromeURLRequestContext. |
170 class FactoryForOriginal : public ChromeURLRequestContextFactory { | 109 class FactoryForOriginal : public ChromeURLRequestContextFactory { |
171 public: | 110 public: |
172 FactoryForOriginal(Profile* profile, | 111 FactoryForOriginal(Profile* profile, |
173 const FilePath& cookie_store_path, | 112 const FilePath& cookie_store_path, |
174 const FilePath& disk_cache_path, | 113 const FilePath& disk_cache_path, |
175 int cache_size) | 114 int cache_size) |
176 : ChromeURLRequestContextFactory(profile), | 115 : ChromeURLRequestContextFactory(profile), |
177 cookie_store_path_(cookie_store_path), | 116 cookie_store_path_(cookie_store_path), |
(...skipping 10 matching lines...) Expand all Loading... |
188 virtual ChromeURLRequestContext* Create(); | 127 virtual ChromeURLRequestContext* Create(); |
189 | 128 |
190 private: | 129 private: |
191 FilePath cookie_store_path_; | 130 FilePath cookie_store_path_; |
192 FilePath disk_cache_path_; | 131 FilePath disk_cache_path_; |
193 int cache_size_; | 132 int cache_size_; |
194 | 133 |
195 scoped_ptr<net::ProxyConfigService> proxy_config_service_; | 134 scoped_ptr<net::ProxyConfigService> proxy_config_service_; |
196 }; | 135 }; |
197 | 136 |
198 // static | |
199 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOriginal( | |
200 Profile* profile, const FilePath& cookie_store_path, | |
201 const FilePath& disk_cache_path, int cache_size) { | |
202 DCHECK(!profile->IsOffTheRecord()); | |
203 return new ChromeURLRequestContextGetter( | |
204 profile, | |
205 new FactoryForOriginal(profile, | |
206 cookie_store_path, | |
207 disk_cache_path, | |
208 cache_size)); | |
209 } | |
210 | |
211 ChromeURLRequestContext* FactoryForOriginal::Create() { | 137 ChromeURLRequestContext* FactoryForOriginal::Create() { |
212 ChromeURLRequestContext* context = new ChromeURLRequestContext; | 138 ChromeURLRequestContext* context = new ChromeURLRequestContext; |
213 ApplyProfileParametersToContext(context); | 139 ApplyProfileParametersToContext(context); |
214 | 140 |
215 // Global host resolver for the context. | 141 // Global host resolver for the context. |
216 context->set_host_resolver(chrome_browser_net::GetGlobalHostResolver()); | 142 context->set_host_resolver(chrome_browser_net::GetGlobalHostResolver()); |
217 | 143 |
218 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 144 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
219 | 145 |
220 context->set_proxy_service( | 146 context->set_proxy_service( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 context->appcache_service()->set_request_context(context); | 197 context->appcache_service()->set_request_context(context); |
272 | 198 |
273 #if defined(OS_LINUX) | 199 #if defined(OS_LINUX) |
274 // TODO(ukai): find a better way to set the URLRequestContext for OCSP. | 200 // TODO(ukai): find a better way to set the URLRequestContext for OCSP. |
275 net::SetURLRequestContextForOCSP(context); | 201 net::SetURLRequestContextForOCSP(context); |
276 #endif | 202 #endif |
277 | 203 |
278 return context; | 204 return context; |
279 } | 205 } |
280 | 206 |
281 // static | |
282 ChromeURLRequestContextGetter* | |
283 ChromeURLRequestContextGetter::CreateOriginalForMedia( | |
284 Profile* profile, const FilePath& disk_cache_path, int cache_size) { | |
285 DCHECK(!profile->IsOffTheRecord()); | |
286 return CreateRequestContextForMedia(profile, disk_cache_path, cache_size, | |
287 false); | |
288 } | |
289 | |
290 // Factory that creates the ChromeURLRequestContext for extensions. | 207 // Factory that creates the ChromeURLRequestContext for extensions. |
291 class FactoryForExtensions : public ChromeURLRequestContextFactory { | 208 class FactoryForExtensions : public ChromeURLRequestContextFactory { |
292 public: | 209 public: |
293 FactoryForExtensions(Profile* profile, const FilePath& cookie_store_path) | 210 FactoryForExtensions(Profile* profile, const FilePath& cookie_store_path) |
294 : ChromeURLRequestContextFactory(profile), | 211 : ChromeURLRequestContextFactory(profile), |
295 cookie_store_path_(cookie_store_path) { | 212 cookie_store_path_(cookie_store_path) { |
296 } | 213 } |
297 | 214 |
298 virtual ChromeURLRequestContext* Create(); | 215 virtual ChromeURLRequestContext* Create(); |
299 | 216 |
300 private: | 217 private: |
301 FilePath cookie_store_path_; | 218 FilePath cookie_store_path_; |
302 }; | 219 }; |
303 | 220 |
304 // static | |
305 ChromeURLRequestContextGetter* | |
306 ChromeURLRequestContextGetter::CreateOriginalForExtensions( | |
307 Profile* profile, const FilePath& cookie_store_path) { | |
308 DCHECK(!profile->IsOffTheRecord()); | |
309 return new ChromeURLRequestContextGetter( | |
310 profile, | |
311 new FactoryForExtensions(profile, cookie_store_path)); | |
312 } | |
313 | |
314 ChromeURLRequestContext* FactoryForExtensions::Create() { | 221 ChromeURLRequestContext* FactoryForExtensions::Create() { |
315 ChromeURLRequestContext* context = new ChromeURLRequestContext; | 222 ChromeURLRequestContext* context = new ChromeURLRequestContext; |
316 ApplyProfileParametersToContext(context); | 223 ApplyProfileParametersToContext(context); |
317 | 224 |
318 // All we care about for extensions is the cookie store. | 225 // All we care about for extensions is the cookie store. |
319 DCHECK(!cookie_store_path_.empty()); | 226 DCHECK(!cookie_store_path_.empty()); |
320 | 227 |
321 scoped_refptr<SQLitePersistentCookieStore> cookie_db = | 228 scoped_refptr<SQLitePersistentCookieStore> cookie_db = |
322 new SQLitePersistentCookieStore(cookie_store_path_); | 229 new SQLitePersistentCookieStore(cookie_store_path_); |
323 net::CookieMonster* cookie_monster = new net::CookieMonster(cookie_db.get()); | 230 net::CookieMonster* cookie_monster = new net::CookieMonster(cookie_db.get()); |
(...skipping 15 matching lines...) Expand all Loading... |
339 static_cast<ChromeURLRequestContextGetter*>( | 246 static_cast<ChromeURLRequestContextGetter*>( |
340 profile->GetOriginalProfile()->GetRequestContext())) { | 247 profile->GetOriginalProfile()->GetRequestContext())) { |
341 } | 248 } |
342 | 249 |
343 virtual ChromeURLRequestContext* Create(); | 250 virtual ChromeURLRequestContext* Create(); |
344 | 251 |
345 private: | 252 private: |
346 scoped_refptr<ChromeURLRequestContextGetter> original_context_getter_; | 253 scoped_refptr<ChromeURLRequestContextGetter> original_context_getter_; |
347 }; | 254 }; |
348 | 255 |
349 // static | |
350 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOffTheRecord
( | |
351 Profile* profile) { | |
352 DCHECK(profile->IsOffTheRecord()); | |
353 return new ChromeURLRequestContextGetter( | |
354 profile, new FactoryForOffTheRecord(profile)); | |
355 } | |
356 | |
357 ChromeURLRequestContext* FactoryForOffTheRecord::Create() { | 256 ChromeURLRequestContext* FactoryForOffTheRecord::Create() { |
358 ChromeURLRequestContext* context = new ChromeURLRequestContext; | 257 ChromeURLRequestContext* context = new ChromeURLRequestContext; |
359 ApplyProfileParametersToContext(context); | 258 ApplyProfileParametersToContext(context); |
360 | 259 |
361 ChromeURLRequestContext* original_context = | 260 ChromeURLRequestContext* original_context = |
362 original_context_getter_->GetIOContext(); | 261 original_context_getter_->GetIOContext(); |
363 | 262 |
364 // Share the same proxy service and host resolver as the original profile. | 263 // Share the same proxy service and host resolver as the original profile. |
365 // TODO(eroman): although ProxyService is reference counted, this sharing | 264 // TODO(eroman): although ProxyService is reference counted, this sharing |
366 // still has a subtle dependency on the lifespan of the original profile -- | 265 // still has a subtle dependency on the lifespan of the original profile -- |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 // mode. | 302 // mode. |
404 class FactoryForOffTheRecordExtensions | 303 class FactoryForOffTheRecordExtensions |
405 : public ChromeURLRequestContextFactory { | 304 : public ChromeURLRequestContextFactory { |
406 public: | 305 public: |
407 FactoryForOffTheRecordExtensions(Profile* profile) | 306 FactoryForOffTheRecordExtensions(Profile* profile) |
408 : ChromeURLRequestContextFactory(profile) {} | 307 : ChromeURLRequestContextFactory(profile) {} |
409 | 308 |
410 virtual ChromeURLRequestContext* Create(); | 309 virtual ChromeURLRequestContext* Create(); |
411 }; | 310 }; |
412 | 311 |
413 // static | |
414 ChromeURLRequestContextGetter* | |
415 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions(Profile* profile)
{ | |
416 DCHECK(profile->IsOffTheRecord()); | |
417 return new ChromeURLRequestContextGetter( | |
418 profile, | |
419 new FactoryForOffTheRecordExtensions(profile)); | |
420 } | |
421 | |
422 ChromeURLRequestContext* FactoryForOffTheRecordExtensions::Create() { | 312 ChromeURLRequestContext* FactoryForOffTheRecordExtensions::Create() { |
423 ChromeURLRequestContext* context = new ChromeURLRequestContext; | 313 ChromeURLRequestContext* context = new ChromeURLRequestContext; |
424 ApplyProfileParametersToContext(context); | 314 ApplyProfileParametersToContext(context); |
425 | 315 |
426 net::CookieMonster* cookie_monster = new net::CookieMonster; | 316 net::CookieMonster* cookie_monster = new net::CookieMonster; |
427 | 317 |
428 // Enable cookies for extension URLs only. | 318 // Enable cookies for extension URLs only. |
429 const char* schemes[] = {chrome::kExtensionScheme}; | 319 const char* schemes[] = {chrome::kExtensionScheme}; |
430 cookie_monster->SetCookieableSchemes(schemes, 1); | 320 cookie_monster->SetCookieableSchemes(schemes, 1); |
431 context->set_cookie_store(cookie_monster); | 321 context->set_cookie_store(cookie_monster); |
(...skipping 20 matching lines...) Expand all Loading... |
452 | 342 |
453 virtual ChromeURLRequestContext* Create(); | 343 virtual ChromeURLRequestContext* Create(); |
454 | 344 |
455 private: | 345 private: |
456 scoped_refptr<ChromeURLRequestContextGetter> main_context_getter_; | 346 scoped_refptr<ChromeURLRequestContextGetter> main_context_getter_; |
457 | 347 |
458 FilePath disk_cache_path_; | 348 FilePath disk_cache_path_; |
459 int cache_size_; | 349 int cache_size_; |
460 }; | 350 }; |
461 | 351 |
462 // static | |
463 ChromeURLRequestContextGetter* | |
464 ChromeURLRequestContextGetter::CreateRequestContextForMedia( | |
465 Profile* profile, const FilePath& disk_cache_path, int cache_size, | |
466 bool off_the_record) { | |
467 return new ChromeURLRequestContextGetter( | |
468 profile, | |
469 new FactoryForMedia(profile, | |
470 disk_cache_path, | |
471 cache_size, | |
472 off_the_record)); | |
473 } | |
474 | |
475 ChromeURLRequestContext* FactoryForMedia::Create() { | 352 ChromeURLRequestContext* FactoryForMedia::Create() { |
476 ChromeURLRequestContext* context = new ChromeURLRequestContext; | 353 ChromeURLRequestContext* context = new ChromeURLRequestContext; |
477 ApplyProfileParametersToContext(context); | 354 ApplyProfileParametersToContext(context); |
478 | 355 |
479 ChromeURLRequestContext* main_context = | 356 ChromeURLRequestContext* main_context = |
480 main_context_getter_->GetIOContext(); | 357 main_context_getter_->GetIOContext(); |
481 | 358 |
482 // Share the same proxy service of the common profile. | 359 // Share the same proxy service of the common profile. |
483 context->set_proxy_service(main_context->proxy_service()); | 360 context->set_proxy_service(main_context->proxy_service()); |
484 // Also share the cookie store of the common profile. | 361 // Also share the cookie store of the common profile. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 | 394 |
518 cache->set_type(net::MEDIA_CACHE); | 395 cache->set_type(net::MEDIA_CACHE); |
519 context->set_http_transaction_factory(cache); | 396 context->set_http_transaction_factory(cache); |
520 | 397 |
521 // Use the same appcache service as the profile's main context. | 398 // Use the same appcache service as the profile's main context. |
522 context->set_appcache_service(main_context->appcache_service()); | 399 context->set_appcache_service(main_context->appcache_service()); |
523 | 400 |
524 return context; | 401 return context; |
525 } | 402 } |
526 | 403 |
| 404 } // namespace |
| 405 |
| 406 // ---------------------------------------------------------------------------- |
| 407 // ChromeURLRequestContextGetter |
| 408 // ---------------------------------------------------------------------------- |
| 409 |
527 ChromeURLRequestContextGetter::ChromeURLRequestContextGetter( | 410 ChromeURLRequestContextGetter::ChromeURLRequestContextGetter( |
528 Profile* profile, | 411 Profile* profile, |
529 ChromeURLRequestContextFactory* factory) | 412 ChromeURLRequestContextFactory* factory) |
530 : prefs_(NULL), | 413 : prefs_(NULL), |
531 factory_(factory), | 414 factory_(factory), |
532 url_request_context_(NULL) { | 415 url_request_context_(NULL) { |
533 DCHECK(factory); | 416 DCHECK(factory); |
534 | 417 |
535 // If a base profile was specified, listen for changes to the preferences. | 418 // If a base profile was specified, listen for changes to the preferences. |
536 if (profile) | 419 if (profile) |
537 RegisterPrefsObserver(profile); | 420 RegisterPrefsObserver(profile); |
538 } | 421 } |
539 | 422 |
540 // Extract values from |profile| and copy them into | 423 ChromeURLRequestContextGetter::~ChromeURLRequestContextGetter() { |
541 // ChromeURLRequestContextFactory. We will use them later when constructing the | 424 CheckCurrentlyOnIOThread(); |
542 // ChromeURLRequestContext on the IO thread (see | |
543 // ApplyProfileParametersToContext() which reverses this). | |
544 ChromeURLRequestContextFactory::ChromeURLRequestContextFactory(Profile* profile) | |
545 : is_media_(false), | |
546 is_off_the_record_(profile->IsOffTheRecord()) { | |
547 CheckCurrentlyOnMainThread(); | |
548 PrefService* prefs = profile->GetPrefs(); | |
549 | 425 |
550 // Set up Accept-Language and Accept-Charset header values | 426 DCHECK(!prefs_) << "Probably didn't call CleanupOnUIThread"; |
551 accept_language_ = net::HttpUtil::GenerateAcceptLanguageHeader( | |
552 WideToASCII(prefs->GetString(prefs::kAcceptLanguages))); | |
553 std::string default_charset = | |
554 WideToASCII(prefs->GetString(prefs::kDefaultCharset)); | |
555 accept_charset_ = | |
556 net::HttpUtil::GenerateAcceptCharsetHeader(default_charset); | |
557 | 427 |
558 // At this point, we don't know the charset of the referring page | 428 // Either we already transformed the factory into a URLRequestContext, or |
559 // where a url request originates from. This is used to get a suggested | 429 // we still have a pending factory. |
560 // filename from Content-Disposition header made of raw 8bit characters. | 430 DCHECK((factory_.get() && !url_request_context_.get()) || |
561 // Down the road, it can be overriden if it becomes known (for instance, | 431 (!factory_.get() && url_request_context_.get())); |
562 // when download request is made through the context menu in a web page). | |
563 // At the moment, it'll remain 'undeterministic' when a user | |
564 // types a URL in the omnibar or click on a download link in a page. | |
565 // For the latter, we need a change on the webkit-side. | |
566 // We initialize it to the default charset here and a user will | |
567 // have an *arguably* better default charset for interpreting a raw 8bit | |
568 // C-D header field. It means the native OS codepage fallback in | |
569 // net_util::GetSuggestedFilename is unlikely to be taken. | |
570 referrer_charset_ = default_charset; | |
571 | 432 |
572 cookie_policy_type_ = net::CookiePolicy::FromInt( | 433 // The scoped_refptr / scoped_ptr destructors take care of releasing |
573 prefs->GetInteger(prefs::kCookieBehavior)); | 434 // |factory_| and |url_request_context_| now. |
| 435 } |
574 | 436 |
575 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! | 437 // Lazily create a ChromeURLRequestContext using our factory. |
576 blacklist_ = profile->GetBlacklist(); | 438 URLRequestContext* ChromeURLRequestContextGetter::GetURLRequestContext() { |
| 439 CheckCurrentlyOnIOThread(); |
577 | 440 |
578 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! | 441 if (!url_request_context_) { |
579 strict_transport_security_state_ = profile->GetStrictTransportSecurityState(); | 442 DCHECK(factory_.get()); |
580 | 443 url_request_context_ = factory_->Create(); |
581 if (profile->GetExtensionsService()) { | 444 factory_.reset(); |
582 const ExtensionList* extensions = | |
583 profile->GetExtensionsService()->extensions(); | |
584 for (ExtensionList::const_iterator iter = extensions->begin(); | |
585 iter != extensions->end(); ++iter) { | |
586 extension_paths_[(*iter)->id()] = (*iter)->path(); | |
587 } | |
588 } | 445 } |
589 | 446 |
590 if (profile->GetUserScriptMaster()) | 447 return url_request_context_; |
591 user_script_dir_path_ = profile->GetUserScriptMaster()->user_script_dir(); | |
592 | |
593 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! | |
594 ssl_config_service_ = profile->GetSSLConfigService(); | |
595 | |
596 profile_dir_path_ = profile->GetPath(); | |
597 } | 448 } |
598 | 449 |
599 ChromeURLRequestContextFactory::~ChromeURLRequestContextFactory() { | 450 net::CookieStore* ChromeURLRequestContextGetter::GetCookieStore() { |
600 CheckCurrentlyOnIOThread(); | 451 // If we are running on the IO thread this is real easy. |
| 452 if (ChromeThread::CurrentlyOn(ChromeThread::IO)) |
| 453 return GetURLRequestContext()->cookie_store(); |
| 454 |
| 455 // If we aren't running on the IO thread, we cannot call |
| 456 // GetURLRequestContext(). Instead we will post a task to the IO loop |
| 457 // and wait for it to complete. |
| 458 |
| 459 base::WaitableEvent completion(false, false); |
| 460 net::CookieStore* result = NULL; |
| 461 |
| 462 ChromeThread::PostTask( |
| 463 ChromeThread::IO, FROM_HERE, |
| 464 NewRunnableMethod(this, |
| 465 &ChromeURLRequestContextGetter::GetCookieStoreAsyncHelper, |
| 466 &completion, |
| 467 &result)); |
| 468 |
| 469 completion.Wait(); |
| 470 DCHECK(result); |
| 471 return result; |
601 } | 472 } |
602 | 473 |
603 void ChromeURLRequestContextFactory::ApplyProfileParametersToContext( | 474 // static |
604 ChromeURLRequestContext* context) { | 475 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOriginal( |
605 // Apply all the parameters. NOTE: keep this in sync with | 476 Profile* profile, const FilePath& cookie_store_path, |
606 // ChromeURLRequestContextFactory(Profile*). | 477 const FilePath& disk_cache_path, int cache_size) { |
607 context->set_is_media(is_media_); | 478 DCHECK(!profile->IsOffTheRecord()); |
608 context->set_is_off_the_record(is_off_the_record_); | 479 return new ChromeURLRequestContextGetter( |
609 context->set_accept_language(accept_language_); | 480 profile, |
610 context->set_accept_charset(accept_charset_); | 481 new FactoryForOriginal(profile, |
611 context->set_referrer_charset(referrer_charset_); | 482 cookie_store_path, |
612 context->set_cookie_policy_type(cookie_policy_type_); | 483 disk_cache_path, |
613 context->set_extension_paths(extension_paths_); | 484 cache_size)); |
614 context->set_user_script_dir_path(user_script_dir_path_); | |
615 context->set_blacklist(blacklist_); | |
616 context->set_strict_transport_security_state( | |
617 strict_transport_security_state_); | |
618 context->set_ssl_config_service(ssl_config_service_); | |
619 } | 485 } |
620 | 486 |
621 void ChromeURLRequestContextGetter::RegisterPrefsObserver(Profile* profile) { | 487 // static |
| 488 ChromeURLRequestContextGetter* |
| 489 ChromeURLRequestContextGetter::CreateOriginalForMedia( |
| 490 Profile* profile, const FilePath& disk_cache_path, int cache_size) { |
| 491 DCHECK(!profile->IsOffTheRecord()); |
| 492 return CreateRequestContextForMedia(profile, disk_cache_path, cache_size, |
| 493 false); |
| 494 } |
| 495 |
| 496 // static |
| 497 ChromeURLRequestContextGetter* |
| 498 ChromeURLRequestContextGetter::CreateOriginalForExtensions( |
| 499 Profile* profile, const FilePath& cookie_store_path) { |
| 500 DCHECK(!profile->IsOffTheRecord()); |
| 501 return new ChromeURLRequestContextGetter( |
| 502 profile, |
| 503 new FactoryForExtensions(profile, cookie_store_path)); |
| 504 } |
| 505 |
| 506 // static |
| 507 ChromeURLRequestContextGetter* ChromeURLRequestContextGetter::CreateOffTheRecord
( |
| 508 Profile* profile) { |
| 509 DCHECK(profile->IsOffTheRecord()); |
| 510 return new ChromeURLRequestContextGetter( |
| 511 profile, new FactoryForOffTheRecord(profile)); |
| 512 } |
| 513 |
| 514 // static |
| 515 ChromeURLRequestContextGetter* |
| 516 ChromeURLRequestContextGetter::CreateOffTheRecordForExtensions(Profile* profile)
{ |
| 517 DCHECK(profile->IsOffTheRecord()); |
| 518 return new ChromeURLRequestContextGetter( |
| 519 profile, |
| 520 new FactoryForOffTheRecordExtensions(profile)); |
| 521 } |
| 522 |
| 523 void ChromeURLRequestContextGetter::CleanupOnUIThread() { |
622 CheckCurrentlyOnMainThread(); | 524 CheckCurrentlyOnMainThread(); |
623 | 525 |
624 prefs_ = profile->GetPrefs(); | 526 if (prefs_) { |
| 527 // Unregister for pref notifications. |
| 528 prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this); |
| 529 prefs_->RemovePrefObserver(prefs::kCookieBehavior, this); |
| 530 prefs_->RemovePrefObserver(prefs::kDefaultCharset, this); |
| 531 prefs_ = NULL; |
| 532 } |
625 | 533 |
626 prefs_->AddPrefObserver(prefs::kAcceptLanguages, this); | 534 // TODO(eroman): Doesn't look like this member is even used... |
627 prefs_->AddPrefObserver(prefs::kCookieBehavior, this); | 535 // can probably be deleted. |
628 prefs_->AddPrefObserver(prefs::kDefaultCharset, this); | 536 registrar_.RemoveAll(); |
629 } | 537 } |
630 | 538 |
631 ChromeURLRequestContext::ChromeURLRequestContext( | 539 void ChromeURLRequestContextGetter::OnNewExtensions(const std::string& id, |
632 ChromeURLRequestContext* other) { | 540 const FilePath& path) { |
633 CheckCurrentlyOnIOThread(); | 541 GetIOContext()->OnNewExtensions(id, path); |
| 542 } |
634 | 543 |
635 // Set URLRequestContext members | 544 void ChromeURLRequestContextGetter::OnUnloadedExtension( |
636 host_resolver_ = other->host_resolver_; | 545 const std::string& id) { |
637 proxy_service_ = other->proxy_service_; | 546 GetIOContext()->OnUnloadedExtension(id); |
638 ssl_config_service_ = other->ssl_config_service_; | |
639 http_transaction_factory_ = other->http_transaction_factory_; | |
640 ftp_transaction_factory_ = other->ftp_transaction_factory_; | |
641 cookie_store_ = other->cookie_store_; | |
642 cookie_policy_.set_type(other->cookie_policy_.type()); | |
643 strict_transport_security_state_ = other->strict_transport_security_state_; | |
644 accept_language_ = other->accept_language_; | |
645 accept_charset_ = other->accept_charset_; | |
646 referrer_charset_ = other->referrer_charset_; | |
647 | |
648 // Set ChromeURLRequestContext members | |
649 appcache_service_ = other->appcache_service_; | |
650 extension_paths_ = other->extension_paths_; | |
651 user_script_dir_path_ = other->user_script_dir_path_; | |
652 blacklist_ = other->blacklist_; | |
653 is_media_ = other->is_media_; | |
654 is_off_the_record_ = other->is_off_the_record_; | |
655 } | 547 } |
656 | 548 |
657 // NotificationObserver implementation. | 549 // NotificationObserver implementation. |
658 void ChromeURLRequestContextGetter::Observe(NotificationType type, | 550 void ChromeURLRequestContextGetter::Observe( |
659 const NotificationSource& source, | 551 NotificationType type, |
660 const NotificationDetails& details) { | 552 const NotificationSource& source, |
| 553 const NotificationDetails& details) { |
661 CheckCurrentlyOnMainThread(); | 554 CheckCurrentlyOnMainThread(); |
662 | 555 |
663 if (NotificationType::PREF_CHANGED == type) { | 556 if (NotificationType::PREF_CHANGED == type) { |
664 std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); | 557 std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); |
665 PrefService* prefs = Source<PrefService>(source).ptr(); | 558 PrefService* prefs = Source<PrefService>(source).ptr(); |
666 DCHECK(pref_name_in && prefs); | 559 DCHECK(pref_name_in && prefs); |
667 if (*pref_name_in == prefs::kAcceptLanguages) { | 560 if (*pref_name_in == prefs::kAcceptLanguages) { |
668 std::string accept_language = | 561 std::string accept_language = |
669 WideToASCII(prefs->GetString(prefs::kAcceptLanguages)); | 562 WideToASCII(prefs->GetString(prefs::kAcceptLanguages)); |
670 ChromeThread::PostTask( | 563 ChromeThread::PostTask( |
(...skipping 19 matching lines...) Expand all Loading... |
690 NewRunnableMethod( | 583 NewRunnableMethod( |
691 this, | 584 this, |
692 &ChromeURLRequestContextGetter::OnDefaultCharsetChange, | 585 &ChromeURLRequestContextGetter::OnDefaultCharsetChange, |
693 default_charset)); | 586 default_charset)); |
694 } | 587 } |
695 } else { | 588 } else { |
696 NOTREACHED(); | 589 NOTREACHED(); |
697 } | 590 } |
698 } | 591 } |
699 | 592 |
| 593 void ChromeURLRequestContextGetter::RegisterPrefsObserver(Profile* profile) { |
| 594 CheckCurrentlyOnMainThread(); |
| 595 |
| 596 prefs_ = profile->GetPrefs(); |
| 597 |
| 598 prefs_->AddPrefObserver(prefs::kAcceptLanguages, this); |
| 599 prefs_->AddPrefObserver(prefs::kCookieBehavior, this); |
| 600 prefs_->AddPrefObserver(prefs::kDefaultCharset, this); |
| 601 } |
| 602 |
| 603 // static |
| 604 ChromeURLRequestContextGetter* |
| 605 ChromeURLRequestContextGetter::CreateRequestContextForMedia( |
| 606 Profile* profile, const FilePath& disk_cache_path, int cache_size, |
| 607 bool off_the_record) { |
| 608 return new ChromeURLRequestContextGetter( |
| 609 profile, |
| 610 new FactoryForMedia(profile, |
| 611 disk_cache_path, |
| 612 cache_size, |
| 613 off_the_record)); |
| 614 } |
| 615 |
700 void ChromeURLRequestContextGetter::OnAcceptLanguageChange( | 616 void ChromeURLRequestContextGetter::OnAcceptLanguageChange( |
701 const std::string& accept_language) { | 617 const std::string& accept_language) { |
702 GetIOContext()->OnAcceptLanguageChange(accept_language); | 618 GetIOContext()->OnAcceptLanguageChange(accept_language); |
703 } | 619 } |
704 | 620 |
705 void ChromeURLRequestContextGetter::OnCookiePolicyChange( | 621 void ChromeURLRequestContextGetter::OnCookiePolicyChange( |
706 net::CookiePolicy::Type type) { | 622 net::CookiePolicy::Type type) { |
707 GetIOContext()->OnCookiePolicyChange(type); | 623 GetIOContext()->OnCookiePolicyChange(type); |
708 } | 624 } |
709 | 625 |
710 void ChromeURLRequestContextGetter::OnDefaultCharsetChange( | 626 void ChromeURLRequestContextGetter::OnDefaultCharsetChange( |
711 const std::string& default_charset) { | 627 const std::string& default_charset) { |
712 GetIOContext()->OnDefaultCharsetChange(default_charset); | 628 GetIOContext()->OnDefaultCharsetChange(default_charset); |
713 } | 629 } |
714 | 630 |
715 void ChromeURLRequestContextGetter::OnNewExtensions(const std::string& id, | 631 void ChromeURLRequestContextGetter::GetCookieStoreAsyncHelper( |
716 const FilePath& path) { | 632 base::WaitableEvent* completion, |
717 GetIOContext()->OnNewExtensions(id, path); | 633 net::CookieStore** result) { |
| 634 // Note that CookieStore is refcounted, yet we do not add a reference. |
| 635 *result = GetURLRequestContext()->cookie_store(); |
| 636 completion->Signal(); |
718 } | 637 } |
719 | 638 |
720 void ChromeURLRequestContextGetter::OnUnloadedExtension( | 639 // ---------------------------------------------------------------------------- |
721 const std::string& id) { | 640 // ChromeURLRequestContext |
722 GetIOContext()->OnUnloadedExtension(id); | 641 // ---------------------------------------------------------------------------- |
723 } | |
724 | |
725 void ChromeURLRequestContextGetter::CleanupOnUIThread() { | |
726 CheckCurrentlyOnMainThread(); | |
727 | |
728 if (prefs_) { | |
729 // Unregister for pref notifications. | |
730 prefs_->RemovePrefObserver(prefs::kAcceptLanguages, this); | |
731 prefs_->RemovePrefObserver(prefs::kCookieBehavior, this); | |
732 prefs_->RemovePrefObserver(prefs::kDefaultCharset, this); | |
733 prefs_ = NULL; | |
734 } | |
735 | |
736 // TODO(eroman): Doesn't look like this member is even used... | |
737 // can probably be deleted. | |
738 registrar_.RemoveAll(); | |
739 } | |
740 | |
741 ChromeURLRequestContextGetter::~ChromeURLRequestContextGetter() { | |
742 CheckCurrentlyOnIOThread(); | |
743 | |
744 DCHECK(!prefs_) << "Probably didn't call CleanupOnUIThread"; | |
745 | |
746 // Either we already transformed the factory into a URLRequestContext, or | |
747 // we still have a pending factory. | |
748 DCHECK((factory_.get() && !url_request_context_.get()) || | |
749 (!factory_.get() && url_request_context_.get())); | |
750 | |
751 // The scoped_refptr / scoped_ptr destructors take care of releasing | |
752 // |factory_| and |url_request_context_| now. | |
753 } | |
754 | 642 |
755 ChromeURLRequestContext::ChromeURLRequestContext() { | 643 ChromeURLRequestContext::ChromeURLRequestContext() { |
756 CheckCurrentlyOnIOThread(); | 644 CheckCurrentlyOnIOThread(); |
757 } | 645 } |
758 | 646 |
| 647 ChromeURLRequestContext::~ChromeURLRequestContext() { |
| 648 CheckCurrentlyOnIOThread(); |
| 649 if (appcache_service_.get() && appcache_service_->request_context() == this) |
| 650 appcache_service_->set_request_context(NULL); |
| 651 |
| 652 NotificationService::current()->Notify( |
| 653 NotificationType::URL_REQUEST_CONTEXT_RELEASED, |
| 654 Source<URLRequestContext>(this), |
| 655 NotificationService::NoDetails()); |
| 656 |
| 657 delete ftp_transaction_factory_; |
| 658 delete http_transaction_factory_; |
| 659 } |
| 660 |
759 FilePath ChromeURLRequestContext::GetPathForExtension(const std::string& id) { | 661 FilePath ChromeURLRequestContext::GetPathForExtension(const std::string& id) { |
760 ExtensionPaths::iterator iter = extension_paths_.find(id); | 662 ExtensionPaths::iterator iter = extension_paths_.find(id); |
761 if (iter != extension_paths_.end()) { | 663 if (iter != extension_paths_.end()) { |
762 return iter->second; | 664 return iter->second; |
763 } else { | 665 } else { |
764 return FilePath(); | 666 return FilePath(); |
765 } | 667 } |
766 } | 668 } |
767 | 669 |
768 const std::string& ChromeURLRequestContext::GetUserAgent( | 670 const std::string& ChromeURLRequestContext::GetUserAgent( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 NotificationType::BLACKLIST_BLOCKED_RESOURCE, | 705 NotificationType::BLACKLIST_BLOCKED_RESOURCE, |
804 Source<const ChromeURLRequestContext>(this), | 706 Source<const ChromeURLRequestContext>(this), |
805 Details<const URLRequest>(request)); | 707 Details<const URLRequest>(request)); |
806 | 708 |
807 return false; | 709 return false; |
808 } | 710 } |
809 } | 711 } |
810 return true; | 712 return true; |
811 } | 713 } |
812 | 714 |
| 715 void ChromeURLRequestContext::OnNewExtensions(const std::string& id, |
| 716 const FilePath& path) { |
| 717 if (!is_off_the_record_) |
| 718 extension_paths_[id] = path; |
| 719 } |
| 720 |
| 721 void ChromeURLRequestContext::OnUnloadedExtension(const std::string& id) { |
| 722 CheckCurrentlyOnIOThread(); |
| 723 if (is_off_the_record_) |
| 724 return; |
| 725 ExtensionPaths::iterator iter = extension_paths_.find(id); |
| 726 DCHECK(iter != extension_paths_.end()); |
| 727 extension_paths_.erase(iter); |
| 728 } |
| 729 |
| 730 ChromeURLRequestContext::ChromeURLRequestContext( |
| 731 ChromeURLRequestContext* other) { |
| 732 CheckCurrentlyOnIOThread(); |
| 733 |
| 734 // Set URLRequestContext members |
| 735 host_resolver_ = other->host_resolver_; |
| 736 proxy_service_ = other->proxy_service_; |
| 737 ssl_config_service_ = other->ssl_config_service_; |
| 738 http_transaction_factory_ = other->http_transaction_factory_; |
| 739 ftp_transaction_factory_ = other->ftp_transaction_factory_; |
| 740 cookie_store_ = other->cookie_store_; |
| 741 cookie_policy_.set_type(other->cookie_policy_.type()); |
| 742 strict_transport_security_state_ = other->strict_transport_security_state_; |
| 743 accept_language_ = other->accept_language_; |
| 744 accept_charset_ = other->accept_charset_; |
| 745 referrer_charset_ = other->referrer_charset_; |
| 746 |
| 747 // Set ChromeURLRequestContext members |
| 748 appcache_service_ = other->appcache_service_; |
| 749 extension_paths_ = other->extension_paths_; |
| 750 user_script_dir_path_ = other->user_script_dir_path_; |
| 751 blacklist_ = other->blacklist_; |
| 752 is_media_ = other->is_media_; |
| 753 is_off_the_record_ = other->is_off_the_record_; |
| 754 } |
| 755 |
813 void ChromeURLRequestContext::OnAcceptLanguageChange( | 756 void ChromeURLRequestContext::OnAcceptLanguageChange( |
814 const std::string& accept_language) { | 757 const std::string& accept_language) { |
815 CheckCurrentlyOnIOThread(); | 758 CheckCurrentlyOnIOThread(); |
816 accept_language_ = | 759 accept_language_ = |
817 net::HttpUtil::GenerateAcceptLanguageHeader(accept_language); | 760 net::HttpUtil::GenerateAcceptLanguageHeader(accept_language); |
818 } | 761 } |
819 | 762 |
820 void ChromeURLRequestContext::OnCookiePolicyChange( | 763 void ChromeURLRequestContext::OnCookiePolicyChange( |
821 net::CookiePolicy::Type type) { | 764 net::CookiePolicy::Type type) { |
822 CheckCurrentlyOnIOThread(); | 765 CheckCurrentlyOnIOThread(); |
823 cookie_policy_.set_type(type); | 766 cookie_policy_.set_type(type); |
824 } | 767 } |
825 | 768 |
826 void ChromeURLRequestContext::OnDefaultCharsetChange( | 769 void ChromeURLRequestContext::OnDefaultCharsetChange( |
827 const std::string& default_charset) { | 770 const std::string& default_charset) { |
828 CheckCurrentlyOnIOThread(); | 771 CheckCurrentlyOnIOThread(); |
829 referrer_charset_ = default_charset; | 772 referrer_charset_ = default_charset; |
830 accept_charset_ = | 773 accept_charset_ = |
831 net::HttpUtil::GenerateAcceptCharsetHeader(default_charset); | 774 net::HttpUtil::GenerateAcceptCharsetHeader(default_charset); |
832 } | 775 } |
833 | 776 |
834 void ChromeURLRequestContext::OnNewExtensions(const std::string& id, | 777 // ---------------------------------------------------------------------------- |
835 const FilePath& path) { | 778 // ChromeURLRequestContextFactory |
836 if (!is_off_the_record_) | 779 // ---------------------------------------------------------------------------- |
837 extension_paths_[id] = path; | 780 |
| 781 // Extract values from |profile| and copy them into |
| 782 // ChromeURLRequestContextFactory. We will use them later when constructing the |
| 783 // ChromeURLRequestContext on the IO thread (see |
| 784 // ApplyProfileParametersToContext() which reverses this). |
| 785 ChromeURLRequestContextFactory::ChromeURLRequestContextFactory(Profile* profile) |
| 786 : is_media_(false), |
| 787 is_off_the_record_(profile->IsOffTheRecord()) { |
| 788 CheckCurrentlyOnMainThread(); |
| 789 PrefService* prefs = profile->GetPrefs(); |
| 790 |
| 791 // Set up Accept-Language and Accept-Charset header values |
| 792 accept_language_ = net::HttpUtil::GenerateAcceptLanguageHeader( |
| 793 WideToASCII(prefs->GetString(prefs::kAcceptLanguages))); |
| 794 std::string default_charset = |
| 795 WideToASCII(prefs->GetString(prefs::kDefaultCharset)); |
| 796 accept_charset_ = |
| 797 net::HttpUtil::GenerateAcceptCharsetHeader(default_charset); |
| 798 |
| 799 // At this point, we don't know the charset of the referring page |
| 800 // where a url request originates from. This is used to get a suggested |
| 801 // filename from Content-Disposition header made of raw 8bit characters. |
| 802 // Down the road, it can be overriden if it becomes known (for instance, |
| 803 // when download request is made through the context menu in a web page). |
| 804 // At the moment, it'll remain 'undeterministic' when a user |
| 805 // types a URL in the omnibar or click on a download link in a page. |
| 806 // For the latter, we need a change on the webkit-side. |
| 807 // We initialize it to the default charset here and a user will |
| 808 // have an *arguably* better default charset for interpreting a raw 8bit |
| 809 // C-D header field. It means the native OS codepage fallback in |
| 810 // net_util::GetSuggestedFilename is unlikely to be taken. |
| 811 referrer_charset_ = default_charset; |
| 812 |
| 813 cookie_policy_type_ = net::CookiePolicy::FromInt( |
| 814 prefs->GetInteger(prefs::kCookieBehavior)); |
| 815 |
| 816 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! |
| 817 blacklist_ = profile->GetBlacklist(); |
| 818 |
| 819 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! |
| 820 strict_transport_security_state_ = profile->GetStrictTransportSecurityState(); |
| 821 |
| 822 if (profile->GetExtensionsService()) { |
| 823 const ExtensionList* extensions = |
| 824 profile->GetExtensionsService()->extensions(); |
| 825 for (ExtensionList::const_iterator iter = extensions->begin(); |
| 826 iter != extensions->end(); ++iter) { |
| 827 extension_paths_[(*iter)->id()] = (*iter)->path(); |
| 828 } |
| 829 } |
| 830 |
| 831 if (profile->GetUserScriptMaster()) |
| 832 user_script_dir_path_ = profile->GetUserScriptMaster()->user_script_dir(); |
| 833 |
| 834 // TODO(eroman): this doesn't look safe; sharing between IO and UI threads! |
| 835 ssl_config_service_ = profile->GetSSLConfigService(); |
| 836 |
| 837 profile_dir_path_ = profile->GetPath(); |
838 } | 838 } |
839 | 839 |
840 void ChromeURLRequestContext::OnUnloadedExtension(const std::string& id) { | 840 ChromeURLRequestContextFactory::~ChromeURLRequestContextFactory() { |
841 CheckCurrentlyOnIOThread(); | 841 CheckCurrentlyOnIOThread(); |
842 if (is_off_the_record_) | |
843 return; | |
844 ExtensionPaths::iterator iter = extension_paths_.find(id); | |
845 DCHECK(iter != extension_paths_.end()); | |
846 extension_paths_.erase(iter); | |
847 } | 842 } |
848 | 843 |
849 ChromeURLRequestContext::~ChromeURLRequestContext() { | 844 void ChromeURLRequestContextFactory::ApplyProfileParametersToContext( |
850 CheckCurrentlyOnIOThread(); | 845 ChromeURLRequestContext* context) { |
851 if (appcache_service_.get() && appcache_service_->request_context() == this) | 846 // Apply all the parameters. NOTE: keep this in sync with |
852 appcache_service_->set_request_context(NULL); | 847 // ChromeURLRequestContextFactory(Profile*). |
853 | 848 context->set_is_media(is_media_); |
854 NotificationService::current()->Notify( | 849 context->set_is_off_the_record(is_off_the_record_); |
855 NotificationType::URL_REQUEST_CONTEXT_RELEASED, | 850 context->set_accept_language(accept_language_); |
856 Source<URLRequestContext>(this), | 851 context->set_accept_charset(accept_charset_); |
857 NotificationService::NoDetails()); | 852 context->set_referrer_charset(referrer_charset_); |
858 | 853 context->set_cookie_policy_type(cookie_policy_type_); |
859 delete ftp_transaction_factory_; | 854 context->set_extension_paths(extension_paths_); |
860 delete http_transaction_factory_; | 855 context->set_user_script_dir_path(user_script_dir_path_); |
| 856 context->set_blacklist(blacklist_); |
| 857 context->set_strict_transport_security_state( |
| 858 strict_transport_security_state_); |
| 859 context->set_ssl_config_service(ssl_config_service_); |
861 } | 860 } |
862 | 861 |
863 net::CookieStore* ChromeURLRequestContextGetter::GetCookieStore() { | 862 // ---------------------------------------------------------------------------- |
864 // If we are running on the IO thread this is real easy. | |
865 if (ChromeThread::CurrentlyOn(ChromeThread::IO)) | |
866 return GetURLRequestContext()->cookie_store(); | |
867 | 863 |
868 // If we aren't running on the IO thread, we cannot call | 864 net::ProxyConfig* CreateProxyConfig(const CommandLine& command_line) { |
869 // GetURLRequestContext(). Instead we will post a task to the IO loop | 865 // Scan for all "enable" type proxy switches. |
870 // and wait for it to complete. | 866 static const char* proxy_switches[] = { |
| 867 switches::kProxyServer, |
| 868 switches::kProxyPacUrl, |
| 869 switches::kProxyAutoDetect, |
| 870 switches::kProxyBypassList |
| 871 }; |
871 | 872 |
872 base::WaitableEvent completion(false, false); | 873 bool found_enable_proxy_switch = false; |
873 net::CookieStore* result = NULL; | 874 for (size_t i = 0; i < arraysize(proxy_switches); i++) { |
| 875 if (command_line.HasSwitch(proxy_switches[i])) { |
| 876 found_enable_proxy_switch = true; |
| 877 break; |
| 878 } |
| 879 } |
874 | 880 |
875 ChromeThread::PostTask( | 881 if (!found_enable_proxy_switch && |
876 ChromeThread::IO, FROM_HERE, | 882 !command_line.HasSwitch(switches::kNoProxyServer)) { |
877 NewRunnableMethod(this, | 883 return NULL; |
878 &ChromeURLRequestContextGetter::GetCookieStoreAsyncHelper, | 884 } |
879 &completion, | |
880 &result)); | |
881 | 885 |
882 completion.Wait(); | 886 net::ProxyConfig* proxy_config = new net::ProxyConfig(); |
883 DCHECK(result); | 887 if (command_line.HasSwitch(switches::kNoProxyServer)) { |
884 return result; | 888 // Ignore (and warn about) all the other proxy config switches we get if |
| 889 // the --no-proxy-server command line argument is present. |
| 890 if (found_enable_proxy_switch) { |
| 891 LOG(WARNING) << "Additional command line proxy switches found when --" |
| 892 << switches::kNoProxyServer << " was specified."; |
| 893 } |
| 894 return proxy_config; |
| 895 } |
| 896 |
| 897 if (command_line.HasSwitch(switches::kProxyServer)) { |
| 898 const std::wstring& proxy_server = |
| 899 command_line.GetSwitchValue(switches::kProxyServer); |
| 900 proxy_config->proxy_rules.ParseFromString(WideToASCII(proxy_server)); |
| 901 } |
| 902 |
| 903 if (command_line.HasSwitch(switches::kProxyPacUrl)) { |
| 904 proxy_config->pac_url = |
| 905 GURL(WideToASCII(command_line.GetSwitchValue( |
| 906 switches::kProxyPacUrl))); |
| 907 } |
| 908 |
| 909 if (command_line.HasSwitch(switches::kProxyAutoDetect)) { |
| 910 proxy_config->auto_detect = true; |
| 911 } |
| 912 |
| 913 if (command_line.HasSwitch(switches::kProxyBypassList)) { |
| 914 proxy_config->ParseNoProxyList( |
| 915 WideToASCII(command_line.GetSwitchValue( |
| 916 switches::kProxyBypassList))); |
| 917 } |
| 918 |
| 919 return proxy_config; |
885 } | 920 } |
886 | |
887 void ChromeURLRequestContextGetter::GetCookieStoreAsyncHelper( | |
888 base::WaitableEvent* completion, | |
889 net::CookieStore** result) { | |
890 // Note that CookieStore is refcounted, yet we do not add a reference. | |
891 *result = GetURLRequestContext()->cookie_store(); | |
892 completion->Signal(); | |
893 } | |
OLD | NEW |