Index: chrome/browser/net/net_pref_observer.cc |
diff --git a/chrome/browser/net/net_pref_observer.cc b/chrome/browser/net/net_pref_observer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3ae75408b183ff4ff69dae3356d7da429dd36e95 |
--- /dev/null |
+++ b/chrome/browser/net/net_pref_observer.cc |
@@ -0,0 +1,93 @@ |
+// 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/net/net_pref_observer.h" |
+ |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/logging.h" |
+#include "chrome/browser/browser_process.h" |
+#include "chrome/browser/io_thread.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/safe_browsing/safe_browsing_service.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "chrome/common/pref_names.h" |
+#include "components/pref_registry/pref_registry_syncable.h" |
+#include "components/prefs/pref_service.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+#include "net/http/http_network_session.h" |
+ |
+using content::BrowserThread; |
+ |
+NetPrefObserver::NetPrefObserver( |
+ PrefService* prefs, |
+ UpdateNetParamsCallback update_net_params_callback) |
+ : update_net_params_callback_(update_net_params_callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ DCHECK(prefs); |
+ |
+ base::Closure prefs_callback = |
+ base::Bind(&NetPrefObserver::ApplySettings, base::Unretained(this)); |
+ quic_allowed_.Init(prefs::kQuicAllowed, prefs, prefs_callback); |
+ |
+ ApplySettings(); |
+} |
+ |
+NetPrefObserver::~NetPrefObserver() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+} |
+ |
+void NetPrefObserver::ApplySettings() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ IOThread* io_thread = g_browser_process->io_thread(); |
+ if (!io_thread) |
+ return; |
+ |
+ bool quic_allowed_for_profile = true; |
+ if (quic_allowed_.IsManaged()) |
+ quic_allowed_for_profile = *quic_allowed_; |
+ |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&NetPrefObserver::ApplySettingsOnIOThread, |
+ base::Unretained(this), io_thread, |
+ base::Unretained(g_browser_process->safe_browsing_service()), |
+ quic_allowed_for_profile)); |
+} |
+ |
+void NetPrefObserver::ApplySettingsOnIOThread( |
+ IOThread* io_thread, |
+ safe_browsing::SafeBrowsingService* safe_browsing_service, |
+ bool quic_allowed_for_profile) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ // Never re-enable QUIC if it was disallowed on startup, e.g. due to command |
+ // line flags. |
+ bool quic_allowed_on_startup = io_thread->NetworkSessionParams().enable_quic; |
+ if (!quic_allowed_on_startup) |
+ return; |
+ |
+ net::HttpNetworkSession::ParamsUpdate params_update; |
+ params_update.enable_quic_new = quic_allowed_for_profile; |
+ |
+ // Make the profile adhere to its current setting by invoking its callback. |
+ update_net_params_callback_.Run(params_update); |
+ |
+ // Globally-owned HttpNetworkSessions should only be updated if QUIC is being |
+ // turned off. This "kill switch"-like behavior prevents multiple Profiles |
+ // fighting over QUIC enablement for global HttpNetworkSessions. |
+ if (!quic_allowed_for_profile) { |
+ io_thread->UpdateNetworkSessionParams(params_update); |
+ |
+ safe_browsing_service->UpdateNetParamsOnIOThread(params_update); |
+ } |
+} |
+ |
+// static |
+void NetPrefObserver::RegisterProfilePrefs( |
+ user_prefs::PrefRegistrySyncable* registry) { |
+ registry->RegisterBooleanPref(prefs::kQuicAllowed, true); |
+} |