Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/extensions/extension_prefs.cc

Issue 10545104: Refactor chrome.alarms interface to support absolute alarm deadlines. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Matt's comments, and re-merge create() Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 |time| as a string value mapped to |key| in |dictionary|.
766 // 'T' should be base::Time or base::TimeDelta.
767 template<typename T>
766 void SaveTime(DictionaryValue* dictionary, 768 void SaveTime(DictionaryValue* dictionary,
767 const char* key, 769 const char* key,
768 const base::Time& time) { 770 const T& time) {
769 if (!dictionary) 771 if (!dictionary)
770 return; 772 return;
771 std::string string_value = base::Int64ToString(time.ToInternalValue()); 773 std::string string_value = base::Int64ToString(time.ToInternalValue());
772 dictionary->SetString(key, string_value); 774 dictionary->SetString(key, string_value);
773 } 775 }
774 776
775 // The opposite of SaveTime. If |key| is not found, this returns an empty Time 777 // The opposite of SaveTime. If |key| is not found, this returns an empty Time
776 // (is_null() will return true). 778 // or TimeDelta (is_null() will return true).
777 base::Time ReadTime(const DictionaryValue* dictionary, const char* key) { 779 template<typename T>
780 T ReadTime(const DictionaryValue* dictionary, const char* key) {
778 if (!dictionary) 781 if (!dictionary)
779 return base::Time(); 782 return T();
780 std::string string_value; 783 std::string string_value;
781 int64 value; 784 int64 value;
782 if (dictionary->GetString(key, &string_value)) { 785 if (dictionary->GetString(key, &string_value)) {
783 if (base::StringToInt64(string_value, &value)) { 786 if (base::StringToInt64(string_value, &value)) {
784 return base::Time::FromInternalValue(value); 787 return T::FromInternalValue(value);
785 } 788 }
786 } 789 }
787 return base::Time(); 790 return T();
788 } 791 }
789 792
790 } // namespace 793 } // namespace
791 794
792 base::Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const { 795 base::Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const {
793 DCHECK(Extension::IdIsValid(extension_id)); 796 DCHECK(Extension::IdIsValid(extension_id));
794 return ReadTime(GetExtensionPref(extension_id), kLastPingDay); 797 return ReadTime<base::Time>(GetExtensionPref(extension_id), kLastPingDay);
795 } 798 }
796 799
797 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id, 800 void ExtensionPrefs::SetLastPingDay(const std::string& extension_id,
798 const base::Time& time) { 801 const base::Time& time) {
799 DCHECK(Extension::IdIsValid(extension_id)); 802 DCHECK(Extension::IdIsValid(extension_id));
800 ScopedExtensionPrefUpdate update(prefs_, extension_id); 803 ScopedExtensionPrefUpdate update(prefs_, extension_id);
801 SaveTime(update.Get(), kLastPingDay, time); 804 SaveTime<base::Time>(update.Get(), kLastPingDay, time);
802 } 805 }
803 806
804 base::Time ExtensionPrefs::BlacklistLastPingDay() const { 807 base::Time ExtensionPrefs::BlacklistLastPingDay() const {
805 return ReadTime(prefs_->GetDictionary(kExtensionsBlacklistUpdate), 808 return ReadTime<base::Time>(prefs_->GetDictionary(kExtensionsBlacklistUpdate),
806 kLastPingDay); 809 kLastPingDay);
807 } 810 }
808 811
809 void ExtensionPrefs::SetBlacklistLastPingDay(const base::Time& time) { 812 void ExtensionPrefs::SetBlacklistLastPingDay(const base::Time& time) {
810 DictionaryPrefUpdate update(prefs_, kExtensionsBlacklistUpdate); 813 DictionaryPrefUpdate update(prefs_, kExtensionsBlacklistUpdate);
811 SaveTime(update.Get(), kLastPingDay, time); 814 SaveTime<base::Time>(update.Get(), kLastPingDay, time);
812 } 815 }
813 816
814 base::Time ExtensionPrefs::LastActivePingDay(const std::string& extension_id) { 817 base::Time ExtensionPrefs::LastActivePingDay(const std::string& extension_id) {
815 DCHECK(Extension::IdIsValid(extension_id)); 818 DCHECK(Extension::IdIsValid(extension_id));
816 return ReadTime(GetExtensionPref(extension_id), kLastActivePingDay); 819 return ReadTime<base::Time>(GetExtensionPref(extension_id),
820 kLastActivePingDay);
817 } 821 }
818 822
819 void ExtensionPrefs::SetLastActivePingDay(const std::string& extension_id, 823 void ExtensionPrefs::SetLastActivePingDay(const std::string& extension_id,
820 const base::Time& time) { 824 const base::Time& time) {
821 DCHECK(Extension::IdIsValid(extension_id)); 825 DCHECK(Extension::IdIsValid(extension_id));
822 ScopedExtensionPrefUpdate update(prefs_, extension_id); 826 ScopedExtensionPrefUpdate update(prefs_, extension_id);
823 SaveTime(update.Get(), kLastActivePingDay, time); 827 SaveTime<base::Time>(update.Get(), kLastActivePingDay, time);
824 } 828 }
825 829
826 bool ExtensionPrefs::GetActiveBit(const std::string& extension_id) { 830 bool ExtensionPrefs::GetActiveBit(const std::string& extension_id) {
827 const DictionaryValue* dictionary = GetExtensionPref(extension_id); 831 const DictionaryValue* dictionary = GetExtensionPref(extension_id);
828 bool result = false; 832 bool result = false;
829 if (dictionary && dictionary->GetBoolean(kActiveBit, &result)) 833 if (dictionary && dictionary->GetBoolean(kActiveBit, &result))
830 return result; 834 return result;
831 return false; 835 return false;
832 } 836 }
833 837
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 void ExtensionPrefs::SetRegisteredEvents( 954 void ExtensionPrefs::SetRegisteredEvents(
951 const std::string& extension_id, const std::set<std::string>& events) { 955 const std::string& extension_id, const std::set<std::string>& events) {
952 ListValue* value = new ListValue(); 956 ListValue* value = new ListValue();
953 for (std::set<std::string>::const_iterator it = events.begin(); 957 for (std::set<std::string>::const_iterator it = events.begin();
954 it != events.end(); ++it) { 958 it != events.end(); ++it) {
955 value->Append(new StringValue(*it)); 959 value->Append(new StringValue(*it));
956 } 960 }
957 UpdateExtensionPref(extension_id, kRegisteredEvents, value); 961 UpdateExtensionPref(extension_id, kRegisteredEvents, value);
958 } 962 }
959 963
960 std::vector<extensions::AlarmPref> ExtensionPrefs::GetRegisteredAlarms( 964 std::vector<extensions::Alarm> ExtensionPrefs::GetRegisteredAlarms(
961 const std::string& extension_id) { 965 const std::string& extension_id) {
962 std::vector<extensions::AlarmPref> alarms; 966 std::vector<extensions::Alarm> alarms;
963 const base::DictionaryValue* extension = GetExtensionPref(extension_id); 967 const base::DictionaryValue* extension = GetExtensionPref(extension_id);
964 if (!extension) 968 if (!extension)
965 return alarms; 969 return alarms;
966 970
967 base::ListValue* list = NULL; 971 base::ListValue* list = NULL;
968 if (!extension->GetList(kRegisteredAlarms, &list)) 972 if (!extension->GetList(kRegisteredAlarms, &list))
969 return alarms; 973 return alarms;
970 974
971 typedef extensions::AlarmManager::Alarm Alarm;
972 for (size_t i = 0; i < list->GetSize(); ++i) { 975 for (size_t i = 0; i < list->GetSize(); ++i) {
973 base::DictionaryValue* alarm_dict = NULL; 976 base::DictionaryValue* alarm_dict = NULL;
974 extensions::AlarmPref alarm; 977 extensions::Alarm alarm;
975 alarm.alarm.reset(new Alarm());
976 if (list->GetDictionary(i, &alarm_dict) && 978 if (list->GetDictionary(i, &alarm_dict) &&
977 Alarm::Populate(*alarm_dict, alarm.alarm.get())) { 979 extensions::api::alarms::Alarm::Populate(*alarm_dict,
978 alarm.scheduled_run_time = ReadTime(alarm_dict, kAlarmScheduledRunTime); 980 alarm.js_alarm.get())) {
981 alarm.granularity =
982 ReadTime<base::TimeDelta>(alarm_dict, kAlarmGranularity);
979 alarms.push_back(alarm); 983 alarms.push_back(alarm);
980 } 984 }
981 } 985 }
982 return alarms; 986 return alarms;
983 } 987 }
984 988
985 void ExtensionPrefs::SetRegisteredAlarms( 989 void ExtensionPrefs::SetRegisteredAlarms(
986 const std::string& extension_id, 990 const std::string& extension_id,
987 const std::vector<extensions::AlarmPref>& alarms) { 991 const std::vector<extensions::Alarm>& alarms) {
988 base::ListValue* list = new ListValue(); 992 base::ListValue* list = new ListValue();
989 for (size_t i = 0; i < alarms.size(); ++i) { 993 for (size_t i = 0; i < alarms.size(); ++i) {
990 scoped_ptr<base::DictionaryValue> alarm = alarms[i].alarm->ToValue().Pass(); 994 scoped_ptr<base::DictionaryValue> alarm =
991 SaveTime(alarm.get(), kAlarmScheduledRunTime, alarms[i].scheduled_run_time); 995 alarms[i].js_alarm->ToValue().Pass();
996 SaveTime<base::TimeDelta>(alarm.get(), kAlarmGranularity,
997 alarms[i].granularity);
992 list->Append(alarm.release()); 998 list->Append(alarm.release());
993 } 999 }
994 UpdateExtensionPref(extension_id, kRegisteredAlarms, list); 1000 UpdateExtensionPref(extension_id, kRegisteredAlarms, list);
995 } 1001 }
996 1002
997 extensions::ExtensionOmniboxSuggestion 1003 extensions::ExtensionOmniboxSuggestion
998 ExtensionPrefs::GetOmniboxDefaultSuggestion(const std::string& extension_id) { 1004 ExtensionPrefs::GetOmniboxDefaultSuggestion(const std::string& extension_id) {
999 extensions::ExtensionOmniboxSuggestion suggestion; 1005 extensions::ExtensionOmniboxSuggestion suggestion;
1000 1006
1001 const base::DictionaryValue* extension = GetExtensionPref(extension_id); 1007 const base::DictionaryValue* extension = GetExtensionPref(extension_id);
(...skipping 946 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 PrefService::UNSYNCABLE_PREF); 1954 PrefService::UNSYNCABLE_PREF);
1949 prefs->RegisterInt64Pref(prefs::kLastExtensionsUpdateCheck, 1955 prefs->RegisterInt64Pref(prefs::kLastExtensionsUpdateCheck,
1950 0, // default value 1956 0, // default value
1951 PrefService::UNSYNCABLE_PREF); 1957 PrefService::UNSYNCABLE_PREF);
1952 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck, 1958 prefs->RegisterInt64Pref(prefs::kNextExtensionsUpdateCheck,
1953 0, // default value 1959 0, // default value
1954 PrefService::UNSYNCABLE_PREF); 1960 PrefService::UNSYNCABLE_PREF);
1955 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites, 1961 prefs->RegisterListPref(prefs::kExtensionAllowedInstallSites,
1956 PrefService::UNSYNCABLE_PREF); 1962 PrefService::UNSYNCABLE_PREF);
1957 } 1963 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698