OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_content_settings_store.h" | 5 #include "chrome/browser/extensions/extension_content_settings_store.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/extension_content_settings_api_constants.h" | 12 #include "chrome/browser/extensions/extension_content_settings_api_constants.h" |
13 #include "chrome/browser/extensions/extension_content_settings_helpers.h" | 13 #include "chrome/browser/extensions/extension_content_settings_helpers.h" |
14 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
15 | 15 |
16 namespace helpers = extension_content_settings_helpers; | 16 namespace helpers = extension_content_settings_helpers; |
17 namespace keys = extension_content_settings_api_constants; | 17 namespace keys = extension_content_settings_api_constants; |
18 | 18 |
| 19 namespace { |
| 20 |
| 21 bool ComparePatternPairs(const ContentSettingsPattern& first_primary, |
| 22 const ContentSettingsPattern& first_secondary, |
| 23 const ContentSettingsPattern& second_primary, |
| 24 const ContentSettingsPattern& second_secondary) { |
| 25 ContentSettingsPattern::Relation relation = |
| 26 first_primary.Compare(second_primary); |
| 27 if (relation == ContentSettingsPattern::SUCCESSOR) |
| 28 return true; |
| 29 if (relation == ContentSettingsPattern::PREDECESSOR) |
| 30 return false; |
| 31 return (first_secondary.Compare(second_secondary) == |
| 32 ContentSettingsPattern::SUCCESSOR); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
19 using content_settings::ResourceIdentifier; | 37 using content_settings::ResourceIdentifier; |
20 | 38 |
21 struct ExtensionContentSettingsStore::ExtensionEntry { | 39 struct ExtensionContentSettingsStore::ExtensionEntry { |
22 // Installation time of the extension. | 40 // Installation time of the extension. |
23 base::Time install_time; | 41 base::Time install_time; |
24 // Whether extension is enabled in the profile. | 42 // Whether extension is enabled in the profile. |
25 bool enabled; | 43 bool enabled; |
26 // Content settings. | 44 // Content settings. |
27 ContentSettingSpecList settings; | 45 ContentSettingSpecList settings; |
28 // Persistent incognito content settings. | 46 // Persistent incognito content settings. |
29 ContentSettingSpecList incognito_persistent_settings; | 47 ContentSettingSpecList incognito_persistent_settings; |
30 // Session-only incognito content settings. | 48 // Session-only incognito content settings. |
31 ContentSettingSpecList incognito_session_only_settings; | 49 ContentSettingSpecList incognito_session_only_settings; |
32 }; | 50 }; |
33 | 51 |
34 ExtensionContentSettingsStore::ExtensionContentSettingsStore() { | 52 ExtensionContentSettingsStore::ExtensionContentSettingsStore() { |
35 DCHECK(OnCorrectThread()); | 53 DCHECK(OnCorrectThread()); |
36 } | 54 } |
37 | 55 |
38 ExtensionContentSettingsStore::~ExtensionContentSettingsStore() { | 56 ExtensionContentSettingsStore::~ExtensionContentSettingsStore() { |
39 DCHECK(OnCorrectThread()); | 57 DCHECK(OnCorrectThread()); |
40 NotifyOfDestruction(); | 58 NotifyOfDestruction(); |
41 STLDeleteValues(&entries_); | 59 STLDeleteValues(&entries_); |
42 } | 60 } |
43 | 61 |
44 void ExtensionContentSettingsStore::SetExtensionContentSetting( | 62 void ExtensionContentSettingsStore::SetExtensionContentSetting( |
45 const std::string& ext_id, | 63 const std::string& ext_id, |
46 const ContentSettingsPattern& embedded_pattern, | 64 const ContentSettingsPattern& primary_pattern, |
47 const ContentSettingsPattern& top_level_pattern, | 65 const ContentSettingsPattern& secondary_pattern, |
48 ContentSettingsType type, | 66 ContentSettingsType type, |
49 const content_settings::ResourceIdentifier& identifier, | 67 const content_settings::ResourceIdentifier& identifier, |
50 ContentSetting setting, | 68 ContentSetting setting, |
51 ExtensionPrefsScope scope) { | 69 ExtensionPrefsScope scope) { |
52 { | 70 { |
53 base::AutoLock lock(lock_); | 71 base::AutoLock lock(lock_); |
54 ContentSettingSpecList* setting_spec_list = | 72 ContentSettingSpecList* setting_spec_list = |
55 GetContentSettingSpecList(ext_id, scope); | 73 GetContentSettingSpecList(ext_id, scope); |
56 | 74 |
57 // Find |ContentSettingSpec|. | 75 // Find |ContentSettingSpec|. |
58 ContentSettingSpecList::iterator setting_spec = setting_spec_list->begin(); | 76 ContentSettingSpecList::iterator setting_spec = setting_spec_list->begin(); |
59 while (setting_spec != setting_spec_list->end()) { | 77 while (setting_spec != setting_spec_list->end()) { |
60 if (setting_spec->embedded_pattern == embedded_pattern && | 78 if (setting_spec->primary_pattern == primary_pattern && |
61 setting_spec->top_level_pattern == top_level_pattern && | 79 setting_spec->secondary_pattern == secondary_pattern && |
62 setting_spec->content_type == type && | 80 setting_spec->content_type == type && |
63 setting_spec->resource_identifier == identifier) { | 81 setting_spec->resource_identifier == identifier) { |
64 break; | 82 break; |
65 } | 83 } |
66 ++setting_spec; | 84 ++setting_spec; |
67 } | 85 } |
68 if (setting_spec == setting_spec_list->end()) { | 86 if (setting_spec == setting_spec_list->end()) { |
69 setting_spec_list->push_back(ContentSettingSpec(embedded_pattern, | 87 setting_spec_list->push_back(ContentSettingSpec(primary_pattern, |
70 top_level_pattern, | 88 secondary_pattern, |
71 type, | 89 type, |
72 identifier, | 90 identifier, |
73 setting)); | 91 setting)); |
74 } else if (setting != CONTENT_SETTING_DEFAULT) { | 92 } else if (setting != CONTENT_SETTING_DEFAULT) { |
75 // Update setting. | 93 // Update setting. |
76 setting_spec->setting = setting; | 94 setting_spec->setting = setting; |
77 } else { | 95 } else { |
78 setting_spec_list->erase(setting_spec); | 96 setting_spec_list->erase(setting_spec); |
79 } | 97 } |
80 } | 98 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 case kExtensionPrefsScopeIncognitoPersistent: | 192 case kExtensionPrefsScopeIncognitoPersistent: |
175 return &(i->second->incognito_persistent_settings); | 193 return &(i->second->incognito_persistent_settings); |
176 case kExtensionPrefsScopeIncognitoSessionOnly: | 194 case kExtensionPrefsScopeIncognitoSessionOnly: |
177 return &(i->second->incognito_session_only_settings); | 195 return &(i->second->incognito_session_only_settings); |
178 } | 196 } |
179 NOTREACHED(); | 197 NOTREACHED(); |
180 return NULL; | 198 return NULL; |
181 } | 199 } |
182 | 200 |
183 ContentSetting ExtensionContentSettingsStore::GetContentSettingFromSpecList( | 201 ContentSetting ExtensionContentSettingsStore::GetContentSettingFromSpecList( |
184 const GURL& embedded_url, | 202 const GURL& primary_url, |
185 const GURL& top_level_url, | 203 const GURL& secondary_url, |
186 ContentSettingsType type, | 204 ContentSettingsType type, |
187 const content_settings::ResourceIdentifier& identifier, | 205 const content_settings::ResourceIdentifier& identifier, |
188 const ContentSettingSpecList& setting_spec_list) const { | 206 const ContentSettingSpecList& setting_spec_list) const { |
189 ContentSettingSpecList::const_iterator winner_spec = setting_spec_list.end(); | 207 ContentSettingSpecList::const_iterator winner_spec = setting_spec_list.end(); |
190 | 208 |
191 for (ContentSettingSpecList::const_iterator spec = setting_spec_list.begin(); | 209 for (ContentSettingSpecList::const_iterator spec = setting_spec_list.begin(); |
192 spec != setting_spec_list.end(); ++spec) { | 210 spec != setting_spec_list.end(); ++spec) { |
193 if (!spec->embedded_pattern.Matches(embedded_url) || | 211 if (!spec->primary_pattern.Matches(primary_url) || |
194 !spec->top_level_pattern.Matches(top_level_url) || | 212 !spec->secondary_pattern.Matches(secondary_url) || |
195 spec->content_type != type || | 213 spec->content_type != type || |
196 spec->resource_identifier != identifier) { | 214 spec->resource_identifier != identifier) { |
197 continue; | 215 continue; |
198 } | 216 } |
199 | 217 |
200 // TODO(markusheintz): Compare embedded patterns as well? | |
201 if (winner_spec == setting_spec_list.end() || | 218 if (winner_spec == setting_spec_list.end() || |
202 winner_spec->top_level_pattern.Compare(spec->top_level_pattern) == | 219 ComparePatternPairs(winner_spec->primary_pattern, |
203 ContentSettingsPattern::SUCCESSOR) { | 220 winner_spec->secondary_pattern, |
| 221 spec->primary_pattern, |
| 222 spec->secondary_pattern)) { |
204 winner_spec = spec; | 223 winner_spec = spec; |
205 } | 224 } |
206 } | 225 } |
207 | 226 |
208 return (winner_spec != setting_spec_list.end()) ? winner_spec->setting | 227 return (winner_spec != setting_spec_list.end()) ? winner_spec->setting |
209 : CONTENT_SETTING_DEFAULT; | 228 : CONTENT_SETTING_DEFAULT; |
210 } | 229 } |
211 | 230 |
212 ContentSetting ExtensionContentSettingsStore::GetEffectiveContentSetting( | 231 ContentSetting ExtensionContentSettingsStore::GetEffectiveContentSetting( |
213 const GURL& embedded_url, | 232 const GURL& embedded_url, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 // static | 296 // static |
278 void ExtensionContentSettingsStore::AddRules( | 297 void ExtensionContentSettingsStore::AddRules( |
279 ContentSettingsType type, | 298 ContentSettingsType type, |
280 const content_settings::ResourceIdentifier& identifier, | 299 const content_settings::ResourceIdentifier& identifier, |
281 const ContentSettingSpecList* setting_spec_list, | 300 const ContentSettingSpecList* setting_spec_list, |
282 content_settings::ProviderInterface::Rules* rules) { | 301 content_settings::ProviderInterface::Rules* rules) { |
283 ContentSettingSpecList::const_iterator it; | 302 ContentSettingSpecList::const_iterator it; |
284 for (it = setting_spec_list->begin(); it != setting_spec_list->end(); ++it) { | 303 for (it = setting_spec_list->begin(); it != setting_spec_list->end(); ++it) { |
285 if (it->content_type == type && it->resource_identifier == identifier) { | 304 if (it->content_type == type && it->resource_identifier == identifier) { |
286 rules->push_back(content_settings::ProviderInterface::Rule( | 305 rules->push_back(content_settings::ProviderInterface::Rule( |
287 it->embedded_pattern, it->top_level_pattern, it->setting)); | 306 it->primary_pattern, it->secondary_pattern, it->setting)); |
288 } | 307 } |
289 } | 308 } |
290 } | 309 } |
291 | 310 |
292 void ExtensionContentSettingsStore::GetContentSettingsForContentType( | 311 void ExtensionContentSettingsStore::GetContentSettingsForContentType( |
293 ContentSettingsType type, | 312 ContentSettingsType type, |
294 const content_settings::ResourceIdentifier& identifier, | 313 const content_settings::ResourceIdentifier& identifier, |
295 bool incognito, | 314 bool incognito, |
296 content_settings::ProviderInterface::Rules* rules) const { | 315 content_settings::ProviderInterface::Rules* rules) const { |
297 base::AutoLock lock(lock_); | 316 base::AutoLock lock(lock_); |
(...skipping 23 matching lines...) Expand all Loading... |
321 ListValue* ExtensionContentSettingsStore::GetSettingsForExtension( | 340 ListValue* ExtensionContentSettingsStore::GetSettingsForExtension( |
322 const std::string& extension_id, | 341 const std::string& extension_id, |
323 ExtensionPrefsScope scope) const { | 342 ExtensionPrefsScope scope) const { |
324 base::AutoLock lock(lock_); | 343 base::AutoLock lock(lock_); |
325 ListValue* settings = new ListValue(); | 344 ListValue* settings = new ListValue(); |
326 const ContentSettingSpecList* setting_spec_list = | 345 const ContentSettingSpecList* setting_spec_list = |
327 GetContentSettingSpecList(extension_id, scope); | 346 GetContentSettingSpecList(extension_id, scope); |
328 ContentSettingSpecList::const_iterator it; | 347 ContentSettingSpecList::const_iterator it; |
329 for (it = setting_spec_list->begin(); it != setting_spec_list->end(); ++it) { | 348 for (it = setting_spec_list->begin(); it != setting_spec_list->end(); ++it) { |
330 DictionaryValue* setting_dict = new DictionaryValue(); | 349 DictionaryValue* setting_dict = new DictionaryValue(); |
331 setting_dict->SetString(keys::kEmbeddedPatternKey, | 350 setting_dict->SetString(keys::kPrimaryPatternKey, |
332 it->embedded_pattern.ToString()); | 351 it->primary_pattern.ToString()); |
333 setting_dict->SetString(keys::kTopLevelPatternKey, | 352 setting_dict->SetString(keys::kSecondaryPatternKey, |
334 it->top_level_pattern.ToString()); | 353 it->secondary_pattern.ToString()); |
335 setting_dict->SetString( | 354 setting_dict->SetString( |
336 keys::kContentSettingsTypeKey, | 355 keys::kContentSettingsTypeKey, |
337 helpers::ContentSettingsTypeToString(it->content_type)); | 356 helpers::ContentSettingsTypeToString(it->content_type)); |
338 setting_dict->SetString(keys::kResourceIdentifierKey, | 357 setting_dict->SetString(keys::kResourceIdentifierKey, |
339 it->resource_identifier); | 358 it->resource_identifier); |
340 setting_dict->SetString(keys::kContentSettingKey, | 359 setting_dict->SetString(keys::kContentSettingKey, |
341 helpers::ContentSettingToString(it->setting)); | 360 helpers::ContentSettingToString(it->setting)); |
342 settings->Append(setting_dict); | 361 settings->Append(setting_dict); |
343 } | 362 } |
344 return settings; | 363 return settings; |
345 } | 364 } |
346 | 365 |
347 void ExtensionContentSettingsStore::SetExtensionContentSettingsFromList( | 366 void ExtensionContentSettingsStore::SetExtensionContentSettingsFromList( |
348 const std::string& extension_id, | 367 const std::string& extension_id, |
349 const ListValue* list, | 368 const ListValue* list, |
350 ExtensionPrefsScope scope) { | 369 ExtensionPrefsScope scope) { |
351 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { | 370 for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) { |
352 if ((*it)->GetType() != Value::TYPE_DICTIONARY) { | 371 if ((*it)->GetType() != Value::TYPE_DICTIONARY) { |
353 NOTREACHED(); | 372 NOTREACHED(); |
354 continue; | 373 continue; |
355 } | 374 } |
356 DictionaryValue* dict = static_cast<DictionaryValue*>(*it); | 375 DictionaryValue* dict = static_cast<DictionaryValue*>(*it); |
357 std::string pattern_str; | 376 std::string primary_pattern_str; |
358 dict->GetString(keys::kTopLevelPatternKey, &pattern_str); | 377 dict->GetString(keys::kPrimaryPatternKey, &primary_pattern_str); |
359 ContentSettingsPattern pattern = | 378 ContentSettingsPattern primary_pattern = |
360 ContentSettingsPattern::LegacyFromString(pattern_str); | 379 ContentSettingsPattern::LegacyFromString(primary_pattern_str); |
361 DCHECK(pattern.IsValid()); | 380 DCHECK(primary_pattern.IsValid()); |
362 | 381 |
363 std::string embedded_pattern_str; | 382 std::string secondary_pattern_str; |
364 dict->GetString(keys::kEmbeddedPatternKey, &embedded_pattern_str); | 383 dict->GetString(keys::kSecondaryPatternKey, &secondary_pattern_str); |
365 ContentSettingsPattern embedded_pattern = | 384 ContentSettingsPattern secondary_pattern = |
366 ContentSettingsPattern::LegacyFromString(embedded_pattern_str); | 385 ContentSettingsPattern::LegacyFromString(secondary_pattern_str); |
367 DCHECK(embedded_pattern.IsValid()); | 386 DCHECK(secondary_pattern.IsValid()); |
368 | 387 |
369 std::string content_settings_type_str; | 388 std::string content_settings_type_str; |
370 dict->GetString(keys::kContentSettingsTypeKey, &content_settings_type_str); | 389 dict->GetString(keys::kContentSettingsTypeKey, &content_settings_type_str); |
371 ContentSettingsType content_settings_type = | 390 ContentSettingsType content_settings_type = |
372 helpers::StringToContentSettingsType(content_settings_type_str); | 391 helpers::StringToContentSettingsType(content_settings_type_str); |
373 DCHECK_NE(CONTENT_SETTINGS_TYPE_DEFAULT, content_settings_type); | 392 DCHECK_NE(CONTENT_SETTINGS_TYPE_DEFAULT, content_settings_type); |
374 | 393 |
375 std::string resource_identifier; | 394 std::string resource_identifier; |
376 dict->GetString(keys::kResourceIdentifierKey, &resource_identifier); | 395 dict->GetString(keys::kResourceIdentifierKey, &resource_identifier); |
377 | 396 |
378 std::string content_setting_string; | 397 std::string content_setting_string; |
379 dict->GetString(keys::kContentSettingKey, &content_setting_string); | 398 dict->GetString(keys::kContentSettingKey, &content_setting_string); |
380 ContentSetting setting = CONTENT_SETTING_DEFAULT; | 399 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
381 bool result = | 400 bool result = |
382 helpers::StringToContentSetting(content_setting_string, &setting); | 401 helpers::StringToContentSetting(content_setting_string, &setting); |
383 DCHECK(result); | 402 DCHECK(result); |
384 | 403 |
385 SetExtensionContentSetting(extension_id, | 404 SetExtensionContentSetting(extension_id, |
386 pattern, | 405 primary_pattern, |
387 embedded_pattern, | 406 secondary_pattern, |
388 content_settings_type, | 407 content_settings_type, |
389 resource_identifier, | 408 resource_identifier, |
390 setting, | 409 setting, |
391 scope); | 410 scope); |
392 } | 411 } |
393 } | 412 } |
394 | 413 |
395 void ExtensionContentSettingsStore::AddObserver(Observer* observer) { | 414 void ExtensionContentSettingsStore::AddObserver(Observer* observer) { |
396 DCHECK(OnCorrectThread()); | 415 DCHECK(OnCorrectThread()); |
397 observers_.AddObserver(observer); | 416 observers_.AddObserver(observer); |
398 } | 417 } |
399 | 418 |
400 void ExtensionContentSettingsStore::RemoveObserver(Observer* observer) { | 419 void ExtensionContentSettingsStore::RemoveObserver(Observer* observer) { |
401 DCHECK(OnCorrectThread()); | 420 DCHECK(OnCorrectThread()); |
402 observers_.RemoveObserver(observer); | 421 observers_.RemoveObserver(observer); |
403 } | 422 } |
404 | 423 |
405 ExtensionContentSettingsStore::ContentSettingSpec::ContentSettingSpec( | 424 ExtensionContentSettingsStore::ContentSettingSpec::ContentSettingSpec( |
406 const ContentSettingsPattern& embedded_pattern, | 425 const ContentSettingsPattern& primary_pattern, |
407 const ContentSettingsPattern& top_level_pattern, | 426 const ContentSettingsPattern& secondary_pattern, |
408 ContentSettingsType type, | 427 ContentSettingsType type, |
409 const content_settings::ResourceIdentifier& identifier, | 428 const content_settings::ResourceIdentifier& identifier, |
410 ContentSetting setting) | 429 ContentSetting setting) |
411 : embedded_pattern(embedded_pattern), | 430 : primary_pattern(primary_pattern), |
412 top_level_pattern(top_level_pattern), | 431 secondary_pattern(secondary_pattern), |
413 content_type(type), | 432 content_type(type), |
414 resource_identifier(identifier), | 433 resource_identifier(identifier), |
415 setting(setting) { | 434 setting(setting) { |
416 } | 435 } |
417 | 436 |
418 void ExtensionContentSettingsStore::NotifyOfDestruction() { | 437 void ExtensionContentSettingsStore::NotifyOfDestruction() { |
419 FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer, | 438 FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer, |
420 observers_, | 439 observers_, |
421 OnDestruction()); | 440 OnDestruction()); |
422 } | 441 } |
423 | 442 |
424 void ExtensionContentSettingsStore::NotifyOfContentSettingChanged( | 443 void ExtensionContentSettingsStore::NotifyOfContentSettingChanged( |
425 const std::string& extension_id, | 444 const std::string& extension_id, |
426 bool incognito) { | 445 bool incognito) { |
427 FOR_EACH_OBSERVER( | 446 FOR_EACH_OBSERVER( |
428 ExtensionContentSettingsStore::Observer, | 447 ExtensionContentSettingsStore::Observer, |
429 observers_, | 448 observers_, |
430 OnContentSettingChanged(extension_id, incognito)); | 449 OnContentSettingChanged(extension_id, incognito)); |
431 } | 450 } |
432 | 451 |
433 bool ExtensionContentSettingsStore::OnCorrectThread() { | 452 bool ExtensionContentSettingsStore::OnCorrectThread() { |
434 // If there is no UI thread, we're most likely in a unit test. | 453 // If there is no UI thread, we're most likely in a unit test. |
435 return !BrowserThread::IsWellKnownThread(BrowserThread::UI) || | 454 return !BrowserThread::IsWellKnownThread(BrowserThread::UI) || |
436 BrowserThread::CurrentlyOn(BrowserThread::UI); | 455 BrowserThread::CurrentlyOn(BrowserThread::UI); |
437 } | 456 } |
OLD | NEW |