OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/admin_policy.h" | 10 #include "chrome/browser/extensions/admin_policy.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 | 166 |
167 // A preference that contains extension-set content settings. | 167 // A preference that contains extension-set content settings. |
168 const char kPrefIncognitoContentSettings[] = "incognito_content_settings"; | 168 const char kPrefIncognitoContentSettings[] = "incognito_content_settings"; |
169 | 169 |
170 // A list of event names that this extension has registered from its lazy | 170 // A list of event names that this extension has registered from its lazy |
171 // background page. | 171 // background page. |
172 const char kRegisteredEvents[] = "events"; | 172 const char kRegisteredEvents[] = "events"; |
173 | 173 |
174 // A list of alarms that this extension has set. | 174 // A list of alarms that this extension has set. |
175 const char kRegisteredAlarms[] = "alarms"; | 175 const char kRegisteredAlarms[] = "alarms"; |
176 const char kAlarmScheduledRunTime[] = "scheduled_run_time"; | 176 const char kAlarmGranularity[] = "granularity"; |
177 | 177 |
178 // Persisted value for omnibox.setDefaultSuggestion. | 178 // Persisted value for omnibox.setDefaultSuggestion. |
179 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; | 179 const char kOmniboxDefaultSuggestion[] = "omnibox_default_suggestion"; |
180 | 180 |
181 // A preference containing context menu items, persisted for event pages. | 181 // A preference containing context menu items, persisted for event pages. |
182 const char kPrefContextMenus[] = "context_menus"; | 182 const char kPrefContextMenus[] = "context_menus"; |
183 | 183 |
184 // Provider of write access to a dictionary storing extension prefs. | 184 // Provider of write access to a dictionary storing extension prefs. |
185 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { | 185 class ScopedExtensionPrefUpdate : public DictionaryPrefUpdate { |
186 public: | 186 public: |
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
755 Value::CreateBooleanValue(true)); | 755 Value::CreateBooleanValue(true)); |
756 } | 756 } |
757 } | 757 } |
758 for (unsigned int i = 0; i < remove_pref_ids.size(); ++i) { | 758 for (unsigned int i = 0; i < remove_pref_ids.size(); ++i) { |
759 DeleteExtensionPrefs(remove_pref_ids[i]); | 759 DeleteExtensionPrefs(remove_pref_ids[i]); |
760 } | 760 } |
761 } | 761 } |
762 | 762 |
763 namespace { | 763 namespace { |
764 | 764 |
765 // Serializes |time| as a string value mapped to |key| in |dictionary|. | 765 // Serializes |value| as a string value mapped to |key| in |dictionary|. |
766 void SaveTime(DictionaryValue* dictionary, | 766 template<typename T> |
767 const char* key, | 767 void Save(DictionaryValue* dictionary, |
Matt Perry
2012/06/11 22:32:44
I would still call this SaveTime since it only wor
Jeffrey Yasskin
2012/06/12 19:12:31
Done.
| |
768 const base::Time& time) { | 768 const char* key, |
769 const T& value) { | |
769 if (!dictionary) | 770 if (!dictionary) |
770 return; | 771 return; |
771 std::string string_value = base::Int64ToString(time.ToInternalValue()); | 772 std::string string_value = base::Int64ToString(value.ToInternalValue()); |
772 dictionary->SetString(key, string_value); | 773 dictionary->SetString(key, string_value); |
773 } | 774 } |
774 | 775 |
775 // The opposite of SaveTime. If |key| is not found, this returns an empty Time | 776 // The opposite of Save. If |key| is not found, this returns a |
776 // (is_null() will return true). | 777 // default-constructed T (for Times and TimeDeltas, is_null() will return true). |
777 base::Time ReadTime(const DictionaryValue* dictionary, const char* key) { | 778 template<typename T> |
779 T Read(const DictionaryValue* dictionary, const char* key) { | |
778 if (!dictionary) | 780 if (!dictionary) |
779 return base::Time(); | 781 return T(); |
780 std::string string_value; | 782 std::string string_value; |
781 int64 value; | 783 int64 value; |
782 if (dictionary->GetString(key, &string_value)) { | 784 if (dictionary->GetString(key, &string_value)) { |
783 if (base::StringToInt64(string_value, &value)) { | 785 if (base::StringToInt64(string_value, &value)) { |
784 return base::Time::FromInternalValue(value); | 786 return T::FromInternalValue(value); |
785 } | 787 } |
786 } | 788 } |
787 return base::Time(); | 789 return T(); |
788 } | 790 } |
789 | 791 |
790 } // namespace | 792 } // namespace |
791 | 793 |
792 base::Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { | 794 base::Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { |
793 DCHECK(Extension::IdIsValid(extension_id)); | 795 DCHECK(Extension::IdIsValid(extension_id)); |
794 return ReadTime(GetExtensionPref(extension_id), kLastPingDay); | 796 return Read<base::Time>(GetExtensionPref(extension_id), kLastPingDay); |
795 } | 797 } |
796 | 798 |
797 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, | 799 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, |
798 const base::Time& time) { | 800 const base::Time& time) { |
799 DCHECK(Extension::IdIsValid(extension_id)); | 801 DCHECK(Extension::IdIsValid(extension_id)); |
800 ScopedExtensionPrefUpdate update(prefs_, extension_id); | 802 ScopedExtensionPrefUpdate update(prefs_, extension_id); |
801 SaveTime(update.Get(), kLastPingDay, time); | 803 Save<base::Time>(update.Get(), kLastPingDay, time); |
802 } | 804 } |
803 | 805 |
804 base::Time ExtensionPrefs::BlacklistLastPingDay() const { | 806 base::Time ExtensionPrefs::BlacklistLastPingDay() const { |
805 return ReadTime(prefs_->GetDictionary(kExtensionsBlacklistUpdate), | 807 return Read<base::Time>(prefs_->GetDictionary(kExtensionsBlacklistUpdate), |
806 kLastPingDay); | 808 kLastPingDay); |
807 } | 809 } |
808 | 810 |
809 void ExtensionPrefs::SetBlacklistLastPingDay(const base::Time& time) { | 811 void ExtensionPrefs::SetBlacklistLastPingDay(const base::Time& time) { |
810 DictionaryPrefUpdate update(prefs_, kExtensionsBlacklistUpdate); | 812 DictionaryPrefUpdate update(prefs_, kExtensionsBlacklistUpdate); |
811 SaveTime(update.Get(), kLastPingDay, time); | 813 Save<base::Time>(update.Get(), kLastPingDay, time); |
812 } | 814 } |
813 | 815 |
814 base::Time ExtensionPrefs::LastActivePingDay(const std::string& extension_id) { | 816 base::Time ExtensionPrefs::LastActivePingDay(const std::string& extension_id) { |
815 DCHECK(Extension::IdIsValid(extension_id)); | 817 DCHECK(Extension::IdIsValid(extension_id)); |
816 return ReadTime(GetExtensionPref(extension_id), kLastActivePingDay); | 818 return Read<base::Time>(GetExtensionPref(extension_id), kLastActivePingDay); |
817 } | 819 } |
818 | 820 |
819 void ExtensionPrefs::SetLastActivePingDay(const std::string& extension_id, | 821 void ExtensionPrefs::SetLastActivePingDay(const std::string& extension_id, |
820 const base::Time& time) { | 822 const base::Time& time) { |
821 DCHECK(Extension::IdIsValid(extension_id)); | 823 DCHECK(Extension::IdIsValid(extension_id)); |
822 ScopedExtensionPrefUpdate update(prefs_, extension_id); | 824 ScopedExtensionPrefUpdate update(prefs_, extension_id); |
823 SaveTime(update.Get(), kLastActivePingDay, time); | 825 Save<base::Time>(update.Get(), kLastActivePingDay, time); |
824 } | 826 } |
825 | 827 |
826 bool ExtensionPrefs::GetActiveBit(const std::string& extension_id) { | 828 bool ExtensionPrefs::GetActiveBit(const std::string& extension_id) { |
827 const DictionaryValue* dictionary = GetExtensionPref(extension_id); | 829 const DictionaryValue* dictionary = GetExtensionPref(extension_id); |
828 bool result = false; | 830 bool result = false; |
829 if (dictionary && dictionary->GetBoolean(kActiveBit, &result)) | 831 if (dictionary && dictionary->GetBoolean(kActiveBit, &result)) |
830 return result; | 832 return result; |
831 return false; | 833 return false; |
832 } | 834 } |
833 | 835 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
950 void ExtensionPrefs::SetRegisteredEvents( | 952 void ExtensionPrefs::SetRegisteredEvents( |
951 const std::string& extension_id, const std::set<std::string>& events) { | 953 const std::string& extension_id, const std::set<std::string>& events) { |
952 ListValue* value = new ListValue(); | 954 ListValue* value = new ListValue(); |
953 for (std::set<std::string>::const_iterator it = events.begin(); | 955 for (std::set<std::string>::const_iterator it = events.begin(); |
954 it != events.end(); ++it) { | 956 it != events.end(); ++it) { |
955 value->Append(new StringValue(*it)); | 957 value->Append(new StringValue(*it)); |
956 } | 958 } |
957 UpdateExtensionPref(extension_id, kRegisteredEvents, value); | 959 UpdateExtensionPref(extension_id, kRegisteredEvents, value); |
958 } | 960 } |
959 | 961 |
960 std::vector<extensions::AlarmPref> ExtensionPrefs::GetRegisteredAlarms( | 962 std::vector<extensions::Alarm> ExtensionPrefs::GetRegisteredAlarms( |
961 const std::string& extension_id) { | 963 const std::string& extension_id) { |
962 std::vector<extensions::AlarmPref> alarms; | 964 std::vector<extensions::Alarm> alarms; |
963 const base::DictionaryValue* extension = GetExtensionPref(extension_id); | 965 const base::DictionaryValue* extension = GetExtensionPref(extension_id); |
964 if (!extension) | 966 if (!extension) |
965 return alarms; | 967 return alarms; |
966 | 968 |
967 base::ListValue* list = NULL; | 969 base::ListValue* list = NULL; |
968 if (!extension->GetList(kRegisteredAlarms, &list)) | 970 if (!extension->GetList(kRegisteredAlarms, &list)) |
969 return alarms; | 971 return alarms; |
970 | 972 |
971 typedef extensions::AlarmManager::Alarm Alarm; | |
972 for (size_t i = 0; i < list->GetSize(); ++i) { | 973 for (size_t i = 0; i < list->GetSize(); ++i) { |
973 base::DictionaryValue* alarm_dict = NULL; | 974 base::DictionaryValue* alarm_dict = NULL; |
974 extensions::AlarmPref alarm; | 975 extensions::Alarm alarm; |
975 alarm.alarm.reset(new Alarm()); | |
976 if (list->GetDictionary(i, &alarm_dict) && | 976 if (list->GetDictionary(i, &alarm_dict) && |
977 Alarm::Populate(*alarm_dict, alarm.alarm.get())) { | 977 extensions::api::alarms::Alarm::Populate(*alarm_dict, |
978 alarm.scheduled_run_time = ReadTime(alarm_dict, kAlarmScheduledRunTime); | 978 alarm.js_alarm.get())) { |
979 alarm.granularity = Read<base::TimeDelta>(alarm_dict, kAlarmGranularity); | |
979 alarms.push_back(alarm); | 980 alarms.push_back(alarm); |
980 } | 981 } |
981 } | 982 } |
982 return alarms; | 983 return alarms; |
983 } | 984 } |
984 | 985 |
985 void ExtensionPrefs::SetRegisteredAlarms( | 986 void ExtensionPrefs::SetRegisteredAlarms( |
986 const std::string& extension_id, | 987 const std::string& extension_id, |
987 const std::vector<extensions::AlarmPref>& alarms) { | 988 const std::vector<extensions::Alarm>& alarms) { |
988 base::ListValue* list = new ListValue(); | 989 base::ListValue* list = new ListValue(); |
989 for (size_t i = 0; i < alarms.size(); ++i) { | 990 for (size_t i = 0; i < alarms.size(); ++i) { |
990 scoped_ptr<base::DictionaryValue> alarm = alarms[i].alarm->ToValue().Pass(); | 991 scoped_ptr<base::DictionaryValue> alarm = |
991 SaveTime(alarm.get(), kAlarmScheduledRunTime, alarms[i].scheduled_run_time); | 992 alarms[i].js_alarm->ToValue().Pass(); |
993 Save<base::TimeDelta>(alarm.get(), kAlarmGranularity, | |
994 alarms[i].granularity); | |
992 list->Append(alarm.release()); | 995 list->Append(alarm.release()); |
993 } | 996 } |
994 UpdateExtensionPref(extension_id, kRegisteredAlarms, list); | 997 UpdateExtensionPref(extension_id, kRegisteredAlarms, list); |
995 } | 998 } |
996 | 999 |
997 extensions::ExtensionOmniboxSuggestion | 1000 extensions::ExtensionOmniboxSuggestion |
998 ExtensionPrefs::GetOmniboxDefaultSuggestion(const std::string& extension_id) { | 1001 ExtensionPrefs::GetOmniboxDefaultSuggestion(const std::string& extension_id) { |
999 extensions::ExtensionOmniboxSuggestion suggestion; | 1002 extensions::ExtensionOmniboxSuggestion suggestion; |
1000 | 1003 |
1001 const base::DictionaryValue* extension = GetExtensionPref(extension_id); | 1004 const base::DictionaryValue* extension = GetExtensionPref(extension_id); |
(...skipping 946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1948 PrefService::UNSYNCABLE_PREF); | 1951 PrefService::UNSYNCABLE_PREF); |
1949 prefs->RegisterInt64Pref(prefs::kLastExtensionsUpdateCheck, | 1952 prefs->RegisterInt64Pref(prefs::kLastExtensionsUpdateCheck, |
1950 0, // default value | 1953 0, // default value |
1951 PrefService::UNSYNCABLE_PREF); | 1954 PrefService::UNSYNCABLE_PREF); |
1952 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck, | 1955 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck, |
1953 0, // default value | 1956 0, // default value |
1954 PrefService::UNSYNCABLE_PREF); | 1957 PrefService::UNSYNCABLE_PREF); |
1955 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites, | 1958 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites, |
1956 PrefService::UNSYNCABLE_PREF); | 1959 PrefService::UNSYNCABLE_PREF); |
1957 } | 1960 } |
OLD | NEW |