1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile_impl_io_data.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/field_trial.h"
#include "base/prefs/pref_member.h"
#include "base/prefs/pref_service.h"
#include "base/profiler/scoped_tracker.h"
#include "base/sequenced_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/net/chrome_net_log.h"
#include "chrome/browser/net/chrome_network_delegate.h"
#include "chrome/browser/net/connect_interceptor.h"
#include "chrome/browser/net/cookie_store_util.h"
#include "chrome/browser/net/http_server_properties_manager_factory.h"
#include "chrome/browser/net/predictor.h"
#include "chrome/browser/net/quota_policy_channel_id_store.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_auth_request_handler.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_configurator.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_usage_stats.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/domain_reliability/monitor.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cookie_store_factory.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/extension_protocols.h"
#include "extensions/common/constants.h"
#include "net/base/cache_type.h"
#include "net/base/sdch_manager.h"
#include "net/ftp/ftp_network_layer.h"
#include "net/http/http_cache.h"
#include "net/http/http_server_properties_manager.h"
#include "net/sdch/sdch_owner.h"
#include "net/ssl/channel_id_service.h"
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "storage/browser/quota/special_storage_policy.h"
namespace {
net::BackendType ChooseCacheBackendType() {
#if defined(OS_ANDROID)
return net::CACHE_BACKEND_SIMPLE;
#else
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kUseSimpleCacheBackend)) {
const std::string opt_value =
command_line.GetSwitchValueASCII(switches::kUseSimpleCacheBackend);
if (LowerCaseEqualsASCII(opt_value, "off"))
return net::CACHE_BACKEND_BLOCKFILE;
if (opt_value == "" || LowerCaseEqualsASCII(opt_value, "on"))
return net::CACHE_BACKEND_SIMPLE;
}
const std::string experiment_name =
base::FieldTrialList::FindFullName("SimpleCacheTrial");
if (experiment_name == "ExperimentYes" ||
experiment_name == "ExperimentYes2") {
return net::CACHE_BACKEND_SIMPLE;
}
return net::CACHE_BACKEND_BLOCKFILE;
#endif
}
} // namespace
using content::BrowserThread;
using data_reduction_proxy::DataReductionProxyParams;
ProfileImplIOData::Handle::Handle(Profile* profile)
: io_data_(new ProfileImplIOData),
profile_(profile),
initialized_(false) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(profile);
}
ProfileImplIOData::Handle::~Handle() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (io_data_->predictor_ != NULL) {
// io_data_->predictor_ might be NULL if Init() was never called
// (i.e. we shut down before ProfileImpl::DoFinalInit() got called).
bool save_prefs = true;
#if defined(OS_CHROMEOS)
save_prefs = !chromeos::ProfileHelper::IsSigninProfile(profile_);
#endif
if (save_prefs)
io_data_->predictor_->SaveStateForNextStartupAndTrim();
io_data_->predictor_->ShutdownOnUIThread();
}
if (io_data_->http_server_properties_manager_)
io_data_->http_server_properties_manager_->ShutdownOnPrefThread();
io_data_->data_reduction_proxy_enabled_.Destroy();
io_data_->ShutdownOnUIThread(GetAllContextGetters().Pass());
}
void ProfileImplIOData::Handle::Init(
const base::FilePath& cookie_path,
const base::FilePath& channel_id_path,
const base::FilePath& cache_path,
int cache_max_size,
const base::FilePath& media_cache_path,
int media_cache_max_size,
const base::FilePath& extensions_cookie_path,
const base::FilePath& profile_path,
const base::FilePath& infinite_cache_path,
chrome_browser_net::Predictor* predictor,
content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
storage::SpecialStoragePolicy* special_storage_policy,
scoped_ptr<domain_reliability::DomainReliabilityMonitor>
domain_reliability_monitor,
const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
scoped_ptr<data_reduction_proxy::DataReductionProxyConfigurator>
data_reduction_proxy_configurator,
scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
data_reduction_proxy_params,
base::WeakPtr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>
data_reduction_proxy_statistics_prefs,
scoped_ptr<data_reduction_proxy::DataReductionProxyEventStore>
data_reduction_proxy_event_store) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!io_data_->lazy_params_);
DCHECK(predictor);
LazyParams* lazy_params = new LazyParams();
lazy_params->cookie_path = cookie_path;
lazy_params->channel_id_path = channel_id_path;
lazy_params->cache_path = cache_path;
lazy_params->cache_max_size = cache_max_size;
lazy_params->media_cache_path = media_cache_path;
lazy_params->media_cache_max_size = media_cache_max_size;
lazy_params->extensions_cookie_path = extensions_cookie_path;
lazy_params->infinite_cache_path = infinite_cache_path;
lazy_params->session_cookie_mode = session_cookie_mode;
lazy_params->special_storage_policy = special_storage_policy;
io_data_->lazy_params_.reset(lazy_params);
// Keep track of profile path and cache sizes separately so we can use them
// on demand when creating storage isolated URLRequestContextGetters.
io_data_->profile_path_ = profile_path;
io_data_->app_cache_max_size_ = cache_max_size;
io_data_->app_media_cache_max_size_ = media_cache_max_size;
io_data_->predictor_.reset(predictor);
io_data_->domain_reliability_monitor_ = domain_reliability_monitor.Pass();
io_data_->InitializeMetricsEnabledStateOnUIThread();
if (io_data_->domain_reliability_monitor_)
io_data_->domain_reliability_monitor_->MoveToNetworkThread();
io_data_->set_data_reduction_proxy_unavailable_callback(
data_reduction_proxy_unavailable);
io_data_->set_data_reduction_proxy_configurator(
data_reduction_proxy_configurator.Pass());
io_data_->set_data_reduction_proxy_params(data_reduction_proxy_params.Pass());
io_data_->set_data_reduction_proxy_statistics_prefs(
data_reduction_proxy_statistics_prefs);
io_data_->set_data_reduction_proxy_event_store(
data_reduction_proxy_event_store.Pass());
}
content::ResourceContext*
ProfileImplIOData::Handle::GetResourceContext() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LazyInitialize();
return GetResourceContextNoInit();
}
content::ResourceContext*
ProfileImplIOData::Handle::GetResourceContextNoInit() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Don't call LazyInitialize here, since the resource context is created at
// the beginning of initalization and is used by some members while they're
// being initialized (i.e. AppCacheService).
return io_data_->GetResourceContext();
}
scoped_refptr<ChromeURLRequestContextGetter>
ProfileImplIOData::Handle::CreateMainRequestContextGetter(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors,
PrefService* local_state,
IOThread* io_thread) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LazyInitialize();
DCHECK(!main_request_context_getter_.get());
main_request_context_getter_ = ChromeURLRequestContextGetter::Create(
profile_, io_data_, protocol_handlers, request_interceptors.Pass());
io_data_->predictor_
->InitNetworkPredictor(profile_->GetPrefs(),
local_state,
io_thread,
main_request_context_getter_.get(),
io_data_);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_PROFILE_URL_REQUEST_CONTEXT_GETTER_INITIALIZED,
content::Source<Profile>(profile_),
content::NotificationService::NoDetails());
return main_request_context_getter_;
}
scoped_refptr<ChromeURLRequestContextGetter>
ProfileImplIOData::Handle::GetMediaRequestContextGetter() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LazyInitialize();
if (!media_request_context_getter_.get()) {
media_request_context_getter_ =
ChromeURLRequestContextGetter::CreateForMedia(profile_, io_data_);
}
return media_request_context_getter_;
}
scoped_refptr<ChromeURLRequestContextGetter>
ProfileImplIOData::Handle::GetExtensionsRequestContextGetter() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LazyInitialize();
if (!extensions_request_context_getter_.get()) {
extensions_request_context_getter_ =
ChromeURLRequestContextGetter::CreateForExtensions(profile_, io_data_);
}
return extensions_request_context_getter_;
}
scoped_refptr<ChromeURLRequestContextGetter>
ProfileImplIOData::Handle::CreateIsolatedAppRequestContextGetter(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// Check that the partition_path is not the same as the base profile path. We
// expect isolated partition, which will never go to the default profile path.
CHECK(partition_path != profile_->GetPath());
LazyInitialize();
// Keep a map of request context getters, one per requested storage partition.
StoragePartitionDescriptor descriptor(partition_path, in_memory);
ChromeURLRequestContextGetterMap::iterator iter =
app_request_context_getter_map_.find(descriptor);
if (iter != app_request_context_getter_map_.end())
return iter->second;
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
protocol_handler_interceptor(
ProtocolHandlerRegistryFactory::GetForBrowserContext(profile_)->
CreateJobInterceptorFactory());
ChromeURLRequestContextGetter* context =
ChromeURLRequestContextGetter::CreateForIsolatedApp(
profile_,
io_data_,
descriptor,
protocol_handler_interceptor.Pass(),
protocol_handlers,
request_interceptors.Pass());
app_request_context_getter_map_[descriptor] = context;
return context;
}
scoped_refptr<ChromeURLRequestContextGetter>
ProfileImplIOData::Handle::GetIsolatedMediaRequestContextGetter(
const base::FilePath& partition_path,
bool in_memory) const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// We must have a non-default path, or this will act like the default media
// context.
CHECK(partition_path != profile_->GetPath());
LazyInitialize();
// Keep a map of request context getters, one per requested storage partition.
StoragePartitionDescriptor descriptor(partition_path, in_memory);
ChromeURLRequestContextGetterMap::iterator iter =
isolated_media_request_context_getter_map_.find(descriptor);
if (iter != isolated_media_request_context_getter_map_.end())
return iter->second;
// Get the app context as the starting point for the media context, so that
// it uses the app's cookie store.
ChromeURLRequestContextGetterMap::const_iterator app_iter =
app_request_context_getter_map_.find(descriptor);
DCHECK(app_iter != app_request_context_getter_map_.end());
ChromeURLRequestContextGetter* app_context = app_iter->second.get();
ChromeURLRequestContextGetter* context =
ChromeURLRequestContextGetter::CreateForIsolatedMedia(
profile_, app_context, io_data_, descriptor);
isolated_media_request_context_getter_map_[descriptor] = context;
return context;
}
DevToolsNetworkController*
ProfileImplIOData::Handle::GetDevToolsNetworkController() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return io_data_->network_controller();
}
void ProfileImplIOData::Handle::ClearNetworkingHistorySince(
base::Time time,
const base::Closure& completion) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
LazyInitialize();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(
&ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread,
base::Unretained(io_data_),
time,
completion));
}
void ProfileImplIOData::Handle::LazyInitialize() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (initialized_)
return;
// Set initialized_ to true at the beginning in case any of the objects
// below try to get the ResourceContext pointer.
initialized_ = true;
PrefService* pref_service = profile_->GetPrefs();
io_data_->http_server_properties_manager_ =
chrome_browser_net::HttpServerPropertiesManagerFactory::CreateManager(
pref_service);
io_data_->set_http_server_properties(
scoped_ptr<net::HttpServerProperties>(
io_data_->http_server_properties_manager_));
io_data_->session_startup_pref()->Init(
prefs::kRestoreOnStartup, pref_service);
io_data_->session_startup_pref()->MoveToThread(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
#if defined(FULL_SAFE_BROWSING) || defined(MOBILE_SAFE_BROWSING)
io_data_->safe_browsing_enabled()->Init(prefs::kSafeBrowsingEnabled,
pref_service);
io_data_->safe_browsing_enabled()->MoveToThread(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
#endif
io_data_->data_reduction_proxy_enabled_.Init(
data_reduction_proxy::prefs::kDataReductionProxyEnabled, pref_service);
io_data_->data_reduction_proxy_enabled_.MoveToThread(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
io_data_->InitializeOnUIThread(profile_);
}
scoped_ptr<ProfileIOData::ChromeURLRequestContextGetterVector>
ProfileImplIOData::Handle::GetAllContextGetters() {
ChromeURLRequestContextGetterMap::iterator iter;
scoped_ptr<ChromeURLRequestContextGetterVector> context_getters(
new ChromeURLRequestContextGetterVector());
iter = isolated_media_request_context_getter_map_.begin();
for (; iter != isolated_media_request_context_getter_map_.end(); ++iter)
context_getters->push_back(iter->second);
iter = app_request_context_getter_map_.begin();
for (; iter != app_request_context_getter_map_.end(); ++iter)
context_getters->push_back(iter->second);
if (extensions_request_context_getter_.get())
context_getters->push_back(extensions_request_context_getter_);
if (media_request_context_getter_.get())
context_getters->push_back(media_request_context_getter_);
if (main_request_context_getter_.get())
context_getters->push_back(main_request_context_getter_);
return context_getters.Pass();
}
ProfileImplIOData::LazyParams::LazyParams()
: cache_max_size(0),
media_cache_max_size(0),
session_cookie_mode(
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES) {}
ProfileImplIOData::LazyParams::~LazyParams() {}
ProfileImplIOData::ProfileImplIOData()
: ProfileIOData(Profile::REGULAR_PROFILE),
http_server_properties_manager_(NULL),
app_cache_max_size_(0),
app_media_cache_max_size_(0) {
}
ProfileImplIOData::~ProfileImplIOData() {
DestroyResourceContext();
if (media_request_context_)
media_request_context_->AssertNoURLRequests();
}
void ProfileImplIOData::InitializeInternal(
scoped_ptr<ChromeNetworkDelegate> chrome_network_delegate,
ProfileParams* profile_params,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const {
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal"));
net::URLRequestContext* main_context = main_request_context();
IOThread* const io_thread = profile_params->io_thread;
IOThread::Globals* const io_thread_globals = io_thread->globals();
chrome_network_delegate->set_predictor(predictor_.get());
if (domain_reliability_monitor_) {
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile1(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal1"));
domain_reliability::DomainReliabilityMonitor* monitor =
domain_reliability_monitor_.get();
monitor->InitURLRequestContext(main_context);
monitor->AddBakedInConfigs();
monitor->SetDiscardUploads(!GetMetricsEnabledStateOnIOThread());
chrome_network_delegate->set_domain_reliability_monitor(monitor);
}
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile2(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal2"));
set_data_reduction_proxy_auth_request_handler(
scoped_ptr<data_reduction_proxy::DataReductionProxyAuthRequestHandler>
(new data_reduction_proxy::DataReductionProxyAuthRequestHandler(
DataReductionProxyChromeSettings::GetClient(),
data_reduction_proxy_params(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))));
set_data_reduction_proxy_usage_stats(
scoped_ptr<data_reduction_proxy::DataReductionProxyUsageStats>
(new data_reduction_proxy::DataReductionProxyUsageStats(
data_reduction_proxy_params(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)
.get())));
data_reduction_proxy_usage_stats()->set_unavailable_callback(
data_reduction_proxy_unavailable_callback());
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile3(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal3"));
scoped_ptr<data_reduction_proxy::DataReductionProxyNetworkDelegate>
data_reduction_proxy_network_delegate(
new data_reduction_proxy::DataReductionProxyNetworkDelegate(
chrome_network_delegate.Pass(),
data_reduction_proxy_params(),
data_reduction_proxy_auth_request_handler(),
base::Bind(
&data_reduction_proxy::DataReductionProxyConfigurator::
GetProxyConfigOnIOThread,
base::Unretained(data_reduction_proxy_configurator()))));
data_reduction_proxy_network_delegate->InitProxyConfigOverrider(
base::Bind(data_reduction_proxy::OnResolveProxyHandler));
data_reduction_proxy_network_delegate->InitStatisticsPrefsAndUMA(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
data_reduction_proxy_statistics_prefs(),
&data_reduction_proxy_enabled_,
data_reduction_proxy_usage_stats());
network_delegate_ = data_reduction_proxy_network_delegate.Pass();
// Initialize context members.
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile4(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal4"));
ApplyProfileParamsToContext(main_context);
if (http_server_properties_manager_)
http_server_properties_manager_->InitializeOnNetworkThread();
main_context->set_transport_security_state(transport_security_state());
main_context->set_net_log(io_thread->net_log());
main_context->set_network_delegate(network_delegate_.get());
main_context->set_http_server_properties(http_server_properties());
main_context->set_host_resolver(
io_thread_globals->host_resolver.get());
main_context->set_cert_transparency_verifier(
io_thread_globals->cert_transparency_verifier.get());
main_context->set_http_auth_handler_factory(
io_thread_globals->http_auth_handler_factory.get());
main_context->set_fraudulent_certificate_reporter(
fraudulent_certificate_reporter());
main_context->set_throttler_manager(
io_thread_globals->throttler_manager.get());
main_context->set_proxy_service(proxy_service());
scoped_refptr<net::CookieStore> cookie_store = NULL;
net::ChannelIDService* channel_id_service = NULL;
if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
// Don't use existing cookies and use an in-memory store.
using content::CookieStoreConfig;
cookie_store = content::CreateCookieStore(CookieStoreConfig(
base::FilePath(),
CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
NULL,
profile_params->cookie_monster_delegate.get()));
// Don't use existing channel ids and use an in-memory store.
channel_id_service = new net::ChannelIDService(
new net::DefaultChannelIDStore(NULL),
base::WorkerPool::GetTaskRunner(true));
}
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile5(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal5"));
// setup cookie store
if (!cookie_store.get()) {
DCHECK(!lazy_params_->cookie_path.empty());
content::CookieStoreConfig cookie_config(
lazy_params_->cookie_path,
lazy_params_->session_cookie_mode,
lazy_params_->special_storage_policy.get(),
profile_params->cookie_monster_delegate.get());
cookie_config.crypto_delegate =
chrome_browser_net::GetCookieCryptoDelegate();
cookie_store = content::CreateCookieStore(cookie_config);
}
main_context->set_cookie_store(cookie_store.get());
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile6(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal6"));
// Setup server bound cert service.
if (!channel_id_service) {
DCHECK(!lazy_params_->channel_id_path.empty());
scoped_refptr<QuotaPolicyChannelIDStore> channel_id_db =
new QuotaPolicyChannelIDStore(
lazy_params_->channel_id_path,
BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
BrowserThread::GetBlockingPool()->GetSequenceToken()),
lazy_params_->special_storage_policy.get());
channel_id_service = new net::ChannelIDService(
new net::DefaultChannelIDStore(channel_id_db.get()),
base::WorkerPool::GetTaskRunner(true));
}
set_channel_id_service(channel_id_service);
main_context->set_channel_id_service(channel_id_service);
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile7(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal7"));
net::HttpCache::DefaultBackend* main_backend =
new net::HttpCache::DefaultBackend(
net::DISK_CACHE,
ChooseCacheBackendType(),
lazy_params_->cache_path,
lazy_params_->cache_max_size,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
scoped_ptr<net::HttpCache> main_cache = CreateMainHttpFactory(
profile_params, main_backend);
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile71(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal71"));
main_cache->InitializeInfiniteCache(lazy_params_->infinite_cache_path);
if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
main_cache->set_mode(
chrome_browser_net::IsCookieRecordMode() ?
net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
}
main_http_factory_.reset(main_cache.release());
main_context->set_http_transaction_factory(main_http_factory_.get());
#if !defined(DISABLE_FTP_SUPPORT)
ftp_factory_.reset(
new net::FtpNetworkLayer(io_thread_globals->host_resolver.get()));
#endif // !defined(DISABLE_FTP_SUPPORT)
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile8(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal8"));
scoped_ptr<net::URLRequestJobFactoryImpl> main_job_factory(
new net::URLRequestJobFactoryImpl());
InstallProtocolHandlers(main_job_factory.get(), protocol_handlers);
// The data reduction proxy interceptor should be as close to the network
// as possible.
request_interceptors.insert(
request_interceptors.begin(),
new data_reduction_proxy::DataReductionProxyInterceptor(
data_reduction_proxy_params(),
data_reduction_proxy_usage_stats(),
data_reduction_proxy_event_store()));
main_job_factory_ = SetUpJobFactoryDefaults(
main_job_factory.Pass(),
request_interceptors.Pass(),
profile_params->protocol_handler_interceptor.Pass(),
main_context->network_delegate(),
ftp_factory_.get());
main_context->set_job_factory(main_job_factory_.get());
// TODO(vadimt): Remove ScopedTracker below once crbug.com/436671 is fixed.
tracked_objects::ScopedTracker tracking_profile9(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"436671 ProfileImplIOData::InitializeInternal9"));
#if defined(ENABLE_EXTENSIONS)
InitializeExtensionsRequestContext(profile_params);
#endif
// Setup SDCH for this profile.
sdch_manager_.reset(new net::SdchManager);
sdch_policy_.reset(new net::SdchOwner(sdch_manager_.get(), main_context));
main_context->set_sdch_manager(sdch_manager_.get());
// Create a media request context based on the main context, but using a
// media cache. It shares the same job factory as the main context.
StoragePartitionDescriptor details(profile_path_, false);
media_request_context_.reset(InitializeMediaRequestContext(main_context,
details));
lazy_params_.reset();
}
void ProfileImplIOData::
InitializeExtensionsRequestContext(ProfileParams* profile_params) const {
net::URLRequestContext* extensions_context = extensions_request_context();
IOThread* const io_thread = profile_params->io_thread;
IOThread::Globals* const io_thread_globals = io_thread->globals();
ApplyProfileParamsToContext(extensions_context);
extensions_context->set_transport_security_state(transport_security_state());
extensions_context->set_net_log(io_thread->net_log());
extensions_context->set_throttler_manager(
io_thread_globals->throttler_manager.get());
content::CookieStoreConfig cookie_config(
lazy_params_->extensions_cookie_path,
lazy_params_->session_cookie_mode,
NULL, NULL);
cookie_config.crypto_delegate =
chrome_browser_net::GetCookieCryptoDelegate();
net::CookieStore* extensions_cookie_store =
content::CreateCookieStore(cookie_config);
// Enable cookies for devtools and extension URLs.
const char* const schemes[] = {
content::kChromeDevToolsScheme,
extensions::kExtensionScheme
};
extensions_cookie_store->GetCookieMonster()->SetCookieableSchemes(
schemes, arraysize(schemes));
extensions_context->set_cookie_store(extensions_cookie_store);
scoped_ptr<net::URLRequestJobFactoryImpl> extensions_job_factory(
new net::URLRequestJobFactoryImpl());
// TODO(shalev): The extensions_job_factory has a NULL NetworkDelegate.
// Without a network_delegate, this protocol handler will never
// handle file: requests, but as a side effect it makes
// job_factory::IsHandledProtocol return true, which prevents attempts to
// handle the protocol externally. We pass NULL in to
// SetUpJobFactory() to get this effect.
extensions_job_factory_ = SetUpJobFactoryDefaults(
extensions_job_factory.Pass(),
content::URLRequestInterceptorScopedVector(),
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>(),
NULL,
ftp_factory_.get());
extensions_context->set_job_factory(extensions_job_factory_.get());
}
net::URLRequestContext* ProfileImplIOData::InitializeAppRequestContext(
net::URLRequestContext* main_context,
const StoragePartitionDescriptor& partition_descriptor,
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
protocol_handler_interceptor,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const {
// Copy most state from the main context.
AppRequestContext* context = new AppRequestContext();
context->CopyFrom(main_context);
base::FilePath cookie_path = partition_descriptor.path.Append(
chrome::kCookieFilename);
base::FilePath cache_path =
partition_descriptor.path.Append(chrome::kCacheDirname);
// Use a separate HTTP disk cache for isolated apps.
net::HttpCache::BackendFactory* app_backend = NULL;
if (partition_descriptor.in_memory) {
app_backend = net::HttpCache::DefaultBackend::InMemory(0);
} else {
app_backend = new net::HttpCache::DefaultBackend(
net::DISK_CACHE,
ChooseCacheBackendType(),
cache_path,
app_cache_max_size_,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
}
net::HttpNetworkSession* main_network_session =
main_http_factory_->GetSession();
scoped_ptr<net::HttpCache> app_http_cache =
CreateHttpFactory(main_network_session, app_backend);
scoped_refptr<net::CookieStore> cookie_store = NULL;
if (partition_descriptor.in_memory) {
cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
} else if (chrome_browser_net::ShouldUseInMemoryCookiesAndCache()) {
// Don't use existing cookies and use an in-memory store.
// TODO(creis): We should have a cookie delegate for notifying the cookie
// extensions API, but we need to update it to understand isolated apps
// first.
cookie_store = content::CreateCookieStore(content::CookieStoreConfig());
app_http_cache->set_mode(
chrome_browser_net::IsCookieRecordMode() ?
net::HttpCache::RECORD : net::HttpCache::PLAYBACK);
}
// Use an app-specific cookie store.
if (!cookie_store.get()) {
DCHECK(!cookie_path.empty());
// TODO(creis): We should have a cookie delegate for notifying the cookie
// extensions API, but we need to update it to understand isolated apps
// first.
content::CookieStoreConfig cookie_config(
cookie_path,
content::CookieStoreConfig::EPHEMERAL_SESSION_COOKIES,
NULL, NULL);
cookie_config.crypto_delegate =
chrome_browser_net::GetCookieCryptoDelegate();
cookie_store = content::CreateCookieStore(cookie_config);
}
// Transfer ownership of the cookies and cache to AppRequestContext.
context->SetCookieStore(cookie_store.get());
context->SetHttpTransactionFactory(app_http_cache.Pass());
scoped_ptr<net::URLRequestJobFactoryImpl> job_factory(
new net::URLRequestJobFactoryImpl());
InstallProtocolHandlers(job_factory.get(), protocol_handlers);
// The data reduction proxy interceptor should be as close to the network
// as possible.
request_interceptors.insert(
request_interceptors.begin(),
new data_reduction_proxy::DataReductionProxyInterceptor(
data_reduction_proxy_params(),
data_reduction_proxy_usage_stats(),
data_reduction_proxy_event_store()));
scoped_ptr<net::URLRequestJobFactory> top_job_factory(
SetUpJobFactoryDefaults(job_factory.Pass(),
request_interceptors.Pass(),
protocol_handler_interceptor.Pass(),
main_context->network_delegate(),
ftp_factory_.get()));
context->SetJobFactory(top_job_factory.Pass());
return context;
}
net::URLRequestContext*
ProfileImplIOData::InitializeMediaRequestContext(
net::URLRequestContext* original_context,
const StoragePartitionDescriptor& partition_descriptor) const {
// Copy most state from the original context.
MediaRequestContext* context = new MediaRequestContext();
context->CopyFrom(original_context);
// For in-memory context, return immediately after creating the new
// context before attaching a separate cache. It is important to return
// a new context rather than just reusing |original_context| because
// the caller expects to take ownership of the pointer.
if (partition_descriptor.in_memory)
return context;
using content::StoragePartition;
base::FilePath cache_path;
int cache_max_size = app_media_cache_max_size_;
if (partition_descriptor.path == profile_path_) {
// lazy_params_ is only valid for the default media context creation.
cache_path = lazy_params_->media_cache_path;
cache_max_size = lazy_params_->media_cache_max_size;
} else {
cache_path = partition_descriptor.path.Append(chrome::kMediaCacheDirname);
}
// Use a separate HTTP disk cache for isolated apps.
net::HttpCache::BackendFactory* media_backend =
new net::HttpCache::DefaultBackend(
net::MEDIA_CACHE,
ChooseCacheBackendType(),
cache_path,
cache_max_size,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE));
net::HttpNetworkSession* main_network_session =
main_http_factory_->GetSession();
scoped_ptr<net::HttpCache> media_http_cache =
CreateHttpFactory(main_network_session, media_backend);
// Transfer ownership of the cache to MediaRequestContext.
context->SetHttpTransactionFactory(media_http_cache.Pass());
// Note that we do not create a new URLRequestJobFactory because
// the media context should behave exactly like its parent context
// in all respects except for cache behavior on media subresources.
// The CopyFrom() step above means that our media context will use
// the same URLRequestJobFactory instance that our parent context does.
return context;
}
net::URLRequestContext*
ProfileImplIOData::AcquireMediaRequestContext() const {
DCHECK(media_request_context_);
return media_request_context_.get();
}
net::URLRequestContext* ProfileImplIOData::AcquireIsolatedAppRequestContext(
net::URLRequestContext* main_context,
const StoragePartitionDescriptor& partition_descriptor,
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
protocol_handler_interceptor,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const {
// We create per-app contexts on demand, unlike the others above.
net::URLRequestContext* app_request_context =
InitializeAppRequestContext(main_context,
partition_descriptor,
protocol_handler_interceptor.Pass(),
protocol_handlers,
request_interceptors.Pass());
DCHECK(app_request_context);
return app_request_context;
}
net::URLRequestContext*
ProfileImplIOData::AcquireIsolatedMediaRequestContext(
net::URLRequestContext* app_context,
const StoragePartitionDescriptor& partition_descriptor) const {
// We create per-app media contexts on demand, unlike the others above.
net::URLRequestContext* media_request_context =
InitializeMediaRequestContext(app_context, partition_descriptor);
DCHECK(media_request_context);
return media_request_context;
}
void ProfileImplIOData::ClearNetworkingHistorySinceOnIOThread(
base::Time time,
const base::Closure& completion) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
DCHECK(initialized());
DCHECK(transport_security_state());
// Completes synchronously.
transport_security_state()->DeleteAllDynamicDataSince(time);
DCHECK(http_server_properties_manager_);
http_server_properties_manager_->Clear(completion);
}
bool ProfileImplIOData::IsDataReductionProxyEnabled() const {
return data_reduction_proxy_enabled_.GetValue() ||
base::CommandLine::ForCurrentProcess()->HasSwitch(
data_reduction_proxy::switches::kEnableDataReductionProxy);
}
|