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"; | |
Aaron Boodman
2010/12/03 08:58:10
I think it is OK to assume the reader has read the
battre (please use the other)
2010/12/03 19:32:58
Done.
| |
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 const FilePath& root_dir) |
147 : profile_(profile), | |
148 prefs_(NULL), | |
138 install_directory_(root_dir) { | 149 install_directory_(root_dir) { |
150 prefs_ = profile_->GetPrefs(); | |
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 UpdateWinningPrefs(pref_keys); | |
706 } | 731 } |
707 | 732 |
708 Extension::State ExtensionPrefs::GetExtensionState( | 733 Extension::State ExtensionPrefs::GetExtensionState( |
709 const std::string& extension_id) { | 734 const std::string& extension_id) const { |
710 DictionaryValue* extension = GetExtensionPref(extension_id); | 735 DictionaryValue* extension = GetExtensionPref(extension_id); |
711 | 736 |
712 // If the extension doesn't have a pref, it's a --load-extension. | 737 // If the extension doesn't have a pref, it's a --load-extension. |
713 if (!extension) | 738 if (!extension) |
714 return Extension::ENABLED; | 739 return Extension::ENABLED; |
715 | 740 |
716 int state = -1; | 741 int state = -1; |
717 if (!extension->GetInteger(kPrefState, &state) || | 742 if (!extension->GetInteger(kPrefState, &state) || |
718 state < 0 || state >= Extension::NUM_STATES) { | 743 state < 0 || state >= Extension::NUM_STATES) { |
719 LOG(ERROR) << "Bad or missing pref 'state' for extension '" | 744 LOG(ERROR) << "Bad or missing pref 'state' for extension '" |
720 << extension_id << "'"; | 745 << extension_id << "'"; |
721 return Extension::ENABLED; | 746 return Extension::ENABLED; |
722 } | 747 } |
723 return static_cast<Extension::State>(state); | 748 return static_cast<Extension::State>(state); |
724 } | 749 } |
725 | 750 |
726 void ExtensionPrefs::SetExtensionState(const Extension* extension, | 751 void ExtensionPrefs::SetExtensionState(const Extension* extension, |
727 Extension::State state) { | 752 Extension::State state) { |
728 UpdateExtensionPref(extension->id(), kPrefState, | 753 UpdateExtensionPref(extension->id(), kPrefState, |
729 Value::CreateIntegerValue(state)); | 754 Value::CreateIntegerValue(state)); |
755 | |
756 PrefKeySet pref_keys; | |
757 GetExtensionControlledPrefKeys(extension->id(), &pref_keys); | |
758 UpdateWinningPrefs(pref_keys); | |
759 | |
730 SavePrefsAndNotify(); | 760 SavePrefsAndNotify(); |
731 } | 761 } |
732 | 762 |
733 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { | 763 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { |
734 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); | 764 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); |
735 bool visible = false; | 765 bool visible = false; |
736 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) | 766 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) |
737 return true; | 767 return true; |
738 | 768 |
739 return false; | 769 return false; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
823 DictionaryValue* ExtensionPrefs::GetExtensionPref( | 853 DictionaryValue* ExtensionPrefs::GetExtensionPref( |
824 const std::string& extension_id) const { | 854 const std::string& extension_id) const { |
825 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 855 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
826 if (!dict) | 856 if (!dict) |
827 return NULL; | 857 return NULL; |
828 DictionaryValue* extension = NULL; | 858 DictionaryValue* extension = NULL; |
829 dict->GetDictionary(extension_id, &extension); | 859 dict->GetDictionary(extension_id, &extension); |
830 return extension; | 860 return extension; |
831 } | 861 } |
832 | 862 |
863 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | |
864 const std::string& extension_id) const { | |
865 DictionaryValue* extension = GetExtensionPref(extension_id); | |
866 | |
867 // If the extension doesn't have a pref, it's a --load-extension. | |
868 if (!extension) | |
869 return NULL; | |
870 DictionaryValue* preferences = NULL; | |
871 extension->GetDictionary(kPrefPreferences, &preferences); | |
872 return preferences; | |
873 } | |
874 | |
833 // Helper function for GetInstalledExtensionsInfo. | 875 // Helper function for GetInstalledExtensionsInfo. |
834 static ExtensionInfo* GetInstalledExtensionInfoImpl( | 876 static ExtensionInfo* GetInstalledExtensionInfoImpl( |
835 DictionaryValue* extension_data, | 877 DictionaryValue* extension_data, |
836 DictionaryValue::key_iterator extension_id) { | 878 DictionaryValue::key_iterator extension_id) { |
837 DictionaryValue* ext; | 879 DictionaryValue* ext; |
838 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { | 880 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { |
839 LOG(WARNING) << "Invalid pref for extension " << *extension_id; | 881 LOG(WARNING) << "Invalid pref for extension " << *extension_id; |
840 NOTREACHED(); | 882 NOTREACHED(); |
841 return NULL; | 883 return NULL; |
842 } | 884 } |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1082 std::string ExtensionPrefs::GetUpdateUrlData(const std::string& extension_id) { | 1124 std::string ExtensionPrefs::GetUpdateUrlData(const std::string& extension_id) { |
1083 DictionaryValue* dictionary = GetExtensionPref(extension_id); | 1125 DictionaryValue* dictionary = GetExtensionPref(extension_id); |
1084 if (!dictionary) | 1126 if (!dictionary) |
1085 return std::string(); | 1127 return std::string(); |
1086 | 1128 |
1087 std::string data; | 1129 std::string data; |
1088 dictionary->GetString(kUpdateUrlData, &data); | 1130 dictionary->GetString(kUpdateUrlData, &data); |
1089 return data; | 1131 return data; |
1090 } | 1132 } |
1091 | 1133 |
1134 base::Time ExtensionPrefs::GetCurrentTime() const { | |
1135 return base::Time::Now(); | |
1136 } | |
1137 | |
1138 base::Time ExtensionPrefs::GetInstallTime( | |
1139 const std::string& extension_id) const { | |
1140 const DictionaryValue* extension = GetExtensionPref(extension_id); | |
1141 | |
1142 // If the extension doesn't have a pref, it's a --load-extension. | |
1143 if (!extension) | |
1144 return base::Time::Time(); | |
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 PrefKeySet ext_controlled_prefs; | |
1199 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | |
1200 ext_id != extension_ids.end(); ++ext_id) { | |
1201 GetExtensionControlledPrefKeys(*ext_id, &ext_controlled_prefs); | |
1202 } | |
1203 std::sort(ext_controlled_prefs.begin(), ext_controlled_prefs.end()); | |
Aaron Boodman
2010/12/03 08:58:10
Did you consider having PrefKeySet actually be a s
battre (please use the other)
2010/12/03 19:32:58
Done.
| |
1204 PrefKeySet unique_ext_controlled_prefs; | |
1205 std::unique_copy(ext_controlled_prefs.begin(), | |
1206 ext_controlled_prefs.end(), | |
1207 std::back_insert_iterator<PrefKeySet>( | |
1208 unique_ext_controlled_prefs)); | |
1209 | |
1210 // Store winning preference for each extension controlled preference. | |
1211 UpdateWinningPrefs(unique_ext_controlled_prefs); | |
1212 } | |
1213 | |
1214 const Value* ExtensionPrefs::WinningExtensionControlledPrefValue( | |
1215 const std::string& key) const { | |
1216 Value *winner = NULL; | |
1217 base::Time winners_install_time = base::Time::Time(); | |
1218 | |
1219 ExtensionIdSet extension_ids; | |
1220 GetEnabledExtensions(&extension_ids); | |
1221 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | |
1222 ext_id != extension_ids.end(); ++ext_id) { | |
1223 base::Time extension_install_time = GetInstallTime(*ext_id); | |
1224 | |
1225 // We do not need to consider extensions that were installed before the | |
1226 // most recent extension found that provides the requested preference. | |
1227 if (extension_install_time < winners_install_time) | |
1228 continue; | |
1229 | |
1230 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); | |
1231 Value *value = NULL; | |
1232 if (preferences && preferences->GetWithoutPathExpansion(key, &value)) { | |
1233 // This extension is more recent than the last one providing this pref. | |
1234 winner = value; | |
1235 winners_install_time = extension_install_time; | |
1236 } | |
1237 } | |
1238 | |
1239 return winner; | |
1240 } | |
1241 | |
1242 void ExtensionPrefs::UpdateWinningPrefs( | |
1243 const ExtensionPrefs::PrefKeySet& pref_keys) { | |
1244 for (PrefKeySet::const_iterator i = pref_keys.begin(); | |
1245 i != pref_keys.end(); ++i) | |
1246 UpdateWinningPref(*i); | |
1247 } | |
1248 | |
1249 void ExtensionPrefs::UpdateWinningPref(const std::string& pref_key) { | |
1250 PrefStore* extension_pref_store = profile_->GetExtensionPrefStore(); | |
1251 if (extension_pref_store == NULL) | |
1252 return; // Profile is being shut down, Pref Service is already gone. | |
1253 const Value* winning_pref_value = | |
1254 WinningExtensionControlledPrefValue(pref_key); | |
1255 Value* old_value = NULL; | |
1256 extension_pref_store->prefs()->Get(pref_key, &old_value); | |
1257 bool changed = !Value::Equals(winning_pref_value, old_value); | |
1258 | |
1259 if (winning_pref_value) { | |
1260 extension_pref_store->prefs()->Set(pref_key, | |
1261 winning_pref_value->DeepCopy()); | |
1262 } else { | |
1263 extension_pref_store->prefs()->Remove(pref_key, NULL); | |
1264 } | |
1265 | |
1266 if (changed) { | |
1267 pref_service()->pref_notifier()->OnPreferenceSet( | |
1268 pref_key.c_str(), PrefNotifier::EXTENSION_STORE); | |
1269 } | |
1270 } | |
1271 | |
1272 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, | |
1273 const std::string& pref_key, | |
1274 Value* value) { | |
1275 DCHECK(pref_service()->FindPreference(pref_key.c_str())) | |
1276 << "Extension controlled preference key " << pref_key | |
1277 << " not registered."; | |
1278 DictionaryValue* extension_preferences = | |
1279 GetExtensionControlledPrefs(extension_id); | |
1280 | |
1281 if (extension_preferences == NULL) { // May be pruned when writing to disk. | |
1282 DictionaryValue* extension = GetExtensionPref(extension_id); | |
1283 if (extension == NULL) { | |
1284 LOG(WARNING) << "Not setting preference for " << pref_key | |
Aaron Boodman
2010/12/03 08:58:10
This warning doesn't seem quite right... We do sto
battre (please use the other)
2010/12/03 19:32:58
Done.
| |
1285 << " because extension is --load-extension initiated."; | |
1286 return; | |
1287 } | |
1288 extension_preferences = new DictionaryValue; | |
1289 extension->Set(kPrefPreferences, extension_preferences); | |
1290 } | |
1291 | |
1292 Value* oldValue = NULL; | |
1293 extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); | |
1294 bool modified = !Value::Equals(oldValue, value); | |
1295 if (!modified) | |
1296 return; | |
1297 | |
1298 if (value == NULL) | |
1299 extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL); | |
1300 else | |
1301 extension_preferences->SetWithoutPathExpansion(pref_key, value); | |
1302 pref_service()->ScheduleSavePersistentPrefs(); | |
1303 | |
1304 UpdateWinningPref(pref_key); | |
1305 } | |
1306 | |
1307 void ExtensionPrefs::GetExtensionControlledPrefKeys( | |
1308 const std::string& extension_id, PrefKeySet *out) const { | |
1309 DCHECK(out != NULL); | |
1310 DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id); | |
1311 if (ext_prefs) { | |
1312 for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); | |
1313 i != ext_prefs->end_keys(); ++i) { | |
1314 out->push_back(*i); | |
1315 } | |
1316 } | |
1317 } | |
1318 | |
1092 // static | 1319 // static |
1093 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 1320 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
1094 prefs->RegisterDictionaryPref(kExtensionsPref); | 1321 prefs->RegisterDictionaryPref(kExtensionsPref); |
1095 prefs->RegisterListPref(kExtensionToolbar); | 1322 prefs->RegisterListPref(kExtensionToolbar); |
1096 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 1323 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
1097 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); | 1324 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); |
1098 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); | 1325 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); |
1099 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); | 1326 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); |
1100 prefs->RegisterListPref(prefs::kExtensionInstallForceList); | 1327 prefs->RegisterListPref(prefs::kExtensionInstallForceList); |
1101 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); | 1328 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); |
1102 } | 1329 } |
OLD | NEW |