| 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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 ListValue* toolstrip_urls = prefs_->GetMutableList(kExtensionShelf); | 292 ListValue* toolstrip_urls = prefs_->GetMutableList(kExtensionShelf); |
| 293 toolstrip_urls->Clear(); | 293 toolstrip_urls->Clear(); |
| 294 for (size_t i = 0; i < urls.size(); ++i) { | 294 for (size_t i = 0; i < urls.size(); ++i) { |
| 295 GURL url = urls[i]; | 295 GURL url = urls[i]; |
| 296 toolstrip_urls->Append(new StringValue(url.spec())); | 296 toolstrip_urls->Append(new StringValue(url.spec())); |
| 297 } | 297 } |
| 298 prefs_->ScheduleSavePersistentPrefs(); | 298 prefs_->ScheduleSavePersistentPrefs(); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void ExtensionPrefs::OnExtensionInstalled(Extension* extension) { | 301 void ExtensionPrefs::OnExtensionInstalled(Extension* extension) { |
| 302 std::string id = extension->id(); | 302 const std::string& id = extension->id(); |
| 303 UpdateExtensionPref(id, kPrefState, | 303 UpdateExtensionPref(id, kPrefState, |
| 304 Value::CreateIntegerValue(Extension::ENABLED)); | 304 Value::CreateIntegerValue(Extension::ENABLED)); |
| 305 UpdateExtensionPref(id, kPrefLocation, | 305 UpdateExtensionPref(id, kPrefLocation, |
| 306 Value::CreateIntegerValue(extension->location())); | 306 Value::CreateIntegerValue(extension->location())); |
| 307 FilePath::StringType path = MakePathRelative(install_directory_, | 307 FilePath::StringType path = MakePathRelative(install_directory_, |
| 308 extension->path(), NULL); | 308 extension->path(), NULL); |
| 309 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); | 309 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); |
| 310 prefs_->SavePersistentPrefs(); | 310 prefs_->SavePersistentPrefs(); |
| 311 } | 311 } |
| 312 | 312 |
| 313 void ExtensionPrefs::OnExtensionUninstalled(const Extension* extension, | 313 void ExtensionPrefs::OnExtensionUninstalled(const Extension* extension, |
| 314 bool external_uninstall) { | 314 bool external_uninstall) { |
| 315 // For external extensions, we save a preference reminding ourself not to try | 315 // For external extensions, we save a preference reminding ourself not to try |
| 316 // and install the extension anymore (except when |external_uninstall| is | 316 // and install the extension anymore (except when |external_uninstall| is |
| 317 // true, which signifies that the registry key was deleted or the pref file | 317 // true, which signifies that the registry key was deleted or the pref file |
| 318 // no longer lists the extension). | 318 // no longer lists the extension). |
| 319 if (!external_uninstall && | 319 if (!external_uninstall && |
| 320 Extension::IsExternalLocation(extension->location())) { | 320 Extension::IsExternalLocation(extension->location())) { |
| 321 UpdateExtensionPref(extension->id(), kPrefState, | 321 UpdateExtensionPref(extension->id(), kPrefState, |
| 322 Value::CreateIntegerValue(Extension::KILLBIT)); | 322 Value::CreateIntegerValue(Extension::KILLBIT)); |
| 323 prefs_->ScheduleSavePersistentPrefs(); | 323 prefs_->ScheduleSavePersistentPrefs(); |
| 324 } else { | 324 } else { |
| 325 DeleteExtensionPrefs(extension->id()); | 325 DeleteExtensionPrefs(extension->id()); |
| 326 } | 326 } |
| 327 } | 327 } |
| 328 | 328 |
| 329 Extension::State ExtensionPrefs::GetExtensionState( |
| 330 const std::string& extension_id) { |
| 331 DictionaryValue* extension = GetExtensionPref(extension_id); |
| 332 |
| 333 // If the extension doesn't have a pref, it's a --load-extension. |
| 334 if (!extension) |
| 335 return Extension::ENABLED; |
| 336 |
| 337 int state = -1; |
| 338 if (!extension->GetInteger(kPrefState, &state) || |
| 339 state < 0 || state >= Extension::NUM_STATES) { |
| 340 LOG(ERROR) << "Bad or missing pref 'state' for extension '" |
| 341 << extension_id << "'"; |
| 342 return Extension::ENABLED; |
| 343 } |
| 344 return static_cast<Extension::State>(state); |
| 345 } |
| 346 |
| 347 void ExtensionPrefs::SetExtensionState(Extension* extension, |
| 348 Extension::State state) { |
| 349 UpdateExtensionPref(extension->id(), kPrefState, |
| 350 Value::CreateIntegerValue(state)); |
| 351 prefs_->SavePersistentPrefs(); |
| 352 } |
| 353 |
| 329 bool ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id, | 354 bool ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id, |
| 330 const std::wstring& key, | 355 const std::wstring& key, |
| 331 Value* data_value) { | 356 Value* data_value) { |
| 332 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); | 357 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); |
| 333 if (!extension->Set(key, data_value)) { | 358 if (!extension->Set(key, data_value)) { |
| 334 NOTREACHED() << L"Cannot modify key: '" << key.c_str() | 359 NOTREACHED() << "Cannot modify key: '" << key.c_str() |
| 335 << "' for extension: '" << extension_id.c_str() << "'"; | 360 << "' for extension: '" << extension_id.c_str() << "'"; |
| 336 return false; | 361 return false; |
| 337 } | 362 } |
| 338 return true; | 363 return true; |
| 339 } | 364 } |
| 340 | 365 |
| 341 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { | 366 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { |
| 342 std::wstring id = ASCIIToWide(extension_id); | 367 std::wstring id = ASCIIToWide(extension_id); |
| 343 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 368 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
| 344 if (dict->HasKey(id)) { | 369 if (dict->HasKey(id)) { |
| 345 dict->Remove(id, NULL); | 370 dict->Remove(id, NULL); |
| 346 prefs_->ScheduleSavePersistentPrefs(); | 371 prefs_->ScheduleSavePersistentPrefs(); |
| 347 } | 372 } |
| 348 } | 373 } |
| 349 | 374 |
| 350 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( | 375 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( |
| 351 const std::string& extension_id) { | 376 const std::string& extension_id) { |
| 352 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 377 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
| 353 DictionaryValue* extension = NULL; | 378 DictionaryValue* extension = NULL; |
| 354 std::wstring id = ASCIIToWide(extension_id); | 379 std::wstring id = ASCIIToWide(extension_id); |
| 355 if (!dict->GetDictionary(id, &extension)) { | 380 if (!dict->GetDictionary(id, &extension)) { |
| 356 // Extension pref does not exist, create it. | 381 // Extension pref does not exist, create it. |
| 357 extension = new DictionaryValue(); | 382 extension = new DictionaryValue(); |
| 358 dict->Set(id, extension); | 383 dict->Set(id, extension); |
| 359 } | 384 } |
| 360 return extension; | 385 return extension; |
| 361 } | 386 } |
| 387 |
| 388 DictionaryValue* ExtensionPrefs::GetExtensionPref( |
| 389 const std::string& extension_id) { |
| 390 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 391 DictionaryValue* extension = NULL; |
| 392 std::wstring id = ASCIIToWide(extension_id); |
| 393 dict->GetDictionary(id, &extension); |
| 394 return extension; |
| 395 } |
| OLD | NEW |