| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "components/login/screens/screen_context.h" | 5 #include "components/login/screens/screen_context.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 | 9 |
| 10 namespace login { | 10 namespace login { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 String16List ScreenContext::GetString16List(const KeyType& key) const { | 115 String16List ScreenContext::GetString16List(const KeyType& key) const { |
| 116 return Get<String16List>(key); | 116 return Get<String16List>(key); |
| 117 } | 117 } |
| 118 | 118 |
| 119 String16List ScreenContext::GetString16List( | 119 String16List ScreenContext::GetString16List( |
| 120 const KeyType& key, | 120 const KeyType& key, |
| 121 const String16List& default_value) const { | 121 const String16List& default_value) const { |
| 122 return Get(key, default_value); | 122 return Get(key, default_value); |
| 123 } | 123 } |
| 124 | 124 |
| 125 scoped_ptr<ScreenContext::KeyObserverSubscription> |
| 126 ScreenContext::AddKeyObserver(const KeyType& key, const KeyObserver& observer) { |
| 127 auto it = key_observers_.find(key); |
| 128 linked_ptr<KeyObserversList> observers_list; |
| 129 if (it == key_observers_.end()) { |
| 130 observers_list.reset(new KeyObserversList()); |
| 131 key_observers_[key] = observers_list; |
| 132 } else { |
| 133 observers_list = it->second; |
| 134 } |
| 135 return observers_list->Add(observer); |
| 136 } |
| 137 |
| 125 bool ScreenContext::HasKey(const KeyType& key) const { | 138 bool ScreenContext::HasKey(const KeyType& key) const { |
| 126 DCHECK(CalledOnValidThread()); | 139 DCHECK(CalledOnValidThread()); |
| 127 return storage_.HasKey(key); | 140 return storage_.HasKey(key); |
| 128 } | 141 } |
| 129 | 142 |
| 130 bool ScreenContext::HasChanges() const { | 143 bool ScreenContext::HasChanges() const { |
| 131 DCHECK(CalledOnValidThread()); | 144 DCHECK(CalledOnValidThread()); |
| 132 return !changes_.empty(); | 145 return !changes_.empty(); |
| 133 } | 146 } |
| 134 | 147 |
| 135 void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) { | 148 void ScreenContext::GetChangesAndReset(base::DictionaryValue* diff) { |
| 136 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
| 137 DCHECK(diff); | 150 DCHECK(diff); |
| 138 changes_.Swap(diff); | 151 changes_.Swap(diff); |
| 139 changes_.Clear(); | 152 changes_.Clear(); |
| 140 } | 153 } |
| 141 | 154 |
| 142 void ScreenContext::ApplyChanges(const base::DictionaryValue& diff, | 155 void ScreenContext::ApplyChanges(const base::DictionaryValue& diff, |
| 143 std::vector<std::string>* keys) { | 156 std::vector<std::string>* keys) { |
| 144 DCHECK(CalledOnValidThread()); | 157 DCHECK(CalledOnValidThread()); |
| 145 DCHECK(!HasChanges()); | 158 DCHECK(!HasChanges()); |
| 146 if (keys) { | 159 if (keys) { |
| 147 keys->clear(); | 160 keys->clear(); |
| 148 keys->reserve(diff.size()); | 161 keys->reserve(diff.size()); |
| 149 } | 162 } |
| 150 base::DictionaryValue::Iterator it(diff); | 163 |
| 151 while (!it.IsAtEnd()) { | 164 for (base::DictionaryValue::Iterator it(diff); !it.IsAtEnd(); it.Advance()) { |
| 152 Set(it.key(), it.value().DeepCopy()); | 165 Set(it.key(), it.value().DeepCopy()); |
| 153 if (keys) | 166 if (keys) |
| 154 keys->push_back(it.key()); | 167 keys->push_back(it.key()); |
| 155 it.Advance(); | 168 |
| 169 linked_ptr<KeyObserversList> observers = key_observers_[it.key()]; |
| 170 if (observers.get()) |
| 171 observers->Notify(it.key(), it.value()); |
| 156 } | 172 } |
| 157 changes_.Clear(); | 173 changes_.Clear(); |
| 158 } | 174 } |
| 159 | 175 |
| 160 bool ScreenContext::Set(const KeyType& key, base::Value* value) { | 176 bool ScreenContext::Set(const KeyType& key, base::Value* value) { |
| 161 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
| 162 DCHECK(value); | 178 DCHECK(value); |
| 163 scoped_ptr<base::Value> new_value(value); | 179 scoped_ptr<base::Value> new_value(value); |
| 164 | 180 |
| 165 base::Value* current_value; | 181 base::Value* current_value; |
| 166 bool in_storage = storage_.Get(key, ¤t_value); | 182 bool in_storage = storage_.Get(key, ¤t_value); |
| 167 | 183 |
| 168 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair. | 184 // Don't do anything if |storage_| already contains <|key|, |new_value|> pair. |
| 169 if (in_storage && new_value->Equals(current_value)) | 185 if (in_storage && new_value->Equals(current_value)) |
| 170 return false; | 186 return false; |
| 171 | 187 |
| 172 changes_.Set(key, new_value->DeepCopy()); | 188 changes_.Set(key, new_value->DeepCopy()); |
| 173 storage_.Set(key, new_value.release()); | 189 storage_.Set(key, new_value.release()); |
| 174 return true; | 190 return true; |
| 175 } | 191 } |
| 176 | 192 |
| 177 } // namespace login | 193 } // namespace login |
| OLD | NEW |