| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extensions/extension_pref_store.h" | 5 #include "chrome/browser/extensions/extension_pref_store.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 i != extension_stack_.end(); ++i) { | 80 i != extension_stack_.end(); ++i) { |
| 81 (*result).push_back((*i)->extension->id()); | 81 (*result).push_back((*i)->extension->id()); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 // This could be sped up by keeping track of which extension currently controls | 85 // This could be sped up by keeping track of which extension currently controls |
| 86 // a given preference, among other optimizations. But probably fewer than 10 | 86 // a given preference, among other optimizations. But probably fewer than 10 |
| 87 // installed extensions will be trying to control any preferences, so stick | 87 // installed extensions will be trying to control any preferences, so stick |
| 88 // with this simpler algorithm until it causes a problem. | 88 // with this simpler algorithm until it causes a problem. |
| 89 void ExtensionPrefStore::UpdateOnePref(const char* path) { | 89 void ExtensionPrefStore::UpdateOnePref(const char* path) { |
| 90 scoped_ptr<Value> old_value; | |
| 91 PrefService* pref_service = GetPrefService(); | 90 PrefService* pref_service = GetPrefService(); |
| 92 | 91 |
| 93 // There are at least two PrefServices, one for local state and one for | 92 // There are at least two PrefServices, one for local state and one for |
| 94 // user prefs. (See browser_main.cc.) Different preferences are registered | 93 // user prefs. (See browser_main.cc.) Different preferences are registered |
| 95 // in each; if this one doesn't have the desired pref registered, we ignore | 94 // in each; if this one doesn't have the desired pref registered, we ignore |
| 96 // it and let the other one handle it. | 95 // it and let the other one handle it. |
| 97 // The pref_service may be NULL in unit testing. | 96 // The pref_service may be NULL in unit testing. |
| 98 if (pref_service) { | 97 if (pref_service && !pref_service->FindPreference(path)) |
| 99 const PrefService::Preference* pref = pref_service->FindPreference(path); | |
| 100 if (!pref) | |
| 101 return; | 98 return; |
| 102 old_value.reset(pref->GetValue()->DeepCopy()); | 99 |
| 103 } | 100 // Save the old value before removing it from the local cache. |
| 101 Value* my_old_value_ptr = NULL; |
| 102 prefs_->Get(path, &my_old_value_ptr); |
| 103 scoped_ptr<Value> my_old_value; |
| 104 if (my_old_value_ptr) |
| 105 my_old_value.reset(my_old_value_ptr->DeepCopy()); |
| 104 | 106 |
| 105 // DictionaryValue::Set complains if a key is overwritten with the same | 107 // DictionaryValue::Set complains if a key is overwritten with the same |
| 106 // value, so delete it first. | 108 // value, so remove it first. |
| 107 prefs_->Remove(path, NULL); | 109 prefs_->Remove(path, NULL); |
| 108 | 110 |
| 109 // Find the first extension that wants to set this pref and use its value. | 111 // Find the first extension that wants to set this pref and use its value. |
| 112 Value* my_new_value = NULL; |
| 110 for (ExtensionStack::iterator ext_iter = extension_stack_.begin(); | 113 for (ExtensionStack::iterator ext_iter = extension_stack_.begin(); |
| 111 ext_iter != extension_stack_.end(); ++ext_iter) { | 114 ext_iter != extension_stack_.end(); ++ext_iter) { |
| 112 PrefValueMap::iterator value_iter = (*ext_iter)->pref_values->find(path); | 115 PrefValueMap::iterator value_iter = (*ext_iter)->pref_values->find(path); |
| 113 if (value_iter != (*ext_iter)->pref_values->end()) { | 116 if (value_iter != (*ext_iter)->pref_values->end()) { |
| 114 prefs_->Set(path, (*value_iter).second->DeepCopy()); | 117 prefs_->Set(path, (*value_iter).second->DeepCopy()); |
| 118 my_new_value = (*value_iter).second; |
| 115 break; | 119 break; |
| 116 } | 120 } |
| 117 } | 121 } |
| 118 | 122 |
| 119 if (pref_service) { | 123 if (pref_service) { |
| 120 pref_service->pref_notifier()->OnPreferenceSet( | 124 bool value_changed = true; |
| 121 path, type_, old_value.get()); | 125 if (!my_old_value.get() && !my_new_value) { |
| 126 value_changed = false; |
| 127 } else if (my_old_value.get() && |
| 128 my_new_value && |
| 129 my_old_value->Equals(my_new_value)) { |
| 130 value_changed = false; |
| 131 } |
| 132 |
| 133 if (value_changed) |
| 134 pref_service->pref_notifier()->OnPreferenceSet(path, type_); |
| 122 } | 135 } |
| 123 } | 136 } |
| 124 | 137 |
| 125 void ExtensionPrefStore::UpdatePrefs(const PrefValueMap* pref_values) { | 138 void ExtensionPrefStore::UpdatePrefs(const PrefValueMap* pref_values) { |
| 126 if (!pref_values) | 139 if (!pref_values) |
| 127 return; | 140 return; |
| 128 | 141 |
| 129 for (PrefValueMap::const_iterator i = pref_values->begin(); | 142 for (PrefValueMap::const_iterator i = pref_values->begin(); |
| 130 i != pref_values->end(); ++i) { | 143 i != pref_values->end(); ++i) { |
| 131 UpdateOnePref(i->first); | 144 UpdateOnePref(i->first); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 190 } |
| 178 } | 191 } |
| 179 | 192 |
| 180 ExtensionPrefStore::ExtensionPrefs::ExtensionPrefs(Extension* extension, | 193 ExtensionPrefStore::ExtensionPrefs::ExtensionPrefs(Extension* extension, |
| 181 PrefValueMap* values) : extension(extension), pref_values(values) {} | 194 PrefValueMap* values) : extension(extension), pref_values(values) {} |
| 182 | 195 |
| 183 ExtensionPrefStore::ExtensionPrefs::~ExtensionPrefs() { | 196 ExtensionPrefStore::ExtensionPrefs::~ExtensionPrefs() { |
| 184 STLDeleteValues(pref_values); | 197 STLDeleteValues(pref_values); |
| 185 delete pref_values; | 198 delete pref_values; |
| 186 } | 199 } |
| OLD | NEW |