Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(593)

Side by Side Diff: chrome/browser/extensions/api/content_settings/content_settings_store.cc

Issue 2436023002: Remove stl_util's deletion function use from chrome/browser/extensions/api/content_settings. (Closed)
Patch Set: reverse sort Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/api/content_settings/content_settings_store. h" 5 #include "chrome/browser/extensions/api/content_settings/content_settings_store. h"
6 6
7 #include <algorithm>
7 #include <memory> 8 #include <memory>
8 #include <set> 9 #include <set>
9 #include <utility> 10 #include <utility>
10 #include <vector>
11 11
12 #include "base/debug/alias.h" 12 #include "base/debug/alias.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
17 #include "base/values.h" 16 #include "base/values.h"
18 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h" 17 #include "chrome/browser/extensions/api/content_settings/content_settings_api_co nstants.h"
19 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h" 18 #include "chrome/browser/extensions/api/content_settings/content_settings_helper s.h"
20 #include "components/content_settings/core/browser/content_settings_origin_ident ifier_value_map.h" 19 #include "components/content_settings/core/browser/content_settings_origin_ident ifier_value_map.h"
21 #include "components/content_settings/core/browser/content_settings_rule.h" 20 #include "components/content_settings/core/browser/content_settings_rule.h"
22 #include "components/content_settings/core/browser/content_settings_utils.h" 21 #include "components/content_settings/core/browser/content_settings_utils.h"
23 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
24 23
25 using content::BrowserThread; 24 using content::BrowserThread;
26 using content_settings::ConcatenationIterator; 25 using content_settings::ConcatenationIterator;
27 using content_settings::Rule; 26 using content_settings::Rule;
28 using content_settings::RuleIterator; 27 using content_settings::RuleIterator;
29 using content_settings::OriginIdentifierValueMap; 28 using content_settings::OriginIdentifierValueMap;
30 using content_settings::ResourceIdentifier; 29 using content_settings::ResourceIdentifier;
31 using content_settings::ValueToContentSetting; 30 using content_settings::ValueToContentSetting;
32 31
33 namespace extensions { 32 namespace extensions {
34 33
35 namespace helpers = content_settings_helpers; 34 namespace helpers = content_settings_helpers;
36 namespace keys = content_settings_api_constants; 35 namespace keys = content_settings_api_constants;
37 36
38 struct ContentSettingsStore::ExtensionEntry { 37 struct ContentSettingsStore::ExtensionEntry {
39 // Extension id 38 // Extension id.
40 std::string id; 39 std::string id;
40 // Installation time.
41 base::Time install_time;
41 // Whether extension is enabled in the profile. 42 // Whether extension is enabled in the profile.
42 bool enabled; 43 bool enabled;
43 // Content settings. 44 // Content settings.
44 OriginIdentifierValueMap settings; 45 OriginIdentifierValueMap settings;
45 // Persistent incognito content settings. 46 // Persistent incognito content settings.
46 OriginIdentifierValueMap incognito_persistent_settings; 47 OriginIdentifierValueMap incognito_persistent_settings;
47 // Session-only incognito content settings. 48 // Session-only incognito content settings.
48 OriginIdentifierValueMap incognito_session_only_settings; 49 OriginIdentifierValueMap incognito_session_only_settings;
49 }; 50 };
50 51
51 ContentSettingsStore::ContentSettingsStore() { 52 ContentSettingsStore::ContentSettingsStore() {
52 DCHECK(OnCorrectThread()); 53 DCHECK(OnCorrectThread());
53 } 54 }
54 55
55 ContentSettingsStore::~ContentSettingsStore() { 56 ContentSettingsStore::~ContentSettingsStore() {
56 base::STLDeleteValues(&entries_);
57 } 57 }
58 58
59 std::unique_ptr<RuleIterator> ContentSettingsStore::GetRuleIterator( 59 std::unique_ptr<RuleIterator> ContentSettingsStore::GetRuleIterator(
60 ContentSettingsType type, 60 ContentSettingsType type,
61 const content_settings::ResourceIdentifier& identifier, 61 const content_settings::ResourceIdentifier& identifier,
62 bool incognito) const { 62 bool incognito) const {
63 std::vector<std::unique_ptr<RuleIterator>> iterators; 63 std::vector<std::unique_ptr<RuleIterator>> iterators;
64 // Iterate the extensions based on install time (last installed extensions
65 // first).
66 ExtensionEntryMap::const_reverse_iterator entry_it;
67 64
68 // The individual |RuleIterators| shouldn't lock; pass |lock_| to the 65 // The individual |RuleIterators| shouldn't lock; pass |lock_| to the
69 // |ConcatenationIterator| in a locked state. 66 // |ConcatenationIterator| in a locked state.
70 std::unique_ptr<base::AutoLock> auto_lock(new base::AutoLock(lock_)); 67 std::unique_ptr<base::AutoLock> auto_lock(new base::AutoLock(lock_));
71 68
72 for (entry_it = entries_.rbegin(); entry_it != entries_.rend(); ++entry_it) { 69 // Iterate the extensions based on install time (last installed extensions
Devlin 2016/12/14 15:54:01 not your code, but something like "most recently i
Avi (use Gerrit) 2016/12/14 19:29:06 Done.
73 auto* entry = entry_it->second; 70 // first).
71 for (const auto& entry : entries_) {
74 if (!entry->enabled) 72 if (!entry->enabled)
75 continue; 73 continue;
76 74
77 std::unique_ptr<RuleIterator> rule_it; 75 std::unique_ptr<RuleIterator> rule_it;
78 if (incognito) { 76 if (incognito) {
79 rule_it = entry->incognito_session_only_settings.GetRuleIterator( 77 rule_it = entry->incognito_session_only_settings.GetRuleIterator(
80 type, identifier, nullptr); 78 type, identifier, nullptr);
81 if (rule_it) 79 if (rule_it)
82 iterators.push_back(std::move(rule_it)); 80 iterators.push_back(std::move(rule_it));
83 rule_it = entry->incognito_persistent_settings.GetRuleIterator( 81 rule_it = entry->incognito_persistent_settings.GetRuleIterator(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 // be masked by another extension.) 119 // be masked by another extension.)
122 NotifyOfContentSettingChanged(ext_id, 120 NotifyOfContentSettingChanged(ext_id,
123 scope != kExtensionPrefsScopeRegular); 121 scope != kExtensionPrefsScopeRegular);
124 } 122 }
125 123
126 void ContentSettingsStore::RegisterExtension( 124 void ContentSettingsStore::RegisterExtension(
127 const std::string& ext_id, 125 const std::string& ext_id,
128 const base::Time& install_time, 126 const base::Time& install_time,
129 bool is_enabled) { 127 bool is_enabled) {
130 base::AutoLock lock(lock_); 128 base::AutoLock lock(lock_);
131 ExtensionEntryMap::iterator i = FindEntry(ext_id); 129 auto i = FindEntry(ext_id);
Devlin 2016/12/14 15:54:01 Using auto with all these FindEntry's makes me a l
Avi (use Gerrit) 2016/12/14 19:29:06 Sounds reasonable, though I can't delegate like th
132 ExtensionEntry* entry; 130 ExtensionEntry* entry;
Devlin 2016/12/14 15:54:01 init to nullptr
Avi (use Gerrit) 2016/12/14 19:29:06 Done.
133 if (i != entries_.end()) { 131 if (i != entries_.end()) {
134 entry = i->second; 132 entry = i->get();
135 } else { 133 } else {
136 entry = new ExtensionEntry; 134 entry = new ExtensionEntry;
137 entries_.insert(std::make_pair(install_time, entry)); 135 entry->install_time = install_time;
136 entries_.push_back(base::WrapUnique(entry));
137 std::stable_sort(entries_.begin(), entries_.end(),
Devlin 2016/12/14 15:54:01 Would it be better to use std::find_if() to find t
Avi (use Gerrit) 2016/12/14 19:29:06 std::upper_bound?
Devlin 2016/12/14 19:54:34 Even better. :)
138 [](const std::unique_ptr<ExtensionEntry>& a,
139 const std::unique_ptr<ExtensionEntry>& b) {
140 // Sort in reverse-chronological order to maintain the
141 // invariant.
142 return a->install_time > b->install_time;
143 });
138 } 144 }
139 145
140 entry->id = ext_id; 146 entry->id = ext_id;
141 entry->enabled = is_enabled; 147 entry->enabled = is_enabled;
142 } 148 }
143 149
144 void ContentSettingsStore::UnregisterExtension( 150 void ContentSettingsStore::UnregisterExtension(
145 const std::string& ext_id) { 151 const std::string& ext_id) {
146 bool notify = false; 152 bool notify = false;
147 bool notify_incognito = false; 153 bool notify_incognito = false;
148 { 154 {
149 base::AutoLock lock(lock_); 155 base::AutoLock lock(lock_);
150 ExtensionEntryMap::iterator i = FindEntry(ext_id); 156 auto i = FindEntry(ext_id);
151 if (i == entries_.end()) 157 if (i == entries_.end())
152 return; 158 return;
153 notify = !i->second->settings.empty(); 159 notify = !(*i)->settings.empty();
154 notify_incognito = !i->second->incognito_persistent_settings.empty() || 160 notify_incognito = !(*i)->incognito_persistent_settings.empty() ||
155 !i->second->incognito_session_only_settings.empty(); 161 !(*i)->incognito_session_only_settings.empty();
156 162
157 delete i->second;
158 entries_.erase(i); 163 entries_.erase(i);
159 } 164 }
160 if (notify) 165 if (notify)
161 NotifyOfContentSettingChanged(ext_id, false); 166 NotifyOfContentSettingChanged(ext_id, false);
162 if (notify_incognito) 167 if (notify_incognito)
163 NotifyOfContentSettingChanged(ext_id, true); 168 NotifyOfContentSettingChanged(ext_id, true);
164 } 169 }
165 170
166 void ContentSettingsStore::SetExtensionState( 171 void ContentSettingsStore::SetExtensionState(
167 const std::string& ext_id, bool is_enabled) { 172 const std::string& ext_id, bool is_enabled) {
168 bool notify = false; 173 bool notify = false;
169 bool notify_incognito = false; 174 bool notify_incognito = false;
170 { 175 {
171 base::AutoLock lock(lock_); 176 base::AutoLock lock(lock_);
172 ExtensionEntryMap::const_iterator i = FindEntry(ext_id); 177 auto i = FindEntry(ext_id);
173 if (i == entries_.end()) 178 if (i == entries_.end())
174 return; 179 return;
175 notify = !i->second->settings.empty(); 180 notify = !(*i)->settings.empty();
176 notify_incognito = !i->second->incognito_persistent_settings.empty() || 181 notify_incognito = !(*i)->incognito_persistent_settings.empty() ||
177 !i->second->incognito_session_only_settings.empty(); 182 !(*i)->incognito_session_only_settings.empty();
178 183
179 i->second->enabled = is_enabled; 184 (*i)->enabled = is_enabled;
180 } 185 }
181 if (notify) 186 if (notify)
182 NotifyOfContentSettingChanged(ext_id, false); 187 NotifyOfContentSettingChanged(ext_id, false);
183 if (notify_incognito) 188 if (notify_incognito)
184 NotifyOfContentSettingChanged(ext_id, true); 189 NotifyOfContentSettingChanged(ext_id, true);
185 } 190 }
186 191
187 OriginIdentifierValueMap* ContentSettingsStore::GetValueMap( 192 OriginIdentifierValueMap* ContentSettingsStore::GetValueMap(
188 const std::string& ext_id, 193 const std::string& ext_id,
189 ExtensionPrefsScope scope) { 194 ExtensionPrefsScope scope) {
190 ExtensionEntryMap::const_iterator i = FindEntry(ext_id); 195 auto i = FindEntry(ext_id);
191 if (i != entries_.end()) { 196 if (i != entries_.end()) {
192 switch (scope) { 197 switch (scope) {
193 case kExtensionPrefsScopeRegular: 198 case kExtensionPrefsScopeRegular:
194 return &(i->second->settings); 199 return &((*i)->settings);
195 case kExtensionPrefsScopeRegularOnly: 200 case kExtensionPrefsScopeRegularOnly:
196 // TODO(bauerb): Implement regular-only content settings. 201 // TODO(bauerb): Implement regular-only content settings.
197 NOTREACHED(); 202 NOTREACHED();
198 return NULL; 203 return nullptr;
199 case kExtensionPrefsScopeIncognitoPersistent: 204 case kExtensionPrefsScopeIncognitoPersistent:
200 return &(i->second->incognito_persistent_settings); 205 return &((*i)->incognito_persistent_settings);
201 case kExtensionPrefsScopeIncognitoSessionOnly: 206 case kExtensionPrefsScopeIncognitoSessionOnly:
202 return &(i->second->incognito_session_only_settings); 207 return &((*i)->incognito_session_only_settings);
203 } 208 }
204 } 209 }
205 return NULL; 210 return nullptr;
206 } 211 }
207 212
208 const OriginIdentifierValueMap* ContentSettingsStore::GetValueMap( 213 const OriginIdentifierValueMap* ContentSettingsStore::GetValueMap(
209 const std::string& ext_id, 214 const std::string& ext_id,
210 ExtensionPrefsScope scope) const { 215 ExtensionPrefsScope scope) const {
211 ExtensionEntryMap::const_iterator i = FindEntry(ext_id); 216 auto i = FindEntry(ext_id);
212 if (i == entries_.end()) 217 if (i == entries_.end())
213 return NULL; 218 return nullptr;
214 219
215 switch (scope) { 220 switch (scope) {
216 case kExtensionPrefsScopeRegular: 221 case kExtensionPrefsScopeRegular:
217 return &(i->second->settings); 222 return &((*i)->settings);
218 case kExtensionPrefsScopeRegularOnly: 223 case kExtensionPrefsScopeRegularOnly:
219 // TODO(bauerb): Implement regular-only content settings. 224 // TODO(bauerb): Implement regular-only content settings.
220 NOTREACHED(); 225 NOTREACHED();
221 return NULL; 226 return nullptr;
222 case kExtensionPrefsScopeIncognitoPersistent: 227 case kExtensionPrefsScopeIncognitoPersistent:
223 return &(i->second->incognito_persistent_settings); 228 return &((*i)->incognito_persistent_settings);
224 case kExtensionPrefsScopeIncognitoSessionOnly: 229 case kExtensionPrefsScopeIncognitoSessionOnly:
225 return &(i->second->incognito_session_only_settings); 230 return &((*i)->incognito_session_only_settings);
226 } 231 }
227 232
228 NOTREACHED(); 233 NOTREACHED();
229 return NULL; 234 return nullptr;
230 } 235 }
231 236
232 void ContentSettingsStore::ClearContentSettingsForExtension( 237 void ContentSettingsStore::ClearContentSettingsForExtension(
233 const std::string& ext_id, 238 const std::string& ext_id,
234 ExtensionPrefsScope scope) { 239 ExtensionPrefsScope scope) {
235 bool notify = false; 240 bool notify = false;
236 { 241 {
237 base::AutoLock lock(lock_); 242 base::AutoLock lock(lock_);
238 OriginIdentifierValueMap* map = GetValueMap(ext_id, scope); 243 OriginIdentifierValueMap* map = GetValueMap(ext_id, scope);
239 if (!map) { 244 if (!map) {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 for (auto& observer : observers_) 373 for (auto& observer : observers_)
369 observer.OnContentSettingChanged(extension_id, incognito); 374 observer.OnContentSettingChanged(extension_id, incognito);
370 } 375 }
371 376
372 bool ContentSettingsStore::OnCorrectThread() { 377 bool ContentSettingsStore::OnCorrectThread() {
373 // If there is no UI thread, we're most likely in a unit test. 378 // If there is no UI thread, we're most likely in a unit test.
374 return !BrowserThread::IsThreadInitialized(BrowserThread::UI) || 379 return !BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
375 BrowserThread::CurrentlyOn(BrowserThread::UI); 380 BrowserThread::CurrentlyOn(BrowserThread::UI);
376 } 381 }
377 382
378 ContentSettingsStore::ExtensionEntryMap::iterator 383 ContentSettingsStore::ExtensionEntries::iterator
379 ContentSettingsStore::FindEntry(const std::string& ext_id) { 384 ContentSettingsStore::FindEntry(const std::string& ext_id) {
380 ExtensionEntryMap::iterator i; 385 return std::find_if(entries_.begin(), entries_.end(),
381 for (i = entries_.begin(); i != entries_.end(); ++i) { 386 [ext_id](const std::unique_ptr<ExtensionEntry>& entry) {
382 if (i->second->id == ext_id) 387 return entry->id == ext_id;
383 return i; 388 });
384 }
385 return entries_.end();
386 } 389 }
387 390
388 ContentSettingsStore::ExtensionEntryMap::const_iterator 391 ContentSettingsStore::ExtensionEntries::const_iterator
389 ContentSettingsStore::FindEntry(const std::string& ext_id) const { 392 ContentSettingsStore::FindEntry(const std::string& ext_id) const {
390 ExtensionEntryMap::const_iterator i; 393 return std::find_if(entries_.begin(), entries_.end(),
391 for (i = entries_.begin(); i != entries_.end(); ++i) { 394 [ext_id](const std::unique_ptr<ExtensionEntry>& entry) {
392 if (i->second->id == ext_id) 395 return entry->id == ext_id;
393 return i; 396 });
394 }
395 return entries_.end();
396 } 397 }
397 398
398 } // namespace extensions 399 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698