| OLD | NEW | 
|    1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2010 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/content_settings/pref_content_settings_provider.h" |    5 #include "chrome/browser/content_settings/pref_content_settings_provider.h" | 
|    6  |    6  | 
|    7 #include <string> |    7 #include <string> | 
|    8 #include <vector> |    8 #include <vector> | 
|    9 #include <utility> |    9 #include <utility> | 
|   10  |   10  | 
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   53 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { |   53 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | 
|   54   "cookies", |   54   "cookies", | 
|   55   "images", |   55   "images", | 
|   56   "javascript", |   56   "javascript", | 
|   57   "plugins", |   57   "plugins", | 
|   58   "popups", |   58   "popups", | 
|   59   NULL,  // Not used for Geolocation |   59   NULL,  // Not used for Geolocation | 
|   60   NULL,  // Not used for Notifications |   60   NULL,  // Not used for Notifications | 
|   61 }; |   61 }; | 
|   62  |   62  | 
|   63 // True if a given content settings type requires additional resource |  | 
|   64 // identifiers. |  | 
|   65 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { |  | 
|   66   false,  // CONTENT_SETTINGS_TYPE_COOKIES |  | 
|   67   false,  // CONTENT_SETTINGS_TYPE_IMAGES |  | 
|   68   false,  // CONTET_SETTINGS_TYPE_JAVASCRIPT |  | 
|   69   true,   // CONTENT_SETTINGS_TYPE_PLUGINS |  | 
|   70   false,  // CONTENT_SETTINGS_TYPE_POPUPS |  | 
|   71   false,  // Not used for Geolocation |  | 
|   72   false,  // Not used for Notifications |  | 
|   73 }; |  | 
|   74  |  | 
|   75 // Map ASK for the plugins content type to BLOCK if click-to-play is |  | 
|   76 // not enabled. |  | 
|   77 ContentSetting ClickToPlayFixup(ContentSettingsType content_type, |  | 
|   78                                 ContentSetting setting) { |  | 
|   79   if (setting == CONTENT_SETTING_ASK && |  | 
|   80       content_type == CONTENT_SETTINGS_TYPE_PLUGINS && |  | 
|   81       !CommandLine::ForCurrentProcess()->HasSwitch( |  | 
|   82           switches::kEnableClickToPlay)) { |  | 
|   83     return CONTENT_SETTING_BLOCK; |  | 
|   84   } |  | 
|   85   return setting; |  | 
|   86 } |  | 
|   87  |  | 
|   88 }  // namespace |   63 }  // namespace | 
|   89  |   64  | 
|   90 namespace content_settings { |   65 namespace content_settings { | 
|   91  |   66  | 
|   92 struct PrefProvider::ExtendedContentSettings { |  | 
|   93   ContentSettings content_settings; |  | 
|   94   ResourceContentSettings content_settings_for_resources; |  | 
|   95 }; |  | 
|   96  |  | 
|   97 PrefDefaultProvider::PrefDefaultProvider(Profile* profile) |   67 PrefDefaultProvider::PrefDefaultProvider(Profile* profile) | 
|   98     : profile_(profile), |   68     : profile_(profile), | 
|   99       is_off_the_record_(profile_->IsOffTheRecord()), |   69       is_off_the_record_(profile_->IsOffTheRecord()), | 
|  100       updating_preferences_(false) { |   70       updating_preferences_(false) { | 
|  101   PrefService* prefs = profile->GetPrefs(); |   71   PrefService* prefs = profile->GetPrefs(); | 
|  102  |   72  | 
|  103   // Read global defaults. |   73   // Read global defaults. | 
|  104   DCHECK_EQ(arraysize(kTypeNames), |   74   DCHECK_EQ(arraysize(kTypeNames), | 
|  105             static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); |   75             static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | 
|  106   ReadDefaultSettings(true); |   76   ReadDefaultSettings(true); | 
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  266         break; |  236         break; | 
|  267       } |  237       } | 
|  268     } |  238     } | 
|  269   } |  239   } | 
|  270   // Migrate obsolete cookie prompt mode. |  240   // Migrate obsolete cookie prompt mode. | 
|  271   if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == |  241   if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | 
|  272       CONTENT_SETTING_ASK) |  242       CONTENT_SETTING_ASK) | 
|  273     settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; |  243     settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | 
|  274  |  244  | 
|  275   settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = |  245   settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | 
|  276       ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, |  246       BaseProvider::ClickToPlayFixup( | 
|  277                        settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); |  247           CONTENT_SETTINGS_TYPE_PLUGINS, | 
 |  248           settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | 
