Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 *result = tmp; | 170 *result = tmp; |
| 171 return true; | 171 return true; |
| 172 } | 172 } |
| 173 | 173 |
| 174 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { | 174 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
| 175 observers_.AddObserver(observer); | 175 observers_.AddObserver(observer); |
| 176 } | 176 } |
| 177 | 177 |
| 178 void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { | 178 void JsonPrefStore::RemoveObserver(PrefStore::Observer* observer) { |
| 179 observers_.RemoveObserver(observer); | 179 observers_.RemoveObserver(observer); |
| 180 ScopedVector<PrefStore::Observer>::iterator ownership = | |
| 181 std::find(owned_observers_.begin(), owned_observers_.end(), observer); | |
|
gab
2013/11/27 23:43:27
When would you want to remove an owned observer? I
| |
| 182 if (ownership != owned_observers_.end()) | |
| 183 owned_observers_.erase(ownership); | |
| 180 } | 184 } |
| 181 | 185 |
| 182 bool JsonPrefStore::HasObservers() const { | 186 bool JsonPrefStore::HasObservers() const { |
| 183 return observers_.might_have_observers(); | 187 return observers_.might_have_observers(); |
| 184 } | 188 } |
| 185 | 189 |
| 186 bool JsonPrefStore::IsInitializationComplete() const { | 190 bool JsonPrefStore::IsInitializationComplete() const { |
| 187 return initialized_; | 191 return initialized_; |
| 188 } | 192 } |
| 189 | 193 |
| 194 void JsonPrefStore::AddOwnedObserver(scoped_ptr<PrefStore::Observer> observer) { | |
| 195 observers_.AddObserver(observer.get()); | |
| 196 owned_observers_.push_back(observer.release()); | |
| 197 } | |
| 198 | |
| 190 bool JsonPrefStore::GetMutableValue(const std::string& key, | 199 bool JsonPrefStore::GetMutableValue(const std::string& key, |
| 191 base::Value** result) { | 200 base::Value** result) { |
| 192 return prefs_->Get(key, result); | 201 return prefs_->Get(key, result); |
| 193 } | 202 } |
| 194 | 203 |
| 195 void JsonPrefStore::SetValue(const std::string& key, base::Value* value) { | 204 void JsonPrefStore::SetValue(const std::string& key, base::Value* value) { |
| 196 DCHECK(value); | 205 DCHECK(value); |
| 197 scoped_ptr<base::Value> new_value(value); | 206 scoped_ptr<base::Value> new_value(value); |
| 198 base::Value* old_value = NULL; | 207 base::Value* old_value = NULL; |
| 199 prefs_->Get(key, &old_value); | 208 prefs_->Get(key, &old_value); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 if (error_delegate_.get() && error != PREF_READ_ERROR_NONE) | 323 if (error_delegate_.get() && error != PREF_READ_ERROR_NONE) |
| 315 error_delegate_->OnError(error); | 324 error_delegate_->OnError(error); |
| 316 | 325 |
| 317 FOR_EACH_OBSERVER(PrefStore::Observer, | 326 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 318 observers_, | 327 observers_, |
| 319 OnInitializationCompleted(true)); | 328 OnInitializationCompleted(true)); |
| 320 } | 329 } |
| 321 | 330 |
| 322 JsonPrefStore::~JsonPrefStore() { | 331 JsonPrefStore::~JsonPrefStore() { |
| 323 CommitPendingWrite(); | 332 CommitPendingWrite(); |
| 333 for (ScopedVector<PrefStore::Observer>::iterator it = | |
| 334 owned_observers_.begin(); | |
| 335 it != owned_observers_.end(); | |
| 336 ++it) { | |
| 337 RemoveObserver(*it); | |
|
gab
2013/11/27 23:43:27
This is operating on owned_observers_ while iterat
robertshield
2013/11/28 02:54:26
doesn't this call owned_observers_.erase() invalid
erikwright (departed)
2013/11/28 17:48:07
You're both right, it's a mistake that this affect
| |
| 338 } | |
| 324 } | 339 } |
| 325 | 340 |
| 326 bool JsonPrefStore::SerializeData(std::string* output) { | 341 bool JsonPrefStore::SerializeData(std::string* output) { |
| 327 // TODO(tc): Do we want to prune webkit preferences that match the default | 342 // TODO(tc): Do we want to prune webkit preferences that match the default |
| 328 // value? | 343 // value? |
| 329 JSONStringValueSerializer serializer(output); | 344 JSONStringValueSerializer serializer(output); |
| 330 serializer.set_pretty_print(true); | 345 serializer.set_pretty_print(true); |
| 331 scoped_ptr<base::DictionaryValue> copy( | 346 scoped_ptr<base::DictionaryValue> copy( |
| 332 prefs_->DeepCopyWithoutEmptyChildren()); | 347 prefs_->DeepCopyWithoutEmptyChildren()); |
| 333 | 348 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 349 copy->Set(key, new base::ListValue); | 364 copy->Set(key, new base::ListValue); |
| 350 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { | 365 } else if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 351 const base::DictionaryValue* dict = NULL; | 366 const base::DictionaryValue* dict = NULL; |
| 352 if (value->GetAsDictionary(&dict) && dict->empty()) | 367 if (value->GetAsDictionary(&dict) && dict->empty()) |
| 353 copy->Set(key, new base::DictionaryValue); | 368 copy->Set(key, new base::DictionaryValue); |
| 354 } | 369 } |
| 355 } | 370 } |
| 356 | 371 |
| 357 return serializer.Serialize(*(copy.get())); | 372 return serializer.Serialize(*(copy.get())); |
| 358 } | 373 } |
| OLD | NEW |