Chromium Code Reviews| 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 "extensions/browser/api/alarms/alarm_manager.h" | 5 #include "extensions/browser/api/alarms/alarm_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/time/clock.h" | 15 #include "base/time/clock.h" |
| 16 #include "base/time/default_clock.h" | 16 #include "base/time/default_clock.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/value_conversions.h" | 18 #include "base/value_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "extensions/browser/api/alarms/alarms_api_constants.h" | |
| 20 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
| 21 #include "extensions/browser/extension_registry.h" | 22 #include "extensions/browser/extension_registry.h" |
| 22 #include "extensions/browser/extension_system.h" | 23 #include "extensions/browser/extension_system.h" |
| 23 #include "extensions/browser/state_store.h" | 24 #include "extensions/browser/state_store.h" |
| 24 #include "extensions/common/api/alarms.h" | 25 #include "extensions/common/api/alarms.h" |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 namespace alarms = api::alarms; | 29 namespace alarms = api::alarms; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // A list of alarms that this extension has set. | 33 // A list of alarms that this extension has set. |
| 33 const char kRegisteredAlarms[] = "alarms"; | 34 const char kRegisteredAlarms[] = "alarms"; |
| 34 const char kAlarmGranularity[] = "granularity"; | 35 const char kAlarmGranularity[] = "granularity"; |
| 36 const char kAlarmMinimumGranularity[] = "minimum_granularity"; | |
| 37 | |
| 38 const int kSecondsPerMinute = 60; | |
| 35 | 39 |
| 36 // The minimum period between polling for alarms to run. | 40 // The minimum period between polling for alarms to run. |
| 37 const base::TimeDelta kDefaultMinPollPeriod() { | 41 const base::TimeDelta kDefaultMinPollPeriod() { |
| 38 return base::TimeDelta::FromDays(1); | 42 return base::TimeDelta::FromDays(1); |
| 39 } | 43 } |
| 40 | 44 |
| 41 class DefaultAlarmDelegate : public AlarmManager::Delegate { | 45 class DefaultAlarmDelegate : public AlarmManager::Delegate { |
| 42 public: | 46 public: |
| 43 explicit DefaultAlarmDelegate(content::BrowserContext* context) | 47 explicit DefaultAlarmDelegate(content::BrowserContext* context) |
| 44 : browser_context_(context) {} | 48 : browser_context_(context) {} |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 59 | 63 |
| 60 // Creates a TimeDelta from a delay as specified in the API. | 64 // Creates a TimeDelta from a delay as specified in the API. |
| 61 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { | 65 base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) { |
| 62 return base::TimeDelta::FromMicroseconds(delay_in_minutes * | 66 return base::TimeDelta::FromMicroseconds(delay_in_minutes * |
| 63 base::Time::kMicrosecondsPerMinute); | 67 base::Time::kMicrosecondsPerMinute); |
| 64 } | 68 } |
| 65 | 69 |
| 66 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { | 70 std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) { |
| 67 std::vector<Alarm> alarms; | 71 std::vector<Alarm> alarms; |
| 68 for (size_t i = 0; i < list->GetSize(); ++i) { | 72 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 69 const base::DictionaryValue* alarm_dict = NULL; | 73 const base::DictionaryValue* alarm_dict = nullptr; |
| 70 Alarm alarm; | 74 Alarm alarm; |
| 71 if (list->GetDictionary(i, &alarm_dict) && | 75 if (list->GetDictionary(i, &alarm_dict) && |
| 72 alarms::Alarm::Populate(*alarm_dict, alarm.js_alarm.get())) { | 76 alarms::Alarm::Populate(*alarm_dict, alarm.js_alarm.get())) { |
| 73 const base::Value* time_value = NULL; | 77 const base::Value* time_value = nullptr; |
| 74 if (alarm_dict->Get(kAlarmGranularity, &time_value)) | 78 if (alarm_dict->Get(kAlarmGranularity, &time_value)) |
| 75 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); | 79 base::GetValueAsTimeDelta(*time_value, &alarm.granularity); |
| 80 const base::Value* minimum_granularity_value = nullptr; | |
| 81 if (alarm_dict->Get(kAlarmMinimumGranularity, | |
| 82 &minimum_granularity_value)) { | |
| 83 base::GetValueAsTimeDelta(*time_value, &alarm.minimum_granularity); | |
| 84 } else { | |
| 85 // We didn't use to write kAlarmMinimumGranularity to storage, so it is | |
| 86 // possible that the value is missing. At this point we cannot readily | |
| 87 // know whether this alarm's extension was loaded from a release (.crx) | |
|
asargent_no_longer_on_chrome
2016/06/09 19:44:56
Can you just look up the extension by id, and call
lazyboy
2016/06/09 22:00:21
deleted kAlarmMinimumGranularity, thanks.
Done.
| |
| 88 // extension or a dev (unpacked) extension. Use the relaxed limit from | |
| 89 // dev version in this case. This should happen only once ever per | |
| 90 // alarm, the next WriteToStorage will fix things up. | |
| 91 // Also note that it is important to restrict | |
| 92 // |alarm.minimum_granularity| to make sure we do not fire alarms too | |
| 93 // often. | |
| 94 alarm.minimum_granularity = base::TimeDelta::FromSecondsD( | |
| 95 alarms_api_constants::kDevDelayMinimum * kSecondsPerMinute); | |
| 96 } | |
| 97 if (alarm.granularity < alarm.minimum_granularity) | |
| 98 alarm.granularity = alarm.minimum_granularity; | |
| 76 alarms.push_back(alarm); | 99 alarms.push_back(alarm); |
| 77 } | 100 } |
| 78 } | 101 } |
| 79 return alarms; | 102 return alarms; |
| 80 } | 103 } |
| 81 | 104 |
| 82 std::unique_ptr<base::ListValue> AlarmsToValue( | 105 std::unique_ptr<base::ListValue> AlarmsToValue( |
| 83 const std::vector<Alarm>& alarms) { | 106 const std::vector<Alarm>& alarms) { |
| 84 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 107 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 85 for (size_t i = 0; i < alarms.size(); ++i) { | 108 for (size_t i = 0; i < alarms.size(); ++i) { |
| 86 std::unique_ptr<base::DictionaryValue> alarm = | 109 std::unique_ptr<base::DictionaryValue> alarm = |
| 87 alarms[i].js_alarm->ToValue(); | 110 alarms[i].js_alarm->ToValue(); |
| 88 alarm->Set(kAlarmGranularity, | 111 alarm->Set(kAlarmGranularity, |
| 89 base::CreateTimeDeltaValue(alarms[i].granularity)); | 112 base::CreateTimeDeltaValue(alarms[i].granularity)); |
| 113 alarm->Set(kAlarmMinimumGranularity, | |
| 114 base::CreateTimeDeltaValue(alarms[i].minimum_granularity)); | |
| 90 list->Append(alarm.release()); | 115 list->Append(alarm.release()); |
| 91 } | 116 } |
| 92 return list; | 117 return list; |
| 93 } | 118 } |
| 94 | 119 |
| 95 } // namespace | 120 } // namespace |
| 96 | 121 |
| 97 // AlarmManager | 122 // AlarmManager |
| 98 | 123 |
| 99 AlarmManager::AlarmManager(content::BrowserContext* context) | 124 AlarmManager::AlarmManager(content::BrowserContext* context) |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 new double(*create_info.period_in_minutes)); | 491 new double(*create_info.period_in_minutes)); |
| 467 } | 492 } |
| 468 } | 493 } |
| 469 | 494 |
| 470 Alarm::Alarm(const Alarm& other) = default; | 495 Alarm::Alarm(const Alarm& other) = default; |
| 471 | 496 |
| 472 Alarm::~Alarm() { | 497 Alarm::~Alarm() { |
| 473 } | 498 } |
| 474 | 499 |
| 475 } // namespace extensions | 500 } // namespace extensions |
| OLD | NEW |