summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/net_pref_observer.cc
blob: b125daebe2cc394e864555f09f84fac00279e620 (plain)
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
// Copyright (c) 2011 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/task.h"
#include "chrome/browser/net/predictor_api.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prerender/prerender_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_type.h"
#include "content/common/notification_details.h"
#include "net/http/http_stream_factory.h"
#include "net/url_request/url_request_throttler_manager.h"

namespace {

// Function (for NewRunnableFunction) to call the set_enforce_throttling
// member on the URLRequestThrottlerManager singleton.
void SetEnforceThrottlingOnThrottlerManager(bool enforce) {
  net::URLRequestThrottlerManager::GetInstance()->set_enforce_throttling(
      enforce);
}

}

NetPrefObserver::NetPrefObserver(PrefService* prefs,
                                 prerender::PrerenderManager* prerender_manager)
    : prerender_manager_(prerender_manager) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(prefs);
  network_prediction_enabled_.Init(prefs::kNetworkPredictionEnabled, prefs,
                                   this);
  spdy_disabled_.Init(prefs::kDisableSpdy, prefs, this);
  http_throttling_enabled_.Init(prefs::kHttpThrottlingEnabled, prefs, this);

  ApplySettings(NULL);
}

NetPrefObserver::~NetPrefObserver() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

void NetPrefObserver::Observe(NotificationType type,
                              const NotificationSource& source,
                              const NotificationDetails& details) {
  DCHECK_EQ(type.value, NotificationType::PREF_CHANGED);

  std::string* pref_name = Details<std::string>(details).ptr();
  ApplySettings(pref_name);
}

void NetPrefObserver::ApplySettings(const std::string* pref_name) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  chrome_browser_net::EnablePredictor(*network_prediction_enabled_);
  if (prerender_manager_)
    prerender_manager_->set_enabled(*network_prediction_enabled_);
  net::HttpStreamFactory::set_spdy_enabled(!*spdy_disabled_);

  if (!pref_name || *pref_name == prefs::kHttpThrottlingEnabled) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        NewRunnableFunction(SetEnforceThrottlingOnThrottlerManager,
                            *http_throttling_enabled_));
  }
}

// static
void NetPrefObserver::RegisterPrefs(PrefService* prefs) {
  prefs->RegisterBooleanPref(prefs::kNetworkPredictionEnabled,
                             true,
                             PrefService::SYNCABLE_PREF);
  prefs->RegisterBooleanPref(prefs::kDisableSpdy,
                             false,
                             PrefService::UNSYNCABLE_PREF);
  prefs->RegisterBooleanPref(prefs::kHttpThrottlingEnabled,
                             false,
                             PrefService::UNSYNCABLE_PREF);
}