Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_impl_io_data.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/profiles/profile_impl_io_data.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/prefs/pref_member.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/threading/sequenced_worker_pool.h"
17 #include "base/threading/worker_pool.h"
18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/chromeos/profiles/profile_helper.h"
20 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
21 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
22 #include "chrome/browser/extensions/extension_protocols.h"
23 #include "chrome/browser/io_thread.h"
24 #include "chrome/browser/net/chrome_net_log.h"
25 #include "chrome/browser/net/chrome_network_delegate.h"
26 #include "chrome/browser/net/connect_interceptor.h"
27 #include "chrome/browser/net/cookie_store_util.h"
28 #include "chrome/browser/net/http_server_properties_manager.h"
29 #include "chrome/browser/net/predictor.h"
30 #include "chrome/browser/net/sqlite_server_bound_cert_store.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/common/chrome_constants.h"
33 #include "chrome/common/chrome_switches.h"
34 #include "chrome/common/pref_names.h"
35 #include "chrome/common/url_constants.h"
36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/cookie_store_factory.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/resource_context.h"
40 #include "content/public/browser/storage_partition.h"
41 #include "extensions/common/constants.h"
42 #include "net/base/cache_type.h"
43 #include "net/ftp/ftp_network_layer.h"
44 #include "net/http/http_cache.h"
45 #include "net/ssl/server_bound_cert_service.h"
46 #include "net/url_request/protocol_intercept_job_factory.h"
47 #include "net/url_request/url_request_job_factory_impl.h"
48 #include "webkit/browser/quota/special_storage_policy.h"
49
50 #if defined(OS_ANDROID) || defined(OS_IOS)
51 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
52 #endif
53
54 namespace {
55
56 net::BackendType ChooseCacheBackendType() {
57   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
58   if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
59     const std::string opt_value =
60         command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
61     if (LowerCaseEqualsASCII(opt_value, "off"))
62       return net::CACHE_BACKEND_BLOCKFILE;
63     if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
64       return net::CACHE_BACKEND_SIMPLE;
65   }
66   const std::string experiment_name =
67       base::FieldTrialList::FindFullName("SimpleCacheTrial");
68 #if defined(OS_ANDROID)
69   if (experiment_name == "ExperimentNo" ||
70       experiment_name == "ExperimentControl") {
71     return net::CACHE_BACKEND_BLOCKFILE;
72   }
73   return net::CACHE_BACKEND_SIMPLE;
74 #else
75   if (experiment_name == "ExperimentYes" ||
76       experiment_name == "ExperimentYes2") {
77     return net::CACHE_BACKEND_SIMPLE;
78   }
79   return net::CACHE_BACKEND_BLOCKFILE;
80 #endif
81 }
82
83 }  // namespace
84
85 using content::BrowserThread;
86
87 ProfileImplIOData::Handle::Handle(Profile* profile)
88     : io_data_(new ProfileImplIOData),
89       profile_(profile),
90       initialized_(false) {
91   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
92   DCHECK(profile);
93 }
94
95 ProfileImplIOData::Handle::~Handle() {
96   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97   if (io_data_->predictor_ != NULL) {
98     // io_data_->predictor_ might be NULL if Init() was never called
99     // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
100     bool save_prefs = true;
101 #if defined(OS_CHROMEOS)
102     save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
103 #endif
104     if (save_prefs) {
105       io_data_->predictor_->SaveStateForNextStartupAndTrim(
106           profile_->GetPrefs());
107     }
108     io_data_->predictor_->ShutdownOnUIThread();
109   }
110
111   if (io_data_->http_server_properties_manager_)
112     io_data_->http_server_properties_manager_->ShutdownOnUIThread();
113   io_data_->ShutdownOnUIThread();
114 }
115
116 void ProfileImplIOData::Handle::Init(
117       const base::FilePath& cookie_path,
118       const base::FilePath& server_bound_cert_path,
119       const base::FilePath& cache_path,
120       int cache_max_size,
121       const base::FilePath& media_cache_path,
122       int media_cache_max_size,
123       const base::FilePath& extensions_cookie_path,
124       const base::FilePath& profile_path,
125       const base::FilePath& infinite_cache_path,
126       chrome_browser_net::Predictor* predictor,
127       content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
128       quota::SpecialStoragePolicy* special_storage_policy) {
129   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
130   DCHECK(!io_data_->lazy_params_);
131   DCHECK(predictor);
132
133   LazyParams* lazy_params = new LazyParams();
134
135   lazy_params->cookie_path = cookie_path;
136   lazy_params->server_bound_cert_path = server_bound_cert_path;
137   lazy_params->cache_path = cache_path;
138   lazy_params->cache_max_size = cache_max_size;
139   lazy_params->media_cache_path = media_cache_path;
140   lazy_params->media_cache_max_size = media_cache_max_size;
141   lazy_params->extensions_cookie_path = extensions_cookie_path;
142   lazy_params->infinite_cache_path = infinite_cache_path;
143   lazy_params->session_cookie_mode = session_cookie_mode;
144   lazy_params->special_storage_policy = special_storage_policy;
145
146   io_data_->lazy_params_.reset(lazy_params);
147
148   // Keep track of profile path and cache sizes separately so we can use them
149   // on demand when creating storage isolated URLRequestContextGetters.
150   io_data_->profile_path_ = profile_path;
151   io_data_->app_cache_max_size_ = cache_max_size;
152   io_data_->app_media_cache_max_size_ = media_cache_max_size;
153
154   io_data_->predictor_.reset(predictor);
155
156   io_data_->InitializeMetricsEnabledStateOnUIThread();
157 }
158
159 content::ResourceContext*
160     ProfileImplIOData::Handle::GetResourceContext() const {
161   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162   LazyInitialize();
163   return GetResourceContextNoInit();
164 }
165
166 content::ResourceContext*
167 ProfileImplIOData::Handle::GetResourceContextNoInit() const {
168   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
169   // Don't call LazyInitialize here, since the resource context is created at
170   // the beginning of initalization and is used by some members while they're
171   // being initialized (i.e. AppCacheService).
172   return io_data_->GetResourceContext();
173 }
174
175 scoped_refptr<ChromeURLRequestContextGetter>
176 ProfileImplIOData::Handle::CreateMainRequestContextGetter(
177     content::ProtocolHandlerMap* protocol_handlers,
178     PrefService* local_state,
179     IOThread* io_thread) const {
180   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181   LazyInitialize();
182   DCHECK(!main_request_context_getter_.get());
183   main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
184       profile_, io_data_, protocol_handlers);
185
186   io_data_->predictor_
187       ->InitNetworkPredictor(profile_->GetPrefs(),
188                              local_state,
189                              io_thread,
190                              main_request_context_getter_.get());
191
192   content::NotificationService::current()->Notify(
193       chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
194       content::Source<Profile>(profile_),
195       content::NotificationService::NoDetails());
196   return main_request_context_getter_;
197 }
198
199 scoped_refptr<ChromeURLRequestContextGetter>
200 ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
201   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
202   LazyInitialize();
203   if (!media_request_context_getter_.get()) {
204     media_request_context_getter_ =
205         ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
206   }
207   return media_request_context_getter_;
208 }
209
210 scoped_refptr<ChromeURLRequestContextGetter>
211 ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
212   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
213   LazyInitialize();
214   if (!extensions_request_context_getter_.get()) {
215     extensions_request_context_getter_ =
216         ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
217   }
218   return extensions_request_context_getter_;
219 }
220
221 scoped_refptr<ChromeURLRequestContextGetter>
222 ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
223     const base::FilePath& partition_path,
224     bool in_memory,
225     content::ProtocolHandlerMap* protocol_handlers) const {
226   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
227   // Check that the partition_path is not the same as the base profile path. We
228   // expect isolated partition, which will never go to the default profile path.
229   CHECK(partition_path != profile_->GetPath());
230   LazyInitialize();
231
232   // Keep a map of request context getters, one per requested storage partition.
233   StoragePartitionDescriptor descriptor(partition_path, in_memory);
234   ChromeURLRequestContextGetterMap::iterator iter =
235       app_request_context_getter_map_.find(descriptor);
236   if (iter != app_request_context_getter_map_.end())
237     return iter->second;
238
239   scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
240       protocol_handler_interceptor(
241           ProtocolHandlerRegistryFactory::GetForProfile(profile_)->
242               CreateJobInterceptorFactory());
243   ChromeURLRequestContextGetter* context =
244       ChromeURLRequestContextGetter::CreateForIsolatedApp(
245           profile_, io_data_, descriptor,
246           protocol_handler_interceptor.Pass(),
247           protocol_handlers);
248   app_request_context_getter_map_[descriptor] = context;
249
250   return context;
251 }
252
253 scoped_refptr<ChromeURLRequestContextGetter>
254 ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
255     const base::FilePath& partition_path,
256     bool in_memory) const {
257   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
258   // We must have a non-default path, or this will act like the default media
259   // context.
260   CHECK(partition_path != profile_->GetPath());
261   LazyInitialize();
262
263   // Keep a map of request context getters, one per requested storage partition.
264   StoragePartitionDescriptor descriptor(partition_path, in_memory);
265   ChromeURLRequestContextGetterMap::iterator iter =
266       isolated_media_request_context_getter_map_.find(descriptor);
267   if (iter != isolated_media_request_context_getter_map_.end())
268     return iter->second;
269
270   // Get the app context as the starting point for the media context, so that
271   // it uses the app's cookie store.
272   ChromeURLRequestContextGetterMap::const_iterator app_iter =
273       app_request_context_getter_map_.find(descriptor);
274   DCHECK(app_iter != app_request_context_getter_map_.end());
275   ChromeURLRequestContextGetter* app_context = app_iter->second.get();
276   ChromeURLRequestContextGetter* context =
277       ChromeURLRequestContextGetter::CreateForIsolatedMedia(
278           profile_, app_context, io_data_, descriptor);
279   isolated_media_request_context_getter_map_[descriptor] = context;
280
281   return context;
282 }
283
284 void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
285     base::Time time,
286     const base::Closure& completion) {
287   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
288   LazyInitialize();
289
290   BrowserThread::PostTask(
291       BrowserThread::IO, FROM_HERE,
292       base::Bind(
293           &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
294           base::Unretained(io_data_),
295           time,
296           completion));
297 }
298
299 void ProfileImplIOData::Handle::LazyInitialize() const {
300   if (initialized_)
301     return;
302
303   // Set initialized_ to true at the beginning in case any of the objects
304   // below try to get the ResourceContext pointer.
305   initialized_ = true;
306   PrefService* pref_service = profile_->GetPrefs();
307   io_data_->http_server_properties_manager_ =
308       new chrome_browser_net::HttpServerPropertiesManager(pref_service);
309   io_data_->set_http_server_properties(
310       scoped_ptr<net::HttpServerProperties>(
311           io_data_->http_server_properties_manager_));
312   io_data_->session_startup_pref()->Init(
313       prefs::kRestoreOnStartup, pref_service);
314   io_data_->session_startup_pref()->MoveToThread(
315       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
316 #if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
317   io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
318       pref_service);
319   io_data_->safe_browsing_enabled()->MoveToThread(
320       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
321 #endif
322   io_data_->InitializeOnUIThread(profile_);
323 }
324
325 ProfileImplIOData::LazyParams::LazyParams()
326     : cache_max_size(0),
327       media_cache_max_size(0),
328       session_cookie_mode(
329           content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
330
331 ProfileImplIOData::LazyParams::~LazyParams() {}
332
333 ProfileImplIOData::ProfileImplIOData()
334     : ProfileIOData(false),
335       http_server_properties_manager_(NULL),
336       app_cache_max_size_(0),
337       app_media_cache_max_size_(0) {
338 }
339
340 ProfileImplIOData::~ProfileImplIOData() {
341   DestroyResourceContext();
342
343   if (media_request_context_)
344     media_request_context_->AssertNoURLRequests();
345 }
346
347 void ProfileImplIOData::InitializeInternal(
348     ProfileParams* profile_params,
349     content::ProtocolHandlerMap* protocol_handlers) const {
350   ChromeURLRequestContext* main_context = main_request_context();
351
352   IOThread* const io_thread = profile_params->io_thread;
353   IOThread::Globals* const io_thread_globals = io_thread->globals();
354
355   network_delegate()->set_predictor(predictor_.get());
356
357   // Initialize context members.
358
359   ApplyProfileParamsToContext(main_context);
360
361   if (http_server_properties_manager_)
362     http_server_properties_manager_->InitializeOnIOThread();
363
364   main_context->set_transport_security_state(transport_security_state());
365
366   main_context->set_net_log(io_thread->net_log());
367
368   main_context->set_network_delegate(network_delegate());
369
370   main_context->set_http_server_properties(http_server_properties());
371
372   main_context->set_host_resolver(
373       io_thread_globals->host_resolver.get());
374   main_context->set_cert_transparency_verifier(
375       io_thread_globals->cert_transparency_verifier.get());
376   main_context->set_http_auth_handler_factory(
377       io_thread_globals->http_auth_handler_factory.get());
378
379   main_context->set_fraudulent_certificate_reporter(
380       fraudulent_certificate_reporter());
381
382   main_context->set_throttler_manager(
383       io_thread_globals->throttler_manager.get());
384
385   main_context->set_proxy_service(proxy_service());
386
387   scoped_refptr<net::CookieStore> cookie_store = NULL;
388   net::ServerBoundCertService* server_bound_cert_service = NULL;
389   if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
390     // Don't use existing cookies and use an in-memory store.
391     using content::CookieStoreConfig;
392     cookie_store = content::CreateCookieStore(CookieStoreConfig(
393         base::FilePath(),
394         CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
395         NULL,
396         profile_params->cookie_monster_delegate.get()));
397     // Don't use existing server-bound certs and use an in-memory store.
398     server_bound_cert_service = new net::ServerBoundCertService(
399         new net::DefaultServerBoundCertStore(NULL),
400         base::WorkerPool::GetTaskRunner(true));
401   }
402
403
404   // setup cookie store
405   if (!cookie_store.get()) {
406     DCHECK(!lazy_params_->cookie_path.empty());
407
408     content::CookieStoreConfig cookie_config(
409         lazy_params_->cookie_path,
410         lazy_params_->session_cookie_mode,
411         lazy_params_->special_storage_policy.get(),
412         profile_params->cookie_monster_delegate.get());
413     cookie_config.crypto_delegate =
414       chrome_browser_net::GetCookieCryptoDelegate();
415     cookie_store = content::CreateCookieStore(cookie_config);
416   }
417
418   main_context->set_cookie_store(cookie_store.get());
419
420   // Setup server bound cert service.
421   if (!server_bound_cert_service) {
422     DCHECK(!lazy_params_->server_bound_cert_path.empty());
423
424     scoped_refptr<SQLiteServerBoundCertStore> server_bound_cert_db =
425         new SQLiteServerBoundCertStore(
426             lazy_params_->server_bound_cert_path,
427             BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
428                 BrowserThread::GetBlockingPool()->GetSequenceToken()),
429             lazy_params_->special_storage_policy.get());
430     server_bound_cert_service = new net::ServerBoundCertService(
431         new net::DefaultServerBoundCertStore(server_bound_cert_db.get()),
432         base::WorkerPool::GetTaskRunner(true));
433   }
434
435   set_server_bound_cert_service(server_bound_cert_service);
436   main_context->set_server_bound_cert_service(server_bound_cert_service);
437
438   net::HttpCache::DefaultBackend* main_backend =
439       new net::HttpCache::DefaultBackend(
440           net::DISK_CACHE,
441           ChooseCacheBackendType(),
442           lazy_params_->cache_path,
443           lazy_params_->cache_max_size,
444           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
445               .get());
446   net::HttpNetworkSession::Params network_session_params;
447   PopulateNetworkSessionParams(profile_params, &network_session_params);
448   net::HttpCache* main_cache = new net::HttpCache(
449       network_session_params, main_backend);
450   main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
451
452 #if defined(OS_ANDROID) || defined(OS_IOS)
453   DataReductionProxySettings::InitDataReductionProxySession(
454       main_cache->GetSession());
455 #endif
456
457   if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
458     main_cache->set_mode(
459         chrome_browser_net::IsCookieRecordMode() ?
460         net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
461   }
462
463   main_http_factory_.reset(main_cache);
464   main_context->set_http_transaction_factory(main_cache);
465
466 #if !defined(DISABLE_FTP_SUPPORT)
467   ftp_factory_.reset(
468       new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
469 #endif  // !defined(DISABLE_FTP_SUPPORT)
470
471   scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
472       new net::URLRequestJobFactoryImpl());
473   InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
474   main_job_factory_ = SetUpJobFactoryDefaults(
475       main_job_factory.Pass(),
476       profile_params->protocol_handler_interceptor.Pass(),
477       network_delegate(),
478       ftp_factory_.get());
479   main_context->set_job_factory(main_job_factory_.get());
480
481 #if defined(ENABLE_EXTENSIONS)
482   InitializeExtensionsRequestContext(profile_params);
483 #endif
484
485   // Create a media request context based on the main context, but using a
486   // media cache.  It shares the same job factory as the main context.
487   StoragePartitionDescriptor details(profile_path_, false);
488   media_request_context_.reset(InitializeMediaRequestContext(main_context,
489                                                              details));
490
491   lazy_params_.reset();
492 }
493
494 void ProfileImplIOData::
495     InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
496   ChromeURLRequestContext* extensions_context = extensions_request_context();
497   IOThread* const io_thread = profile_params->io_thread;
498   IOThread::Globals* const io_thread_globals = io_thread->globals();
499   ApplyProfileParamsToContext(extensions_context);
500
501   extensions_context->set_transport_security_state(transport_security_state());
502
503   extensions_context->set_net_log(io_thread->net_log());
504
505   extensions_context->set_throttler_manager(
506       io_thread_globals->throttler_manager.get());
507
508   content::CookieStoreConfig cookie_config(
509       lazy_params_->extensions_cookie_path,
510       lazy_params_->session_cookie_mode,
511       NULL, NULL);
512   cookie_config.crypto_delegate =
513       chrome_browser_net::GetCookieCryptoDelegate();
514   net::CookieStore* extensions_cookie_store =
515       content::CreateCookieStore(cookie_config);
516   // Enable cookies for devtools and extension URLs.
517   const char* schemes[] = {content::kChromeDevToolsScheme,
518                            extensions::kExtensionScheme};
519   extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(schemes, 2);
520   extensions_context->set_cookie_store(extensions_cookie_store);
521
522   scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
523       new net::URLRequestJobFactoryImpl());
524   // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
525   // Without a network_delegate, this protocol handler will never
526   // handle file: requests, but as a side effect it makes
527   // job_factory::IsHandledProtocol return true, which prevents attempts to
528   // handle the protocol externally. We pass NULL in to
529   // SetUpJobFactory() to get this effect.
530   extensions_job_factory_ = SetUpJobFactoryDefaults(
531       extensions_job_factory.Pass(),
532       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
533       NULL,
534       ftp_factory_.get());
535   extensions_context->set_job_factory(extensions_job_factory_.get());
536 }
537
538 ChromeURLRequestContext*
539 ProfileImplIOData::InitializeAppRequestContext(
540     ChromeURLRequestContext* main_context,
541     const StoragePartitionDescriptor& partition_descriptor,
542     scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
543         protocol_handler_interceptor,
544     content::ProtocolHandlerMap* protocol_handlers) const {
545   // Copy most state from the main context.
546   AppRequestContext* context = new AppRequestContext();
547   context->CopyFrom(main_context);
548
549   base::FilePath cookie_path = partition_descriptor.path.Append(
550       chrome::kCookieFilename);
551   base::FilePath cache_path =
552       partition_descriptor.path.Append(chrome::kCacheDirname);
553
554   // Use a separate HTTP disk cache for isolated apps.
555   net::HttpCache::BackendFactory* app_backend = NULL;
556   if (partition_descriptor.in_memory) {
557     app_backend = net::HttpCache::DefaultBackend::InMemory(0);
558   } else {
559     app_backend = new net::HttpCache::DefaultBackend(
560         net::DISK_CACHE,
561         ChooseCacheBackendType(),
562         cache_path,
563         app_cache_max_size_,
564         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
565             .get());
566   }
567   net::HttpNetworkSession* main_network_session =
568       main_http_factory_->GetSession();
569   net::HttpCache* app_http_cache =
570       new net::HttpCache(main_network_session, app_backend);
571
572   scoped_refptr<net::CookieStore> cookie_store = NULL;
573   if (partition_descriptor.in_memory) {
574     cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
575   } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
576     // Don't use existing cookies and use an in-memory store.
577     // TODO(creis): We should have a cookie delegate for notifying the cookie
578     // extensions API, but we need to update it to understand isolated apps
579     // first.
580     cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
581     app_http_cache->set_mode(
582         chrome_browser_net::IsCookieRecordMode() ?
583         net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
584   }
585
586   // Use an app-specific cookie store.
587   if (!cookie_store.get()) {
588     DCHECK(!cookie_path.empty());
589
590     // TODO(creis): We should have a cookie delegate for notifying the cookie
591     // extensions API, but we need to update it to understand isolated apps
592     // first.
593     content::CookieStoreConfig cookie_config(
594         cookie_path,
595         content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
596         NULL, NULL);
597     cookie_config.crypto_delegate =
598       chrome_browser_net::GetCookieCryptoDelegate();
599     cookie_store = content::CreateCookieStore(cookie_config);
600   }
601
602   // Transfer ownership of the cookies and cache to AppRequestContext.
603   context->SetCookieStore(cookie_store.get());
604   context->SetHttpTransactionFactory(
605       scoped_ptr<net::HttpTransactionFactory>(app_http_cache));
606
607   scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
608       new net::URLRequestJobFactoryImpl());
609   InstallProtocolHandlers(job_factory.get(), protocol_handlers);
610   scoped_ptr<net::URLRequestJobFactory> top_job_factory(
611       SetUpJobFactoryDefaults(
612           job_factory.Pass(), protocol_handler_interceptor.Pass(),
613           network_delegate(),
614           ftp_factory_.get()));
615   context->SetJobFactory(top_job_factory.Pass());
616
617   return context;
618 }
619
620 ChromeURLRequestContext*
621 ProfileImplIOData::InitializeMediaRequestContext(
622     ChromeURLRequestContext* original_context,
623     const StoragePartitionDescriptor& partition_descriptor) const {
624   // Copy most state from the original context.
625   MediaRequestContext* context = new MediaRequestContext();
626   context->CopyFrom(original_context);
627
628   // For in-memory context, return immediately after creating the new
629   // context before attaching a separate cache. It is important to return
630   // a new context rather than just reusing |original_context| because
631   // the caller expects to take ownership of the pointer.
632   if (partition_descriptor.in_memory)
633     return context;
634
635   using content::StoragePartition;
636   base::FilePath cache_path;
637   int cache_max_size = app_media_cache_max_size_;
638   if (partition_descriptor.path == profile_path_) {
639     // lazy_params_ is only valid for the default media context creation.
640     cache_path = lazy_params_->media_cache_path;
641     cache_max_size = lazy_params_->media_cache_max_size;
642   } else {
643     cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
644   }
645
646   // Use a separate HTTP disk cache for isolated apps.
647   net::HttpCache::BackendFactory* media_backend =
648       new net::HttpCache::DefaultBackend(
649           net::MEDIA_CACHE,
650           ChooseCacheBackendType(),
651           cache_path,
652           cache_max_size,
653           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE)
654               .get());
655   net::HttpNetworkSession* main_network_session =
656       main_http_factory_->GetSession();
657   scoped_ptr<net::HttpTransactionFactory> media_http_cache(
658       new net::HttpCache(main_network_session, media_backend));
659
660   // Transfer ownership of the cache to MediaRequestContext.
661   context->SetHttpTransactionFactory(media_http_cache.Pass());
662
663   // Note that we do not create a new URLRequestJobFactory because
664   // the media context should behave exactly like its parent context
665   // in all respects except for cache behavior on media subresources.
666   // The CopyFrom() step above means that our media context will use
667   // the same URLRequestJobFactory instance that our parent context does.
668
669   return context;
670 }
671
672 ChromeURLRequestContext*
673 ProfileImplIOData::AcquireMediaRequestContext() const {
674   DCHECK(media_request_context_);
675   return media_request_context_.get();
676 }
677
678 ChromeURLRequestContext*
679 ProfileImplIOData::AcquireIsolatedAppRequestContext(
680     ChromeURLRequestContext* main_context,
681     const StoragePartitionDescriptor& partition_descriptor,
682     scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
683         protocol_handler_interceptor,
684     content::ProtocolHandlerMap* protocol_handlers) const {
685   // We create per-app contexts on demand, unlike the others above.
686   ChromeURLRequestContext* app_request_context =
687       InitializeAppRequestContext(main_context, partition_descriptor,
688                                   protocol_handler_interceptor.Pass(),
689                                   protocol_handlers);
690   DCHECK(app_request_context);
691   return app_request_context;
692 }
693
694 ChromeURLRequestContext*
695 ProfileImplIOData::AcquireIsolatedMediaRequestContext(
696     ChromeURLRequestContext* app_context,
697     const StoragePartitionDescriptor& partition_descriptor) const {
698   // We create per-app media contexts on demand, unlike the others above.
699   ChromeURLRequestContext* media_request_context =
700       InitializeMediaRequestContext(app_context, partition_descriptor);
701   DCHECK(media_request_context);
702   return media_request_context;
703 }
704
705 void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
706     base::Time time,
707     const base::Closure& completion) {
708   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
709   DCHECK(initialized());
710
711   DCHECK(transport_security_state());
712   // Completes synchronously.
713   transport_security_state()->DeleteAllDynamicDataSince(time);
714   DCHECK(http_server_properties_manager_);
715   http_server_properties_manager_->Clear(completion);
716 }