OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extensions/extension_pref_store.h" | 10 #include "chrome/browser/extensions/extension_pref_store.h" |
11 #include "chrome/browser/prefs/pref_notifier.h" | 11 #include "chrome/browser/prefs/pref_notifier.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 std::set<std::string>* result) { | 134 std::set<std::string>* result) { |
135 ExtensionExtent::PatternList patterns = host_extent.patterns(); | 135 ExtensionExtent::PatternList patterns = host_extent.patterns(); |
136 ExtensionExtent::PatternList::const_iterator i; | 136 ExtensionExtent::PatternList::const_iterator i; |
137 | 137 |
138 for (i = patterns.begin(); i != patterns.end(); ++i) | 138 for (i = patterns.begin(); i != patterns.end(); ++i) |
139 result->insert(i->GetAsString()); | 139 result->insert(i->GetAsString()); |
140 } | 140 } |
141 | 141 |
142 } // namespace | 142 } // namespace |
143 | 143 |
144 ExtensionPrefs::ExtensionPrefs(PrefService* prefs, | 144 ExtensionPrefs::ExtensionPrefs( |
145 const FilePath& root_dir, | 145 PrefService* prefs, |
146 ExtensionPrefStore* pref_store) | 146 const FilePath& root_dir, |
147 ExtensionPrefStore* pref_store, | |
148 ExtensionPrefStore* incognito_pref_store) | |
147 : prefs_(prefs), | 149 : prefs_(prefs), |
148 install_directory_(root_dir), | 150 install_directory_(root_dir), |
149 pref_store_(pref_store) { | 151 pref_store_(pref_store), |
152 incognito_pref_store_(incognito_pref_store) { | |
150 // TODO(asargent) - Remove this in a couple of months. (See comment above | 153 // TODO(asargent) - Remove this in a couple of months. (See comment above |
151 // CleanupBadExtensionKeys). | 154 // CleanupBadExtensionKeys). |
152 CleanupBadExtensionKeys(prefs_); | 155 CleanupBadExtensionKeys(prefs_); |
153 | 156 |
154 MakePathsRelative(); | 157 MakePathsRelative(); |
155 | 158 |
159 if (pref_store_) { | |
160 pref_store_->SetExtensionPrefValueMap(&extension_pref_value_map_); | |
161 extension_pref_value_map_.AddObserver(pref_store_); | |
162 } | |
163 if (incognito_pref_store_) { | |
164 incognito_pref_store_->SetExtensionPrefValueMap(&extension_pref_value_map_); | |
165 extension_pref_value_map_.AddObserver(incognito_pref_store_); | |
166 } | |
167 | |
156 InitPrefStore(); | 168 InitPrefStore(); |
157 } | 169 } |
158 | 170 |
159 ExtensionPrefs::~ExtensionPrefs() {} | 171 ExtensionPrefs::~ExtensionPrefs() { |
172 if (incognito_pref_store_) { | |
173 extension_pref_value_map_.RemoveObserver(incognito_pref_store_.get()); | |
174 incognito_pref_store_->SetExtensionPrefValueMap(NULL); | |
175 } | |
176 if (pref_store_) { | |
177 extension_pref_value_map_.RemoveObserver(pref_store_.get()); | |
178 pref_store_->SetExtensionPrefValueMap(NULL); | |
179 } | |
180 } | |
160 | 181 |
161 // static | 182 // static |
162 const char ExtensionPrefs::kExtensionsPref[] = "extensions.settings"; | 183 const char ExtensionPrefs::kExtensionsPref[] = "extensions.settings"; |
163 | 184 |
164 static FilePath::StringType MakePathRelative(const FilePath& parent, | 185 static FilePath::StringType MakePathRelative(const FilePath& parent, |
165 const FilePath& child, | 186 const FilePath& child, |
166 bool *dirty) { | 187 bool *dirty) { |
167 if (!parent.IsParent(child)) | 188 if (!parent.IsParent(child)) |
168 return child.value(); | 189 return child.value(); |
169 | 190 |
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 extension->path(), NULL); | 711 extension->path(), NULL); |
691 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); | 712 UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path)); |
692 // We store prefs about LOAD extensions, but don't cache their manifest | 713 // We store prefs about LOAD extensions, but don't cache their manifest |
693 // since it may change on disk. | 714 // since it may change on disk. |
694 if (extension->location() != Extension::LOAD) { | 715 if (extension->location() != Extension::LOAD) { |
695 UpdateExtensionPref(id, kPrefManifest, | 716 UpdateExtensionPref(id, kPrefManifest, |
696 extension->manifest_value()->DeepCopy()); | 717 extension->manifest_value()->DeepCopy()); |
697 } | 718 } |
698 UpdateExtensionPref(id, kPrefAppLaunchIndex, | 719 UpdateExtensionPref(id, kPrefAppLaunchIndex, |
699 Value::CreateIntegerValue(GetNextAppLaunchIndex())); | 720 Value::CreateIntegerValue(GetNextAppLaunchIndex())); |
721 extension_pref_value_map_.RegisterExtension( | |
722 id, install_time, initial_state == Extension::ENABLED); | |
700 SavePrefsAndNotify(); | 723 SavePrefsAndNotify(); |
701 } | 724 } |
702 | 725 |
703 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, | 726 void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id, |
704 const Extension::Location& location, | 727 const Extension::Location& location, |
705 bool external_uninstall) { | 728 bool external_uninstall) { |
706 PrefKeySet pref_keys; | |
707 GetExtensionControlledPrefKeys(extension_id, &pref_keys); | |
708 | |
709 // For external extensions, we save a preference reminding ourself not to try | 729 // For external extensions, we save a preference reminding ourself not to try |
710 // and install the extension anymore (except when |external_uninstall| is | 730 // and install the extension anymore (except when |external_uninstall| is |
711 // true, which signifies that the registry key was deleted or the pref file | 731 // true, which signifies that the registry key was deleted or the pref file |
712 // no longer lists the extension). | 732 // no longer lists the extension). |
713 if (!external_uninstall && Extension::IsExternalLocation(location)) { | 733 if (!external_uninstall && Extension::IsExternalLocation(location)) { |
714 UpdateExtensionPref(extension_id, kPrefState, | 734 UpdateExtensionPref(extension_id, kPrefState, |
715 Value::CreateIntegerValue(Extension::KILLBIT)); | 735 Value::CreateIntegerValue(Extension::KILLBIT)); |
716 SavePrefsAndNotify(); | 736 SavePrefsAndNotify(); |
737 extension_pref_value_map_.UpdateExtensionsState(extension_id, false); | |
717 } else { | 738 } else { |
739 extension_pref_value_map_.UnregisterExtension(extension_id); | |
718 DeleteExtensionPrefs(extension_id); | 740 DeleteExtensionPrefs(extension_id); |
719 } | 741 } |
720 | |
721 UpdatePrefStore(pref_keys); | |
722 } | 742 } |
723 | 743 |
724 Extension::State ExtensionPrefs::GetExtensionState( | 744 Extension::State ExtensionPrefs::GetExtensionState( |
725 const std::string& extension_id) const { | 745 const std::string& extension_id) const { |
726 DictionaryValue* extension = GetExtensionPref(extension_id); | 746 DictionaryValue* extension = GetExtensionPref(extension_id); |
727 | 747 |
728 // If the extension doesn't have a pref, it's a --load-extension. | 748 // If the extension doesn't have a pref, it's a --load-extension. |
729 if (!extension) | 749 if (!extension) |
730 return Extension::ENABLED; | 750 return Extension::ENABLED; |
731 | 751 |
732 int state = -1; | 752 int state = -1; |
733 if (!extension->GetInteger(kPrefState, &state) || | 753 if (!extension->GetInteger(kPrefState, &state) || |
734 state < 0 || state >= Extension::NUM_STATES) { | 754 state < 0 || state >= Extension::NUM_STATES) { |
735 LOG(ERROR) << "Bad or missing pref 'state' for extension '" | 755 LOG(ERROR) << "Bad or missing pref 'state' for extension '" |
736 << extension_id << "'"; | 756 << extension_id << "'"; |
737 return Extension::ENABLED; | 757 return Extension::ENABLED; |
738 } | 758 } |
739 return static_cast<Extension::State>(state); | 759 return static_cast<Extension::State>(state); |
740 } | 760 } |
741 | 761 |
742 void ExtensionPrefs::SetExtensionState(const Extension* extension, | 762 void ExtensionPrefs::SetExtensionState(const Extension* extension, |
743 Extension::State state) { | 763 Extension::State state) { |
744 UpdateExtensionPref(extension->id(), kPrefState, | 764 UpdateExtensionPref(extension->id(), kPrefState, |
745 Value::CreateIntegerValue(state)); | 765 Value::CreateIntegerValue(state)); |
766 SavePrefsAndNotify(); | |
746 | 767 |
747 PrefKeySet pref_keys; | 768 bool enabled = (state == Extension::ENABLED); |
748 GetExtensionControlledPrefKeys(extension->id(), &pref_keys); | 769 extension_pref_value_map_.UpdateExtensionsState(extension->id(), enabled); |
749 UpdatePrefStore(pref_keys); | |
750 | |
751 SavePrefsAndNotify(); | |
752 } | 770 } |
753 | 771 |
754 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { | 772 bool ExtensionPrefs::GetBrowserActionVisibility(const Extension* extension) { |
755 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); | 773 DictionaryValue* extension_prefs = GetExtensionPref(extension->id()); |
756 if (!extension_prefs) | 774 if (!extension_prefs) |
757 return true; | 775 return true; |
758 bool visible = false; | 776 bool visible = false; |
759 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) | 777 if (!extension_prefs->GetBoolean(kBrowserActionVisible, &visible) || visible) |
760 return true; | 778 return true; |
761 | 779 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
816 Value* data_value) { | 834 Value* data_value) { |
817 if (!Extension::IdIsValid(extension_id)) { | 835 if (!Extension::IdIsValid(extension_id)) { |
818 NOTREACHED() << "Invalid extension_id " << extension_id; | 836 NOTREACHED() << "Invalid extension_id " << extension_id; |
819 return; | 837 return; |
820 } | 838 } |
821 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); | 839 DictionaryValue* extension = GetOrCreateExtensionPref(extension_id); |
822 extension->Set(key, data_value); | 840 extension->Set(key, data_value); |
823 } | 841 } |
824 | 842 |
825 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { | 843 void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) { |
844 if (!prefs_) // May be null for unit tests. | |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Is that actually the case? Why not just pass a Tes
battre
2011/01/05 20:23:08
Is not NULL any more.
Removed.
| |
845 return; | |
826 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 846 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
827 if (dict->HasKey(extension_id)) { | 847 if (dict->HasKey(extension_id)) { |
828 dict->Remove(extension_id, NULL); | 848 dict->Remove(extension_id, NULL); |
829 SavePrefsAndNotify(); | 849 SavePrefsAndNotify(); |
830 } | 850 } |
851 extension_pref_value_map_.UnregisterExtension(extension_id); | |
831 } | 852 } |
832 | 853 |
833 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( | 854 DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref( |
834 const std::string& extension_id) { | 855 const std::string& extension_id) { |
835 DCHECK(Extension::IdIsValid(extension_id)); | 856 DCHECK(Extension::IdIsValid(extension_id)); |
836 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); | 857 DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref); |
837 DictionaryValue* extension = NULL; | 858 DictionaryValue* extension = NULL; |
838 if (!dict->GetDictionary(extension_id, &extension)) { | 859 if (!dict->GetDictionary(extension_id, &extension)) { |
839 // Extension pref does not exist, create it. | 860 // Extension pref does not exist, create it. |
840 extension = new DictionaryValue(); | 861 extension = new DictionaryValue(); |
841 dict->Set(extension_id, extension); | 862 dict->Set(extension_id, extension); |
842 } | 863 } |
843 return extension; | 864 return extension; |
844 } | 865 } |
845 | 866 |
846 DictionaryValue* ExtensionPrefs::GetExtensionPref( | 867 DictionaryValue* ExtensionPrefs::GetExtensionPref( |
847 const std::string& extension_id) const { | 868 const std::string& extension_id) const { |
848 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); | 869 const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref); |
849 if (!dict) | 870 if (!dict) |
850 return NULL; | 871 return NULL; |
851 DictionaryValue* extension = NULL; | 872 DictionaryValue* extension = NULL; |
852 dict->GetDictionary(extension_id, &extension); | 873 dict->GetDictionary(extension_id, &extension); |
853 return extension; | 874 return extension; |
854 } | 875 } |
855 | 876 |
856 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | |
857 const std::string& extension_id) const { | |
858 DictionaryValue* extension = GetExtensionPref(extension_id); | |
859 if (!extension) { | |
860 NOTREACHED(); | |
861 return NULL; | |
862 } | |
863 DictionaryValue* preferences = NULL; | |
864 extension->GetDictionary(kPrefPreferences, &preferences); | |
865 return preferences; | |
866 } | |
867 | |
868 // Helper function for GetInstalledExtensionsInfo. | 877 // Helper function for GetInstalledExtensionsInfo. |
869 static ExtensionInfo* GetInstalledExtensionInfoImpl( | 878 static ExtensionInfo* GetInstalledExtensionInfoImpl( |
870 DictionaryValue* extension_data, | 879 DictionaryValue* extension_data, |
871 DictionaryValue::key_iterator extension_id) { | 880 DictionaryValue::key_iterator extension_id) { |
872 DictionaryValue* ext; | 881 DictionaryValue* ext; |
873 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { | 882 if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) { |
874 LOG(WARNING) << "Invalid pref for extension " << *extension_id; | 883 LOG(WARNING) << "Invalid pref for extension " << *extension_id; |
875 NOTREACHED(); | 884 NOTREACHED(); |
876 return NULL; | 885 return NULL; |
877 } | 886 } |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1137 } | 1146 } |
1138 std::string install_time_str("0"); | 1147 std::string install_time_str("0"); |
1139 extension->GetString(kPrefInstallTime, &install_time_str); | 1148 extension->GetString(kPrefInstallTime, &install_time_str); |
1140 int64 install_time_i64 = 0; | 1149 int64 install_time_i64 = 0; |
1141 base::StringToInt64(install_time_str, &install_time_i64); | 1150 base::StringToInt64(install_time_str, &install_time_i64); |
1142 LOG_IF(ERROR, install_time_i64 == 0) | 1151 LOG_IF(ERROR, install_time_i64 == 0) |
1143 << "Error parsing installation time of an extension."; | 1152 << "Error parsing installation time of an extension."; |
1144 return base::Time::FromInternalValue(install_time_i64); | 1153 return base::Time::FromInternalValue(install_time_i64); |
1145 } | 1154 } |
1146 | 1155 |
1147 void ExtensionPrefs::GetEnabledExtensions(ExtensionIdSet* out) const { | 1156 void ExtensionPrefs::GetExtensions(ExtensionIdSet* out) const { |
1148 CHECK(out); | 1157 CHECK(out); |
1149 const DictionaryValue* extensions = | 1158 const DictionaryValue* extensions = |
1150 pref_service()->GetDictionary(kExtensionsPref); | 1159 pref_service()->GetDictionary(kExtensionsPref); |
1151 | 1160 |
1152 for (DictionaryValue::key_iterator ext_id = extensions->begin_keys(); | 1161 for (DictionaryValue::key_iterator ext_id = extensions->begin_keys(); |
1153 ext_id != extensions->end_keys(); ++ext_id) { | 1162 ext_id != extensions->end_keys(); ++ext_id) { |
1154 if (GetExtensionState(*ext_id) != Extension::ENABLED) | |
1155 continue; | |
1156 out->push_back(*ext_id); | 1163 out->push_back(*ext_id); |
1157 } | 1164 } |
1158 } | 1165 } |
1159 | 1166 |
1160 void ExtensionPrefs::FixMissingPrefs(const ExtensionIdSet& extension_ids) { | 1167 void ExtensionPrefs::FixMissingPrefs(const ExtensionIdSet& extension_ids) { |
1161 // Fix old entries that did not get an installation time entry when they | 1168 // Fix old entries that did not get an installation time entry when they |
1162 // were installed or don't have a preferences field. | 1169 // were installed or don't have a preferences field. |
1163 bool persist_required = false; | 1170 bool persist_required = false; |
1164 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin(); | 1171 for (ExtensionIdSet::const_iterator ext_id = extension_ids.begin(); |
1165 ext_id != extension_ids.end(); ++ext_id) { | 1172 ext_id != extension_ids.end(); ++ext_id) { |
1166 DictionaryValue* extension = GetExtensionPref(*ext_id); | 1173 DictionaryValue* extension = GetExtensionPref(*ext_id); |
1167 CHECK(extension); | 1174 CHECK(extension); |
1168 | 1175 |
1169 if (GetInstallTime(*ext_id) == base::Time()) { | 1176 if (GetInstallTime(*ext_id) == base::Time()) { |
1170 const base::Time install_time = GetCurrentTime(); | 1177 const base::Time install_time = GetCurrentTime(); |
1171 extension->Set(kPrefInstallTime, | 1178 extension->Set(kPrefInstallTime, |
1172 Value::CreateStringValue( | 1179 Value::CreateStringValue( |
1173 base::Int64ToString(install_time.ToInternalValue()))); | 1180 base::Int64ToString(install_time.ToInternalValue()))); |
1174 persist_required = true; | 1181 persist_required = true; |
1175 } | 1182 } |
1176 } | 1183 } |
1177 if (persist_required) | 1184 if (persist_required) |
1178 SavePrefsAndNotify(); | 1185 SavePrefsAndNotify(); |
1179 } | 1186 } |
1180 | 1187 |
1188 DictionaryValue* ExtensionPrefs::GetExtensionControlledPrefs( | |
1189 const std::string& extension_id) const { | |
1190 DictionaryValue* source_dict = prefs_->GetMutableDictionary(kExtensionsPref); | |
1191 DictionaryValue* preferences = NULL; | |
1192 std::string key = extension_id + std::string(".") + kPrefPreferences; | |
1193 if (!source_dict->GetDictionary(key, &preferences)) { | |
1194 source_dict->Set(key, new DictionaryValue); | |
1195 bool success = source_dict->GetDictionary(key, &preferences); | |
1196 DCHECK(success); | |
1197 } | |
1198 return preferences; | |
1199 } | |
1200 | |
1181 void ExtensionPrefs::InitPrefStore() { | 1201 void ExtensionPrefs::InitPrefStore() { |
1182 // When this is called, the PrefService is initialized and provides access | 1202 // When this is called, the PrefService is initialized and provides access |
1183 // to the user preferences stored in a JSON file. | 1203 // to the user preferences stored in a JSON file. |
1184 ExtensionIdSet extension_ids; | 1204 ExtensionIdSet extension_ids; |
1185 GetEnabledExtensions(&extension_ids); | 1205 GetExtensions(&extension_ids); |
1186 FixMissingPrefs(extension_ids); | 1206 FixMissingPrefs(extension_ids); |
1187 | 1207 |
1188 // Collect the unique extension controlled preference keys of all extensions. | 1208 // Collect the unique extension controlled preference keys of all extensions. |
1189 PrefKeySet ext_controlled_prefs; | |
1190 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | 1209 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); |
1191 ext_id != extension_ids.end(); ++ext_id) { | 1210 ext_id != extension_ids.end(); ++ext_id) { |
1192 GetExtensionControlledPrefKeys(*ext_id, &ext_controlled_prefs); | 1211 extension_pref_value_map_.RegisterExtension( |
1212 *ext_id, | |
1213 GetInstallTime(*ext_id), | |
1214 GetExtensionState(*ext_id) == Extension::ENABLED); | |
1215 | |
1216 DictionaryValue* prefs = GetExtensionControlledPrefs(*ext_id); | |
1217 for (DictionaryValue::key_iterator i = prefs->begin_keys(); | |
1218 i != prefs->end_keys(); ++i) { | |
1219 Value* value; | |
1220 if (!prefs->GetWithoutPathExpansion(*i, &value)) | |
1221 continue; | |
1222 extension_pref_value_map_.SetExtensionPref( | |
1223 *ext_id, *i, false, value->DeepCopy()); | |
1224 } | |
1193 } | 1225 } |
1194 | 1226 |
1195 // Store winning preference for each extension controlled preference. | 1227 // Store winning preference for each extension controlled preference. |
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Comment is outdated.
battre
2011/01/05 20:23:08
Replaced "Collect the uniqe..." with this.
Done.
| |
1196 UpdatePrefStore(ext_controlled_prefs); | 1228 extension_pref_value_map_.OnInitializationCompleted(); |
1197 pref_store_->OnInitializationCompleted(); | |
1198 } | 1229 } |
1199 | 1230 |
1200 const Value* ExtensionPrefs::GetWinningExtensionControlledPrefValue( | |
1201 const std::string& key) const { | |
1202 Value *winner = NULL; | |
1203 base::Time winners_install_time = base::Time(); | |
1204 | |
1205 ExtensionIdSet extension_ids; | |
1206 GetEnabledExtensions(&extension_ids); | |
1207 for (ExtensionIdSet::iterator ext_id = extension_ids.begin(); | |
1208 ext_id != extension_ids.end(); ++ext_id) { | |
1209 base::Time extension_install_time = GetInstallTime(*ext_id); | |
1210 | |
1211 // We do not need to consider extensions that were installed before the | |
1212 // most recent extension found that provides the requested preference. | |
1213 if (extension_install_time < winners_install_time) | |
1214 continue; | |
1215 | |
1216 DictionaryValue* preferences = GetExtensionControlledPrefs(*ext_id); | |
1217 Value *value = NULL; | |
1218 if (preferences && preferences->GetWithoutPathExpansion(key, &value)) { | |
1219 // This extension is more recent than the last one providing this pref. | |
1220 winner = value; | |
1221 winners_install_time = extension_install_time; | |
1222 } | |
1223 } | |
1224 | |
1225 return winner; | |
1226 } | |
1227 | |
1228 void ExtensionPrefs::UpdatePrefStore( | |
1229 const ExtensionPrefs::PrefKeySet& pref_keys) { | |
1230 for (PrefKeySet::const_iterator i = pref_keys.begin(); | |
1231 i != pref_keys.end(); ++i) { | |
1232 UpdatePrefStore(*i); | |
1233 } | |
1234 } | |
1235 | |
1236 void ExtensionPrefs::UpdatePrefStore(const std::string& pref_key) { | |
1237 if (pref_store_ == NULL) | |
1238 return; | |
1239 const Value* winning_pref_value = | |
1240 GetWinningExtensionControlledPrefValue(pref_key); | |
1241 | |
1242 if (winning_pref_value) | |
1243 pref_store_->SetExtensionPref(pref_key, winning_pref_value->DeepCopy()); | |
1244 else | |
1245 pref_store_->RemoveExtensionPref(pref_key); | |
1246 } | |
1247 | 1231 |
1248 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, | 1232 void ExtensionPrefs::SetExtensionControlledPref(const std::string& extension_id, |
1249 const std::string& pref_key, | 1233 const std::string& pref_key, |
1234 bool incognito, | |
1250 Value* value) { | 1235 Value* value) { |
1251 scoped_ptr<Value> scoped_value(value); | |
1252 DCHECK(pref_service()->FindPreference(pref_key.c_str())) | 1236 DCHECK(pref_service()->FindPreference(pref_key.c_str())) |
1253 << "Extension controlled preference key " << pref_key | 1237 << "Extension controlled preference key " << pref_key |
1254 << " not registered."; | 1238 << " not registered."; |
1255 DictionaryValue* extension_preferences = | |
1256 GetExtensionControlledPrefs(extension_id); | |
1257 | 1239 |
1258 if (extension_preferences == NULL) { // May be pruned when writing to disk. | 1240 if (!incognito) { |
1259 DictionaryValue* extension = GetExtensionPref(extension_id); | 1241 // Also store in persisted Preferences file to recover after a |
1260 if (extension == NULL) { | 1242 // browser restart. |
1261 LOG(ERROR) << "Extension preference for " << extension_id << " undefined"; | 1243 DictionaryValue* dict = GetExtensionControlledPrefs(extension_id); |
1262 return; | 1244 dict->SetWithoutPathExpansion(pref_key, value->DeepCopy()); |
1263 } | 1245 pref_service()->ScheduleSavePersistentPrefs(); |
1264 extension_preferences = new DictionaryValue; | |
1265 extension->Set(kPrefPreferences, extension_preferences); | |
1266 } | 1246 } |
1267 | 1247 |
1268 Value* oldValue = NULL; | 1248 extension_pref_value_map_.SetExtensionPref( |
1269 extension_preferences->GetWithoutPathExpansion(pref_key, &oldValue); | 1249 extension_id, pref_key, incognito, value); |
1270 bool modified = !Value::Equals(oldValue, scoped_value.get()); | |
1271 if (!modified) | |
1272 return; | |
1273 | |
1274 if (scoped_value.get() == NULL) | |
1275 extension_preferences->RemoveWithoutPathExpansion(pref_key, NULL); | |
1276 else | |
1277 extension_preferences->SetWithoutPathExpansion(pref_key, | |
1278 scoped_value.release()); | |
1279 pref_service()->ScheduleSavePersistentPrefs(); | |
1280 | |
1281 UpdatePrefStore(pref_key); | |
1282 } | 1250 } |
1283 | 1251 |
1284 void ExtensionPrefs::GetExtensionControlledPrefKeys( | 1252 PrefService* ExtensionPrefs::CreateIncognitoPrefService() const { |
1285 const std::string& extension_id, PrefKeySet *out) const { | 1253 return pref_service()->CreateIncognitoPrefService( |
1286 DCHECK(out != NULL); | 1254 incognito_pref_store_.get()); |
1287 DictionaryValue* ext_prefs = GetExtensionControlledPrefs(extension_id); | |
1288 if (ext_prefs) { | |
1289 for (DictionaryValue::key_iterator i = ext_prefs->begin_keys(); | |
1290 i != ext_prefs->end_keys(); ++i) { | |
1291 out->insert(*i); | |
1292 } | |
1293 } | |
1294 } | 1255 } |
1295 | 1256 |
1296 // static | 1257 // static |
1297 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { | 1258 void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) { |
1298 prefs->RegisterDictionaryPref(kExtensionsPref); | 1259 prefs->RegisterDictionaryPref(kExtensionsPref); |
1299 prefs->RegisterListPref(kExtensionToolbar); | 1260 prefs->RegisterListPref(kExtensionToolbar); |
1300 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); | 1261 prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1); |
1301 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); | 1262 prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate); |
1302 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); | 1263 prefs->RegisterListPref(prefs::kExtensionInstallAllowList); |
1303 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); | 1264 prefs->RegisterListPref(prefs::kExtensionInstallDenyList); |
1304 prefs->RegisterListPref(prefs::kExtensionInstallForceList); | 1265 prefs->RegisterListPref(prefs::kExtensionInstallForceList); |
1305 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); | 1266 prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */); |
1306 } | 1267 } |
OLD | NEW |