summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/host_content_settings_map.cc231
-rw-r--r--chrome/browser/host_content_settings_map.h86
-rw-r--r--chrome/browser/host_content_settings_map_unittest.cc145
-rw-r--r--chrome/browser/net/chrome_url_request_context.h4
-rw-r--r--chrome/browser/profile.cc10
-rw-r--r--chrome/browser/profile.h2
-rw-r--r--chrome/browser/views/options/content_settings_window_view.h2
-rw-r--r--chrome/chrome_common.gypi3
-rw-r--r--chrome/common/content_permission_types.h71
-rw-r--r--chrome/common/content_settings.h32
-rw-r--r--chrome/common/content_settings_types.h23
-rw-r--r--chrome/test/testing_profile.h4
12 files changed, 316 insertions, 297 deletions
diff --git a/chrome/browser/host_content_settings_map.cc b/chrome/browser/host_content_settings_map.cc
index 24c64bb..595edbe 100644
--- a/chrome/browser/host_content_settings_map.cc
+++ b/chrome/browser/host_content_settings_map.cc
@@ -10,165 +10,178 @@
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
+// static
+const wchar_t* HostContentSettingsMap::kTypeNames[] = {
+ L"cookies",
+ L"images",
+ L"javascript",
+ L"plugins",
+ L"popups",
+};
+
HostContentSettingsMap::HostContentSettingsMap(Profile* profile)
: profile_(profile) {
- DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
+ DCHECK(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES);
+
+ const DictionaryValue* default_settings_dictionary =
+ profile_->GetPrefs()->GetDictionary(prefs::kDefaultContentSettings);
+ // Careful: The returned value could be NULL if the pref has never been set.
+ if (default_settings_dictionary != NULL) {
+ GetSettingsFromDictionary(default_settings_dictionary,
+ &default_content_settings_);
+ }
- const DictionaryValue* host_content_dictionary =
+ const DictionaryValue* all_settings_dictionary =
profile_->GetPrefs()->GetDictionary(prefs::kPerHostContentSettings);
// Careful: The returned value could be NULL if the pref has never been set.
- if (host_content_dictionary != NULL) {
- for (DictionaryValue::key_iterator i(host_content_dictionary->begin_keys());
- i != host_content_dictionary->end_keys(); ++i) {
+ if (all_settings_dictionary != NULL) {
+ for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
+ i != all_settings_dictionary->end_keys(); ++i) {
std::wstring wide_host(*i);
- int content_settings = 0;
- bool success = host_content_dictionary->GetIntegerWithoutPathExpansion(
- wide_host, &content_settings);
- DCHECK(success);
- host_content_settings_[WideToUTF8(wide_host)] =
- ContentPermissions::FromInteger(content_settings);
+ DictionaryValue* host_settings_dictionary = NULL;
+ bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
+ wide_host, &host_settings_dictionary);
+ DCHECK(found);
+ ContentSettings settings;
+ GetSettingsFromDictionary(host_settings_dictionary, &settings);
+ host_content_settings_[WideToUTF8(wide_host)] = settings;
}
}
- default_content_settings_ = ContentPermissions::FromInteger(
- profile_->GetPrefs()->GetInteger(prefs::kDefaultContentSettings));
}
// static
void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
+ prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings);
prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings);
- prefs->RegisterIntegerPref(prefs::kDefaultContentSettings,
- ContentPermissions::ToInteger(
- ContentPermissions()));
}
-void HostContentSettingsMap::ResetToDefaults() {
- default_content_settings_ = ContentPermissions();
- host_content_settings_.clear();
- profile_->GetPrefs()->ClearPref(prefs::kDefaultContentSettings);
- profile_->GetPrefs()->ClearPref(prefs::kPerHostContentSettings);
+ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
+ ContentSettingsType content_type) const {
+ AutoLock auto_lock(lock_);
+ return default_content_settings_.settings[content_type];
}
-HostContentSettingsMap::HostContentPermissions
- HostContentSettingsMap::GetAllPerHostContentPermissions(
+ContentSetting HostContentSettingsMap::GetContentSetting(
+ const std::string& host,
ContentSettingsType content_type) const {
- HostContentPermissions result;
- for (HostContentSettings::const_iterator i(host_content_settings_.begin());
- i != host_content_settings_.end(); ++i)
- if (i->second.permissions[content_type] !=
- CONTENT_PERMISSION_TYPE_DEFAULT)
- result[i->first] = i->second.permissions[content_type];
- return result;
+ AutoLock auto_lock(lock_);
+ HostContentSettings::const_iterator i(host_content_settings_.find(host));
+ return (i == host_content_settings_.end()) ?
+ CONTENT_SETTING_DEFAULT : i->second.settings[content_type];
}
-ContentPermissions HostContentSettingsMap::GetPerHostContentSettings(
+ContentSettings HostContentSettingsMap::GetContentSettings(
const std::string& host) const {
AutoLock auto_lock(lock_);
HostContentSettings::const_iterator i(host_content_settings_.find(host));
- ContentPermissions result = default_content_settings_;
- if (i != host_content_settings_.end()) {
- for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j)
- if (i->second.permissions[j] != CONTENT_PERMISSION_TYPE_DEFAULT)
- result.permissions[j] = i->second.permissions[j];
+ return (i == host_content_settings_.end()) ? ContentSettings() : i->second;
+}
+
+void HostContentSettingsMap::GetHostContentSettingsForOneType(
+ ContentSettingsType content_type,
+ HostContentSettingsForOneType* settings) const {
+ DCHECK(settings);
+ settings->clear();
+
+ AutoLock auto_lock(lock_);
+ for (HostContentSettings::const_iterator i(host_content_settings_.begin());
+ i != host_content_settings_.end(); ++i) {
+ ContentSetting setting = i->second.settings[content_type];
+ if (setting != CONTENT_SETTING_DEFAULT)
+ (*settings)[i->first] = setting;
}
- return result;
}
-bool HostContentSettingsMap::SetDefaultContentPermission(
- ContentSettingsType type, ContentPermissionType permission) {
+void HostContentSettingsMap::SetDefaultContentSetting(
+ ContentSettingsType content_type,
+ ContentSetting setting) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
- if (permission == CONTENT_PERMISSION_TYPE_DEFAULT)
- return false;
-
{
AutoLock auto_lock(lock_);
- default_content_settings_.permissions[type] = permission;
+ default_content_settings_.settings[content_type] = setting;
}
- // Persist new content settings if we're not off the record.
- if (!profile_->IsOffTheRecord()) {
- profile_->GetPrefs()->SetInteger(prefs::kDefaultContentSettings,
- ContentPermissions::ToInteger(default_content_settings_));
- }
- return true;
+ profile_->GetPrefs()->GetMutableDictionary(prefs::kDefaultContentSettings)->
+ SetWithoutPathExpansion(std::wstring(kTypeNames[content_type]),
+ Value::CreateIntegerValue(setting));
}
-void HostContentSettingsMap::SetPerHostContentPermission(
- const std::string& host, ContentSettingsType type,
- ContentPermissionType permission) {
-
+void HostContentSettingsMap::SetContentSetting(const std::string& host,
+ ContentSettingsType content_type,
+ ContentSetting setting) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
- if (host.empty())
- return;
-
- bool erase_entry = true;
- ContentPermissions permissions;
+ bool all_default = true;
{
AutoLock auto_lock(lock_);
- HostContentSettings::const_iterator i(host_content_settings_.find(host));
- if (i == host_content_settings_.end()) {
- for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j)
- permissions.permissions[j] = CONTENT_PERMISSION_TYPE_DEFAULT;
- } else {
- permissions = i->second;
+ if (!host_content_settings_.count(host))
+ host_content_settings_[host] = ContentSettings();
+ HostContentSettings::iterator i(host_content_settings_.find(host));
+ ContentSettings& settings = i->second;
+ settings.settings[content_type] = setting;
+ for (int i = 0; i < arraysize(settings.settings); ++i) {
+ if (settings.settings[i] != CONTENT_SETTING_DEFAULT) {
+ all_default = false;
+ break;
+ }
}
- permissions.permissions[type] = permission;
- for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j)
- if (permissions.permissions[j] != CONTENT_PERMISSION_TYPE_DEFAULT)
- erase_entry = false;
- if (erase_entry)
- host_content_settings_.erase(host);
- else
- host_content_settings_[host] = permissions;
+ if (all_default)
+ host_content_settings_.erase(i);
}
- // Persist new content settings if we're not off the record.
- if (!profile_->IsOffTheRecord()) {
- DictionaryValue* host_content_dictionary =
- profile_->GetPrefs()->GetMutableDictionary(
- prefs::kPerHostContentSettings);
- std::wstring wide_host(UTF8ToWide(host));
- if (erase_entry) {
- host_content_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
- } else {
- host_content_dictionary->SetWithoutPathExpansion(wide_host,
- Value::CreateIntegerValue(ContentPermissions::ToInteger(permissions)));
- }
+ std::wstring wide_host(UTF8ToWide(host));
+ DictionaryValue* all_settings_dictionary =
+ profile_->GetPrefs()->GetMutableDictionary(
+ prefs::kPerHostContentSettings);
+ if (all_default) {
+ all_settings_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
+ return;
}
+ DictionaryValue* host_settings_dictionary;
+ bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
+ wide_host, &host_settings_dictionary);
+ if (!found) {
+ host_settings_dictionary = new DictionaryValue;
+ all_settings_dictionary->SetWithoutPathExpansion(
+ wide_host, host_settings_dictionary);
+ }
+ host_settings_dictionary->SetWithoutPathExpansion(
+ std::wstring(kTypeNames[content_type]),
+ Value::CreateIntegerValue(setting));
}
-void HostContentSettingsMap::SetPerHostContentSettings(const std::string& host,
- const ContentPermissions& permissions) {
+void HostContentSettingsMap::ResetToDefaults() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
- if (host.empty())
- return;
-
- bool erase_entry = true;
-
- for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
- if (permissions.permissions[i] != CONTENT_PERMISSION_TYPE_DEFAULT)
- erase_entry = false;
{
AutoLock auto_lock(lock_);
- if (erase_entry)
- host_content_settings_.erase(host);
- else
- host_content_settings_[host] = permissions;
+ default_content_settings_ = ContentSettings();
+ host_content_settings_.clear();
}
- // Persist new content settings if we're not off the record.
- if (!profile_->IsOffTheRecord()) {
- DictionaryValue* host_content_dictionary =
- profile_->GetPrefs()->GetMutableDictionary(
- prefs::kPerHostContentSettings);
- std::wstring wide_host(UTF8ToWide(host));
- if (erase_entry) {
- host_content_dictionary->RemoveWithoutPathExpansion(wide_host, NULL);
- } else {
- host_content_dictionary->SetWithoutPathExpansion(wide_host,
- Value::CreateIntegerValue(ContentPermissions::ToInteger(permissions)));
+ profile_->GetPrefs()->ClearPref(prefs::kDefaultContentSettings);
+ profile_->GetPrefs()->ClearPref(prefs::kPerHostContentSettings);
+}
+
+HostContentSettingsMap::~HostContentSettingsMap() {
+}
+
+void HostContentSettingsMap::GetSettingsFromDictionary(
+ const DictionaryValue* dictionary,
+ ContentSettings* settings) {
+ for (DictionaryValue::key_iterator i(dictionary->begin_keys());
+ i != dictionary->end_keys(); ++i) {
+ std::wstring content_type(*i);
+ int setting = CONTENT_SETTING_DEFAULT;
+ bool found = dictionary->GetIntegerWithoutPathExpansion(content_type,
+ &setting);
+ DCHECK(found);
+ for (int type = 0; type < arraysize(kTypeNames); ++type) {
+ if (std::wstring(kTypeNames[type]) == content_type) {
+ settings->settings[type] = static_cast<ContentSetting>(setting);
+ break;
+ }
}
}
}
diff --git a/chrome/browser/host_content_settings_map.h b/chrome/browser/host_content_settings_map.h
index bed78b5..4150640 100644
--- a/chrome/browser/host_content_settings_map.h
+++ b/chrome/browser/host_content_settings_map.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// Maps hostnames to custom zoom levels. Written on the UI thread and read on
-// the IO thread. One instance per profile.
+// Maps hostnames to custom content settings. Written on the UI thread and read
+// on any thread. One instance per profile.
#ifndef CHROME_BROWSER_HOST_CONTENT_SETTINGS_MAP_H_
#define CHROME_BROWSER_HOST_CONTENT_SETTINGS_MAP_H_
@@ -13,83 +13,89 @@
#include "base/basictypes.h"
#include "base/lock.h"
-#include "chrome/common/content_permission_types.h"
+#include "base/ref_counted.h"
+#include "chrome/common/content_settings.h"
+class DictionaryValue;
class PrefService;
class Profile;
-class HostContentSettingsMap {
+class HostContentSettingsMap
+ : public base::RefCountedThreadSafe<HostContentSettingsMap> {
public:
- typedef std::map<std::string, ContentPermissionType> HostContentPermissions;
+ typedef std::map<std::string, ContentSetting> HostContentSettingsForOneType;
explicit HostContentSettingsMap(Profile* profile);
static void RegisterUserPrefs(PrefService* prefs);
- void ResetToDefaults();
-
- // Returns a map of all hostnames with per host content settings to
- // their respective settings where a given |content_type| differs
- // from CONTENT_PERMISSION_TYPE_DEFAULT
+ // Returns the default setting for a particular content type.
//
// This may be called on any thread.
- HostContentPermissions GetAllPerHostContentPermissions(
+ ContentSetting GetDefaultContentSetting(
ContentSettingsType content_type) const;
- // Returns the default ContentPermissions for a specific ContentSettingsType.
+ // Returns a single ContentSetting which applies to a given host.
//
// This may be called on any thread.
- ContentPermissionType GetDefaultContentPermission(
- ContentSettingsType type) const {
- return default_content_settings_.permissions[type];
- }
+ ContentSetting GetContentSetting(const std::string& host,
+ ContentSettingsType content_type) const;
- // Returns the ContentPermissions for a specific ContentSettingsType.
+ // Returns all ContentSettings which apply to a given host.
//
// This may be called on any thread.
- ContentPermissionType GetPerHostContentPermission(const std::string& host,
- ContentSettingsType type) const {
- return GetPerHostContentSettings(host).permissions[type];
- }
+ ContentSettings GetContentSettings(const std::string& host) const;
- // Returns the ContentPermissions which apply to a given host.
+ // For a given content type, returns all hosts with a non-default setting,
+ // mapped to their actual settings. |settings| must be a non-NULL outparam.
//
// This may be called on any thread.
- ContentPermissions GetPerHostContentSettings(const std::string& host) const;
+ void GetHostContentSettingsForOneType(
+ ContentSettingsType content_type,
+ HostContentSettingsForOneType* settings) const;
- // Sets the default ContentPermissions. Returns true on success.
+ // Sets the default setting for a particular content type.
//
// This should only be called on the UI thread.
- bool SetDefaultContentPermission(ContentSettingsType type,
- ContentPermissionType permission);
+ void SetDefaultContentSetting(ContentSettingsType content_type,
+ ContentSetting setting);
- // Sets per host ContentPermissions for a given host and CotentSettings. To
- // remove an exception for the host, set the permissions to
- // CONTENT_PERMISSIONS_TYPE_DEFAULT.
+ // Sets the blocking setting for a particular hostname and content type.
+ // Setting the value to CONTENT_SETTING_DEFAULT causes the default setting for
+ // that type to be used when loading pages from this host.
//
// This should only be called on the UI thread.
- void SetPerHostContentPermission(const std::string& host,
- ContentSettingsType type,
- ContentPermissionType permission);
+ void SetContentSetting(const std::string& host,
+ ContentSettingsType content_type,
+ ContentSetting setting);
- // Sets per host ContentPermissions for a given host.
+ // Resets all settings levels.
//
// This should only be called on the UI thread.
- void SetPerHostContentSettings(const std::string& host,
- const ContentPermissions& permissions);
+ void ResetToDefaults();
private:
- typedef std::map<std::string, ContentPermissions> HostContentSettings;
+ friend class base::RefCountedThreadSafe<HostContentSettingsMap>;
+
+ typedef std::map<std::string, ContentSettings> HostContentSettings;
+
+ // The names of the ContentSettingsType values, for use with dictionary prefs.
+ static const wchar_t* kTypeNames[];
+
+ ~HostContentSettingsMap();
+
+ // Sets the fields of |settings| based on the values in |dictionary|.
+ void GetSettingsFromDictionary(const DictionaryValue* dictionary,
+ ContentSettings* settings);
// The profile we're associated with.
Profile* profile_;
- // Copy of the pref data, so that we can read it on the IO thread.
+ // Copies of the pref data, so that we can read it on the IO thread.
+ ContentSettings default_content_settings_;
HostContentSettings host_content_settings_;
- ContentPermissions default_content_settings_;
- // Used around accesses to |host_content_settings_| to guarantee thread
- // safety.
+ // Used around accesses to the settings objects to guarantee thread safety.
mutable Lock lock_;
DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap);
diff --git a/chrome/browser/host_content_settings_map_unittest.cc b/chrome/browser/host_content_settings_map_unittest.cc
index 721a68e..b509129 100644
--- a/chrome/browser/host_content_settings_map_unittest.cc
+++ b/chrome/browser/host_content_settings_map_unittest.cc
@@ -10,6 +10,16 @@
namespace {
+bool SettingsEqual(const ContentSettings& settings1,
+ const ContentSettings& settings2) {
+ for (int i = CONTENT_SETTINGS_FIRST_TYPE; i < CONTENT_SETTINGS_NUM_TYPES;
+ ++i) {
+ if (settings1.settings[i] != settings2.settings[i])
+ return false;
+ }
+ return true;
+}
+
class HostContentSettingsMapTest : public testing::Test {
public:
HostContentSettingsMapTest()
@@ -25,71 +35,82 @@ TEST_F(HostContentSettingsMapTest, DefaultValues) {
HostContentSettingsMap* host_content_settings_map =
profile.GetHostContentSettingsMap();
- // Check setting of default permissions.
- ContentPermissions perm;
- ASSERT_TRUE(host_content_settings_map->SetDefaultContentPermission(
- CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_PERMISSION_TYPE_BLOCK));
- ASSERT_TRUE(host_content_settings_map->SetDefaultContentPermission(
- CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_PERMISSION_TYPE_ASK));
- ASSERT_FALSE(host_content_settings_map->SetDefaultContentPermission(
- CONTENT_SETTINGS_TYPE_JAVASCRIPT,
- CONTENT_PERMISSION_TYPE_DEFAULT));
- ASSERT_TRUE(host_content_settings_map->SetDefaultContentPermission(
- CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_PERMISSION_TYPE_ALLOW));
-
- // Check per host permissions returned.
- perm.permissions[CONTENT_SETTINGS_TYPE_IMAGES] =
- CONTENT_PERMISSION_TYPE_DEFAULT;
- perm.permissions[CONTENT_SETTINGS_TYPE_PLUGINS] =
- CONTENT_PERMISSION_TYPE_ALLOW;
- perm.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
- CONTENT_PERMISSION_TYPE_ALLOW;
- host_content_settings_map->SetPerHostContentSettings("example.com", perm);
- perm = host_content_settings_map->GetPerHostContentSettings("example.com");
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_BLOCK,
- perm.permissions[CONTENT_SETTINGS_TYPE_IMAGES]);
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_ALLOW,
- perm.permissions[CONTENT_SETTINGS_TYPE_PLUGINS]);
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_ALLOW,
- perm.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT]);
- perm = host_content_settings_map->GetPerHostContentSettings("example.org");
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_BLOCK,
- perm.permissions[CONTENT_SETTINGS_TYPE_IMAGES]);
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_ASK,
- perm.permissions[CONTENT_SETTINGS_TYPE_PLUGINS]);
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_ALLOW,
- perm.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT]);
-
- // Check returning settings for a given resource.
- HostContentSettingsMap::HostContentPermissions permissions;
- permissions = host_content_settings_map->GetAllPerHostContentPermissions(
- CONTENT_SETTINGS_TYPE_IMAGES);
- EXPECT_EQ(0U, permissions.size());
- permissions = host_content_settings_map->GetAllPerHostContentPermissions(
- CONTENT_SETTINGS_TYPE_PLUGINS);
- EXPECT_EQ(1U, permissions.size());
- EXPECT_EQ("example.com", permissions.begin()->first);
- EXPECT_EQ(CONTENT_PERMISSION_TYPE_ALLOW, permissions.begin()->second);
-}
+ // Check setting defaults.
+ EXPECT_EQ(CONTENT_SETTING_DEFAULT,
+ host_content_settings_map->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_JAVASCRIPT));
+ host_content_settings_map->SetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
+ EXPECT_EQ(CONTENT_SETTING_BLOCK,
+ host_content_settings_map->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_IMAGES));
+ host_content_settings_map->SetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ASK);
+ EXPECT_EQ(CONTENT_SETTING_ASK,
+ host_content_settings_map->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_PLUGINS));
+ host_content_settings_map->SetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_POPUPS, CONTENT_SETTING_ALLOW);
+ EXPECT_EQ(CONTENT_SETTING_ALLOW,
+ host_content_settings_map->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_POPUPS));
+ host_content_settings_map->ResetToDefaults();
+ EXPECT_EQ(CONTENT_SETTING_DEFAULT,
+ host_content_settings_map->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_POPUPS));
-TEST_F(HostContentSettingsMapTest, ContentPermissions) {
- ContentPermissions perm;
- perm.permissions[CONTENT_SETTINGS_TYPE_IMAGES] =
- CONTENT_PERMISSION_TYPE_BLOCK;
- perm.permissions[CONTENT_SETTINGS_TYPE_PLUGINS] =
- CONTENT_PERMISSION_TYPE_ASK;
- perm.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
- CONTENT_PERMISSION_TYPE_ALLOW;
+ // Check returning individual settings.
+ std::string host("example.com");
+ EXPECT_EQ(CONTENT_SETTING_DEFAULT,
+ host_content_settings_map->GetContentSetting(
+ host, CONTENT_SETTINGS_TYPE_IMAGES));
+ host_content_settings_map->SetContentSetting(host,
+ CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT);
+ EXPECT_EQ(CONTENT_SETTING_DEFAULT,
+ host_content_settings_map->GetContentSetting(
+ host, CONTENT_SETTINGS_TYPE_IMAGES));
+ host_content_settings_map->SetContentSetting(host,
+ CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
+ EXPECT_EQ(CONTENT_SETTING_BLOCK,
+ host_content_settings_map->GetContentSetting(
+ host, CONTENT_SETTINGS_TYPE_IMAGES));
- int serialized = ContentPermissions::ToInteger(perm);
- ContentPermissions result = ContentPermissions::FromInteger(serialized);
+ // Check returning all settings for a host.
+ ContentSettings desired_settings;
+ host_content_settings_map->SetContentSetting(host,
+ CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_DEFAULT);
+ host_content_settings_map->SetContentSetting(host,
+ CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_ALLOW);
+ desired_settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
+ CONTENT_SETTING_ALLOW;
+ host_content_settings_map->SetContentSetting(host,
+ CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_ALLOW);
+ desired_settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] =
+ CONTENT_SETTING_ALLOW;
+ ContentSettings settings =
+ host_content_settings_map->GetContentSettings(host);
+ EXPECT_TRUE(SettingsEqual(desired_settings, settings));
- EXPECT_EQ(perm.permissions[CONTENT_SETTINGS_TYPE_IMAGES],
- result.permissions[CONTENT_SETTINGS_TYPE_IMAGES]);
- EXPECT_EQ(perm.permissions[CONTENT_SETTINGS_TYPE_PLUGINS],
- result.permissions[CONTENT_SETTINGS_TYPE_PLUGINS]);
- EXPECT_EQ(perm.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT],
- result.permissions[CONTENT_SETTINGS_TYPE_JAVASCRIPT]);
+ // Check returning all hosts for a setting.
+ std::string host2("example.org");
+ host_content_settings_map->SetContentSetting(host2,
+ CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK);
+ host_content_settings_map->SetContentSetting(host2,
+ CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK);
+ HostContentSettingsMap::HostContentSettingsForOneType host_settings;
+ host_content_settings_map->GetHostContentSettingsForOneType(
+ CONTENT_SETTINGS_TYPE_IMAGES, &host_settings);
+ EXPECT_EQ(1U, host_settings.size());
+ host_content_settings_map->GetHostContentSettingsForOneType(
+ CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings);
+ EXPECT_EQ(2U, host_settings.size());
+ host_content_settings_map->GetHostContentSettingsForOneType(
+ CONTENT_SETTINGS_TYPE_POPUPS, &host_settings);
+ EXPECT_EQ(0U, host_settings.size());
+ host_content_settings_map->ResetToDefaults();
+ host_content_settings_map->GetHostContentSettingsForOneType(
+ CONTENT_SETTINGS_TYPE_PLUGINS, &host_settings);
+ EXPECT_EQ(0U, host_settings.size());
}
} // namespace
diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h
index 7c60a56..700609a 100644
--- a/chrome/browser/net/chrome_url_request_context.h
+++ b/chrome/browser/net/chrome_url_request_context.h
@@ -205,7 +205,7 @@ class ChromeURLRequestContext : public URLRequestContext {
FilePath user_script_dir_path_;
scoped_refptr<ChromeAppCacheService> appcache_service_;
- HostContentSettingsMap* host_content_settings_map_;
+ scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
scoped_refptr<HostZoomMap> host_zoom_map_;
scoped_refptr<Blacklist> privacy_blacklist_;
@@ -375,7 +375,7 @@ class ChromeURLRequestContextFactory {
// TODO(aa): I think this can go away now as we no longer support standalone
// user scripts.
FilePath user_script_dir_path_;
- HostContentSettingsMap* host_content_settings_map_;
+ scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
scoped_refptr<HostZoomMap> host_zoom_map_;
scoped_refptr<Blacklist> privacy_blacklist_;
net::TransportSecurityState* transport_security_state_;
diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc
index 597b286..1fa0697 100644
--- a/chrome/browser/profile.cc
+++ b/chrome/browser/profile.cc
@@ -401,11 +401,7 @@ class OffTheRecordProfileImpl : public Profile,
}
virtual HostContentSettingsMap* GetHostContentSettingsMap() {
- // Need to use a separate map from the normal one to avoid persisting
- // content setting changes in OTR mode.
- if (!host_content_settings_map_.get())
- host_content_settings_map_.reset(new HostContentSettingsMap(this));
- return host_content_settings_map_.get();
+ return profile_->GetHostContentSettingsMap();
}
virtual HostZoomMap* GetHostZoomMap() {
@@ -532,8 +528,6 @@ class OffTheRecordProfileImpl : public Profile,
scoped_refptr<ChromeURLRequestContextGetter> extensions_request_context_;
- scoped_ptr<HostContentSettingsMap> host_content_settings_map_;
-
// The download manager that only stores downloaded items in memory.
scoped_refptr<DownloadManager> download_manager_;
@@ -961,7 +955,7 @@ net::SSLConfigService* ProfileImpl::GetSSLConfigService() {
HostContentSettingsMap* ProfileImpl::GetHostContentSettingsMap() {
if (!host_content_settings_map_.get())
- host_content_settings_map_.reset(new HostContentSettingsMap(this));
+ host_content_settings_map_ = new HostContentSettingsMap(this);
return host_content_settings_map_.get();
}
diff --git a/chrome/browser/profile.h b/chrome/browser/profile.h
index 946e8b6..3a5ff60 100644
--- a/chrome/browser/profile.h
+++ b/chrome/browser/profile.h
@@ -548,7 +548,7 @@ class ProfileImpl : public Profile,
scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_;
- scoped_ptr<HostContentSettingsMap> host_content_settings_map_;
+ scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
scoped_refptr<HostZoomMap> host_zoom_map_;
scoped_refptr<Blacklist> privacy_blacklist_;
scoped_refptr<DownloadManager> download_manager_;
diff --git a/chrome/browser/views/options/content_settings_window_view.h b/chrome/browser/views/options/content_settings_window_view.h
index 164e580..eed2431 100644
--- a/chrome/browser/views/options/content_settings_window_view.h
+++ b/chrome/browser/views/options/content_settings_window_view.h
@@ -5,7 +5,7 @@
#ifndef CHROME_BROWSER_VIEWS_OPTIONS_CONTENT_SETTINGS_WINDOW_VIEW_H_
#define CHROME_BROWSER_VIEWS_OPTIONS_CONTENT_SETTINGS_WINDOW_VIEW_H_
-#include "chrome/common/content_permission_types.h"
+#include "chrome/common/content_settings_types.h"
#include "chrome/common/pref_member.h"
#include "views/controls/tabbed_pane/tabbed_pane.h"
#include "views/view.h"
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi
index 7a51c8f..f3705b6c 100644
--- a/chrome/chrome_common.gypi
+++ b/chrome/chrome_common.gypi
@@ -201,7 +201,8 @@
'common/command_buffer_messages.h',
'common/command_buffer_messages_internal.h',
'common/common_glue.cc',
- 'common/content_permission_types.h'
+ 'common/content_settings.h'
+ 'common/content_settings_types.h',
'common/css_colors.h',
'common/db_message_filter.cc',
'common/db_message_filter.h',
diff --git a/chrome/common/content_permission_types.h b/chrome/common/content_permission_types.h
deleted file mode 100644
index 2b5acec..0000000
--- a/chrome/common/content_permission_types.h
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_COMMON_CONTENT_PERMISSION_TYPES_H_
-#define CHROME_COMMON_CONTENT_PERMISSION_TYPES_H_
-
-// Indicates different permission levels that can be assigned for a given
-// resource.
-enum ContentPermissionType {
- CONTENT_PERMISSION_FIRST_TYPE = 0,
- CONTENT_PERMISSION_TYPE_BLOCK = CONTENT_PERMISSION_FIRST_TYPE,
- CONTENT_PERMISSION_TYPE_ALLOW,
- CONTENT_PERMISSION_TYPE_ASK,
- CONTENT_PERMISSION_TYPE_DEFAULT,
- CONTENT_PERMISSION_NUM_TYPES
-};
-
-// A particular type of content to care about. We give the user various types
-// of controls over each of these.
-enum ContentSettingsType {
- // "DEFAULT" is only used as an argument to the Content Settings Window
- // opener; there it means "whatever was last shown".
- CONTENT_SETTINGS_TYPE_DEFAULT = -1,
- CONTENT_SETTINGS_FIRST_TYPE = 0,
- CONTENT_SETTINGS_TYPE_COOKIES = CONTENT_SETTINGS_FIRST_TYPE,
- CONTENT_SETTINGS_TYPE_IMAGES,
- CONTENT_SETTINGS_TYPE_JAVASCRIPT,
- CONTENT_SETTINGS_TYPE_PLUGINS,
- CONTENT_SETTINGS_TYPE_POPUPS,
- CONTENT_SETTINGS_NUM_TYPES
-};
-
-// Aggregates the permissions for the different content types.
-struct ContentPermissions {
- ContentPermissionType permissions[CONTENT_SETTINGS_NUM_TYPES];
-
- ContentPermissions() {
- for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
- permissions[i] = CONTENT_PERMISSION_TYPE_ALLOW;
- }
-
- // Converts the struct into an integer used for storing in preferences and
- // sending over IPC.
- static int ToInteger(const ContentPermissions& permissions) {
- int result = 0;
- for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
- result = ((result << CONTENT_PERMISSION_NUM_TYPES) +
- (1 << permissions.permissions[i]));
- return result;
- }
-
- // Converts an integer as stored in preferences or sent over IPC to
- // ContentPermissions.
- static ContentPermissions FromInteger(int settings) {
- ContentPermissions result;
- for (int i = CONTENT_SETTINGS_NUM_TYPES - 1; i >= 0; --i) {
- int bitmask = (settings & ((1 << CONTENT_PERMISSION_NUM_TYPES) - 1));
- settings >>= CONTENT_PERMISSION_NUM_TYPES;
- int value = 0;
- while (bitmask != 1) {
- value++;
- bitmask >>= 1;
- }
- result.permissions[i] = static_cast<ContentPermissionType>(value);
- }
- return result;
- }
-};
-
-#endif // CHROME_COMMON_CONTENT_PERMISSION_TYPES_H_
diff --git a/chrome/common/content_settings.h b/chrome/common/content_settings.h
new file mode 100644
index 0000000..59d58c8
--- /dev/null
+++ b/chrome/common/content_settings.h
@@ -0,0 +1,32 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_CONTENT_SETTINGS_H_
+#define CHROME_COMMON_CONTENT_SETTINGS_H_
+
+#include "chrome/common/content_settings_types.h"
+
+// Different settings that can be assigned for a particular content type. We
+// give the user the ability to set these on a global and per-host basis.
+enum ContentSetting {
+ CONTENT_SETTING_FIRST_SETTING = 0,
+ CONTENT_SETTING_DEFAULT = CONTENT_SETTING_FIRST_SETTING,
+ CONTENT_SETTING_ALLOW,
+ CONTENT_SETTING_BLOCK,
+ CONTENT_SETTING_ASK,
+ CONTENT_SETTING_NUM_SETTINGS
+};
+
+// Aggregates the permissions for the different content types.
+struct ContentSettings {
+ ContentSettings() {
+ for (int i = CONTENT_SETTINGS_FIRST_TYPE; i < CONTENT_SETTINGS_NUM_TYPES;
+ ++i)
+ settings[i] = CONTENT_SETTING_DEFAULT;
+ }
+
+ ContentSetting settings[CONTENT_SETTINGS_NUM_TYPES];
+};
+
+#endif // CHROME_COMMON_CONTENT_SETTINGS_H_
diff --git a/chrome/common/content_settings_types.h b/chrome/common/content_settings_types.h
new file mode 100644
index 0000000..2026bac
--- /dev/null
+++ b/chrome/common/content_settings_types.h
@@ -0,0 +1,23 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_CONTENT_SETTINGS_TYPES_H_
+#define CHROME_COMMON_CONTENT_SETTINGS_TYPES_H_
+
+// A particular type of content to care about. We give the user various types
+// of controls over each of these.
+enum ContentSettingsType {
+ // "DEFAULT" is only used as an argument to the Content Settings Window
+ // opener; there it means "whatever was last shown".
+ CONTENT_SETTINGS_TYPE_DEFAULT = -1,
+ CONTENT_SETTINGS_FIRST_TYPE = 0,
+ CONTENT_SETTINGS_TYPE_COOKIES = CONTENT_SETTINGS_FIRST_TYPE,
+ CONTENT_SETTINGS_TYPE_IMAGES,
+ CONTENT_SETTINGS_TYPE_JAVASCRIPT,
+ CONTENT_SETTINGS_TYPE_PLUGINS,
+ CONTENT_SETTINGS_TYPE_POPUPS,
+ CONTENT_SETTINGS_NUM_TYPES
+};
+
+#endif // CHROME_COMMON_CONTENT_SETTINGS_TYPES_H_
diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h
index 86aa2dd..59049b4 100644
--- a/chrome/test/testing_profile.h
+++ b/chrome/test/testing_profile.h
@@ -180,7 +180,7 @@ class TestingProfile : public Profile {
virtual Blacklist* GetPrivacyBlacklist() { return NULL; }
virtual HostContentSettingsMap* GetHostContentSettingsMap() {
if (!host_content_settings_map_.get())
- host_content_settings_map_.reset(new HostContentSettingsMap(this));
+ host_content_settings_map_ = new HostContentSettingsMap(this);
return host_content_settings_map_.get();
}
virtual HostZoomMap* GetHostZoomMap() { return NULL; }
@@ -300,7 +300,7 @@ class TestingProfile : public Profile {
// WebKitContext, lazily initialized by GetWebKitContext().
scoped_refptr<WebKitContext> webkit_context_;
- scoped_ptr<HostContentSettingsMap> host_content_settings_map_;
+ scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
};
// A profile that derives from another profile. This does not actually