net_pref_observer.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
1// Copyright (c) 2012 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/prefs/pref_service.h"
8#include "chrome/browser/chrome_notification_types.h"
9#include "chrome/browser/net/predictor.h"
10#include "chrome/browser/prerender/prerender_manager.h"
11#include "chrome/common/pref_names.h"
12#include "components/pref_registry/pref_registry_syncable.h"
13#include "content/public/browser/browser_thread.h"
14#include "net/http/http_stream_factory.h"
15
16using content::BrowserThread;
17
18NetPrefObserver::NetPrefObserver(PrefService* prefs,
19                                 prerender::PrerenderManager* prerender_manager,
20                                 chrome_browser_net::Predictor* predictor)
21    : prerender_manager_(prerender_manager),
22      predictor_(predictor) {
23  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
24  DCHECK(prefs);
25  DCHECK(predictor);
26
27  base::Closure prefs_callback = base::Bind(&NetPrefObserver::ApplySettings,
28                                            base::Unretained(this));
29  network_prediction_enabled_.Init(prefs::kNetworkPredictionEnabled, prefs,
30                                   prefs_callback);
31  spdy_disabled_.Init(prefs::kDisableSpdy, prefs, prefs_callback);
32
33  ApplySettings();
34}
35
36NetPrefObserver::~NetPrefObserver() {
37  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38}
39
40void NetPrefObserver::ApplySettings() {
41  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
42
43  if (prerender_manager_)
44    prerender_manager_->set_enabled(*network_prediction_enabled_);
45  if (spdy_disabled_.IsManaged())
46    net::HttpStreamFactory::set_spdy_enabled(!*spdy_disabled_);
47}
48
49// static
50void NetPrefObserver::RegisterProfilePrefs(
51    user_prefs::PrefRegistrySyncable* registry) {
52  registry->RegisterBooleanPref(
53      prefs::kNetworkPredictionEnabled,
54      true,
55      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
56  registry->RegisterBooleanPref(
57      prefs::kDisableSpdy,
58      false,
59      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
60}
61