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