15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile_impl_io_data.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/metrics/field_trial.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_member.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/sequenced_task_runner.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/threading/sequenced_worker_pool.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/worker_pool.h"
187dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/chromeos/profiles/profile_helper.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/io_thread.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/chrome_net_log.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/net/chrome_network_delegate.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/connect_interceptor.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/net/cookie_store_util.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/net/http_server_properties_manager_factory.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/predictor.h"
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/net/quota_policy_channel_id_store.h"
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_request_handler.h"
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.h"
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h"
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
4223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "components/domain_reliability/monitor.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/cookie_store_factory.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/resource_context.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/storage_partition.h"
48a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "extensions/browser/extension_protocols.h"
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "extensions/common/constants.h"
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "net/base/cache_type.h"
516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "net/base/sdch_dictionary_fetcher.h"
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "net/base/sdch_manager.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/ftp/ftp_network_layer.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_cache.h"
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/http/http_server_properties_manager.h"
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/ssl/channel_id_service.h"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job_factory_impl.h"
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "storage/browser/quota/special_storage_policy.h"
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace {
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)net::BackendType ChooseCacheBackendType() {
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#if defined(OS_ANDROID)
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return net::CACHE_BACKEND_SIMPLE;
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#else
66a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
68a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::string opt_value =
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (LowerCaseEqualsASCII(opt_value, "off"))
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return net::CACHE_BACKEND_BLOCKFILE;
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      return net::CACHE_BACKEND_SIMPLE;
74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const std::string experiment_name =
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      base::FieldTrialList::FindFullName("SimpleCacheTrial");
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (experiment_name == "ExperimentYes" ||
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      experiment_name == "ExperimentYes2") {
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return net::CACHE_BACKEND_SIMPLE;
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return net::CACHE_BACKEND_BLOCKFILE;
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)using data_reduction_proxy::DataReductionProxyParams;
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::Handle(Profile* profile)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : io_data_(new ProfileImplIOData),
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      profile_(profile),
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      initialized_(false) {
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(profile);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::~Handle() {
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  io_data_->data_reduction_proxy_statistics_prefs()->WritePrefs();
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
102b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (io_data_->predictor_ != NULL) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // io_data_->predictor_ might be NULL if Init() was never called
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    bool save_prefs = true;
1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#if defined(OS_CHROMEOS)
107f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
1084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (save_prefs)
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      io_data_->predictor_->SaveStateForNextStartupAndTrim();
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    io_data_->predictor_->ShutdownOnUIThread();
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (io_data_->http_server_properties_manager_)
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  io_data_->ShutdownOnUIThread(GetAllContextGetters().Pass());
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileImplIOData::Handle::Init(
12103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& cookie_path,
12203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& channel_id_path,
12303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& cache_path,
12403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    int cache_max_size,
12503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& media_cache_path,
12603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    int media_cache_max_size,
12703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& extensions_cookie_path,
12803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& profile_path,
12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::FilePath& infinite_cache_path,
13003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    chrome_browser_net::Predictor* predictor,
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    storage::SpecialStoragePolicy* special_storage_policy,
13303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    scoped_ptr<domain_reliability::DomainReliabilityMonitor>
13403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        domain_reliability_monitor,
13503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
13603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    scoped_ptr<DataReductionProxyChromeConfigurator>
13703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        data_reduction_proxy_chrome_configurator,
13803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        data_reduction_proxy_params,
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        data_reduction_proxy_statistics_prefs) {
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
143b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  DCHECK(!io_data_->lazy_params_);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(predictor);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LazyParams* lazy_params = new LazyParams();
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->cookie_path = cookie_path;
1495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  lazy_params->channel_id_path = channel_id_path;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->cache_path = cache_path;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->cache_max_size = cache_max_size;
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->media_cache_path = media_cache_path;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->media_cache_max_size = media_cache_max_size;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->extensions_cookie_path = extensions_cookie_path;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->infinite_cache_path = infinite_cache_path;
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  lazy_params->session_cookie_mode = session_cookie_mode;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  lazy_params->special_storage_policy = special_storage_policy;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->lazy_params_.reset(lazy_params);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keep track of profile path and cache sizes separately so we can use them
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // on demand when creating storage isolated URLRequestContextGetters.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->profile_path_ = profile_path;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->app_cache_max_size_ = cache_max_size;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->app_media_cache_max_size_ = media_cache_max_size;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->predictor_.reset(predictor);
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->InitializeMetricsEnabledStateOnUIThread();
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (io_data_->domain_reliability_monitor_)
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    io_data_->domain_reliability_monitor_->MoveToNetworkThread();
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  io_data_->set_data_reduction_proxy_unavailable_callback(
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_unavailable);
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  io_data_->set_data_reduction_proxy_chrome_configurator(
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_chrome_configurator.Pass());
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  io_data_->set_data_reduction_proxy_params(data_reduction_proxy_params.Pass());
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  io_data_->set_data_reduction_proxy_statistics_prefs(
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_statistics_prefs.Pass());
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)content::ResourceContext*
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProfileImplIOData::Handle::GetResourceContext() const {
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetResourceContextNoInit();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)content::ResourceContext*
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::GetResourceContextNoInit() const {
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Don't call LazyInitialize here, since the resource context is created at
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the beginning of initalization and is used by some members while they're
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being initialized (i.e. AppCacheService).
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return io_data_->GetResourceContext();
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<ChromeURLRequestContextGetter>
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ProfileImplIOData::Handle::CreateMainRequestContextGetter(
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    content::ProtocolHandlerMap* protocol_handlers,
202cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::URLRequestInterceptorScopedVector request_interceptors,
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrefService* local_state,
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    IOThread* io_thread) const {
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(!main_request_context_getter_.get());
2084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      profile_, io_data_, protocol_handlers, request_interceptors.Pass());
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  io_data_->predictor_
212868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ->InitNetworkPredictor(profile_->GetPrefs(),
213868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                             local_state,
214868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                             io_thread,
2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             main_request_context_getter_.get(),
2165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             io_data_);
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NotificationService::current()->Notify(
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::Source<Profile>(profile_),
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::NotificationService::NoDetails());
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return main_request_context_getter_;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<ChromeURLRequestContextGetter>
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!media_request_context_getter_.get()) {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    media_request_context_getter_ =
2314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return media_request_context_getter_;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<ChromeURLRequestContextGetter>
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!extensions_request_context_getter_.get()) {
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    extensions_request_context_getter_ =
2424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return extensions_request_context_getter_;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<ChromeURLRequestContextGetter>
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& partition_path,
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool in_memory,
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ProtocolHandlerMap* protocol_handlers,
252cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::URLRequestInterceptorScopedVector request_interceptors) const {
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check that the partition_path is not the same as the base profile path. We
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // expect isolated partition, which will never go to the default profile path.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(partition_path != profile_->GetPath());
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keep a map of request context getters, one per requested storage partition.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StoragePartitionDescriptor descriptor(partition_path, in_memory);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChromeURLRequestContextGetterMap::iterator iter =
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      app_request_context_getter_map_.find(descriptor);
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (iter != app_request_context_getter_map_.end())
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return iter->second;
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      protocol_handler_interceptor(
2685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              CreateJobInterceptorFactory());
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChromeURLRequestContextGetter* context =
2714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ChromeURLRequestContextGetter::CreateForIsolatedApp(
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          profile_,
273a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          io_data_,
274a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          descriptor,
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          protocol_handler_interceptor.Pass(),
276a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          protocol_handlers,
277cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          request_interceptors.Pass());
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  app_request_context_getter_map_[descriptor] = context;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return context;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)scoped_refptr<ChromeURLRequestContextGetter>
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const base::FilePath& partition_path,
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool in_memory) const {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We must have a non-default path, or this will act like the default media
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // context.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(partition_path != profile_->GetPath());
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Keep a map of request context getters, one per requested storage partition.
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StoragePartitionDescriptor descriptor(partition_path, in_memory);
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChromeURLRequestContextGetterMap::iterator iter =
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      isolated_media_request_context_getter_map_.find(descriptor);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (iter != isolated_media_request_context_getter_map_.end())
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return iter->second;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the app context as the starting point for the media context, so that
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it uses the app's cookie store.
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ChromeURLRequestContextGetterMap::const_iterator app_iter =
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      app_request_context_getter_map_.find(descriptor);
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(app_iter != app_request_context_getter_map_.end());
305868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ChromeURLRequestContextGetter* app_context = app_iter->second.get();
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ChromeURLRequestContextGetter* context =
3074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ChromeURLRequestContextGetter::CreateForIsolatedMedia(
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          profile_, app_context, io_data_, descriptor);
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  isolated_media_request_context_getter_map_[descriptor] = context;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return context;
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
31446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)DevToolsNetworkController*
31546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
31646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return io_data_->network_controller();
31846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
31946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Time time,
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Closure& completion) {
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LazyInitialize();
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BrowserThread::PostTask(
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::IO, FROM_HERE,
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::Unretained(io_data_),
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          time,
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          completion));
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileImplIOData::Handle::LazyInitialize() const {
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (initialized_)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set initialized_ to true at the beginning in case any of the objects
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // below try to get the ResourceContext pointer.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  initialized_ = true;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PrefService* pref_service = profile_->GetPrefs();
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  io_data_->http_server_properties_manager_ =
344116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
345116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          pref_service);
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  io_data_->set_http_server_properties(
347ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      scoped_ptr<net::HttpServerProperties>(
348ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch          io_data_->http_server_properties_manager_));
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->session_startup_pref()->Init(
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kRestoreOnStartup, pref_service);
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->session_startup_pref()->MoveToThread(
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pref_service);
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->safe_browsing_enabled()->MoveToThread(
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
359f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  io_data_->data_reduction_proxy_enabled()->Init(
360f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      data_reduction_proxy::prefs::kDataReductionProxyEnabled, pref_service);
361f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  io_data_->data_reduction_proxy_enabled()->MoveToThread(
362f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  io_data_->InitializeOnUIThread(profile_);
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector>
36703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)ProfileImplIOData::Handle::GetAllContextGetters() {
36803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  ChromeURLRequestContextGetterMap::iterator iter;
36903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_ptr<ChromeURLRequestContextGetterVector> context_getters(
37003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      new ChromeURLRequestContextGetterVector());
37103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
37203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  iter = isolated_media_request_context_getter_map_.begin();
37303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
37403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    context_getters->push_back(iter->second);
37503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
37603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  iter = app_request_context_getter_map_.begin();
37703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  for (; iter != app_request_context_getter_map_.end(); ++iter)
37803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    context_getters->push_back(iter->second);
37903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (extensions_request_context_getter_.get())
38103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    context_getters->push_back(extensions_request_context_getter_);
38203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (media_request_context_getter_.get())
38403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    context_getters->push_back(media_request_context_getter_);
38503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (main_request_context_getter_.get())
38703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    context_getters->push_back(main_request_context_getter_);
38803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
38903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  return context_getters.Pass();
39003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
39103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::LazyParams::LazyParams()
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : cache_max_size(0),
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      media_cache_max_size(0),
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      session_cookie_mode(
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::LazyParams::~LazyParams() {}
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::ProfileImplIOData()
401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : ProfileIOData(Profile::REGULAR_PROFILE),
40258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      http_server_properties_manager_(NULL),
40358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      app_cache_max_size_(0),
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      app_media_cache_max_size_(0) {
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::~ProfileImplIOData() {
408116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (initialized())
409116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    network_delegate()->set_domain_reliability_monitor(NULL);
410116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DestroyResourceContext();
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
413b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (media_request_context_)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    media_request_context_->AssertNoURLRequests();
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileImplIOData::InitializeInternal(
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProfileParams* profile_params,
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ProtocolHandlerMap* protocol_handlers,
420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::URLRequestInterceptorScopedVector request_interceptors) const {
4215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  net::URLRequestContext* main_context = main_request_context();
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IOThread* const io_thread = profile_params->io_thread;
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IOThread::Globals* const io_thread_globals = io_thread->globals();
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  set_data_reduction_proxy_auth_request_handler(
4271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr<data_reduction_proxy::DataReductionProxyAuthRequestHandler>
4281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          (new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
4291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              DataReductionProxyChromeSettings::GetClient(),
4301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              data_reduction_proxy_params(),
4311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))));
4321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  set_data_reduction_proxy_usage_stats(
4331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr<data_reduction_proxy::DataReductionProxyUsageStats>
4341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          (new data_reduction_proxy::DataReductionProxyUsageStats(
4351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              data_reduction_proxy_params(),
4361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci              BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)
4371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                  .get())));
4381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  data_reduction_proxy_usage_stats()->set_unavailable_callback(
4391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_unavailable_callback());
4406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
4416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  network_delegate()->set_data_reduction_proxy_params(
4421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_params());
4436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  network_delegate()->set_data_reduction_proxy_usage_stats(
4441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_usage_stats());
4456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  network_delegate()->set_data_reduction_proxy_auth_request_handler(
4461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_auth_request_handler());
4471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  network_delegate()->set_data_reduction_proxy_statistics_prefs(
4481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      data_reduction_proxy_statistics_prefs());
4496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  network_delegate()->set_on_resolve_proxy_handler(
4506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::Bind(data_reduction_proxy::OnResolveProxyHandler));
4516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  network_delegate()->set_proxy_config_getter(
4526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::Bind(
4536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          &DataReductionProxyChromeConfigurator::GetProxyConfigOnIO,
4541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Unretained(data_reduction_proxy_chrome_configurator())));
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  network_delegate()->set_predictor(predictor_.get());
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialize context members.
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ApplyProfileParamsToContext(main_context);
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (http_server_properties_manager_)
462116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    http_server_properties_manager_->InitializeOnNetworkThread();
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_transport_security_state(transport_security_state());
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_net_log(io_thread->net_log());
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_network_delegate(network_delegate());
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  main_context->set_http_server_properties(http_server_properties());
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_host_resolver(
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      io_thread_globals->host_resolver.get());
474a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  main_context->set_cert_transparency_verifier(
475a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      io_thread_globals->cert_transparency_verifier.get());
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_http_auth_handler_factory(
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      io_thread_globals->http_auth_handler_factory.get());
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_fraudulent_certificate_reporter(
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fraudulent_certificate_reporter());
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_throttler_manager(
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      io_thread_globals->throttler_manager.get());
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_context->set_proxy_service(proxy_service());
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<net::CookieStore> cookie_store = NULL;
4885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  net::ChannelIDService* channel_id_service = NULL;
4895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Don't use existing cookies and use an in-memory store.
4915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    using content::CookieStoreConfig;
4925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_store = content::CreateCookieStore(CookieStoreConfig(
4935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::FilePath(),
4945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
4955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        NULL,
4965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        profile_params->cookie_monster_delegate.get()));
4975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Don't use existing channel ids and use an in-memory store.
4985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    channel_id_service = new net::ChannelIDService(
4995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        new net::DefaultChannelIDStore(NULL),
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::WorkerPool::GetTaskRunner(true));
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // setup cookie store
505868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!cookie_store.get()) {
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!lazy_params_->cookie_path.empty());
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::CookieStoreConfig cookie_config(
509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        lazy_params_->cookie_path,
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        lazy_params_->session_cookie_mode,
511868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        lazy_params_->special_storage_policy.get(),
5120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)        profile_params->cookie_monster_delegate.get());
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_config.crypto_delegate =
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      chrome_browser_net::GetCookieCryptoDelegate();
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_store = content::CreateCookieStore(cookie_config);
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
518868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  main_context->set_cookie_store(cookie_store.get());
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Setup server bound cert service.
5215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!channel_id_service) {
5225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DCHECK(!lazy_params_->channel_id_path.empty());
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    scoped_refptr<QuotaPolicyChannelIDStore> channel_id_db =
5251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        new QuotaPolicyChannelIDStore(
5265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            lazy_params_->channel_id_path,
5274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
5284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                BrowserThread::GetBlockingPool()->GetSequenceToken()),
529868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            lazy_params_->special_storage_policy.get());
5305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    channel_id_service = new net::ChannelIDService(
5315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        new net::DefaultChannelIDStore(channel_id_db.get()),
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        base::WorkerPool::GetTaskRunner(true));
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  set_channel_id_service(channel_id_service);
5365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  main_context->set_channel_id_service(channel_id_service);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpCache::DefaultBackend* main_backend =
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new net::HttpCache::DefaultBackend(
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          net::DISK_CACHE,
541a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          ChooseCacheBackendType(),
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          lazy_params_->cache_path,
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          lazy_params_->cache_max_size,
54403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
54546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<net::HttpCache> main_cache = CreateMainHttpFactory(
54646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      profile_params, main_backend);
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    main_cache->set_mode(
5515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        chrome_browser_net::IsCookieRecordMode() ?
5525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  main_http_factory_.reset(main_cache.release());
55646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  main_context->set_http_transaction_factory(main_http_factory_.get());
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(DISABLE_FTP_SUPPORT)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ftp_factory_.reset(
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // !defined(DISABLE_FTP_SUPPORT)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new net::URLRequestJobFactoryImpl());
5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  main_job_factory_ = SetUpJobFactoryDefaults(
5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      main_job_factory.Pass(),
568cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      request_interceptors.Pass(),
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      profile_params->protocol_handler_interceptor.Pass(),
5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      network_delegate(),
571b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ftp_factory_.get());
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  main_context->set_job_factory(main_job_factory_.get());
5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InitializeExtensionsRequestContext(profile_params);
5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif
5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
578f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Setup the SDCHManager for this profile.
579f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  sdch_manager_.reset(new net::SdchManager);
58003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  sdch_manager_->set_sdch_fetcher(scoped_ptr<net::SdchFetcher>(
5811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new net::SdchDictionaryFetcher(sdch_manager_.get(),
5821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                     main_context)).Pass());
583f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  main_context->set_sdch_manager(sdch_manager_.get());
584f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Create a media request context based on the main context, but using a
5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // media cache.  It shares the same job factory as the main context.
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  StoragePartitionDescriptor details(profile_path_, false);
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  media_request_context_.reset(InitializeMediaRequestContext(main_context,
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                             details));
5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
591116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (domain_reliability_monitor_) {
5921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    domain_reliability::DomainReliabilityMonitor* monitor =
5931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        domain_reliability_monitor_.get();
5941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    monitor->InitURLRequestContext(main_context);
5951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    monitor->AddBakedInConfigs();
596ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch    monitor->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
5971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    network_delegate()->set_domain_reliability_monitor(monitor);
59823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
59923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  lazy_params_.reset();
6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ProfileImplIOData::
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
6055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  net::URLRequestContext* extensions_context = extensions_request_context();
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IOThread* const io_thread = profile_params->io_thread;
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  IOThread::Globals* const io_thread_globals = io_thread->globals();
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ApplyProfileParamsToContext(extensions_context);
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extensions_context->set_transport_security_state(transport_security_state());
6112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extensions_context->set_net_log(io_thread->net_log());
6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extensions_context->set_throttler_manager(
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      io_thread_globals->throttler_manager.get());
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::CookieStoreConfig cookie_config(
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      lazy_params_->extensions_cookie_path,
6195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      lazy_params_->session_cookie_mode,
6205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      NULL, NULL);
6215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cookie_config.crypto_delegate =
6225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      chrome_browser_net::GetCookieCryptoDelegate();
623c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  net::CookieStore* extensions_cookie_store =
6245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::CreateCookieStore(cookie_config);
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Enable cookies for devtools and extension URLs.
626116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const char* const schemes[] = {
627116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      content::kChromeDevToolsScheme,
628116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extensions::kExtensionScheme
629116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
630116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
631116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      schemes, arraysize(schemes));
6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extensions_context->set_cookie_store(extensions_cookie_store);
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new net::URLRequestJobFactoryImpl());
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Without a network_delegate, this protocol handler will never
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handle file: requests, but as a side effect it makes
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // job_factory::IsHandledProtocol return true, which prevents attempts to
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handle the protocol externally. We pass NULL in to
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SetUpJobFactory() to get this effect.
6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  extensions_job_factory_ = SetUpJobFactoryDefaults(
6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      extensions_job_factory.Pass(),
644cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      content::URLRequestInterceptorScopedVector(),
6457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NULL,
647b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ftp_factory_.get());
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  extensions_context->set_job_factory(extensions_job_factory_.get());
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
6525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    net::URLRequestContext* main_context,
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const StoragePartitionDescriptor& partition_descriptor,
6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        protocol_handler_interceptor,
656a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ProtocolHandlerMap* protocol_handlers,
657cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::URLRequestInterceptorScopedVector request_interceptors) const {
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copy most state from the main context.
6595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AppRequestContext* context = new AppRequestContext();
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  context->CopyFrom(main_context);
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath cookie_path = partition_descriptor.path.Append(
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      chrome::kCookieFilename);
6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath cache_path =
6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      partition_descriptor.path.Append(chrome::kCacheDirname);
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use a separate HTTP disk cache for isolated apps.
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpCache::BackendFactory* app_backend = NULL;
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (partition_descriptor.in_memory) {
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    app_backend = net::HttpCache::DefaultBackend::InMemory(0);
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    app_backend = new net::HttpCache::DefaultBackend(
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net::DISK_CACHE,
674a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        ChooseCacheBackendType(),
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        cache_path,
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        app_cache_max_size_,
67703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpNetworkSession* main_network_session =
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      main_http_factory_->GetSession();
68146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<net::HttpCache> app_http_cache =
68246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      CreateHttpFactory(main_network_session, app_backend);
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<net::CookieStore> cookie_store = NULL;
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (partition_descriptor.in_memory) {
6865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
6875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Don't use existing cookies and use an in-memory store.
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(creis): We should have a cookie delegate for notifying the cookie
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // extensions API, but we need to update it to understand isolated apps
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // first.
6925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    app_http_cache->set_mode(
6945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        chrome_browser_net::IsCookieRecordMode() ?
6955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use an app-specific cookie store.
699868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!cookie_store.get()) {
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!cookie_path.empty());
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(creis): We should have a cookie delegate for notifying the cookie
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // extensions API, but we need to update it to understand isolated apps
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // first.
7055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    content::CookieStoreConfig cookie_config(
706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        cookie_path,
7075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
7085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        NULL, NULL);
7095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_config.crypto_delegate =
7105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      chrome_browser_net::GetCookieCryptoDelegate();
7115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    cookie_store = content::CreateCookieStore(cookie_config);
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Transfer ownership of the cookies and cache to AppRequestContext.
715868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  context->SetCookieStore(cookie_store.get());
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  context->SetHttpTransactionFactory(
71746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      scoped_ptr<net::HttpTransactionFactory>(
71846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          app_http_cache.PassAs<net::HttpTransactionFactory>()));
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      new net::URLRequestJobFactoryImpl());
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InstallProtocolHandlers(job_factory.get(), protocol_handlers);
7234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  scoped_ptr<net::URLRequestJobFactory> top_job_factory(
724a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      SetUpJobFactoryDefaults(job_factory.Pass(),
725cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                              request_interceptors.Pass(),
726a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              protocol_handler_interceptor.Pass(),
727a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              network_delegate(),
728a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              ftp_factory_.get()));
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  context->SetJobFactory(top_job_factory.Pass());
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return context;
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)net::URLRequestContext*
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::InitializeMediaRequestContext(
7365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    net::URLRequestContext* original_context,
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const StoragePartitionDescriptor& partition_descriptor) const {
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Copy most state from the original context.
7395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  MediaRequestContext* context = new MediaRequestContext();
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  context->CopyFrom(original_context);
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // For in-memory context, return immediately after creating the new
7433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // context before attaching a separate cache. It is important to return
7443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // a new context rather than just reusing |original_context| because
7453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // the caller expects to take ownership of the pointer.
7463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (partition_descriptor.in_memory)
7473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return context;
7483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  using content::StoragePartition;
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath cache_path;
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int cache_max_size = app_media_cache_max_size_;
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (partition_descriptor.path == profile_path_) {
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // lazy_params_ is only valid for the default media context creation.
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cache_path = lazy_params_->media_cache_path;
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cache_max_size = lazy_params_->media_cache_max_size;
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Use a separate HTTP disk cache for isolated apps.
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpCache::BackendFactory* media_backend =
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new net::HttpCache::DefaultBackend(
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          net::MEDIA_CACHE,
764a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          ChooseCacheBackendType(),
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cache_path,
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          cache_max_size,
76703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::HttpNetworkSession* main_network_session =
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      main_http_factory_->GetSession();
77046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<net::HttpCache> media_http_cache =
77146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      CreateHttpFactory(main_network_session, media_backend);
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Transfer ownership of the cache to MediaRequestContext.
77446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  context->SetHttpTransactionFactory(
77546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      media_http_cache.PassAs<net::HttpTransactionFactory>());
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note that we do not create a new URLRequestJobFactory because
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the media context should behave exactly like its parent context
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in all respects except for cache behavior on media subresources.
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The CopyFrom() step above means that our media context will use
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the same URLRequestJobFactory instance that our parent context does.
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return context;
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)net::URLRequestContext*
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::AcquireMediaRequestContext() const {
788b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  DCHECK(media_request_context_);
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return media_request_context_.get();
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
7935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    net::URLRequestContext* main_context,
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const StoragePartitionDescriptor& partition_descriptor,
7952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
7962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        protocol_handler_interceptor,
797a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::ProtocolHandlerMap* protocol_handlers,
798cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::URLRequestInterceptorScopedVector request_interceptors) const {
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We create per-app contexts on demand, unlike the others above.
8005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  net::URLRequestContext* app_request_context =
801a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      InitializeAppRequestContext(main_context,
802a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                  partition_descriptor,
8032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  protocol_handler_interceptor.Pass(),
804a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                  protocol_handlers,
805cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                  request_interceptors.Pass());
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(app_request_context);
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return app_request_context;
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)net::URLRequestContext*
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProfileImplIOData::AcquireIsolatedMediaRequestContext(
8125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    net::URLRequestContext* app_context,
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const StoragePartitionDescriptor& partition_descriptor) const {
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We create per-app media contexts on demand, unlike the others above.
8155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  net::URLRequestContext* media_request_context =
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InitializeMediaRequestContext(app_context, partition_descriptor);
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(media_request_context);
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return media_request_context;
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::Time time,
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::Closure& completion) {
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(initialized());
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(transport_security_state());
8282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Completes synchronously.
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  transport_security_state()->DeleteAllDynamicDataSince(time);
8302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(http_server_properties_manager_);
8312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  http_server_properties_manager_->Clear(completion);
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
833