| 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 "base/string_util.h" | 5 #include "base/string_util.h" |
| 6 #include "chrome/browser/extensions/extension_prefs.h" | 6 #include "chrome/browser/extensions/extension_prefs.h" |
| 7 #include "chrome/common/extensions/extension.h" | 7 #include "chrome/common/extensions/extension.h" |
| 8 #include "chrome/common/pref_names.h" | 8 #include "chrome/common/pref_names.h" |
| 9 | 9 |
| 10 using base::Time; | 10 using base::Time; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 // The dictionary containing the extension's manifest. | 29 // The dictionary containing the extension's manifest. |
| 30 const wchar_t kPrefManifest[] = L"manifest"; | 30 const wchar_t kPrefManifest[] = L"manifest"; |
| 31 | 31 |
| 32 // The version number. | 32 // The version number. |
| 33 const wchar_t kPrefVersion[] = L"manifest.version"; | 33 const wchar_t kPrefVersion[] = L"manifest.version"; |
| 34 | 34 |
| 35 // Indicates if an extension is blacklisted: | 35 // Indicates if an extension is blacklisted: |
| 36 const wchar_t kPrefBlacklist[] = L"blacklist"; | 36 const wchar_t kPrefBlacklist[] = L"blacklist"; |
| 37 | 37 |
| 38 // Indicates whether to show an install warning when the user enables. | 38 // Indicates whether to show an install warning when the user enables. |
| 39 const wchar_t kShowInstallWarning[] = L"install_warning_on_enable"; | 39 const wchar_t kExtensionDidEscalatePermissions[] = L"install_warning_on_enable"; |
| 40 | 40 |
| 41 // A preference that tracks extension shelf configuration. This is a list | 41 // A preference that tracks extension shelf configuration. This is a list |
| 42 // object read from the Preferences file, containing a list of toolstrip URLs. | 42 // object read from the Preferences file, containing a list of toolstrip URLs. |
| 43 const wchar_t kExtensionShelf[] = L"extensions.shelf"; | 43 const wchar_t kExtensionShelf[] = L"extensions.shelf"; |
| 44 | 44 |
| 45 // A preference that tracks browser action toolbar configuration. This is a list | 45 // A preference that tracks browser action toolbar configuration. This is a list |
| 46 // object stored in the Preferences file. The extensions are stored by ID. | 46 // object stored in the Preferences file. The extensions are stored by ID. |
| 47 const wchar_t kExtensionToolbar[] = L"extensions.toolbar"; | 47 const wchar_t kExtensionToolbar[] = L"extensions.toolbar"; |
| 48 | 48 |
| 49 // The key for a serialized Time value indicating the start of the day (from the | 49 // The key for a serialized Time value indicating the start of the day (from the |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { | 181 bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) { |
| 182 return ReadBooleanFromPref(ext, kPrefBlacklist); | 182 return ReadBooleanFromPref(ext, kPrefBlacklist); |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { | 185 bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) { |
| 186 return ReadExtensionPrefBoolean(extension_id, kPrefBlacklist); | 186 return ReadExtensionPrefBoolean(extension_id, kPrefBlacklist); |
| 187 } | 187 } |
| 188 | 188 |
| 189 bool ExtensionPrefs::DidExtensionEscalatePermissions( | 189 bool ExtensionPrefs::DidExtensionEscalatePermissions( |
| 190 const std::string& extension_id) { | 190 const std::string& extension_id) { |
| 191 return ReadExtensionPrefBoolean(extension_id, kShowInstallWarning); | 191 return ReadExtensionPrefBoolean(extension_id, |
| 192 kExtensionDidEscalatePermissions); |
| 193 } |
| 194 |
| 195 void ExtensionPrefs::SetDidExtensionEscalatePermissions( |
| 196 Extension* extension, bool did_escalate) { |
| 197 UpdateExtensionPref(extension->id(), kExtensionDidEscalatePermissions, |
| 198 Value::CreateBooleanValue(did_escalate)); |
| 199 prefs_->SavePersistentPrefs(); |
| 192 } | 200 } |
| 193 | 201 |
| 194 void ExtensionPrefs::UpdateBlacklist( | 202 void ExtensionPrefs::UpdateBlacklist( |
| 195 const std::set<std::string>& blacklist_set) { | 203 const std::set<std::string>& blacklist_set) { |
| 196 std::vector<std::string> remove_pref_ids; | 204 std::vector<std::string> remove_pref_ids; |
| 197 std::set<std::string> used_id_set; | 205 std::set<std::string> used_id_set; |
| 198 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); | 206 const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref); |
| 199 | 207 |
| 200 if (extensions) { | 208 if (extensions) { |
| 201 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); | 209 for (DictionaryValue::key_iterator extension_id = extensions->begin_keys(); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 return static_cast<Extension::State>(state); | 419 return static_cast<Extension::State>(state); |
| 412 } | 420 } |
| 413 | 421 |
| 414 void ExtensionPrefs::SetExtensionState(Extension* extension, | 422 void ExtensionPrefs::SetExtensionState(Extension* extension, |
| 415 Extension::State state) { | 423 Extension::State state) { |
| 416 UpdateExtensionPref(extension->id(), kPrefState, | 424 UpdateExtensionPref(extension->id(), kPrefState, |
| 417 Value::CreateIntegerValue(state)); | 425 Value::CreateIntegerValue(state)); |
| 418 prefs_->SavePersistentPrefs(); | 426 prefs_->SavePersistentPrefs(); |
| 419 } | 427 } |
| 420 | 428 |
| 421 void ExtensionPrefs::SetShowInstallWarningOnEnable( | |
| 422 Extension* extension, bool require) { | |
| 423 UpdateExtensionPref(extension->id(), kShowInstallWarning, | |
| 424 Value::CreateBooleanValue(require)); | |
| 425 prefs_->SavePersistentPrefs(); | |
| 426 } | |
| 427 | |
| 428 std::string ExtensionPrefs::GetVersionString(const std::string& extension_id) { | 429 std::string ExtensionPrefs::GetVersionString(const std::string& extension_id) { |
| 429 DictionaryValue* extension = GetExtensionPref(extension_id); | 430 DictionaryValue* extension = GetExtensionPref(extension_id); |
| 430 if (!extension) | 431 if (!extension) |
| 431 return std::string(); | 432 return std::string(); |
| 432 | 433 |
| 433 std::string version; | 434 std::string version; |
| 434 if (!extension->GetString(kPrefVersion, &version)) { | 435 if (!extension->GetString(kPrefVersion, &version)) { |
| 435 LOG(ERROR) << "Bad or missing pref 'version' for extension '" | 436 LOG(ERROR) << "Bad or missing pref 'version' for extension '" |
| 436 << extension_id << "'"; | 437 << extension_id << "'"; |
| 437 } | 438 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 const std::string& extension_id) { | 493 const std::string& extension_id) { |
| 493 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 494 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 494 if (!dict) | 495 if (!dict) |
| 495 return NULL; | 496 return NULL; |
| 496 DictionaryValue* extension = NULL; | 497 DictionaryValue* extension = NULL; |
| 497 std::wstring id = ASCIIToWide(extension_id); | 498 std::wstring id = ASCIIToWide(extension_id); |
| 498 dict->GetDictionary(id, &extension); | 499 dict->GetDictionary(id, &extension); |
| 499 return extension; | 500 return extension; |
| 500 } | 501 } |
| 501 | 502 |
| 502 // static | 503 // Helper function for GetInstalledExtensionsInfo. |
| 503 ExtensionPrefs::ExtensionsInfo* ExtensionPrefs::CollectExtensionsInfo( | 504 static ExtensionInfo* GetInstalledExtensionInfoImpl( |
| 504 ExtensionPrefs* prefs) { | 505 DictionaryValue* extension_data, |
| 505 scoped_ptr<DictionaryValue> extension_data( | 506 DictionaryValue::key_iterator extension_id) { |
| 506 prefs->CopyCurrentExtensions()); | 507 DictionaryValue* ext; |
| 508 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { |
| 509 LOG(WARNING) << "Invalid pref for extension " << *extension_id; |
| 510 NOTREACHED(); |
| 511 return NULL; |
| 512 } |
| 513 if (ext->HasKey(kPrefBlacklist)) { |
| 514 bool is_blacklisted = false; |
| 515 if (!ext->GetBoolean(kPrefBlacklist, &is_blacklisted)) { |
| 516 NOTREACHED() << "Invalid blacklist pref:" << *extension_id; |
| 517 return NULL; |
| 518 } |
| 519 if (is_blacklisted) { |
| 520 LOG(WARNING) << "Blacklisted extension: " << *extension_id; |
| 521 return NULL; |
| 522 } |
| 523 } |
| 524 int state_value; |
| 525 if (!ext->GetInteger(kPrefState, &state_value)) { |
| 526 LOG(WARNING) << "Missing state pref for extension " << *extension_id; |
| 527 NOTREACHED(); |
| 528 return NULL; |
| 529 } |
| 530 if (state_value == Extension::KILLBIT) { |
| 531 LOG(WARNING) << "External extension has been uninstalled by the user " |
| 532 << *extension_id; |
| 533 return NULL; |
| 534 } |
| 535 FilePath::StringType path; |
| 536 if (!ext->GetString(kPrefPath, &path)) { |
| 537 LOG(WARNING) << "Missing path pref for extension " << *extension_id; |
| 538 NOTREACHED(); |
| 539 return NULL; |
| 540 } |
| 541 int location_value; |
| 542 if (!ext->GetInteger(kPrefLocation, &location_value)) { |
| 543 LOG(WARNING) << "Missing location pref for extension " << *extension_id; |
| 544 NOTREACHED(); |
| 545 return NULL; |
| 546 } |
| 547 |
| 548 // Only the following extension types can be installed permanently in the |
| 549 // preferences. |
| 550 Extension::Location location = |
| 551 static_cast<Extension::Location>(location_value); |
| 552 if (location != Extension::INTERNAL && |
| 553 location != Extension::LOAD && |
| 554 !Extension::IsExternalLocation(location)) { |
| 555 NOTREACHED(); |
| 556 return NULL; |
| 557 } |
| 558 |
| 559 DictionaryValue* manifest = NULL; |
| 560 if (!ext->GetDictionary(kPrefManifest, &manifest)) { |
| 561 LOG(WARNING) << "Missing manifest for extension " << *extension_id; |
| 562 // Just a warning for now. |
| 563 } |
| 564 |
| 565 return new ExtensionInfo(manifest, WideToASCII(*extension_id), |
| 566 FilePath(path), location); |
| 567 } |
| 568 |
| 569 ExtensionPrefs::ExtensionsInfo* ExtensionPrefs::GetInstalledExtensionsInfo() { |
| 570 scoped_ptr<DictionaryValue> extension_data(CopyCurrentExtensions()); |
| 507 | 571 |
| 508 ExtensionsInfo* extensions_info = new ExtensionsInfo; | 572 ExtensionsInfo* extensions_info = new ExtensionsInfo; |
| 509 | 573 |
| 510 for (DictionaryValue::key_iterator extension_id( | 574 for (DictionaryValue::key_iterator extension_id( |
| 511 extension_data->begin_keys()); | 575 extension_data->begin_keys()); |
| 512 extension_id != extension_data->end_keys(); ++extension_id) { | 576 extension_id != extension_data->end_keys(); ++extension_id) { |
| 513 DictionaryValue* ext; | 577 ExtensionInfo* info = GetInstalledExtensionInfoImpl(extension_data.get(), |
| 514 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, | 578 extension_id); |
| 515 &ext)) { | 579 if (info) |
| 516 LOG(WARNING) << "Invalid pref for extension " << *extension_id; | 580 extensions_info->push_back(linked_ptr<ExtensionInfo>(info)); |
| 517 NOTREACHED(); | |
| 518 continue; | |
| 519 } | |
| 520 if (ext->HasKey(kPrefBlacklist)) { | |
| 521 bool is_blacklisted = false; | |
| 522 if (!ext->GetBoolean(kPrefBlacklist, &is_blacklisted)) { | |
| 523 NOTREACHED() << "Invalid blacklist pref:" << *extension_id; | |
| 524 continue; | |
| 525 } | |
| 526 if (is_blacklisted) { | |
| 527 LOG(WARNING) << "Blacklisted extension: " << *extension_id; | |
| 528 continue; | |
| 529 } | |
| 530 } | |
| 531 int state_value; | |
| 532 if (!ext->GetInteger(kPrefState, &state_value)) { | |
| 533 LOG(WARNING) << "Missing state pref for extension " << *extension_id; | |
| 534 NOTREACHED(); | |
| 535 continue; | |
| 536 } | |
| 537 if (state_value == Extension::KILLBIT) { | |
| 538 LOG(WARNING) << "External extension has been uninstalled by the user " | |
| 539 << *extension_id; | |
| 540 continue; | |
| 541 } | |
| 542 FilePath::StringType path; | |
| 543 if (!ext->GetString(kPrefPath, &path)) { | |
| 544 LOG(WARNING) << "Missing path pref for extension " << *extension_id; | |
| 545 NOTREACHED(); | |
| 546 continue; | |
| 547 } | |
| 548 int location_value; | |
| 549 if (!ext->GetInteger(kPrefLocation, &location_value)) { | |
| 550 LOG(WARNING) << "Missing location pref for extension " << *extension_id; | |
| 551 NOTREACHED(); | |
| 552 continue; | |
| 553 } | |
| 554 | |
| 555 // Only the following extension types can be installed permanently in the | |
| 556 // preferences. | |
| 557 Extension::Location location = | |
| 558 static_cast<Extension::Location>(location_value); | |
| 559 if (location != Extension::INTERNAL && | |
| 560 location != Extension::LOAD && | |
| 561 !Extension::IsExternalLocation(location)) { | |
| 562 NOTREACHED(); | |
| 563 continue; | |
| 564 } | |
| 565 | |
| 566 DictionaryValue* manifest = NULL; | |
| 567 if (!ext->GetDictionary(kPrefManifest, &manifest)) { | |
| 568 LOG(WARNING) << "Missing manifest for extension " << *extension_id; | |
| 569 // Just a warning for now. | |
| 570 } | |
| 571 | |
| 572 extensions_info->push_back(linked_ptr<ExtensionInfo>(new ExtensionInfo( | |
| 573 manifest, WideToASCII(*extension_id), FilePath(path), location))); | |
| 574 } | 581 } |
| 575 | 582 |
| 576 return extensions_info; | 583 return extensions_info; |
| 577 } | 584 } |
| 578 | 585 |
| 586 ExtensionInfo* ExtensionPrefs::GetInstalledExtensionInfo( |
| 587 const std::string& extension_id) { |
| 588 scoped_ptr<DictionaryValue> extension_data(CopyCurrentExtensions()); |
| 589 |
| 590 for (DictionaryValue::key_iterator extension_iter( |
| 591 extension_data->begin_keys()); |
| 592 extension_iter != extension_data->end_keys(); ++extension_iter) { |
| 593 if (WideToASCII(*extension_iter) == extension_id) { |
| 594 return GetInstalledExtensionInfoImpl(extension_data.get(), |
| 595 extension_iter); |
| 596 } |
| 597 } |
| 598 |
| 599 return NULL; |
| 600 } |
| 601 |
| 579 // static | 602 // static |
| 580 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 603 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
| 581 prefs->RegisterDictionaryPref(kExtensionsPref); | 604 prefs->RegisterDictionaryPref(kExtensionsPref); |
| 582 prefs->RegisterListPref(kExtensionShelf); | 605 prefs->RegisterListPref(kExtensionShelf); |
| 583 prefs->RegisterListPref(kExtensionToolbar); | 606 prefs->RegisterListPref(kExtensionToolbar); |
| 584 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 607 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
| 585 } | 608 } |
| OLD | NEW |