OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/browser/net/http_server_properties_manager_factory.h" | 5 #include "ios/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 "components/pref_registry/pref_registry_syncable.h" | 9 #include "components/pref_registry/pref_registry_syncable.h" |
8 #include "ios/chrome/browser/pref_names.h" | 10 #include "ios/chrome/browser/pref_names.h" |
9 #include "ios/web/public/web_thread.h" | 11 #include "ios/web/public/web_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 class PrefServiceAdapter |
| 17 : public net::HttpServerPropertiesManager::PrefDelegate { |
| 18 public: |
| 19 explicit PrefServiceAdapter(PrefService* pref_service) |
| 20 : pref_service_(pref_service), path_(prefs::kHttpServerProperties) { |
| 21 pref_change_registrar_.Init(pref_service_); |
| 22 } |
| 23 |
| 24 ~PrefServiceAdapter() override {} |
| 25 |
| 26 // PrefDelegate implementation. |
| 27 bool HasServerProperties() override { |
| 28 return pref_service_->HasPrefPath(path_); |
| 29 } |
| 30 const base::DictionaryValue& GetServerProperties() const override { |
| 31 // Guaranteed not to return null when the pref is registered |
| 32 // (RegisterProfilePrefs was called). |
| 33 return *pref_service_->GetDictionary(path_); |
| 34 } |
| 35 void SetServerProperties(const base::DictionaryValue& value) override { |
| 36 return pref_service_->Set(path_, value); |
| 37 } |
| 38 void StartListeningForUpdates(const base::Closure& callback) override { |
| 39 pref_change_registrar_.Add(path_, callback); |
| 40 } |
| 41 void StopListeningForUpdates() override { |
| 42 pref_change_registrar_.RemoveAll(); |
| 43 } |
| 44 |
| 45 private: |
| 46 PrefService* pref_service_; |
| 47 const std::string path_; |
| 48 PrefChangeRegistrar pref_change_registrar_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(PrefServiceAdapter); |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
12 // static | 55 // static |
13 net::HttpServerPropertiesManager* | 56 net::HttpServerPropertiesManager* |
14 HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service) { | 57 HttpServerPropertiesManagerFactory::CreateManager(PrefService* pref_service) { |
15 return new net::HttpServerPropertiesManager( | 58 return new net::HttpServerPropertiesManager( |
16 pref_service, prefs::kHttpServerProperties, | 59 new PrefServiceAdapter(pref_service), // Transfers ownership. |
17 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO)); | 60 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO)); |
18 } | 61 } |
19 | 62 |
20 // static | 63 // static |
21 void HttpServerPropertiesManagerFactory::RegisterProfilePrefs( | 64 void HttpServerPropertiesManagerFactory::RegisterProfilePrefs( |
22 user_prefs::PrefRegistrySyncable* registry) { | 65 user_prefs::PrefRegistrySyncable* registry) { |
23 registry->RegisterDictionaryPref(prefs::kHttpServerProperties); | 66 registry->RegisterDictionaryPref(prefs::kHttpServerProperties); |
24 } | 67 } |
OLD | NEW |