|  278 } |  249 } | 
|  279  |  250  | 
|  280 void PrefDefaultProvider::NotifyObservers( |  251 void PrefDefaultProvider::NotifyObservers( | 
|  281     const ContentSettingsDetails& details) { |  252     const ContentSettingsDetails& details) { | 
|  282   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  253   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  283   if (profile_ == NULL) |  254   if (profile_ == NULL) | 
|  284     return; |  255     return; | 
|  285   NotificationService::current()->Notify( |  256   NotificationService::current()->Notify( | 
|  286       NotificationType::CONTENT_SETTINGS_CHANGED, |  257       NotificationType::CONTENT_SETTINGS_CHANGED, | 
|  287       Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), |  258       Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), | 
|  288       Details<const ContentSettingsDetails>(&details)); |  259       Details<const ContentSettingsDetails>(&details)); | 
|  289 } |  260 } | 
|  290  |  261  | 
|  291  |  | 
|  292 // static |  262 // static | 
|  293 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) { |  263 void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) { | 
|  294   prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); |  264   prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | 
|  295 } |  265 } | 
|  296  |  266  | 
|  297 // //////////////////////////////////////////////////////////////////////////// |  267 // //////////////////////////////////////////////////////////////////////////// | 
|  298 // PrefProvider:: |  268 // PrefProvider: | 
|  299 // |  269 // | 
|  300  |  270  | 
|  301 // static |  271 // static | 
|  302 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { |  272 void PrefProvider::RegisterUserPrefs(PrefService* prefs) { | 
|  303   prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, |  273   prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, | 
|  304       ContentSettingsPattern::kContentSettingsPatternVersion); |  274       ContentSettingsPattern::kContentSettingsPatternVersion); | 
|  305   prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); |  275   prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); | 
|  306  |  276  | 
|  307   // Obsolete prefs, for migration: |  277   // Obsolete prefs, for migration: | 
|  308   prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); |  278   prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); | 
|  309   prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); |  279   prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); | 
|  310 } |  280 } | 
|  311  |  281  | 
|  312 PrefProvider::PrefProvider(Profile* profile) |  282 PrefProvider::PrefProvider(Profile* profile) | 
|  313   : profile_(profile), |  283   : BaseProvider(profile->IsOffTheRecord()), | 
|  314     is_off_the_record_(profile_->IsOffTheRecord()), |  284     profile_(profile), | 
|  315     updating_preferences_(false) { |  285     updating_preferences_(false) { | 
 |  286   Init(); | 
 |  287 } | 
 |  288  | 
 |  289 void PrefProvider::Init() { | 
|  316   initializing_ = true; |  290   initializing_ = true; | 
|  317   PrefService* prefs = profile_->GetPrefs(); |  291   PrefService* prefs = profile_->GetPrefs(); | 
|  318  |  292  | 
|  319   // Migrate obsolete preferences. |  293   // Migrate obsolete preferences. | 
|  320   MigrateObsoletePerhostPref(prefs); |  294   MigrateObsoletePerhostPref(prefs); | 
|  321   MigrateObsoletePopupsPref(prefs); |  295   MigrateObsoletePopupsPref(prefs); | 
|  322  |  296  | 
|  323   // Verify preferences version. |  297   // Verify preferences version. | 
|  324   if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { |  298   if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 
|  325     prefs->SetInteger(prefs::kContentSettingsVersion, |  299     prefs->SetInteger(prefs::kContentSettingsVersion, | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|  340   notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |  314   notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 
|  341                               Source<Profile>(profile_)); |  315                               Source<Profile>(profile_)); | 
|  342   initializing_ = false; |  316   initializing_ = false; | 
|  343 } |  317 } | 
|  344  |  318  | 
|  345 bool PrefProvider::ContentSettingsTypeIsManaged( |  319 bool PrefProvider::ContentSettingsTypeIsManaged( | 
|  346     ContentSettingsType content_type) { |  320     ContentSettingsType content_type) { | 
|  347   return false; |  321   return false; | 
|  348 } |  322 } | 
|  349  |  323  | 
|  350 ContentSetting PrefProvider::GetContentSetting( |  | 
|  351     const GURL& requesting_url, |  | 
|  352     const GURL& embedding_url, |  | 
|  353     ContentSettingsType content_type, |  | 
|  354     const ResourceIdentifier& resource_identifier) const { |  | 
|  355   // Support for embedding_patterns is not implemented yet. |  | 
|  356   DCHECK(requesting_url == embedding_url); |  | 
|  357  |  | 
|  358   if (!RequiresResourceIdentifier(content_type)) |  | 
|  359     return GetNonDefaultContentSettings(requesting_url).settings[content_type]; |  | 
|  360  |  | 
|  361   if (RequiresResourceIdentifier(content_type) && resource_identifier.empty()) |  | 
|  362     return CONTENT_SETTING_DEFAULT; |  | 
|  363  |  | 
|  364   if (CommandLine::ForCurrentProcess()->HasSwitch( |  | 
|  365       switches::kEnableResourceContentSettings)) { |  | 
|  366     DCHECK(!resource_identifier.empty()); |  | 
|  367   } |  | 
|  368  |  | 
|  369   base::AutoLock auto_lock(lock_); |  | 
|  370  |  | 
|  371   const std::string host(net::GetHostOrSpecFromURL(requesting_url)); |  | 
|  372   ContentSettingsTypeResourceIdentifierPair |  | 
|  373       requested_setting(content_type, resource_identifier); |  | 
|  374  |  | 
|  375   // Check for exact matches first. |  | 
|  376   HostContentSettings::const_iterator i(host_content_settings_.find(host)); |  | 
|  377   if (i != host_content_settings_.end() && |  | 
|  378       i->second.content_settings_for_resources.find(requested_setting) != |  | 
|  379       i->second.content_settings_for_resources.end()) { |  | 
|  380     return i->second.content_settings_for_resources.find( |  | 
|  381         requested_setting)->second; |  | 
|  382   } |  | 
|  383  |  | 
|  384   // If this map is not for an off-the-record profile, these searches will never |  | 
|  385   // match. The additional off-the-record exceptions always overwrite the |  | 
|  386   // regular ones. |  | 
|  387   i = off_the_record_settings_.find(host); |  | 
|  388   if (i != off_the_record_settings_.end() && |  | 
|  389       i->second.content_settings_for_resources.find(requested_setting) != |  | 
|  390       i->second.content_settings_for_resources.end()) { |  | 
|  391     return i->second.content_settings_for_resources.find( |  | 
|  392         requested_setting)->second; |  | 
|  393   } |  | 
|  394  |  | 
|  395   // Match patterns starting with the most concrete pattern match. |  | 
|  396   for (std::string key = |  | 
|  397        std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { |  | 
|  398     HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); |  | 
|  399     if (i != off_the_record_settings_.end() && |  | 
|  400         i->second.content_settings_for_resources.find(requested_setting) != |  | 
|  401         i->second.content_settings_for_resources.end()) { |  | 
|  402       return i->second.content_settings_for_resources.find( |  | 
|  403           requested_setting)->second; |  | 
|  404     } |  | 
|  405  |  | 
|  406     i = host_content_settings_.find(key); |  | 
|  407     if (i != host_content_settings_.end() && |  | 
|  408         i->second.content_settings_for_resources.find(requested_setting) != |  | 
|  409         i->second.content_settings_for_resources.end()) { |  | 
|  410       return i->second.content_settings_for_resources.find( |  | 
|  411           requested_setting)->second; |  | 
|  412     } |  | 
|  413  |  | 
|  414     const size_t next_dot = |  | 
|  415         key.find('.', ContentSettingsPattern::kDomainWildcardLength); |  | 
|  416     if (next_dot == std::string::npos) |  | 
|  417       break; |  | 
|  418     key.erase(ContentSettingsPattern::kDomainWildcardLength, |  | 
|  419               next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |  | 
|  420   } |  | 
|  421  |  | 
|  422   return CONTENT_SETTING_DEFAULT; |  | 
|  423 } |  | 
|  424  |  | 
|  425 void PrefProvider::SetContentSetting( |  324 void PrefProvider::SetContentSetting( | 
|  426     const ContentSettingsPattern& requesting_pattern, |  325     const ContentSettingsPattern& requesting_pattern, | 
|  427     const ContentSettingsPattern& embedding_pattern, |  326     const ContentSettingsPattern& embedding_pattern, | 
|  428     ContentSettingsType content_type, |  327     ContentSettingsType content_type, | 
|  429     const ResourceIdentifier& resource_identifier, |  328     const ResourceIdentifier& resource_identifier, | 
|  430     ContentSetting setting) { |  329     ContentSetting setting) { | 
|  431   // Support for embedding_patterns is not implemented yet. |  330   // Support for embedding_patterns is not implemented yet. | 
|  432   DCHECK(requesting_pattern == embedding_pattern); |  331   DCHECK(requesting_pattern == embedding_pattern); | 
|  433  |  332  | 
|  434   DCHECK(kTypeNames[content_type] != NULL);  // Don't call this for Geolocation. |  333   DCHECK(kTypeNames[content_type] != NULL);  // Don't call this for Geolocation. | 
|  435   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  334   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  436   DCHECK_NE(RequiresResourceIdentifier(content_type), |  335   DCHECK_NE(RequiresResourceIdentifier(content_type), | 
|  437             resource_identifier.empty()); |  336             resource_identifier.empty()); | 
|  438   DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || |  337   DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 
|  439          setting != CONTENT_SETTING_ASK || |  338          setting != CONTENT_SETTING_ASK || | 
|  440          CommandLine::ForCurrentProcess()->HasSwitch( |  339          CommandLine::ForCurrentProcess()->HasSwitch( | 
|  441             switches::kEnableClickToPlay)); |  340             switches::kEnableClickToPlay)); | 
|  442  |  341  | 
|  443   const ContentSettingsPattern pattern( |  342   const ContentSettingsPattern pattern( | 
|  444       requesting_pattern.CanonicalizePattern()); |  343       requesting_pattern.CanonicalizePattern()); | 
|  445  |  344  | 
|  446   bool early_exit = false; |  345   bool early_exit = false; | 
|  447   std::string pattern_str(pattern.AsString()); |  346   std::string pattern_str(pattern.AsString()); | 
|  448   PrefService* prefs = NULL; |  347   PrefService* prefs = NULL; | 
|  449   DictionaryValue* all_settings_dictionary = NULL; |  348   DictionaryValue* all_settings_dictionary = NULL; | 
|  450   HostContentSettings* map_to_modify = &off_the_record_settings_; |  349  | 
|  451   if (!is_off_the_record_) { |  350   // Select content-settings-map to write to. | 
 |  351   HostContentSettings* map_to_modify = off_the_record_settings(); | 
 |  352   if (!is_off_the_record()) { | 
|  452     prefs = profile_->GetPrefs(); |  353     prefs = profile_->GetPrefs(); | 
|  453     all_settings_dictionary = |  354     all_settings_dictionary = | 
|  454         prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |  355         prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 
|  455     map_to_modify = &host_content_settings_; |  356  | 
 |  357     map_to_modify = host_content_settings(); | 
|  456   } |  358   } | 
|  457  |  359  | 
 |  360   // Update content-settings-map. | 
|  458   { |  361   { | 
|  459     base::AutoLock auto_lock(lock_); |  362     base::AutoLock auto_lock(lock()); | 
|  460     if (!map_to_modify->count(pattern_str)) |  363     if (!map_to_modify->count(pattern_str)) | 
|  461       (*map_to_modify)[pattern_str].content_settings = ContentSettings(); |  364       (*map_to_modify)[pattern_str].content_settings = ContentSettings(); | 
|  462     HostContentSettings::iterator i( |  365     HostContentSettings::iterator i( | 
|  463         map_to_modify->find(pattern_str)); |  366         map_to_modify->find(pattern_str)); | 
|  464     ContentSettings& settings = i->second.content_settings; |  367     ContentSettings& settings = i->second.content_settings; | 
|  465     if (RequiresResourceIdentifier(content_type)) { |  368     if (RequiresResourceIdentifier(content_type)) { | 
|  466       settings.settings[content_type] = CONTENT_SETTING_DEFAULT; |  369       settings.settings[content_type] = CONTENT_SETTING_DEFAULT; | 
|  467       if (setting != CONTENT_SETTING_DEFAULT) { |  370       if (setting != CONTENT_SETTING_DEFAULT) { | 
|  468         i->second.content_settings_for_resources[ |  371         i->second.content_settings_for_resources[ | 
|  469             ContentSettingsTypeResourceIdentifierPair(content_type, |  372             ContentSettingsTypeResourceIdentifierPair(content_type, | 
|  470             resource_identifier)] = setting; |  373             resource_identifier)] = setting; | 
|  471       } else { |  374       } else { | 
|  472         i->second.content_settings_for_resources.erase( |  375         i->second.content_settings_for_resources.erase( | 
|  473             ContentSettingsTypeResourceIdentifierPair(content_type, |  376             ContentSettingsTypeResourceIdentifierPair(content_type, | 
|  474             resource_identifier)); |  377             resource_identifier)); | 
|  475       } |  378       } | 
|  476     } else { |  379     } else { | 
|  477       settings.settings[content_type] = setting; |  380       settings.settings[content_type] = setting; | 
|  478     } |  381     } | 
|  479     if (AllDefault(i->second)) { |  382     if (AllDefault(i->second)) { | 
|  480       map_to_modify->erase(i); |  383       map_to_modify->erase(i); | 
|  481       if (all_settings_dictionary) |  384       if (all_settings_dictionary) | 
|  482         all_settings_dictionary->RemoveWithoutPathExpansion(pattern_str, NULL); |  385         all_settings_dictionary->RemoveWithoutPathExpansion(pattern_str, NULL); | 
|  483  |  386  | 
|  484       // We can't just return because |NotifyObservers()| needs to be called, |  387       // We can't just return because |NotifyObservers()| needs to be called, | 
|  485       // without |lock_| being held. |  388       // without lock() being held. | 
|  486       early_exit = true; |  389       early_exit = true; | 
|  487     } |  390     } | 
|  488   } |  391   } | 
|  489  |  392  | 
 |  393   // Update the content_settings preference. | 
|  490   if (!early_exit && all_settings_dictionary) { |  394   if (!early_exit && all_settings_dictionary) { | 
|  491     DictionaryValue* host_settings_dictionary = NULL; |  395     DictionaryValue* host_settings_dictionary = NULL; | 
|  492     bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |  396     bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 
|  493         pattern_str, &host_settings_dictionary); |  397         pattern_str, &host_settings_dictionary); | 
|  494     if (!found) { |  398     if (!found) { | 
|  495       host_settings_dictionary = new DictionaryValue; |  399       host_settings_dictionary = new DictionaryValue; | 
|  496       all_settings_dictionary->SetWithoutPathExpansion( |  400       all_settings_dictionary->SetWithoutPathExpansion( | 
|  497           pattern_str, host_settings_dictionary); |  401           pattern_str, host_settings_dictionary); | 
|  498       DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); |  402       DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); | 
|  499     } |  403     } | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|  519         host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, |  423         host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 
|  520                                                              NULL); |  424                                                              NULL); | 
|  521       } else { |  425       } else { | 
|  522         host_settings_dictionary->SetWithoutPathExpansion( |  426         host_settings_dictionary->SetWithoutPathExpansion( | 
|  523             dictionary_path, Value::CreateIntegerValue(setting)); |  427             dictionary_path, Value::CreateIntegerValue(setting)); | 
|  524       } |  428       } | 
|  525     } |  429     } | 
|  526   } |  430   } | 
|  527  |  431  | 
|  528   updating_preferences_ = true; |  432   updating_preferences_ = true; | 
|  529   if (!is_off_the_record_) |  433   if (!is_off_the_record()) | 
|  530     ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); |  434     ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | 
|  531   updating_preferences_ = false; |  435   updating_preferences_ = false; | 
|  532  |  436  | 
|  533   NotifyObservers(ContentSettingsDetails(pattern, content_type, "")); |  437   NotifyObservers(ContentSettingsDetails(pattern, content_type, "")); | 
|  534 } |  438 } | 
|  535  |  439  | 
|  536 void PrefProvider::GetAllContentSettingsRules( |  | 
|  537     ContentSettingsType content_type, |  | 
|  538     const ResourceIdentifier& resource_identifier, |  | 
|  539     Rules* content_setting_rules) const { |  | 
|  540   DCHECK(RequiresResourceIdentifier(content_type) != |  | 
|  541          resource_identifier.empty()); |  | 
|  542   DCHECK(content_setting_rules); |  | 
|  543   content_setting_rules->clear(); |  | 
|  544  |  | 
|  545   const HostContentSettings* map_to_return = |  | 
|  546       is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; |  | 
|  547   ContentSettingsTypeResourceIdentifierPair requested_setting( |  | 
|  548       content_type, resource_identifier); |  | 
|  549  |  | 
|  550   base::AutoLock auto_lock(lock_); |  | 
|  551   for (HostContentSettings::const_iterator i(map_to_return->begin()); |  | 
|  552        i != map_to_return->end(); ++i) { |  | 
|  553     ContentSetting setting; |  | 
|  554     if (RequiresResourceIdentifier(content_type)) { |  | 
|  555       if (i->second.content_settings_for_resources.find(requested_setting) != |  | 
|  556           i->second.content_settings_for_resources.end()) { |  | 
|  557         setting = i->second.content_settings_for_resources.find( |  | 
|  558             requested_setting)->second; |  | 
|  559       } else { |  | 
|  560         setting = CONTENT_SETTING_DEFAULT; |  | 
|  561       } |  | 
|  562     } else { |  | 
|  563      setting = i->second.content_settings.settings[content_type]; |  | 
|  564     } |  | 
|  565     if (setting != CONTENT_SETTING_DEFAULT) { |  | 
|  566       // Use of push_back() relies on the map iterator traversing in order of |  | 
|  567       // ascending keys. |  | 
|  568       content_setting_rules->push_back(Rule(ContentSettingsPattern(i->first), |  | 
|  569                                             ContentSettingsPattern(i->first), |  | 
|  570                                             setting)); |  | 
|  571     } |  | 
|  572   } |  | 
|  573 } |  | 
|  574  |  | 
|  575 void PrefProvider::ResetToDefaults() { |  440 void PrefProvider::ResetToDefaults() { | 
|  576   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  441   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  577  |  442  | 
|  578   { |  443   { | 
|  579     base::AutoLock auto_lock(lock_); |  444     base::AutoLock auto_lock(lock()); | 
|  580     host_content_settings_.clear(); |  445     host_content_settings()->clear(); | 
|  581     off_the_record_settings_.clear(); |  446     off_the_record_settings()->clear(); | 
|  582   } |  447   } | 
|  583  |  448  | 
|  584   if (!is_off_the_record_) { |  449   if (!is_off_the_record()) { | 
|  585     PrefService* prefs = profile_->GetPrefs(); |  450     PrefService* prefs = profile_->GetPrefs(); | 
|  586     updating_preferences_ = true; |  451     updating_preferences_ = true; | 
|  587     prefs->ClearPref(prefs::kContentSettingsPatterns); |  452     prefs->ClearPref(prefs::kContentSettingsPatterns); | 
|  588     updating_preferences_ = false; |  453     updating_preferences_ = false; | 
|  589   } |  454   } | 
|  590 } |  455 } | 
|  591  |  456  | 
|  592 void PrefProvider::ClearAllContentSettingsRules( |  457 void PrefProvider::ClearAllContentSettingsRules( | 
|  593     ContentSettingsType content_type) { |  458     ContentSettingsType content_type) { | 
|  594   DCHECK(kTypeNames[content_type] != NULL);  // Don't call this for Geolocation. |  459   DCHECK(kTypeNames[content_type] != NULL);  // Don't call this for Geolocation. | 
|  595  |  460  | 
|  596   PrefService* prefs = NULL; |  461   PrefService* prefs = NULL; | 
|  597   DictionaryValue* all_settings_dictionary = NULL; |  462   DictionaryValue* all_settings_dictionary = NULL; | 
|  598   HostContentSettings* map_to_modify = &off_the_record_settings_; |  463   HostContentSettings* map_to_modify = off_the_record_settings(); | 
|  599  |  464  | 
|  600   if (!is_off_the_record_) { |  465   if (!is_off_the_record()) { | 
|  601     prefs = profile_->GetPrefs(); |  466     prefs = profile_->GetPrefs(); | 
|  602     all_settings_dictionary = |  467     all_settings_dictionary = | 
|  603         prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |  468         prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 
|  604     map_to_modify = &host_content_settings_; |  469     map_to_modify = host_content_settings(); | 
|  605   } |  470   } | 
|  606  |  471  | 
|  607   { |  472   { | 
|  608     base::AutoLock auto_lock(lock_); |  473     base::AutoLock auto_lock(lock()); | 
|  609     for (HostContentSettings::iterator i(map_to_modify->begin()); |  474     for (HostContentSettings::iterator i(map_to_modify->begin()); | 
|  610          i != map_to_modify->end(); ) { |  475          i != map_to_modify->end(); ) { | 
|  611       if (RequiresResourceIdentifier(content_type) || |  476       if (RequiresResourceIdentifier(content_type) || | 
|  612           i->second.content_settings.settings[content_type] != |  477           i->second.content_settings.settings[content_type] != | 
|  613           CONTENT_SETTING_DEFAULT) { |  478               CONTENT_SETTING_DEFAULT) { | 
|  614         if (RequiresResourceIdentifier(content_type)) |  479         if (RequiresResourceIdentifier(content_type)) | 
|  615           i->second.content_settings_for_resources.clear(); |  480           i->second.content_settings_for_resources.clear(); | 
|  616         i->second.content_settings.settings[content_type] = |  481         i->second.content_settings.settings[content_type] = | 
|  617             CONTENT_SETTING_DEFAULT; |  482             CONTENT_SETTING_DEFAULT; | 
|  618         std::string host(i->first); |  483         std::string host(i->first); | 
|  619         if (AllDefault(i->second)) { |  484         if (AllDefault(i->second)) { | 
|  620           if (all_settings_dictionary) |  485           if (all_settings_dictionary) | 
|  621             all_settings_dictionary->RemoveWithoutPathExpansion(host, NULL); |  486             all_settings_dictionary->RemoveWithoutPathExpansion(host, NULL); | 
|  622           map_to_modify->erase(i++); |  487           map_to_modify->erase(i++); | 
|  623         } else if (all_settings_dictionary) { |  488         } else if (all_settings_dictionary) { | 
|  624           DictionaryValue* host_settings_dictionary; |  489           DictionaryValue* host_settings_dictionary; | 
|  625           bool found = |  490           bool found = | 
|  626               all_settings_dictionary->GetDictionaryWithoutPathExpansion( |  491               all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 
|  627                   host, &host_settings_dictionary); |  492                   host, &host_settings_dictionary); | 
|  628           DCHECK(found); |  493           DCHECK(found); | 
|  629           host_settings_dictionary->RemoveWithoutPathExpansion( |  494           host_settings_dictionary->RemoveWithoutPathExpansion( | 
|  630               kTypeNames[content_type], NULL); |  495               kTypeNames[content_type], NULL); | 
|  631           ++i; |  496           ++i; | 
|  632         } |  497         } | 
|  633       } else { |  498       } else { | 
|  634         ++i; |  499         ++i; | 
|  635       } |  500       } | 
|  636     } |  501     } | 
|  637   } |  502   } | 
|  638  |  503  | 
|  639   updating_preferences_ = true; |  504   updating_preferences_ = true; | 
|  640   if (!is_off_the_record_) |  505   if (!is_off_the_record()) | 
|  641     ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); |  506     ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | 
|  642   updating_preferences_ = false; |  507   updating_preferences_ = false; | 
|  643  |  508  | 
|  644   NotifyObservers( |  509   NotifyObservers( | 
|  645       ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); |  510       ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | 
|  646 } |  511 } | 
|  647  |  512  | 
|  648 void PrefProvider::Observe( |  513 void PrefProvider::Observe( | 
|  649     NotificationType type, |  514     NotificationType type, | 
|  650     const NotificationSource& source, |  515     const NotificationSource& source, | 
|  651     const NotificationDetails& details) { |  516     const NotificationDetails& details) { | 
|  652     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |  517     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  653  |  518  | 
|  654   if (type == NotificationType::PREF_CHANGED) { |  519   if (type == NotificationType::PREF_CHANGED) { | 
|  655     DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |  520     DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 
|  656     if (updating_preferences_) |  521     if (updating_preferences_) | 
|  657       return; |  522       return; | 
|  658  |  523  | 
|  659     std::string* name = Details<std::string>(details).ptr(); |  524     std::string* name = Details<std::string>(details).ptr(); | 
|  660     if (*name == prefs::kContentSettingsPatterns) { |  525     if (*name == prefs::kContentSettingsPatterns) { | 
|  661       ReadExceptions(true); |  526       ReadExceptions(true); | 
|  662     } else { |  527     } else { | 
|  663       NOTREACHED() << "Unexpected preference observed"; |  528       NOTREACHED() << "Unexpected preference observed"; | 
|  664       return; |  529       return; | 
|  665     } |  530     } | 
|  666  |  531  | 
|  667     if (!is_off_the_record_) { |  532     if (!is_off_the_record()) { | 
|  668       NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |  533       NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 
|  669                                              CONTENT_SETTINGS_TYPE_DEFAULT, |  534                                              CONTENT_SETTINGS_TYPE_DEFAULT, | 
|  670                                              "")); |  535                                              "")); | 
|  671     } |  536     } | 
|  672   } else if (type == NotificationType::PROFILE_DESTROYED) { |  537   } else if (type == NotificationType::PROFILE_DESTROYED) { | 
|  673     DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |  538     DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | 
|  674     UnregisterObservers(); |  539     UnregisterObservers(); | 
|  675   } else { |  540   } else { | 
|  676     NOTREACHED() << "Unexpected notification"; |  541     NOTREACHED() << "Unexpected notification"; | 
|  677   } |  542   } | 
|  678 } |  543 } | 
|  679  |  544  | 
|  680 PrefProvider::~PrefProvider() { |  545 PrefProvider::~PrefProvider() { | 
|  681   UnregisterObservers(); |  546   UnregisterObservers(); | 
|  682 } |  547 } | 
|  683  |  548  | 
|  684 // //////////////////////////////////////////////////////////////////////////// |  549 // //////////////////////////////////////////////////////////////////////////// | 
|  685 // Private |  550 // Private | 
|  686  |  551  | 
|  687 bool PrefProvider::RequiresResourceIdentifier( |  | 
|  688     ContentSettingsType content_type) const { |  | 
|  689   if (CommandLine::ForCurrentProcess()->HasSwitch( |  | 
|  690       switches::kEnableResourceContentSettings)) { |  | 
|  691     return kRequiresResourceIdentifier[content_type]; |  | 
|  692   } else { |  | 
|  693     return false; |  | 
|  694   } |  | 
|  695 } |  | 
|  696  |  | 
|  697 bool PrefProvider::AllDefault( |  | 
|  698     const ExtendedContentSettings& settings) const { |  | 
|  699   for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { |  | 
|  700     if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) |  | 
|  701       return false; |  | 
|  702   } |  | 
|  703   return settings.content_settings_for_resources.empty(); |  | 
|  704 } |  | 
|  705  |  | 
|  706 void PrefProvider::ReadExceptions(bool overwrite) { |  552 void PrefProvider::ReadExceptions(bool overwrite) { | 
|  707   base::AutoLock lock(lock_); |  553   base::AutoLock auto_lock(lock()); | 
|  708  |  554  | 
|  709   PrefService* prefs = profile_->GetPrefs(); |  555   PrefService* prefs = profile_->GetPrefs(); | 
|  710   DictionaryValue* all_settings_dictionary = |  556   DictionaryValue* all_settings_dictionary = | 
|  711       prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |  557       prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 
|  712  |  558  | 
|  713   if (overwrite) |  559   if (overwrite) | 
|  714     host_content_settings_.clear(); |  560     host_content_settings()->clear(); | 
|  715  |  561  | 
|  716   // Careful: The returned value could be NULL if the pref has never been set. |  562   // Careful: The returned value could be NULL if the pref has never been set. | 
|  717   if (all_settings_dictionary != NULL) { |  563   if (all_settings_dictionary != NULL) { | 
|  718     // Convert all Unicode patterns into punycode form, then read. |  564     // Convert all Unicode patterns into punycode form, then read. | 
|  719     CanonicalizeContentSettingsExceptions(all_settings_dictionary); |  565     CanonicalizeContentSettingsExceptions(all_settings_dictionary); | 
|  720  |  566  | 
|  721     for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |  567     for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 
|  722          i != all_settings_dictionary->end_keys(); ++i) { |  568          i != all_settings_dictionary->end_keys(); ++i) { | 
|  723       const std::string& pattern(*i); |  569       const std::string& pattern(*i); | 
|  724       if (!ContentSettingsPattern(pattern).IsValid()) |  570       if (!ContentSettingsPattern(pattern).IsValid()) | 
|  725         LOG(WARNING) << "Invalid pattern stored in content settings"; |  571         LOG(WARNING) << "Invalid pattern stored in content settings"; | 
|  726       DictionaryValue* pattern_settings_dictionary = NULL; |  572       DictionaryValue* pattern_settings_dictionary = NULL; | 
|  727       bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |  573       bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 
|  728           pattern, &pattern_settings_dictionary); |  574           pattern, &pattern_settings_dictionary); | 
|  729       DCHECK(found); |  575       DCHECK(found); | 
|  730       ContentSettings settings; |  576  | 
|  731       GetSettingsFromDictionary(pattern_settings_dictionary, &settings); |  577       ExtendedContentSettings extended_settings; | 
|  732       host_content_settings_[pattern].content_settings = settings; |  578       GetSettingsFromDictionary(pattern_settings_dictionary, | 
 |  579                                 &extended_settings.content_settings); | 
|  733       GetResourceSettingsFromDictionary( |  580       GetResourceSettingsFromDictionary( | 
|  734           pattern_settings_dictionary, |  581           pattern_settings_dictionary, | 
|  735           &host_content_settings_[pattern].content_settings_for_resources); |  582           &extended_settings.content_settings_for_resources); | 
 |  583  | 
 |  584       (*host_content_settings())[pattern] = extended_settings; | 
|  736     } |  585     } | 
|  737   } |  586   } | 
|  738 } |  587 } | 
|  739  |  588  | 
|  740 void PrefProvider::CanonicalizeContentSettingsExceptions( |  589 void PrefProvider::CanonicalizeContentSettingsExceptions( | 
|  741     DictionaryValue* all_settings_dictionary) { |  590     DictionaryValue* all_settings_dictionary) { | 
|  742   DCHECK(all_settings_dictionary); |  591   DCHECK(all_settings_dictionary); | 
|  743  |  592  | 
|  744   std::vector<std::string> remove_items; |  593   std::vector<std::string> remove_items; | 
|  745   std::vector<std::pair<std::string, std::string> > move_items; |  594   std::vector<std::pair<std::string, std::string> > move_items; | 
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  904       SetContentSetting(ContentSettingsPattern(host), |  753       SetContentSetting(ContentSettingsPattern(host), | 
|  905                         ContentSettingsPattern(host), |  754                         ContentSettingsPattern(host), | 
|  906                         CONTENT_SETTINGS_TYPE_POPUPS, |  755                         CONTENT_SETTINGS_TYPE_POPUPS, | 
|  907                         "", |  756                         "", | 
|  908                         CONTENT_SETTING_ALLOW); |  757                         CONTENT_SETTING_ALLOW); | 
|  909     } |  758     } | 
|  910     prefs->ClearPref(prefs::kPopupWhitelistedHosts); |  759     prefs->ClearPref(prefs::kPopupWhitelistedHosts); | 
|  911   } |  760   } | 
|  912 } |  761 } | 
|  913  |  762  | 
|  914 // //////////////////////////////////////////////////////////////////////////// |  | 
|  915 // LEGACY TBR |  | 
|  916 // |  | 
|  917  |  | 
|  918 ContentSettings PrefProvider::GetNonDefaultContentSettings( |  | 
|  919     const GURL& url) const { |  | 
|  920   base::AutoLock auto_lock(lock_); |  | 
|  921  |  | 
|  922   const std::string host(net::GetHostOrSpecFromURL(url)); |  | 
|  923   ContentSettings output; |  | 
|  924   for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |  | 
|  925     output.settings[j] = CONTENT_SETTING_DEFAULT; |  | 
|  926  |  | 
|  927   // Check for exact matches first. |  | 
|  928   HostContentSettings::const_iterator i(host_content_settings_.find(host)); |  | 
|  929   if (i != host_content_settings_.end()) |  | 
|  930     output = i->second.content_settings; |  | 
|  931  |  | 
|  932   // If this map is not for an off-the-record profile, these searches will never |  | 
|  933   // match. The additional off-the-record exceptions always overwrite the |  | 
|  934   // regular ones. |  | 
|  935   i = off_the_record_settings_.find(host); |  | 
|  936   if (i != off_the_record_settings_.end()) { |  | 
|  937     for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |  | 
|  938       if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) |  | 
|  939         output.settings[j] = i->second.content_settings.settings[j]; |  | 
|  940   } |  | 
|  941  |  | 
|  942   // Match patterns starting with the most concrete pattern match. |  | 
|  943   for (std::string key = |  | 
|  944        std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { |  | 
|  945     HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); |  | 
|  946     if (i != off_the_record_settings_.end()) { |  | 
|  947       for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |  | 
|  948         if (output.settings[j] == CONTENT_SETTING_DEFAULT) |  | 
|  949           output.settings[j] = i->second.content_settings.settings[j]; |  | 
|  950       } |  | 
|  951     } |  | 
|  952     i = host_content_settings_.find(key); |  | 
|  953     if (i != host_content_settings_.end()) { |  | 
|  954       for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |  | 
|  955         if (output.settings[j] == CONTENT_SETTING_DEFAULT) |  | 
|  956           output.settings[j] = i->second.content_settings.settings[j]; |  | 
|  957       } |  | 
|  958     } |  | 
|  959     const size_t next_dot = |  | 
|  960         key.find('.', ContentSettingsPattern::kDomainWildcardLength); |  | 
|  961     if (next_dot == std::string::npos) |  | 
|  962       break; |  | 
|  963     key.erase(ContentSettingsPattern::kDomainWildcardLength, |  | 
|  964               next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |  | 
|  965   } |  | 
|  966  |  | 
|  967   return output; |  | 
|  968 } |  | 
|  969  |  | 
|  970 }  // namespace content_settings |  763 }  // namespace content_settings | 
| OLD | NEW |