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 : initialization_complete_(false) { | |
14 } | |
15 | |
16 ExtensionPrefValueMap::~ExtensionPrefValueMap() { | |
17 STLDeleteValues(&entries_); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Hm, let's put an entries_.clear() here just to be
battre
2011/01/05 20:23:08
Done.
| |
18 } | |
19 | |
20 void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, | |
21 const std::string& key, | |
22 bool incognito, | |
23 Value* value) { | |
24 scoped_ptr<Value> scoped_value(value); | |
25 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | |
26 | |
27 Value* oldValue = NULL; | |
28 prefs->GetValue(key, &oldValue); | |
29 | |
30 bool modified = !Value::Equals(oldValue, scoped_value.get()); | |
31 if (!modified) | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
no need for the modified local, just inline the co
battre
2011/01/05 20:23:08
Gone. (due to following comment)
| |
32 return; | |
33 | |
34 prefs->SetValue(key, scoped_value.release()); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
You can just do the following:
if (prefs->SetValu
battre
2011/01/05 20:23:08
Done.
| |
35 NotifyPrefValueChanged(key); | |
36 } | |
37 | |
38 void ExtensionPrefValueMap::RemoveExtensionPref(const std::string& ext_id, | |
39 const std::string& key, | |
40 bool incognito) { | |
41 PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); | |
42 prefs->RemoveValue(key); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
RemoveValue returns a bool indicating whether ther
battre
2011/01/05 20:23:08
Done.
| |
43 NotifyPrefValueChanged(key); | |
44 } | |
45 | |
46 void ExtensionPrefValueMap::OnInitializationCompleted() { | |
47 DCHECK(!initialization_complete_); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
It seems overkill to have that flag only for the s
battre
2011/01/05 20:23:08
Done.
| |
48 initialization_complete_ = true; | |
49 NotifyInitializationCompleted(); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Why not just inline the NotifyInitializationComple
battre
2011/01/05 20:23:08
Done.
| |
50 } | |
51 | |
52 void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id, | |
53 const base::Time& install_time, | |
54 bool is_enabled) { | |
55 if (entries_.find(ext_id) != entries_.end()) { | |
56 LOG(WARNING) << "Extension " << ext_id << " registered multiple times."; | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Make this a NOTREACHED()?
battre
2011/01/05 20:23:08
Cannot be NOTREACHED. An extension can be reinstal
| |
57 UnregisterExtension(ext_id); | |
58 } | |
59 entries_[ext_id] = new ExtensionEntry; | |
60 entries_[ext_id]->install_time = install_time; | |
61 entries_[ext_id]->enabled = is_enabled; | |
62 } | |
63 | |
64 void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) { | |
65 if (entries_.find(ext_id) == entries_.end()) { | |
66 LOG(WARNING) << "Extension " << ext_id << " unregistered without" | |
67 << " being registered."; | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
NOTREACHED() again?
battre
2011/01/05 20:23:08
Everytime the extension black list is updated, all
| |
68 return; | |
69 } | |
70 std::set<std::string> keys; // keys set by this extension | |
71 GetExtensionControlledKeys(ext_id, &keys); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
It seems this function should really operate on Ex
battre
2011/01/05 20:23:08
Done.
| |
72 | |
73 delete entries_[ext_id]; | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
If you do the find() call above, can we use that i
battre
2011/01/05 20:23:08
Done.
| |
74 entries_.erase(ext_id); | |
75 | |
76 NotifyPrefValueChanged(keys); | |
77 } | |
78 | |
79 void ExtensionPrefValueMap::UpdateExtensionsState(const std::string& ext_id, | |
80 bool is_enabled) { | |
81 CHECK(entries_.find(ext_id) != entries_.end()); | |
82 if (entries_[ext_id]->enabled == is_enabled) | |
83 return; | |
84 std::set<std::string> keys; // keys set by this extension | |
85 GetExtensionControlledKeys(ext_id, &keys); | |
86 entries_[ext_id]->enabled = is_enabled; | |
87 NotifyPrefValueChanged(keys); | |
88 } | |
89 | |
90 PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | |
91 const std::string& ext_id, | |
92 bool incognito) { | |
93 CHECK(entries_[ext_id]); | |
94 PrefValueMap* prefs = incognito ? &(entries_[ext_id]->inc_preferences) | |
95 : &(entries_[ext_id]->reg_preferences); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Again, three lookups where one would suffice. Why
battre
2011/01/05 20:23:08
I prefer the visual clarity of a [] lookup over so
| |
96 return prefs; | |
97 } | |
98 | |
99 const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( | |
100 const std::string& ext_id, | |
101 bool incognito) const { | |
102 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); | |
103 CHECK(i != entries_.end()); | |
104 if (incognito) | |
105 return &(i->second->inc_preferences); | |
106 else | |
107 return &(i->second->reg_preferences); | |
108 } | |
109 | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
excess newline
battre
2011/01/05 20:23:08
Done.
| |
110 | |
111 void ExtensionPrefValueMap::GetExtensionControlledKeys( | |
112 const std::string& ext_id, std::set<std::string>* out) const { | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
I think you should break the line after the comma.
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Do you actually require ordering here? If not, I'd
battre
2011/01/05 20:23:08
Done.
battre
2011/01/05 20:23:08
I don't require ordering but uniqueness. An extens
| |
113 PrefValueMap::const_iterator i; | |
114 | |
115 const PrefValueMap* reg_prefs = GetExtensionPrefValueMap(ext_id, false); | |
116 for (i = reg_prefs->begin(); i != reg_prefs->end(); ++i) | |
117 out->insert(i->first); | |
118 | |
119 const PrefValueMap* inc_prefs = GetExtensionPrefValueMap(ext_id, true); | |
120 for (i = inc_prefs->begin(); i != inc_prefs->end(); ++i) | |
121 out->insert(i->first); | |
122 } | |
123 | |
124 const Value* ExtensionPrefValueMap::GetEffectivePrefValue( | |
125 const std::string& key, | |
126 bool incognito) const { | |
127 Value *winner = NULL; | |
128 base::Time winners_install_time = base::Time(); | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
no need for the assignment here, the default const
battre
2011/01/05 20:23:08
right... one a Java programmer by heart you never
| |
129 | |
130 ExtensionEntryMap::const_iterator i; | |
131 for (i = entries_.begin(); i != entries_.end(); ++i) { | |
132 const std::string& ext_id = i->first; | |
133 const base::Time& install_time = i->second->install_time; | |
134 const bool enabled = i->second->enabled; | |
135 | |
136 if (!enabled) | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
inline i->second->enabled?
battre
2011/01/05 20:23:08
I prefer my variant: The first three lines extract
| |
137 continue; | |
138 if (install_time < winners_install_time) | |
139 continue; | |
140 | |
141 Value* value = NULL; | |
142 const PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, false); | |
143 if (prefs->GetValue(key, &value)) { | |
144 winner = value; | |
145 winners_install_time = install_time; | |
146 } | |
147 | |
148 if (!incognito) | |
149 continue; | |
150 | |
151 prefs = GetExtensionPrefValueMap(ext_id, true); | |
152 if (prefs->GetValue(key, &value)) { | |
153 winner = value; | |
154 winners_install_time = install_time; | |
155 } | |
156 } | |
157 return winner; | |
158 } | |
159 | |
160 void ExtensionPrefValueMap::AddObserver(PrefStore::Observer* observer) { | |
161 observers_.AddObserver(observer); | |
162 } | |
163 | |
164 void ExtensionPrefValueMap::RemoveObserver(PrefStore::Observer* observer) { | |
165 observers_.RemoveObserver(observer); | |
166 } | |
167 | |
168 void ExtensionPrefValueMap::NotifyInitializationCompleted() { | |
169 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, | |
170 OnInitializationCompleted()); | |
171 } | |
172 | |
173 void ExtensionPrefValueMap::NotifyPrefValueChanged( | |
174 const std::set<std::string>& keys) { | |
175 std::set<std::string>::const_iterator i; | |
176 for (i = keys.begin(); i != keys.end(); ++i) | |
177 NotifyPrefValueChanged(*i); | |
178 } | |
179 | |
180 void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { | |
181 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | |
182 } | |
OLD | NEW |