Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/profile.h" | |
| 10 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/url_pattern.h" | 12 #include "chrome/common/extensions/url_pattern.h" |
| 12 #include "chrome/common/notification_service.h" | 13 #include "chrome/common/notification_service.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 | 15 |
| 15 using base::Time; | 16 using base::Time; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Additional preferences keys | 20 // Additional preferences keys |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 const char kBrowserActionVisible[] = "browser_action_visible"; | 85 const char kBrowserActionVisible[] = "browser_action_visible"; |
| 85 | 86 |
| 86 // Preferences that hold which permissions the user has granted the extension. | 87 // Preferences that hold which permissions the user has granted the extension. |
| 87 // We explicitly keep track of these so that extensions can contain unknown | 88 // We explicitly keep track of these so that extensions can contain unknown |
| 88 // permissions, for backwards compatibility reasons, and we can still prompt | 89 // permissions, for backwards compatibility reasons, and we can still prompt |
| 89 // the user to accept them once recognized. | 90 // the user to accept them once recognized. |
| 90 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; | 91 const char kPrefGrantedPermissionsAPI[] = "granted_permissions.api"; |
| 91 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; | 92 const char kPrefGrantedPermissionsHost[] = "granted_permissions.host"; |
| 92 const char kPrefGrantedPermissionsAll[] = "granted_permissions.full"; | 93 const char kPrefGrantedPermissionsAll[] = "granted_permissions.full"; |
| 93 | 94 |
| 95 // A preference that indicates when an extension was installed. | |
| 96 const char kPrefInstallTime[] = "install_time"; | |
| 97 | |
| 98 // A preference that indicates the last effective preference values of an | |
| 99 // extension. The value is a dictionary mapping (non-expanded) preference keys | |
| 100 // to the values configured by the extension. | |
| 101 const char kPrefPreferences[] = "preferences"; | |
| 102 | |
| 94 } // namespace | 103 } // namespace |
| 95 | 104 |
| 96 //////////////////////////////////////////////////////////////////////////////// | 105 //////////////////////////////////////////////////////////////////////////////// |
| 97 | 106 |
| 98 namespace { | 107 namespace { |
| 99 | 108 |
| 100 // TODO(asargent) - This is cleanup code for a key that was introduced into | 109 // TODO(asargent) - This is cleanup code for a key that was introduced into |
| 101 // the extensions.settings sub-dictionary which wasn't a valid extension | 110 // the extensions.settings sub-dictionary which wasn't a valid extension |
| 102 // id. We can remove this in a couple of months. (See http://crbug.com/40017 | 111 // id. We can remove this in a couple of months. (See http://crbug.com/40017 |
| 103 // and http://crbug.com/39745 for more details). | 112 // and http://crbug.com/39745 for more details). |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 126 std::set<std::string>* result) { | 135 std::set<std::string>* result) { |
| 127 ExtensionExtent::PatternList patterns = host_extent.patterns(); | 136 ExtensionExtent::PatternList patterns = host_extent.patterns(); |
| 128 ExtensionExtent::PatternList::const_iterator i; | 137 ExtensionExtent::PatternList::const_iterator i; |
| 129 | 138 |
| 130 for (i = patterns.begin(); i != patterns.end(); ++i) | 139 for (i = patterns.begin(); i != patterns.end(); ++i) |
| 131 result->insert(i->GetAsString()); | 140 result->insert(i->GetAsString()); |
| 132 } | 141 } |
| 133 | 142 |
| 134 } // namespace | 143 } // namespace |
| 135 | 144 |
| 136 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir) | 145 ExtensionPrefs::ExtensionPrefs(Profile* profile, |
| 137 : prefs_(prefs), | 146 PrefService* prefs, |
| 147 const FilePath& root_dir) | |
| 148 : profile_(profile), | |
| 149 prefs_(prefs), | |
| 138 install_directory_(root_dir) { | 150 install_directory_(root_dir) { |
| 139 // TODO(asargent) - Remove this in a couple of months. (See comment above | 151 // TODO(asargent) - Remove this in a couple of months. (See comment above |
| 140 // CleanupBadExtensionKeys). | 152 // CleanupBadExtensionKeys). |
| 141 CleanupBadExtensionKeys(prefs); | 153 CleanupBadExtensionKeys(prefs); |
| 142 | 154 |
| 143 MakePathsRelative(); | 155 MakePathsRelative(); |
| 156 | |
| 157 InstallPersistedExtensionControlledPrefs(); | |
| 144 } | 158 } |
| 145 | 159 |
| 146 ExtensionPrefs::~ExtensionPrefs() {} | 160 ExtensionPrefs::~ExtensionPrefs() {} |
| 147 | 161 |
| 148 // static | 162 // static |
| 149 const char ExtensionPrefs::kExtensionsPref[] = "extensions.settings"; | 163 const char ExtensionPrefs::kExtensionsPref[] = "extensions.settings"; |
| 150 | 164 |
| 151 static FilePath::StringType MakePathRelative(const FilePath& parent, | 165 static FilePath::StringType MakePathRelative(const FilePath& parent, |
| 152 const FilePath& child, | 166 const FilePath& child, |
| 153 bool *dirty) { | 167 bool *dirty) { |
| (...skipping 481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 635 // Check to see if the extension has been killed. | 649 // Check to see if the extension has been killed. |
| 636 int state; | 650 int state; |
| 637 if (extension->GetInteger(kPrefState, &state) && | 651 if (extension->GetInteger(kPrefState, &state) && |
| 638 state == static_cast<int>(Extension::KILLBIT)) { | 652 state == static_cast<int>(Extension::KILLBIT)) { |
| 639 killed_ids->insert(StringToLowerASCII(key_name)); | 653 killed_ids->insert(StringToLowerASCII(key_name)); |
| 640 } | 654 } |
| 641 } | 655 } |
| 642 } | 656 } |
| 643 | 657 |
| 644 std::vector<std::string> ExtensionPrefs::GetToolbarOrder() { | 658 std::vector<std::string> ExtensionPrefs::GetToolbarOrder() { |
| 645 std::vector<std::string> extension_ids; | 659 ExtensionPrefs::ExtensionIdSet extension_ids; |
| 646 const ListValue* toolbar_order = prefs_->GetList(kExtensionToolbar); | 660 const ListValue* toolbar_order = prefs_->GetList(kExtensionToolbar); |
| 647 if (toolbar_order) { | 661 if (toolbar_order) { |
| 648 for (size_t i = 0; i < toolbar_order->GetSize(); ++i) { | 662 for (size_t i = 0; i < toolbar_order->GetSize(); ++i) { |
| 649 std::string extension_id; | 663 std::string extension_id; |
| 650 if (toolbar_order->GetString(i, &extension_id)) | 664 if (toolbar_order->GetString(i, &extension_id)) |
| 651 extension_ids.push_back(extension_id); | 665 extension_ids.push_back(extension_id); |
| 652 } | 666 } |
| 653 } | 667 } |
| 654 return extension_ids; | 668 return extension_ids; |
| 655 } | 669 } |
| 656 | 670 |
| 657 void ExtensionPrefs::SetToolbarOrder( | 671 void ExtensionPrefs::SetToolbarOrder( |
| 658 const std::vector<std::string>& extension_ids) { | 672 const std::vector<std::string>& extension_ids) { |
| 659 ListValue* toolbar_order = prefs_->GetMutableList(kExtensionToolbar); | 673 ListValue* toolbar_order = prefs_->GetMutableList(kExtensionToolbar); |
| 660 toolbar_order->Clear(); | 674 toolbar_order->Clear(); |
| 661 for (std::vector<std::string>::const_iterator iter = extension_ids.begin(); | 675 for (std::vector<std::string>::const_iterator iter = extension_ids.begin(); |
| 662 iter != extension_ids.end(); ++iter) { | 676 iter != extension_ids.end(); ++iter) { |
| 663 toolbar_order->Append(new StringValue(*iter)); | 677 toolbar_order->Append(new StringValue(*iter)); |
| 664 } | 678 } |
| 665 SavePrefsAndNotify(); | 679 SavePrefsAndNotify(); |
| 666 } | 680 } |
| 667 | 681 |
| 668 void ExtensionPrefs::OnExtensionInstalled( | 682 void ExtensionPrefs::OnExtensionInstalled( |
| 669 const Extension* extension, Extension::State initial_state, | 683 const Extension* extension, Extension::State initial_state, |
| 670 bool initial_incognito_enabled) { | 684 bool initial_incognito_enabled) { |
| 671 const std::string& id = extension->id(); | 685 const std::string& id = extension->id(); |
| 686 const base::Time install_time = GetCurrentTime(); | |
| 672 UpdateExtensionPref(id, kPrefState, | 687 UpdateExtensionPref(id, kPrefState, |
| 673 Value::CreateIntegerValue(initial_state)); | 688 Value::CreateIntegerValue(initial_state)); |
| 674 UpdateExtensionPref(id, kPrefIncognitoEnabled, | 689 UpdateExtensionPref(id, kPrefIncognitoEnabled, |
| 675 Value::CreateBooleanValue(initial_incognito_enabled)); | 690 Value::CreateBooleanValue(initial_incognito_enabled)); |
| 676 UpdateExtensionPref(id, kPrefLocation, | 691 UpdateExtensionPref(id, kPrefLocation, |
| 677 Value::CreateIntegerValue(extension->location())); | 692 Value::CreateIntegerValue(extension->location())); |
| 693 UpdateExtensionPref(id, kPrefInstallTime, | |
| 694 Value::CreateStringValue( | |
| 695 base::Int64ToString(install_time.ToInternalValue()))); | |
| 696 UpdateExtensionPref(id, kPrefPreferences, new DictionaryValue()); | |
| 697 | |
| 678 FilePath::StringType path = MakePathRelative(install_directory_, | 698 FilePath::StringType path = MakePathRelative(install_directory_, |
| 679 extension->path(), NULL); | 699 extension->path(), NULL); |
| 680 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); | 700 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); |
| 681 // We store prefs about LOAD extensions, but don't cache their manifest | 701 // We store prefs about LOAD extensions, but don't cache their manifest |
| 682 // since it may change on disk. | 702 // since it may change on disk. |
| 683 if (extension->location() != Extension::LOAD) { | 703 if (extension->location() != Extension::LOAD) { |
| 684 UpdateExtensionPref(id, kPrefManifest, | 704 UpdateExtensionPref(id, kPrefManifest, |
| 685 extension->manifest_value()->DeepCopy()); | 705 extension->manifest_value()->DeepCopy()); |
| 686 } | 706 } |
| 687 UpdateExtensionPref(id, kPrefAppLaunchIndex, | 707 UpdateExtensionPref(id, kPrefAppLaunchIndex, |
| 688 Value::CreateIntegerValue(GetNextAppLaunchIndex())); | 708 Value::CreateIntegerValue(GetNextAppLaunchIndex())); |
| 689 SavePrefsAndNotify(); | 709 SavePrefsAndNotify(); |
| 690 } | 710 } |
| 691 | 711 |
| 692 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, | 712 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
| 693 const Extension::Location& location, | 713 const Extension::Location& location, |
| 694 bool external_uninstall) { | 714 bool external_uninstall) { |
| 715 PrefKeySet pref_keys; | |
| 716 GetExtensionControlledPrefKeys(extension_id, &pref_keys); | |
| 717 | |
| 695 // For external extensions, we save a preference reminding ourself not to try | 718 // For external extensions, we save a preference reminding ourself not to try |
| 696 // and install the extension anymore (except when |external_uninstall| is | 719 // and install the extension anymore (except when |external_uninstall| is |
| 697 // true, which signifies that the registry key was deleted or the pref file | 720 // true, which signifies that the registry key was deleted or the pref file |
| 698 // no longer lists the extension). | 721 // no longer lists the extension). |
| 699 if (!external_uninstall && Extension::IsExternalLocation(location)) { | 722 if (!external_uninstall && Extension::IsExternalLocation(location)) { |
| 700 UpdateExtensionPref(extension_id, kPrefState, | 723 UpdateExtensionPref(extension_id, kPrefState, |
| 701 Value::CreateIntegerValue(Extension::KILLBIT)); | 724 Value::CreateIntegerValue(Extension::KILLBIT)); |
| 702 SavePrefsAndNotify(); | 725 SavePrefsAndNotify(); |
| 703 } else { | 726 } else { |
| 704 DeleteExtensionPrefs(extension_id); | 727 DeleteExtensionPrefs(extension_id); |
| 705 } | 728 } |
| 729 | |
| 730 for (PrefKeySet::iterator i = pref_keys.begin(); i != pref_keys.end(); ++i) | |
| 731 UpdateWinningPref(*i, false); | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
I believe Aaron already asked to factor this loop
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 706 } | 732 } |
| 707 | 733 |
| 708 Extension::State ExtensionPrefs::GetExtensionState( | 734 Extension::State ExtensionPrefs::GetExtensionState( |
| 709 const std::string& extension_id) { | 735 const std::string& extension_id) const { |
| 710 DictionaryValue* extension = GetExtensionPref(extension_id); | 736 DictionaryValue* extension = GetExtensionPref(extension_id); |
| 711 | 737 |
| 712 // If the extension doesn't have a pref, it's a --load-extension. | 738 // If the extension doesn't have a pref, it's a --load-extension. |
| 713 if (!extension) | 739 if (!extension) |
| 714 return Extension::ENABLED; | 740 return Extension::ENABLED; |
| 715 | 741 |
| 716 int state = -1; | 742 int state = -1; |
| 717 if (!extension->GetInteger(kPrefState, &state) || | 743 if (!extension->GetInteger(kPrefState, &state) || |
| 718 state < 0 || state >= Extension::NUM_STATES) { | 744 state < 0 || state >= Extension::NUM_STATES) { |
| 719 LOG(ERROR) << "Bad or missing pref 'state' for extension '" | 745 LOG(ERROR) << "Bad or missing pref 'state' for extension '" |
| 720 << extension_id << "'"; | 746 << extension_id << "'"; |
| 721 return Extension::ENABLED; | 747 return Extension::ENABLED; |
| 722 } | 748 } |
| 723 return static_cast<Extension::State>(state); | 749 return static_cast<Extension::State>(state); |
| 724 } | 750 } |
| 725 | 751 |
| 726 void ExtensionPrefs::SetExtensionState(const Extension* extension, | 752 void ExtensionPrefs::SetExtensionState(const Extension* extension, |
| 727 Extension::State state) { | 753 Extension::State state) { |
| 728 UpdateExtensionPref(extension->id(), kPrefState, | 754 UpdateExtensionPref(extension->id(), kPrefState, |
| 729 Value::CreateIntegerValue(state)); | 755 Value::CreateIntegerValue(state)); |
| 756 | |
| 757 PrefKeySet pref_keys; | |
| 758 GetExtensionControlledPrefKeys(extension->id(), &pref_keys); | |
| 759 for (PrefKeySet::iterator i = pref_keys.begin(); i != pref_keys.end(); ++i) | |
| 760 UpdateWinningPref(*i, false); | |
| 761 | |
| 730 SavePrefsAndNotify(); | 762 SavePrefsAndNotify(); |
| 731 } | 763 } |
| 732 | 764 |
| 733 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { | 765 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { |
| 734 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); | 766 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); |
| 735 bool visible = false; | 767 bool visible = false; |
| 736 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) | 768 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) |
| 737 return true; | 769 return true; |
| 738 | 770 |
| 739 return false; | 771 return false; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 823 DictionaryValue* ExtensionPrefs::GetExtensionPref( | 855 DictionaryValue* ExtensionPrefs::GetExtensionPref( |
| 824 const std::string& extension_id) const { | 856 const std::string& extension_id) const { |
| 825 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 857 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
| 826 if (!dict) | 858 if (!dict) |
| 827 return NULL; | 859 return NULL; |
| 828 DictionaryValue* extension = NULL; | 860 DictionaryValue* extension = NULL; |
| 829 dict->GetDictionary(extension_id, &extension); | 861 dict->GetDictionary(extension_id, &extension); |
| 830 return extension; | 862 return extension; |
| 831 } | 863 } |
| 832 | 864 |
| 865 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | |
| 866 const std::string& extension_id) const { | |
| 867 DictionaryValue* extension = GetExtensionPref(extension_id); | |
| 868 // If the extension doesn't have a pref, it's a --load-extension. | |
| 869 if (!extension) | |
| 870 return NULL; | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Add a newline here and above before the comment? J
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 871 DictionaryValue* preferences = NULL; | |
| 872 extension->GetDictionary(kPrefPreferences, &preferences); | |
| 873 return preferences; | |
| 874 } | |
| 875 | |
| 833 // Helper function for GetInstalledExtensionsInfo. | 876 // Helper function for GetInstalledExtensionsInfo. |
| 834 static ExtensionInfo* GetInstalledExtensionInfoImpl( | 877 static ExtensionInfo* GetInstalledExtensionInfoImpl( |
| 835 DictionaryValue* extension_data, | 878 DictionaryValue* extension_data, |
| 836 DictionaryValue::key_iterator extension_id) { | 879 DictionaryValue::key_iterator extension_id) { |
| 837 DictionaryValue* ext; | 880 DictionaryValue* ext; |
| 838 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { | 881 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { |
| 839 LOG(WARNING) << "Invalid pref for extension " << *extension_id; | 882 LOG(WARNING) << "Invalid pref for extension " << *extension_id; |
| 840 NOTREACHED(); | 883 NOTREACHED(); |
| 841 return NULL; | 884 return NULL; |
| 842 } | 885 } |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1082 std::string ExtensionPrefs::GetUpdateUrlData(const std::string& extension_id) { | 1125 std::string ExtensionPrefs::GetUpdateUrlData(const std::string& extension_id) { |
| 1083 DictionaryValue* dictionary = GetExtensionPref(extension_id); | 1126 DictionaryValue* dictionary = GetExtensionPref(extension_id); |
| 1084 if (!dictionary) | 1127 if (!dictionary) |
| 1085 return std::string(); | 1128 return std::string(); |
| 1086 | 1129 |
| 1087 std::string data; | 1130 std::string data; |
| 1088 dictionary->GetString(kUpdateUrlData, &data); | 1131 dictionary->GetString(kUpdateUrlData, &data); |
| 1089 return data; | 1132 return data; |
| 1090 } | 1133 } |
| 1091 | 1134 |
| 1135 base::Time ExtensionPrefs::GetCurrentTime() const { | |
| 1136 return base::Time::Now(); | |
| 1137 } | |
| 1138 | |
| 1139 base::Time ExtensionPrefs::GetInstallTime( | |
| 1140 const std::string& extension_id) const { | |
| 1141 const DictionaryValue* extension = GetExtensionPref(extension_id); | |
| 1142 // If the extension doesn't have a pref, it's a --load-extension. | |
| 1143 if (!extension) | |
| 1144 return base::Time::Time(); | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Some whitespace here, too?
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 1145 std::string install_time_str("0"); | |
| 1146 extension->GetString(kPrefInstallTime, &install_time_str); | |
| 1147 int64 install_time_i64 = 0; | |
| 1148 base::StringToInt64(install_time_str, &install_time_i64); | |
| 1149 LOG_IF(ERROR, install_time_i64 == 0) | |
| 1150 << "Error parsing installation time of an extension."; | |
| 1151 return base::Time::FromInternalValue(install_time_i64); | |
| 1152 } | |
| 1153 | |
| 1154 void ExtensionPrefs::GetEnabledExtensions(ExtensionIdSet* out) const { | |
| 1155 DCHECK(out); | |
| 1156 const DictionaryValue* extensions = | |
| 1157 pref_service()->GetDictionary(kExtensionsPref); | |
| 1158 | |
| 1159 for (DictionaryValue::key_iterator ext_id = extensions->begin_keys(); | |
| 1160 ext_id != extensions->end_keys(); ++ext_id) { | |
| 1161 if (GetExtensionState(*ext_id) != Extension::ENABLED) | |
| 1162 continue; | |
| 1163 out->push_back(*ext_id); | |
| 1164 } | |
| 1165 } | |
| 1166 | |
| 1167 void ExtensionPrefs::FixMissingPrefs(const ExtensionIdSet& extension_ids) { | |
| 1168 // Fix old entries that did not get an installation time entry when they | |
| 1169 // were installed or don't have a preferences field. | |
| 1170 bool persist_required = false; | |
| 1171 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin(); | |
| 1172 ext_id != extension_ids.end(); ++ext_id) { | |
| 1173 DictionaryValue* extension = GetExtensionPref(*ext_id); | |
| 1174 // If the extension doesn't have a pref, it's a --load-extension. | |
| 1175 if (extension == NULL) | |
| 1176 continue; | |
| 1177 | |
| 1178 if (GetInstallTime(*ext_id) == base::Time::Time()) { | |
| 1179 const base::Time install_time = GetCurrentTime(); | |
| 1180 extension->Set(kPrefInstallTime, | |
| 1181 Value::CreateStringValue( | |
| 1182 base::Int64ToString(install_time.ToInternalValue()))); | |
| 1183 persist_required = true; | |
| 1184 } | |
| 1185 } | |
| 1186 if (persist_required) | |
| 1187 SavePrefsAndNotify(); | |
| 1188 } | |
| 1189 | |
| 1190 void ExtensionPrefs::InstallPersistedExtensionControlledPrefs() { | |
| 1191 // When this is called, the PrefService is initialized and provides access | |
| 1192 // to the user preferences stored in a JSON file. | |
| 1193 ExtensionIdSet extension_ids; | |
| 1194 GetEnabledExtensions(&extension_ids); | |
| 1195 FixMissingPrefs(extension_ids); | |
| 1196 | |
| 1197 // Collect the unique extension controlled preference keys of all extensions. | |
| 1198 std::set<std::string> ext_controlled_prefs; | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Isn't this a PrefKeySet?
battre (please use the other)
2010/12/01 17:44:38
No, we need to filter for *unique* entries. The Pr
| |
| 1199 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | |
| 1200 ext_id != extension_ids.end(); ++ext_id) { | |
| 1201 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Why not just call GetExtensionControlledPrefKeys(&
battre (please use the other)
2010/12/01 17:44:38
See above. Done.
| |
| 1202 if (!preferences) | |
| 1203 continue; | |
| 1204 for (DictionaryValue::key_iterator prefkey = preferences->begin_keys(); | |
| 1205 prefkey != preferences->end_keys(); ++prefkey) { | |
| 1206 ext_controlled_prefs.insert(*prefkey); | |
| 1207 } | |
| 1208 } | |
| 1209 | |
| 1210 // Store winning preference for each extension controlled preference. | |
| 1211 // Note that we need to suppress notifications here as the preferences | |
| 1212 // have not been registered with the PrefService, when this function | |
| 1213 // is called. | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
I don't understand why it would be necessary to su
battre (please use the other)
2010/12/01 17:44:38
The notification mechanism checked that the value
| |
| 1214 for (std::set<std::string>::iterator i = ext_controlled_prefs.begin(); | |
| 1215 i != ext_controlled_prefs.end(); ++i) { | |
| 1216 UpdateWinningPref(*i, true); | |
| 1217 } | |
| 1218 } | |
| 1219 | |
| 1220 const Value* ExtensionPrefs::WinningExtensionControlledPrefValue( | |
| 1221 const std::string& key) const { | |
| 1222 Value *winner = NULL; | |
| 1223 base::Time winners_install_time = base::Time::Time(); | |
| 1224 | |
| 1225 ExtensionIdSet extension_ids; | |
| 1226 GetEnabledExtensions(&extension_ids); | |
| 1227 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | |
| 1228 ext_id != extension_ids.end(); ++ext_id) { | |
| 1229 base::Time extension_install_time = GetInstallTime(*ext_id); | |
| 1230 | |
| 1231 // We do not need to consider extensions that were installed before the | |
| 1232 // most recent extension found that provides the requested preference. | |
| 1233 if (extension_install_time < winners_install_time) | |
| 1234 continue; | |
| 1235 | |
| 1236 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); | |
| 1237 Value *value = NULL; | |
| 1238 if (preferences && preferences->GetWithoutPathExpansion(key, &value)) { | |
| 1239 // This extension is more recent than the last one providing this pref. | |
| 1240 winner = value; | |
| 1241 winners_install_time = extension_install_time; | |
| 1242 } | |
| 1243 } | |
| 1244 | |
| 1245 return winner; | |
| 1246 } | |
| 1247 | |
| 1248 void ExtensionPrefs::UpdateWinningPref(const std::string& pref_key, | |
| 1249 bool suppressNotification) { | |
| 1250 PrefStore* extension_pref_store = profile_->GetExtensionPrefStore(); | |
| 1251 const Value* winning_pref_value = | |
| 1252 WinningExtensionControlledPrefValue(pref_key); | |
| 1253 Value* old_value = NULL; | |
| 1254 extension_pref_store->prefs()->Get(pref_key, &old_value); | |
| 1255 bool changed = !Value::Equals(winning_pref_value, old_value); | |
| 1256 | |
| 1257 if (winning_pref_value) | |
| 1258 extension_pref_store->prefs()->Set(pref_key, | |
| 1259 winning_pref_value->DeepCopy()); | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
Need curlies here.
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 1260 else | |
| 1261 extension_pref_store->prefs()->Remove(pref_key, NULL); | |
| 1262 | |
| 1263 if (changed && !suppressNotification) | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
FYI to other reviewers: Note that there is work un
| |
| 1264 pref_service()->pref_notifier()->OnPreferenceSet( | |
| 1265 pref_key.c_str(), PrefNotifier::EXTENSION_STORE); | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
curlies here as well.
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 1266 } | |
| 1267 | |
| 1268 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, | |
| 1269 const std::string& pref_key, | |
| 1270 Value* value) { | |
| 1271 DictionaryValue* extension_preferences = | |
| 1272 GetExtensionControlledPrefs(extension_id); | |
| 1273 | |
| 1274 if (extension_preferences == NULL) { // May be pruned when writing to disk. | |
| 1275 DictionaryValue* extension = GetExtensionPref(extension_id); | |
| 1276 CHECK(extension); | |
| 1277 // TODO(battre) what do we do with --load-extension extensions? | |
| 1278 // extension might be null. | |
|
Mattias Nissler (ping if slow)
2010/12/01 10:36:36
They just can't override prefs? Or should we creat
battre (please use the other)
2010/12/01 17:44:38
Done.
| |
| 1279 extension_preferences = new DictionaryValue; | |
| 1280 extension->Set(kPrefPreferences, extension_preferences); | |
| 1281 } | |
| 1282 | |
| 1283 Value* oldValue = NULL; | |
| 1284 extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); | |
| 1285 bool modified = !Value::Equals(oldValue, value); | |
| 1286 if (!modified) | |
| 1287 return; | |
| 1288 | |
| 1289 if (value == NULL) | |
| 1290 extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL); | |
| 1291 else | |
| 1292 extension_preferences->SetWithoutPathExpansion(pref_key, value); | |
| 1293 pref_service()->ScheduleSavePersistentPrefs(); | |
| 1294 | |
| 1295 UpdateWinningPref(pref_key, false); | |
| 1296 } | |
| 1297 | |
| 1298 void ExtensionPrefs::GetExtensionControlledPrefKeys( | |
| 1299 const std::string& extension_id, PrefKeySet *out) const { | |
| 1300 DCHECK(out != NULL); | |
| 1301 DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id); | |
| 1302 if (ext_prefs) { | |
| 1303 for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); | |
| 1304 i != ext_prefs->end_keys(); ++i) { | |
| 1305 out->push_back(*i); | |
| 1306 } | |
| 1307 } | |
| 1308 } | |
| 1309 | |
| 1092 // static | 1310 // static |
| 1093 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 1311 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
| 1094 prefs->RegisterDictionaryPref(kExtensionsPref); | 1312 prefs->RegisterDictionaryPref(kExtensionsPref); |
| 1095 prefs->RegisterListPref(kExtensionToolbar); | 1313 prefs->RegisterListPref(kExtensionToolbar); |
| 1096 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 1314 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
| 1097 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); | 1315 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); |
| 1098 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); | 1316 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); |
| 1099 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); | 1317 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); |
| 1100 prefs->RegisterListPref(prefs::kExtensionInstallForceList); | 1318 prefs->RegisterListPref(prefs::kExtensionInstallForceList); |
| 1101 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); | 1319 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); |
| 1102 } | 1320 } |
| OLD | NEW |