1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/net_pref_observer.h"
6
7 #include "base/task.h"
8 #include "chrome/browser/net/predictor_api.h"
9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/prerender/prerender_manager.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/pref_names.h"
13 #include "content/browser/browser_thread.h"
14 #include "content/common/notification_type.h"
15 #include "content/common/notification_details.h"
16 #include "net/http/http_stream_factory.h"
17 #include "net/url_request/url_request_throttler_manager.h"
18
19 namespace {
20
21 // Function (for NewRunnableFunction) to call the set_enforce_throttling
22 // member on the URLRequestThrottlerManager singleton.
SetEnforceThrottlingOnThrottlerManager(bool enforce)23 void SetEnforceThrottlingOnThrottlerManager(bool enforce) {
24 net::URLRequestThrottlerManager::GetInstance()->set_enforce_throttling(
25 enforce);
26 }
27
28 }
29
NetPrefObserver(PrefService * prefs,prerender::PrerenderManager * prerender_manager)30 NetPrefObserver::NetPrefObserver(PrefService* prefs,
31 prerender::PrerenderManager* prerender_manager)
32 : prerender_manager_(prerender_manager) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34 DCHECK(prefs);
35 network_prediction_enabled_.Init(prefs::kNetworkPredictionEnabled, prefs,
36 this);
37 spdy_disabled_.Init(prefs::kDisableSpdy, prefs, this);
38 http_throttling_enabled_.Init(prefs::kHttpThrottlingEnabled, prefs, this);
39
40 ApplySettings(NULL);
41 }
42
~NetPrefObserver()43 NetPrefObserver::~NetPrefObserver() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45 }
46
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)47 void NetPrefObserver::Observe(NotificationType type,
48 const NotificationSource& source,
49 const NotificationDetails& details) {
50 DCHECK_EQ(type.value, NotificationType::PREF_CHANGED);
51
52 std::string* pref_name = Details<std::string>(details).ptr();
53 ApplySettings(pref_name);
54 }
55
ApplySettings(const std::string * pref_name)56 void NetPrefObserver::ApplySettings(const std::string* pref_name) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
58
59 chrome_browser_net::EnablePredictor(*network_prediction_enabled_);
60 if (prerender_manager_)
61 prerender_manager_->set_enabled(*network_prediction_enabled_);
62 net::HttpStreamFactory::set_spdy_enabled(!*spdy_disabled_);
63
64 if (!pref_name || *pref_name == prefs::kHttpThrottlingEnabled) {
65 BrowserThread::PostTask(
66 BrowserThread::IO, FROM_HERE,
67 NewRunnableFunction(SetEnforceThrottlingOnThrottlerManager,
68 *http_throttling_enabled_));
69 }
70 }
71
72 // static
RegisterPrefs(PrefService * prefs)73 void NetPrefObserver::RegisterPrefs(PrefService* prefs) {
74 prefs->RegisterBooleanPref(prefs::kNetworkPredictionEnabled, true);
75 prefs->RegisterBooleanPref(prefs::kDisableSpdy, false);
76 prefs->RegisterBooleanPref(prefs::kHttpThrottlingEnabled, false);
77 }
78