OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/chrome_thread.h" |
| 9 #include "chrome/browser/pref_service.h" |
| 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/common/notification_service.h" |
| 12 #include "chrome/common/notification_type.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/common/url_constants.h" |
| 15 #include "net/base/dns_util.h" |
| 16 #include "net/base/static_cookie_policy.h" |
| 17 |
| 18 // static |
| 19 const ContentSetting |
| 20 GeolocationContentSettingsMap::kDefaultSetting = CONTENT_SETTING_ASK; |
| 21 |
| 22 GeolocationContentSettingsMap::GeolocationContentSettingsMap(Profile* profile) |
| 23 : profile_(profile) { |
| 24 PrefService* prefs = profile_->GetPrefs(); |
| 25 |
| 26 // Read global defaults. |
| 27 default_content_setting_ = IntToContentSetting( |
| 28 prefs->GetInteger(prefs::kGeolocationDefaultContentSetting)); |
| 29 if (default_content_setting_ == CONTENT_SETTING_DEFAULT) |
| 30 default_content_setting_ = kDefaultSetting; |
| 31 |
| 32 // Read exceptions. |
| 33 const DictionaryValue* all_settings_dictionary = |
| 34 prefs->GetDictionary(prefs::kGeolocationContentSettings); |
| 35 // Careful: The returned value could be NULL if the pref has never been set. |
| 36 if (all_settings_dictionary != NULL) { |
| 37 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 38 i != all_settings_dictionary->end_keys(); ++i) { |
| 39 const std::wstring& wide_origin(*i); |
| 40 DictionaryValue* requesting_origin_settings_dictionary = NULL; |
| 41 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 42 wide_origin, &requesting_origin_settings_dictionary); |
| 43 DCHECK(found); |
| 44 OneOriginSettings* requesting_origin_settings = |
| 45 &content_settings_[GURL(WideToUTF8(wide_origin))]; |
| 46 GetOneOriginSettingsFromDictionary(requesting_origin_settings_dictionary, |
| 47 requesting_origin_settings); |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 // static |
| 53 void GeolocationContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 54 prefs->RegisterIntegerPref(prefs::kGeolocationDefaultContentSetting, |
| 55 CONTENT_SETTING_ASK); |
| 56 prefs->RegisterDictionaryPref(prefs::kGeolocationContentSettings); |
| 57 } |
| 58 |
| 59 ContentSetting GeolocationContentSettingsMap::GetDefaultContentSetting() const { |
| 60 AutoLock auto_lock(lock_); |
| 61 return default_content_setting_; |
| 62 } |
| 63 |
| 64 ContentSetting GeolocationContentSettingsMap::GetContentSetting( |
| 65 const GURL& requesting_url, |
| 66 const GURL& embedding_url) const { |
| 67 DCHECK(requesting_url.is_valid() && embedding_url.is_valid()); |
| 68 GURL requesting_origin(requesting_url.GetOrigin()); |
| 69 GURL embedding_origin(embedding_url.GetOrigin()); |
| 70 DCHECK(requesting_origin.is_valid() && embedding_origin.is_valid()); |
| 71 AutoLock auto_lock(lock_); |
| 72 AllOriginsSettings::const_iterator i(content_settings_.find( |
| 73 requesting_origin)); |
| 74 if (i != content_settings_.end()) { |
| 75 OneOriginSettings::const_iterator j(i->second.find(embedding_origin)); |
| 76 if (j != i->second.end()) |
| 77 return j->second; |
| 78 if (requesting_origin != embedding_origin) { |
| 79 OneOriginSettings::const_iterator any_embedder(i->second.find(GURL())); |
| 80 if (any_embedder != i->second.end()) |
| 81 return any_embedder->second; |
| 82 } |
| 83 } |
| 84 return default_content_setting_; |
| 85 } |
| 86 |
| 87 GeolocationContentSettingsMap::AllOriginsSettings |
| 88 GeolocationContentSettingsMap::GetAllOriginsSettings() const { |
| 89 AutoLock auto_lock(lock_); |
| 90 return content_settings_; |
| 91 } |
| 92 |
| 93 void GeolocationContentSettingsMap::SetDefaultContentSetting( |
| 94 ContentSetting setting) { |
| 95 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 96 { |
| 97 AutoLock auto_lock(lock_); |
| 98 default_content_setting_ = |
| 99 (setting == CONTENT_SETTING_DEFAULT) ? kDefaultSetting : setting; |
| 100 } |
| 101 profile_->GetPrefs()->SetInteger(prefs::kGeolocationDefaultContentSetting, |
| 102 default_content_setting_); |
| 103 } |
| 104 |
| 105 void GeolocationContentSettingsMap::SetContentSetting( |
| 106 const GURL& requesting_url, |
| 107 const GURL& embedding_url, |
| 108 ContentSetting setting) { |
| 109 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 110 DCHECK(requesting_url.is_valid() && |
| 111 (embedding_url.is_valid() || embedding_url.is_empty())); |
| 112 GURL requesting_origin(requesting_url.GetOrigin()); |
| 113 GURL embedding_origin(embedding_url.GetOrigin()); |
| 114 DCHECK(requesting_origin.is_valid() && |
| 115 (embedding_origin.is_valid() || embedding_url.is_empty())); |
| 116 std::wstring wide_requesting_origin(UTF8ToWide(requesting_origin.spec())); |
| 117 std::wstring wide_embedding_origin(UTF8ToWide(embedding_origin.spec())); |
| 118 DictionaryValue* all_settings_dictionary = |
| 119 profile_->GetPrefs()->GetMutableDictionary( |
| 120 prefs::kGeolocationContentSettings); |
| 121 |
| 122 AutoLock auto_lock(lock_); |
| 123 DictionaryValue* requesting_origin_settings_dictionary; |
| 124 all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 125 wide_requesting_origin, &requesting_origin_settings_dictionary); |
| 126 if (setting == CONTENT_SETTING_DEFAULT) { |
| 127 if (!content_settings_.count(requesting_origin) || |
| 128 !content_settings_[requesting_origin].count(embedding_origin)) |
| 129 return; |
| 130 if (content_settings_[requesting_origin].size() == 1) { |
| 131 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 132 wide_requesting_origin, NULL); |
| 133 content_settings_.erase(requesting_origin); |
| 134 return; |
| 135 } |
| 136 requesting_origin_settings_dictionary->RemoveWithoutPathExpansion( |
| 137 wide_embedding_origin, NULL); |
| 138 content_settings_[requesting_origin].erase(embedding_origin); |
| 139 return; |
| 140 } |
| 141 |
| 142 if (!content_settings_.count(requesting_origin)) { |
| 143 requesting_origin_settings_dictionary = new DictionaryValue; |
| 144 all_settings_dictionary->SetWithoutPathExpansion( |
| 145 wide_requesting_origin, requesting_origin_settings_dictionary); |
| 146 } |
| 147 content_settings_[requesting_origin][embedding_origin] = setting; |
| 148 DCHECK(requesting_origin_settings_dictionary); |
| 149 requesting_origin_settings_dictionary->SetWithoutPathExpansion( |
| 150 wide_embedding_origin, Value::CreateIntegerValue(setting)); |
| 151 } |
| 152 |
| 153 void GeolocationContentSettingsMap::ClearOneRequestingOrigin( |
| 154 const GURL& requesting_origin) { |
| 155 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 156 DCHECK(requesting_origin.is_valid()); |
| 157 |
| 158 { |
| 159 AutoLock auto_lock(lock_); |
| 160 AllOriginsSettings::iterator i(content_settings_.find(requesting_origin)); |
| 161 if (i == content_settings_.end()) |
| 162 return; |
| 163 content_settings_.erase(i); |
| 164 } |
| 165 |
| 166 DictionaryValue* all_settings_dictionary = |
| 167 profile_->GetPrefs()->GetMutableDictionary( |
| 168 prefs::kGeolocationContentSettings); |
| 169 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 170 UTF8ToWide(requesting_origin.spec()), NULL); |
| 171 } |
| 172 |
| 173 void GeolocationContentSettingsMap::ResetToDefault() { |
| 174 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 175 |
| 176 { |
| 177 AutoLock auto_lock(lock_); |
| 178 default_content_setting_ = kDefaultSetting; |
| 179 content_settings_.clear(); |
| 180 } |
| 181 |
| 182 PrefService* prefs = profile_->GetPrefs(); |
| 183 prefs->ClearPref(prefs::kGeolocationDefaultContentSetting); |
| 184 prefs->ClearPref(prefs::kGeolocationContentSettings); |
| 185 } |
| 186 |
| 187 GeolocationContentSettingsMap::~GeolocationContentSettingsMap() { |
| 188 } |
| 189 |
| 190 // static |
| 191 void GeolocationContentSettingsMap::GetOneOriginSettingsFromDictionary( |
| 192 const DictionaryValue* dictionary, |
| 193 OneOriginSettings* one_origin_settings) { |
| 194 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); |
| 195 i != dictionary->end_keys(); ++i) { |
| 196 const std::wstring& target(*i); |
| 197 int setting = kDefaultSetting; |
| 198 bool found = dictionary->GetIntegerWithoutPathExpansion(target, &setting); |
| 199 DCHECK(found); |
| 200 (*one_origin_settings)[GURL(WideToUTF8(target))] = |
| 201 IntToContentSetting(setting); |
| 202 } |
| 203 } |
OLD | NEW |