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