| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "services/preferences/public/cpp/pref_observer_store.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "mojo/public/cpp/bindings/array.h" |
| 9 #include "services/shell/public/cpp/connector.h" |
| 10 |
| 11 PrefObserverStore::PrefObserverStore( |
| 12 prefs::mojom::PreferenceManagerPtr prefs_manager_ptr) |
| 13 : prefs_binding_(this), |
| 14 prefs_manager_ptr_(std::move(prefs_manager_ptr)), |
| 15 prefs_manager_(nullptr), |
| 16 initialized_(false) { |
| 17 prefs_manager_ = prefs_manager_ptr_.get(); |
| 18 } |
| 19 |
| 20 void PrefObserverStore::Init(const std::set<std::string>& keys) { |
| 21 DCHECK(!initialized_); |
| 22 DCHECK(prefs_manager_); |
| 23 keys_ = keys; |
| 24 |
| 25 std::vector<std::string> pref_array; |
| 26 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); |
| 27 prefs_manager_->AddObserver(std::move(pref_array), |
| 28 prefs_binding_.CreateInterfacePtrAndBind()); |
| 29 } |
| 30 |
| 31 bool PrefObserverStore::GetValue(const std::string& key, |
| 32 const base::Value** value) const { |
| 33 DCHECK(initialized_); |
| 34 DCHECK(keys_.find(key) != keys_.end()); |
| 35 |
| 36 return ValueMapPrefStore::GetValue(key, value); |
| 37 } |
| 38 |
| 39 void PrefObserverStore::SetValue(const std::string& key, |
| 40 std::unique_ptr<base::Value> value, |
| 41 uint32_t flags) { |
| 42 DCHECK(keys_.find(key) != keys_.end()); |
| 43 |
| 44 SetValueOnPreferenceManager(key, value.get()); |
| 45 ValueMapPrefStore::SetValue(key, std::move(value), flags); |
| 46 } |
| 47 |
| 48 void PrefObserverStore::RemoveValue(const std::string& key, uint32_t flags) { |
| 49 // TODO(jonross): add preference removal to preferences.mojom |
| 50 NOTIMPLEMENTED(); |
| 51 } |
| 52 |
| 53 bool PrefObserverStore::GetMutableValue(const std::string& key, |
| 54 base::Value** value) { |
| 55 DCHECK(initialized_); |
| 56 DCHECK(keys_.find(key) != keys_.end()); |
| 57 |
| 58 // TODO(jonross): add unittests once a mutable class of base::Value is used. |
| 59 // Such as base::DictionaryValue. |
| 60 return ValueMapPrefStore::GetMutableValue(key, value); |
| 61 } |
| 62 |
| 63 void PrefObserverStore::ReportValueChanged(const std::string& key, |
| 64 uint32_t flags) { |
| 65 ValueMapPrefStore::ReportValueChanged(key, flags); |
| 66 } |
| 67 |
| 68 void PrefObserverStore::SetValueSilently(const std::string& key, |
| 69 std::unique_ptr<base::Value> value, |
| 70 uint32_t flags) { |
| 71 SetValueOnPreferenceManager(key, value.get()); |
| 72 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags); |
| 73 } |
| 74 |
| 75 PrefObserverStore::~PrefObserverStore() {} |
| 76 |
| 77 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key, |
| 78 base::Value* value) { |
| 79 if (keys_.find(key) == keys_.end()) |
| 80 return; |
| 81 |
| 82 // TODO(jonross): replace with struct traits for base::Value |
| 83 prefs::mojom::PreferencePtr pref = prefs::mojom::Preference::New(); |
| 84 switch (value->GetType()) { |
| 85 case base::Value::TYPE_INTEGER: { |
| 86 pref->type = prefs::mojom::Preference::PreferenceType::INT; |
| 87 int val = 0; |
| 88 if (!value->GetAsInteger(&val)) |
| 89 return; |
| 90 pref->integer_value_ = val; |
| 91 } break; |
| 92 case base::Value::TYPE_STRING: { |
| 93 pref->type = prefs::mojom::Preference::PreferenceType::STRING; |
| 94 std::string str; |
| 95 if (!value->GetAsString(&str)) |
| 96 return; |
| 97 pref->string_value_ = str; |
| 98 } break; |
| 99 default: |
| 100 NOTIMPLEMENTED(); |
| 101 return; |
| 102 } |
| 103 |
| 104 std::unordered_map<std::string, prefs::mojom::PreferencePtr> pref_map; |
| 105 pref_map.emplace(std::make_pair(key, std::move(pref))); |
| 106 prefs_manager_->SetPreferences(std::move(pref_map)); |
| 107 } |
| 108 |
| 109 void PrefObserverStore::OnPreferencesChanged( |
| 110 std::unordered_map<std::string, prefs::mojom::PreferencePtr> preferences) { |
| 111 for (auto& it : preferences) { |
| 112 if (keys_.find(it.first) == keys_.end()) |
| 113 continue; |
| 114 // TODO(jonross): replace with struct traits for base::Value |
| 115 base::Value* value = nullptr; |
| 116 switch (it.second->type) { |
| 117 case prefs::mojom::Preference::PreferenceType::INT: |
| 118 value = new base::FundamentalValue((int)it.second->integer_value_); |
| 119 break; |
| 120 case prefs::mojom::Preference::PreferenceType::STRING: { |
| 121 base::Optional<std::string> string_value = it.second->string_value_; |
| 122 if (string_value && !string_value.value().empty()) |
| 123 value = new base::StringValue(string_value.value()); |
| 124 } break; |
| 125 default: |
| 126 NOTIMPLEMENTED(); |
| 127 return; |
| 128 } |
| 129 ValueMapPrefStore::SetValue(it.first, base::WrapUnique(value), 0); |
| 130 } |
| 131 |
| 132 if (!initialized_) { |
| 133 initialized_ = true; |
| 134 NotifyInitializationCompleted(); |
| 135 } |
| 136 } |
| OLD | NEW |