OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/http_server_properties_manager_factory.h" | 5 #include "chrome/browser/net/http_server_properties_manager_factory.h" |
6 | 6 |
| 7 #include "base/prefs/pref_change_registrar.h" |
| 8 #include "base/prefs/pref_service.h" |
7 #include "chrome/common/pref_names.h" | 9 #include "chrome/common/pref_names.h" |
8 #include "components/pref_registry/pref_registry_syncable.h" | 10 #include "components/pref_registry/pref_registry_syncable.h" |
9 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
10 #include "net/http/http_server_properties_manager.h" | 12 #include "net/http/http_server_properties_manager.h" |
11 | 13 |
| 14 namespace { |
| 15 |
| 16 // Connects the HttpServerPropertiesManager's storage to the Chrome prefs. |
| 17 class PrefServiceAdapter |
| 18 : public net::HttpServerPropertiesManager::PrefDelegate { |
| 19 public: |
| 20 explicit PrefServiceAdapter(PrefService* pref_service) |
| 21 : pref_service_(pref_service), |
| 22 path_(prefs::kHttpServerProperties) { |
| 23 pref_change_registrar_.Init(pref_service_); |
| 24 } |
| 25 |
| 26 ~PrefServiceAdapter() override { |
| 27 } |
| 28 |
| 29 // PrefDelegate implementation. |
| 30 bool HasServerProperties() override { |
| 31 return pref_service_->HasPrefPath(path_); |
| 32 } |
| 33 const base::DictionaryValue& GetServerProperties() const override { |
| 34 // Guaranteed not to return null when the pref is registered |
| 35 // (RegisterProfilePrefs was called). |
| 36 return *pref_service_->GetDictionary(path_); |
| 37 } |
| 38 void SetServerProperties(const base::DictionaryValue& value) override { |
| 39 return pref_service_->Set(path_, value); |
| 40 } |
| 41 void StartListeningForUpdates(const base::Closure& callback) override { |
| 42 pref_change_registrar_.Add(path_, callback); |
| 43 } |
| 44 void StopListeningForUpdates() override { |
| 45 pref_change_registrar_.RemoveAll(); |
| 46 } |
| 47 |
| 48 private: |
| 49 PrefService* pref_service_; |
| 50 const std::string path_; |
| 51 PrefChangeRegistrar pref_change_registrar_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(PrefServiceAdapter); |
| 54 }; |
| 55 |
| 56 } // namespace |
| 57 |
12 namespace chrome_browser_net { | 58 namespace chrome_browser_net { |
13 | 59 |
14 /* static */ | 60 /* static */ |
15 net::HttpServerPropertiesManager* | 61 net::HttpServerPropertiesManager* |
16 HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service) { | 62 HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service) { |
17 using content::BrowserThread; | 63 using content::BrowserThread; |
18 return new net::HttpServerPropertiesManager( | 64 return new net::HttpServerPropertiesManager( |
19 pref_service, | 65 new PrefServiceAdapter(pref_service), // Transfers ownership. |
20 prefs::kHttpServerProperties, | |
21 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 66 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
22 } | 67 } |
23 | 68 |
24 /* static */ | 69 /* static */ |
25 void HttpServerPropertiesManagerFactory::RegisterProfilePrefs( | 70 void HttpServerPropertiesManagerFactory::RegisterProfilePrefs( |
26 user_prefs::PrefRegistrySyncable* registry) { | 71 user_prefs::PrefRegistrySyncable* registry) { |
27 registry->RegisterDictionaryPref(prefs::kHttpServerProperties); | 72 registry->RegisterDictionaryPref(prefs::kHttpServerProperties); |
28 } | 73 } |
29 | 74 |
30 } // namespace chrome_browser_net | 75 } // namespace chrome_browser_net |
OLD | NEW |