Index: chrome/browser/geolocation/geolocation_content_settings_map.cc |
diff --git a/chrome/browser/geolocation/geolocation_content_settings_map.cc b/chrome/browser/geolocation/geolocation_content_settings_map.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0164ea40380762a1bc54a5ff7ab8304d827948c9 |
--- /dev/null |
+++ b/chrome/browser/geolocation/geolocation_content_settings_map.cc |
@@ -0,0 +1,184 @@ |
+// 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. |
+ |
+#include "chrome/browser/geolocation/geolocation_content_settings_map.h" |
+ |
+#include "base/utf_string_conversions.h" |
+#include "chrome/browser/chrome_thread.h" |
+#include "chrome/browser/pref_service.h" |
+#include "chrome/browser/profile.h" |
+#include "chrome/common/notification_service.h" |
+#include "chrome/common/notification_type.h" |
+#include "chrome/common/pref_names.h" |
+#include "chrome/common/url_constants.h" |
+#include "net/base/dns_util.h" |
+#include "net/base/static_cookie_policy.h" |
+ |
+// static |
+const ContentSetting |
+ GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; |
+ |
+GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) |
+ : profile_(profile) { |
+ PrefService* prefs = profile_->GetPrefs(); |
+ |
+ // Read global defaults. |
+ int default_content_settting = |
Peter Kasting
2010/03/18 20:59:20
Nit: 3 ts
|
+ prefs->GetInteger(prefs::kGeolocationDefaultContentSetting); |
+ default_content_setting_ = ConvertToContentSetting(default_content_settting); |
+ if (default_content_setting_ == CONTENT_SETTING_DEFAULT) |
+ default_content_setting_ = kDefaultSetting; |
+ |
+ // Read exceptions. |
+ const DictionaryValue* all_settings_dictionary = |
+ prefs->GetDictionary(prefs::kGeolocationContentSettings); |
+ // Careful: The returned value could be NULL if the pref has never been set. |
+ if (all_settings_dictionary != NULL) { |
+ for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
+ i != all_settings_dictionary->end_keys(); ++i) { |
+ const std::wstring& wide_origin(*i); |
+ DictionaryValue* requesting_origin_settings_dictionary = NULL; |
+ bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
+ wide_origin, &requesting_origin_settings_dictionary); |
+ DCHECK(found); |
+ EmbedderSettings* embedder_settings = |
+ &requesting_origin_settings_[GURL(WideToUTF8(wide_origin))]; |
+ GetEmbedderSettingsFromDictionary(requesting_origin_settings_dictionary, |
+ embedder_settings); |
+ } |
+ } |
+} |
+ |
+// static |
+void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
+ prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, |
+ CONTENT_SETTING_ASK); |
+ prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); |
+} |
+ |
+ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
+ AutoLock auto_lock(lock_); |
+ return default_content_setting_; |
+} |
+ |
+ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
+ const GURL& embedder, const GURL& requesting_origin) const { |
Peter Kasting
2010/03/18 20:59:20
Nit: We should check that these are both valid.
|
+ AutoLock auto_lock(lock_); |
+ RequestingOriginSettings::const_iterator i( |
+ requesting_origin_settings_.find(requesting_origin.GetOrigin())); |
+ if (i != requesting_origin_settings_.end()) { |
+ EmbedderSettings::const_iterator j(i->second.find(embedder.GetOrigin())); |
+ if (j != i->second.end()) |
+ return j->second; |
+ EmbedderSettings::const_iterator any_embedder(i->second.find(GURL())); |
Peter Kasting
2010/03/18 20:59:20
We should only do this if embedder != requesting_o
|
+ if (any_embedder != i->second.end()) |
+ return any_embedder->second; |
+ } |
+ return default_content_setting_; |
+} |
+ |
+const GeolocationContentSettingsMap::RequestingOriginSettings& |
+ GeolocationContentSettingsMap::requesting_origin_settings() const { |
+ return requesting_origin_settings_; |
+} |
+ |
+void GeolocationContentSettingsMap::SetDefaultContentSetting( |
+ ContentSetting setting) { |
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
+ { |
+ AutoLock auto_lock(lock_); |
+ default_content_setting_ = setting == CONTENT_SETTING_DEFAULT ? |
Peter Kasting
2010/03/18 20:59:20
Nit: Enclosing the test in parens seems slightly m
|
+ kDefaultSetting : setting; |
+ } |
+ profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, |
+ default_content_setting_); |
+} |
+ |
+void GeolocationContentSettingsMap::SetContentSetting( |
+ const GURL& embedder, const GURL& requesting_origin, |
+ ContentSetting setting) { |
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
+ DCHECK(embedder.is_valid() || embedder.is_empty()); |
+ DCHECK(requesting_origin.is_valid()); |
+ |
+ GURL embedder_internal = embedder.GetOrigin(); |
+ GURL requesting_origin_internal = requesting_origin.GetOrigin(); |
+ std::wstring wide_requesting_origin(UTF8ToWide(requesting_origin.spec())); |
+ DictionaryValue* all_settings_dictionary = |
+ profile_->GetPrefs()->GetMutableDictionary( |
+ prefs::kGeolocationContentSettings); |
+ std::wstring embedder_dictionary_path(UTF8ToWide(embedder_internal.spec())); |
+ |
+ AutoLock auto_lock(lock_); |
+ DictionaryValue* requesting_origin_settings_dictionary; |
+ all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
+ wide_requesting_origin, &requesting_origin_settings_dictionary); |
+ if (setting == CONTENT_SETTING_DEFAULT) { |
+ if (!requesting_origin_settings_.count(requesting_origin_internal) || |
+ !requesting_origin_settings_[requesting_origin_internal].count( |
+ embedder_internal)) |
Peter Kasting
2010/03/18 20:59:20
Nit: I'd indent this 4
|
+ return; |
+ if (requesting_origin_settings_[requesting_origin_internal].size() == 1U) { |
Peter Kasting
2010/03/18 20:59:20
Nit: No need for U here.
|
+ all_settings_dictionary->RemoveWithoutPathExpansion( |
+ wide_requesting_origin, NULL); |
+ requesting_origin_settings_.erase(requesting_origin_internal); |
+ return; |
+ } |
+ requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
+ embedder_dictionary_path, NULL); |
+ requesting_origin_settings_[requesting_origin_internal].erase( |
+ embedder_internal); |
+ return; |
+ } |
+ |
+ if (!requesting_origin_settings_.count(requesting_origin_internal)) { |
+ requesting_origin_settings_dictionary = new DictionaryValue; |
+ all_settings_dictionary->SetWithoutPathExpansion( |
+ wide_requesting_origin, requesting_origin_settings_dictionary); |
+ } |
+ requesting_origin_settings_[requesting_origin_internal][embedder_internal] = |
+ setting; |
+ DCHECK(requesting_origin_settings_dictionary); |
+ requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
+ embedder_dictionary_path, Value::CreateIntegerValue(setting)); |
+} |
+ |
+void GeolocationContentSettingsMap::ResetToDefault() { |
Peter Kasting
2010/03/18 20:59:20
We also need a function to reset the settings for
|
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
+ |
+ { |
+ AutoLock auto_lock(lock_); |
+ default_content_setting_ = kDefaultSetting; |
+ requesting_origin_settings_.clear(); |
+ } |
+ |
+ PrefService* prefs = profile_->GetPrefs(); |
+ prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
+ prefs->ClearPref(prefs::kGeolocationContentSettings); |
+} |
+ |
+GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
+} |
+ |
+// static |
+void GeolocationContentSettingsMap::GetEmbedderSettingsFromDictionary( |
+ const DictionaryValue* dictionary, EmbedderSettings* embedder_settings) { |
+ for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
+ i != dictionary->end_keys(); ++i) { |
+ const std::wstring& target(*i); |
+ int setting = kDefaultSetting; |
+ bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); |
+ DCHECK(found); |
+ (*embedder_settings)[GURL(WideToUTF8(target))] = |
+ ConvertToContentSetting(setting); |
+ } |
+} |
+ |
+// static |
+ContentSetting GeolocationContentSettingsMap::ConvertToContentSetting( |
+ int content_setting) { |
+ DCHECK(content_setting >= CONTENT_SETTING_DEFAULT && |
+ content_setting < CONTENT_SETTING_NUM_SETTINGS); |
+ return static_cast<ContentSetting>(content_setting); |
+} |