Home | History | Annotate | Download | only in net
      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/user_prefs/pref_registry_syncable.h"
     13 #include "content/public/browser/browser_thread.h"
     14 #include "net/http/http_stream_factory.h"
     15 
     16 using content::BrowserThread;
     17 
     18 NetPrefObserver::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 
     36 NetPrefObserver::~NetPrefObserver() {
     37   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     38 }
     39 
     40 void NetPrefObserver::ApplySettings() {
     41   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     42 
     43   predictor_->EnablePredictor(*network_prediction_enabled_);
     44   if (prerender_manager_)
     45     prerender_manager_->set_enabled(*network_prediction_enabled_);
     46   if (spdy_disabled_.IsManaged())
     47     net::HttpStreamFactory::set_spdy_enabled(!*spdy_disabled_);
     48 }
     49 
     50 // static
     51 void NetPrefObserver::RegisterProfilePrefs(
     52     user_prefs::PrefRegistrySyncable* registry) {
     53   registry->RegisterBooleanPref(
     54       prefs::kNetworkPredictionEnabled,
     55       true,
     56       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     57   registry->RegisterBooleanPref(
     58       prefs::kDisableSpdy,
     59       false,
     60       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     61 }
     62