| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_prefs.h" | 5 #include "chrome/browser/extensions/extension_prefs.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 InstalledExtensions::InstalledExtensions(ExtensionPrefs* prefs) { | 43 InstalledExtensions::InstalledExtensions(ExtensionPrefs* prefs) { |
| 44 extension_data_.reset(prefs->CopyCurrentExtensions()); | 44 extension_data_.reset(prefs->CopyCurrentExtensions()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 InstalledExtensions::~InstalledExtensions() { | 47 InstalledExtensions::~InstalledExtensions() { |
| 48 } | 48 } |
| 49 | 49 |
| 50 void InstalledExtensions::VisitInstalledExtensions( | 50 void InstalledExtensions::VisitInstalledExtensions( |
| 51 InstalledExtensions::Callback *callback) { | 51 InstalledExtensions::Callback *callback) { |
| 52 scoped_ptr<InstalledExtensions::Callback> cleanup(callback); | 52 scoped_ptr<InstalledExtensions::Callback> cleanup(callback); |
| 53 DictionaryValue::key_iterator extension_id = extension_data_->begin_keys(); | 53 for (DictionaryValue::key_iterator extension_id( |
| 54 for (; extension_id != extension_data_->end_keys(); ++extension_id) { | 54 extension_data_->begin_keys()); |
| 55 extension_id != extension_data_->end_keys(); ++extension_id) { |
| 55 DictionaryValue* ext; | 56 DictionaryValue* ext; |
| 56 if (!extension_data_->GetDictionary(*extension_id, &ext)) { | 57 if (!extension_data_->GetDictionaryWithoutPathExpansion(*extension_id, |
| 58 &ext)) { |
| 57 LOG(WARNING) << "Invalid pref for extension " << *extension_id; | 59 LOG(WARNING) << "Invalid pref for extension " << *extension_id; |
| 58 NOTREACHED(); | 60 NOTREACHED(); |
| 59 continue; | 61 continue; |
| 60 } | 62 } |
| 61 if (ext->HasKey(kPrefBlacklist)) { | 63 if (ext->HasKey(kPrefBlacklist)) { |
| 62 bool is_blacklisted = false; | 64 bool is_blacklisted = false; |
| 63 if (!ext->GetBoolean(kPrefBlacklist, &is_blacklisted)) { | 65 if (!ext->GetBoolean(kPrefBlacklist, &is_blacklisted)) { |
| 64 NOTREACHED() << "Invalid blacklist pref:" << *extension_id; | 66 NOTREACHED() << "Invalid blacklist pref:" << *extension_id; |
| 65 continue; | 67 continue; |
| 66 } | 68 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 parent.value().length()); | 120 parent.value().length()); |
| 119 if (FilePath::IsSeparator(retval[0])) | 121 if (FilePath::IsSeparator(retval[0])) |
| 120 return retval.substr(1); | 122 return retval.substr(1); |
| 121 else | 123 else |
| 122 return retval; | 124 return retval; |
| 123 } | 125 } |
| 124 | 126 |
| 125 void ExtensionPrefs::MakePathsRelative() { | 127 void ExtensionPrefs::MakePathsRelative() { |
| 126 bool dirty = false; | 128 bool dirty = false; |
| 127 const DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 129 const DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
| 128 if (!dict || dict->GetSize() == 0) | 130 if (!dict || dict->empty()) |
| 129 return; | 131 return; |
| 130 | 132 |
| 131 for (DictionaryValue::key_iterator i = dict->begin_keys(); | 133 for (DictionaryValue::key_iterator i = dict->begin_keys(); |
| 132 i != dict->end_keys(); ++i) { | 134 i != dict->end_keys(); ++i) { |
| 133 DictionaryValue* extension_dict; | 135 DictionaryValue* extension_dict; |
| 134 if (!dict->GetDictionary(*i, &extension_dict)) | 136 if (!dict->GetDictionaryWithoutPathExpansion(*i, &extension_dict)) |
| 135 continue; | 137 continue; |
| 136 FilePath::StringType path_string; | 138 FilePath::StringType path_string; |
| 137 if (!extension_dict->GetString(kPrefPath, &path_string)) | 139 if (!extension_dict->GetString(kPrefPath, &path_string)) |
| 138 continue; | 140 continue; |
| 139 FilePath path(path_string); | 141 FilePath path(path_string); |
| 140 if (path.IsAbsolute()) { | 142 if (path.IsAbsolute()) { |
| 141 extension_dict->SetString(kPrefPath, | 143 extension_dict->SetString(kPrefPath, |
| 142 MakePathRelative(install_directory_, path, &dirty)); | 144 MakePathRelative(install_directory_, path, &dirty)); |
| 143 } | 145 } |
| 144 } | 146 } |
| 145 if (dirty) | 147 if (dirty) |
| 146 prefs_->ScheduleSavePersistentPrefs(); | 148 prefs_->ScheduleSavePersistentPrefs(); |
| 147 } | 149 } |
| 148 | 150 |
| 149 void ExtensionPrefs::MakePathsAbsolute(DictionaryValue* dict) { | 151 void ExtensionPrefs::MakePathsAbsolute(DictionaryValue* dict) { |
| 150 if (!dict || dict->GetSize() == 0) | 152 if (!dict || dict->empty()) |
| 151 return; | 153 return; |
| 152 | 154 |
| 153 for (DictionaryValue::key_iterator i = dict->begin_keys(); | 155 for (DictionaryValue::key_iterator i = dict->begin_keys(); |
| 154 i != dict->end_keys(); ++i) { | 156 i != dict->end_keys(); ++i) { |
| 155 DictionaryValue* extension_dict; | 157 DictionaryValue* extension_dict; |
| 156 if (!dict->GetDictionary(*i, &extension_dict)) { | 158 if (!dict->GetDictionaryWithoutPathExpansion(*i, &extension_dict)) { |
| 157 NOTREACHED(); | 159 NOTREACHED(); |
| 158 continue; | 160 continue; |
| 159 } | 161 } |
| 160 FilePath::StringType path_string; | 162 FilePath::StringType path_string; |
| 161 if (!extension_dict->GetString(kPrefPath, &path_string)) { | 163 if (!extension_dict->GetString(kPrefPath, &path_string)) { |
| 162 if (!IsBlacklistBitSet(extension_dict)) { | 164 if (!IsBlacklistBitSet(extension_dict)) { |
| 163 // We expect the kPrefPath for non-blacklisted extensions. | 165 // We expect the kPrefPath for non-blacklisted extensions. |
| 164 NOTREACHED(); | 166 NOTREACHED(); |
| 165 } | 167 } |
| 166 continue; | 168 continue; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 } | 206 } |
| 205 return IsBlacklistBitSet(ext); | 207 return IsBlacklistBitSet(ext); |
| 206 } | 208 } |
| 207 | 209 |
| 208 void ExtensionPrefs::UpdateBlacklist( | 210 void ExtensionPrefs::UpdateBlacklist( |
| 209 const std::set<std::string>& blacklist_set) { | 211 const std::set<std::string>& blacklist_set) { |
| 210 std::vector<std::string> remove_pref_ids; | 212 std::vector<std::string> remove_pref_ids; |
| 211 std::set<std::string> used_id_set; | 213 std::set<std::string> used_id_set; |
| 212 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 214 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 213 DCHECK(extensions); | 215 DCHECK(extensions); |
| 214 DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 216 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); |
| 215 for (; extension_id != extensions->end_keys(); ++extension_id) { | 217 extension_id != extensions->end_keys(); ++extension_id) { |
| 216 DictionaryValue* ext; | 218 DictionaryValue* ext; |
| 217 std::string id = WideToASCII(*extension_id); | 219 if (!extensions->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { |
| 218 if (!extensions->GetDictionary(*extension_id, &ext)) { | |
| 219 NOTREACHED() << "Invalid pref for extension " << *extension_id; | 220 NOTREACHED() << "Invalid pref for extension " << *extension_id; |
| 220 continue; | 221 continue; |
| 221 } | 222 } |
| 223 std::string id = WideToASCII(*extension_id); |
| 222 if (blacklist_set.find(id) == blacklist_set.end()) { | 224 if (blacklist_set.find(id) == blacklist_set.end()) { |
| 223 if (!IsBlacklistBitSet(ext)) { | 225 if (!IsBlacklistBitSet(ext)) { |
| 224 // This extension is not in blacklist. And it was not blacklisted | 226 // This extension is not in blacklist. And it was not blacklisted |
| 225 // before. | 227 // before. |
| 226 continue; | 228 continue; |
| 227 } else { | 229 } else { |
| 228 if (ext->GetSize() == 1) { | 230 if (ext->size() == 1) { |
| 229 // We should remove the entry if the only flag here is blacklist. | 231 // We should remove the entry if the only flag here is blacklist. |
| 230 remove_pref_ids.push_back(id); | 232 remove_pref_ids.push_back(id); |
| 231 } else { | 233 } else { |
| 232 // Remove the blacklist bit. | 234 // Remove the blacklist bit. |
| 233 ext->Remove(kPrefBlacklist, NULL); | 235 ext->Remove(kPrefBlacklist, NULL); |
| 234 } | 236 } |
| 235 } | 237 } |
| 236 } else { | 238 } else { |
| 237 if (!IsBlacklistBitSet(ext)) { | 239 if (!IsBlacklistBitSet(ext)) { |
| 238 // Only set the blacklist if it was not set. | 240 // Only set the blacklist if it was not set. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 254 for (unsigned int i = 0; i < remove_pref_ids.size(); ++i) { | 256 for (unsigned int i = 0; i < remove_pref_ids.size(); ++i) { |
| 255 DeleteExtensionPrefs(remove_pref_ids[i]); | 257 DeleteExtensionPrefs(remove_pref_ids[i]); |
| 256 } | 258 } |
| 257 // Update persistent registry | 259 // Update persistent registry |
| 258 prefs_->ScheduleSavePersistentPrefs(); | 260 prefs_->ScheduleSavePersistentPrefs(); |
| 259 return; | 261 return; |
| 260 } | 262 } |
| 261 | 263 |
| 262 void ExtensionPrefs::GetKilledExtensionIds(std::set<std::string>* killed_ids) { | 264 void ExtensionPrefs::GetKilledExtensionIds(std::set<std::string>* killed_ids) { |
| 263 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 265 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 264 if (!dict || dict->GetSize() == 0) | 266 if (!dict || dict->empty()) |
| 265 return; | 267 return; |
| 266 | 268 |
| 267 for (DictionaryValue::key_iterator i = dict->begin_keys(); | 269 for (DictionaryValue::key_iterator i = dict->begin_keys(); |
| 268 i != dict->end_keys(); ++i) { | 270 i != dict->end_keys(); ++i) { |
| 269 std::wstring key_name = *i; | 271 std::wstring key_name = *i; |
| 270 if (!Extension::IdIsValid(WideToASCII(key_name))) { | 272 if (!Extension::IdIsValid(WideToASCII(key_name))) { |
| 271 LOG(WARNING) << "Invalid external extension ID encountered: " | 273 LOG(WARNING) << "Invalid external extension ID encountered: " |
| 272 << WideToASCII(key_name); | 274 << WideToASCII(key_name); |
| 273 continue; | 275 continue; |
| 274 } | 276 } |
| 275 | 277 |
| 276 DictionaryValue* extension = NULL; | 278 DictionaryValue* extension; |
| 277 if (!dict->GetDictionary(key_name, &extension)) { | 279 if (!dict->GetDictionary(key_name, &extension)) { |
| 278 NOTREACHED(); | 280 NOTREACHED(); |
| 279 continue; | 281 continue; |
| 280 } | 282 } |
| 281 | 283 |
| 282 // Check to see if the extension has been killed. | 284 // Check to see if the extension has been killed. |
| 283 int state; | 285 int state; |
| 284 if (extension->GetInteger(kPrefState, &state) && | 286 if (extension->GetInteger(kPrefState, &state) && |
| 285 state == static_cast<int>(Extension::KILLBIT)) { | 287 state == static_cast<int>(Extension::KILLBIT)) { |
| 286 StringToLowerASCII(&key_name); | 288 StringToLowerASCII(&key_name); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 return version; | 386 return version; |
| 385 } | 387 } |
| 386 | 388 |
| 387 void ExtensionPrefs::MigrateToPrefs(Extension* extension) { | 389 void ExtensionPrefs::MigrateToPrefs(Extension* extension) { |
| 388 UpdateExtensionPref(extension->id(), kPrefManifest, | 390 UpdateExtensionPref(extension->id(), kPrefManifest, |
| 389 extension->manifest_value()->DeepCopy()); | 391 extension->manifest_value()->DeepCopy()); |
| 390 } | 392 } |
| 391 | 393 |
| 392 FilePath ExtensionPrefs::GetExtensionPath(const std::string& extension_id) { | 394 FilePath ExtensionPrefs::GetExtensionPath(const std::string& extension_id) { |
| 393 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 395 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 394 if (!dict || dict->GetSize() == 0) | 396 if (!dict || dict->empty()) |
| 395 return FilePath(); | 397 return FilePath(); |
| 396 | 398 |
| 397 std::wstring path; | 399 std::wstring path; |
| 398 if (!dict->GetString(ASCIIToWide(extension_id) + L"." + kPrefPath, &path)) | 400 if (!dict->GetString(ASCIIToWide(extension_id) + L"." + kPrefPath, &path)) |
| 399 return FilePath(); | 401 return FilePath(); |
| 400 | 402 |
| 401 return install_directory_.Append(FilePath::FromWStringHack(path)); | 403 return install_directory_.Append(FilePath::FromWStringHack(path)); |
| 402 } | 404 } |
| 403 | 405 |
| 404 bool ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id, | 406 void ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id, |
| 405 const std::wstring& key, | 407 const std::wstring& key, |
| 406 Value* data_value) { | 408 Value* data_value) { |
| 407 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); | 409 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); |
| 408 if (!extension->Set(key, data_value)) { | 410 extension->Set(key, data_value); |
| 409 NOTREACHED() << "Cannot modify key: '" << key.c_str() | |
| 410 << "' for extension: '" << extension_id.c_str() << "'"; | |
| 411 return false; | |
| 412 } | |
| 413 return true; | |
| 414 } | 411 } |
| 415 | 412 |
| 416 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { | 413 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { |
| 417 std::wstring id = ASCIIToWide(extension_id); | 414 std::wstring id = ASCIIToWide(extension_id); |
| 418 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 415 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
| 419 if (dict->HasKey(id)) { | 416 if (dict->HasKey(id)) { |
| 420 dict->Remove(id, NULL); | 417 dict->Remove(id, NULL); |
| 421 prefs_->ScheduleSavePersistentPrefs(); | 418 prefs_->ScheduleSavePersistentPrefs(); |
| 422 } | 419 } |
| 423 } | 420 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 436 } | 433 } |
| 437 | 434 |
| 438 DictionaryValue* ExtensionPrefs::GetExtensionPref( | 435 DictionaryValue* ExtensionPrefs::GetExtensionPref( |
| 439 const std::string& extension_id) { | 436 const std::string& extension_id) { |
| 440 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 437 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 441 DictionaryValue* extension = NULL; | 438 DictionaryValue* extension = NULL; |
| 442 std::wstring id = ASCIIToWide(extension_id); | 439 std::wstring id = ASCIIToWide(extension_id); |
| 443 dict->GetDictionary(id, &extension); | 440 dict->GetDictionary(id, &extension); |
| 444 return extension; | 441 return extension; |
| 445 } | 442 } |
| OLD | NEW |