Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_pref_value_map.h" | |
| 6 | |
| 7 #include "base/scoped_ptr.h" | |
| 8 #include "base/stl_util-inl.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/prefs/pref_value_map.h" | |
| 11 | |
| 12 ExtensionPrefValueMap::ExtensionPrefValueMap() { | |
| 13 } | |
| 14 | |
| 15 ExtensionPrefValueMap::~ExtensionPrefValueMap() { | |
| 16 NotifyOfDestruction(); | |
| 17 STLDeleteValues(&entries_); | |
| 18 entries_.clear(); | |
| 19 } | |
| 20 | |
| 21 void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, | |
| 22 const std::string& key, | |
| 23 bool incognito, | |
| 24 Value* value) { | |
| 25 scoped_ptr<Value> scoped_value(value); | |
|
Mattias Nissler (ping if slow)
2011/01/07 10:12:58
You can just drop the scoped_value declaration and
battre
2011/01/10 16:55:47
Done.
| |
| 26 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | |
| 27 | |
| 28 if (prefs->SetValue(key, scoped_value.release())) | |
| 29 NotifyPrefValueChanged(key); | |
| 30 } | |
| 31 | |
| 32 void ExtensionPrefValueMap::RemoveExtensionPref(const std::string& ext_id, | |
| 33 const std::string& key, | |
| 34 bool incognito) { | |
| 35 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | |
| 36 if (prefs->RemoveValue(key)) | |
| 37 NotifyPrefValueChanged(key); | |
| 38 } | |
| 39 | |
| 40 void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id, | |
| 41 const base::Time& install_time, | |
| 42 bool is_enabled) { | |
| 43 if (entries_.find(ext_id) != entries_.end()) | |
| 44 UnregisterExtension(ext_id); | |
| 45 entries_[ext_id] = new ExtensionEntry; | |
| 46 entries_[ext_id]->install_time = install_time; | |
| 47 entries_[ext_id]->enabled = is_enabled; | |
| 48 } | |
| 49 | |
| 50 void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) { | |
| 51 ExtensionEntryMap::iterator i = entries_.find(ext_id); | |
| 52 if (i == entries_.end()) | |
| 53 return; | |
| 54 std::set<std::string> keys; // keys set by this extension | |
| 55 GetExtensionControlledKeys(*(i->second), &keys); | |
| 56 | |
| 57 delete i->second; | |
| 58 entries_.erase(i); | |
| 59 | |
| 60 NotifyPrefValueChanged(keys); | |
| 61 } | |
| 62 | |
| 63 void ExtensionPrefValueMap::SetExtensionState(const std::string& ext_id, | |
| 64 bool is_enabled) { | |
| 65 ExtensionEntryMap::iterator i = entries_.find(ext_id); | |
| 66 CHECK(i != entries_.end()); | |
| 67 if (i->second->enabled == is_enabled) | |
| 68 return; | |
| 69 std::set<std::string> keys; // keys set by this extension | |
| 70 GetExtensionControlledKeys(*(i->second), &keys); | |
| 71 i->second->enabled = is_enabled; | |
| 72 NotifyPrefValueChanged(keys); | |
| 73 } | |
| 74 | |
| 75 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | |
| 76 const std::string& ext_id, | |
| 77 bool incognito) { | |
| 78 ExtensionEntryMap::iterator i = entries_.find(ext_id); | |
| 79 CHECK(i != entries_.end()); | |
| 80 return incognito ? &(i->second->inc_preferences) | |
| 81 : &(i->second->reg_preferences); | |
| 82 } | |
| 83 | |
| 84 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | |
| 85 const std::string& ext_id, | |
| 86 bool incognito) const { | |
| 87 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | |
| 88 CHECK(i != entries_.end()); | |
| 89 return incognito ? &(i->second->inc_preferences) | |
| 90 : &(i->second->reg_preferences); | |
| 91 } | |
| 92 | |
| 93 void ExtensionPrefValueMap::GetExtensionControlledKeys( | |
| 94 const ExtensionEntry& entry, | |
| 95 std::set<std::string>* out) const { | |
| 96 PrefValueMap::const_iterator i; | |
| 97 | |
| 98 const PrefValueMap& reg_prefs = entry.reg_preferences; | |
| 99 for (i = reg_prefs.begin(); i != reg_prefs.end(); ++i) | |
| 100 out->insert(i->first); | |
| 101 | |
| 102 const PrefValueMap& inc_prefs = entry.inc_preferences; | |
| 103 for (i = inc_prefs.begin(); i != inc_prefs.end(); ++i) | |
| 104 out->insert(i->first); | |
| 105 } | |
| 106 | |
| 107 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( | |
| 108 const std::string& key, | |
| 109 bool incognito) const { | |
| 110 Value *winner = NULL; | |
| 111 base::Time winners_install_time; | |
| 112 | |
| 113 ExtensionEntryMap::const_iterator i; | |
| 114 for (i = entries_.begin(); i != entries_.end(); ++i) { | |
| 115 const std::string& ext_id = i->first; | |
| 116 const base::Time& install_time = i->second->install_time; | |
| 117 const bool enabled = i->second->enabled; | |
| 118 | |
| 119 if (!enabled) | |
| 120 continue; | |
| 121 if (install_time < winners_install_time) | |
| 122 continue; | |
| 123 | |
| 124 Value* value = NULL; | |
| 125 const PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, false); | |
| 126 if (prefs->GetValue(key, &value)) { | |
| 127 winner = value; | |
| 128 winners_install_time = install_time; | |
| 129 } | |
| 130 | |
| 131 if (!incognito) | |
| 132 continue; | |
| 133 | |
| 134 prefs = GetExtensionPrefValueMap(ext_id, true); | |
| 135 if (prefs->GetValue(key, &value)) { | |
| 136 winner = value; | |
| 137 winners_install_time = install_time; | |
| 138 } | |
| 139 } | |
| 140 return winner; | |
| 141 } | |
| 142 | |
| 143 void ExtensionPrefValueMap::AddObserver( | |
| 144 ExtensionPrefValueMap::Observer* observer) { | |
| 145 observers_.AddObserver(observer); | |
| 146 | |
| 147 // Collect all currently used keys and notify the new observer. | |
| 148 std::set<std::string> keys; | |
| 149 ExtensionEntryMap::const_iterator i; | |
| 150 for (i = entries_.begin(); i != entries_.end(); ++i) | |
| 151 GetExtensionControlledKeys(*(i->second), &keys); | |
| 152 | |
| 153 std::set<std::string>::const_iterator j; | |
| 154 for (j = keys.begin(); j != keys.end(); ++j) | |
| 155 observer->OnPrefValueChanged(*j); | |
| 156 | |
| 157 } | |
| 158 | |
| 159 void ExtensionPrefValueMap::RemoveObserver( | |
| 160 ExtensionPrefValueMap::Observer* observer) { | |
| 161 observers_.RemoveObserver(observer); | |
| 162 } | |
| 163 | |
| 164 void ExtensionPrefValueMap::NotifyInitializationCompleted() { | |
| 165 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | |
| 166 OnInitializationCompleted()); | |
| 167 } | |
| 168 | |
| 169 void ExtensionPrefValueMap::NotifyPrefValueChanged( | |
| 170 const std::set<std::string>& keys) { | |
| 171 std::set<std::string>::const_iterator i; | |
| 172 for (i = keys.begin(); i != keys.end(); ++i) | |
| 173 NotifyPrefValueChanged(*i); | |
| 174 } | |
| 175 | |
| 176 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | |
| 177 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | |
| 178 OnPrefValueChanged(key)); | |
| 179 } | |
| 180 | |
| 181 void ExtensionPrefValueMap::NotifyOfDestruction() { | |
| 182 FOR_EACH_OBSERVER(ExtensionPrefValueMap::Observer, observers_, | |
| 183 OnExtensionPrefValueMapDestruction()); | |
| 184 } | |
| OLD | NEW |