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/api/alarms/alarms_api.h" | 5 #include "chrome/browser/extensions/api/alarms/alarms_api.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" | 9 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" |
10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
11 #include "chrome/common/extensions/api/alarms.h" | 11 #include "chrome/common/extensions/api/alarms.h" |
12 #include "extensions/common/error_utils.h" | 12 #include "extensions/common/error_utils.h" |
13 | 13 |
14 namespace alarms = extensions::api::alarms; | 14 namespace alarms = extensions::api::alarms; |
15 | 15 |
16 namespace extensions { | 16 namespace extensions { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const char kDefaultAlarmName[] = ""; | 20 const char kDefaultAlarmName[] = ""; |
21 const char kAlarmNotFound[] = "No alarm named '*' exists."; | 21 const char kAlarmNotFound[] = "No alarm named '*' exists."; |
22 const char kDelayLessThanMinimum[] = "* is less than minimum of * minutes."; | |
23 const char kDelayIsNonInteger[] = "* is not an integer value."; | |
24 const char kBothRelativeAndAbsoluteTime[] = | 22 const char kBothRelativeAndAbsoluteTime[] = |
25 "Cannot set both when and delayInMinutes."; | 23 "Cannot set both when and delayInMinutes."; |
26 const char kNoScheduledTime[] = | 24 const char kNoScheduledTime[] = |
27 "Must set at least one of when, delayInMinutes, or periodInMinutes."; | 25 "Must set at least one of when, delayInMinutes, or periodInMinutes."; |
28 const int kReleaseDelayMinimum = 5; | 26 const int kReleaseDelayMinimum = 5; |
Yoyo Zhou
2013/01/09 00:38:00
Per crbug.com/153838 the minimum was changed from
Matt Perry
2013/01/09 00:58:00
Ah, I forgot we had that bug. It looks like it was
Jeffrey Yasskin
2013/01/09 01:04:45
It probably changed the first poll time but not la
| |
29 const int kDevDelayMinimum = 0; | 27 const int kDevDelayMinimum = 0; |
30 | 28 |
31 bool ValidateDelay(double delay_in_minutes, | |
32 const Extension* extension, | |
33 const std::string& delay_or_period, | |
34 std::string* error) { | |
35 double delay_minimum = kDevDelayMinimum; | |
36 if (extension->location() != Extension::LOAD) { | |
37 // In release mode we check for integer delay values and a stricter delay | |
38 // minimum. | |
39 if (delay_in_minutes != static_cast<int>(delay_in_minutes)) { | |
40 *error = ErrorUtils::FormatErrorMessage( | |
41 kDelayIsNonInteger, | |
42 delay_or_period); | |
43 return false; | |
44 } | |
45 delay_minimum = kReleaseDelayMinimum; | |
46 } | |
47 | |
48 // Validate against our found delay minimum. | |
49 if (delay_in_minutes < delay_minimum) { | |
50 *error = ErrorUtils::FormatErrorMessage( | |
51 kDelayLessThanMinimum, | |
52 delay_or_period, | |
53 base::DoubleToString(delay_minimum)); | |
54 return false; | |
55 } | |
56 | |
57 return true; | |
58 } | |
59 | |
60 bool ValidateAlarmCreateInfo(const alarms::AlarmCreateInfo& create_info, | 29 bool ValidateAlarmCreateInfo(const alarms::AlarmCreateInfo& create_info, |
61 const Extension* extension, | 30 const Extension* extension, |
62 std::string* error) { | 31 std::string* error, |
32 std::vector<std::string>* warnings) { | |
63 if (create_info.delay_in_minutes.get() && | 33 if (create_info.delay_in_minutes.get() && |
64 create_info.when.get()) { | 34 create_info.when.get()) { |
65 *error = kBothRelativeAndAbsoluteTime; | 35 *error = kBothRelativeAndAbsoluteTime; |
66 return false; | 36 return false; |
67 } | 37 } |
68 if (create_info.delay_in_minutes == NULL && | 38 if (create_info.delay_in_minutes == NULL && |
69 create_info.when == NULL && | 39 create_info.when == NULL && |
70 create_info.period_in_minutes == NULL) { | 40 create_info.period_in_minutes == NULL) { |
71 *error = kNoScheduledTime; | 41 *error = kNoScheduledTime; |
72 return false; | 42 return false; |
73 } | 43 } |
74 | 44 |
75 // Users can always use an absolute timeout to request an arbitrarily-short or | 45 // Users can always use an absolute timeout to request an arbitrarily-short or |
76 // negative delay. We won't honor the short timeout, but we can't check it | 46 // negative delay. We won't honor the short timeout, but we can't check it |
77 // and warn the user because it would introduce race conditions (say they | 47 // and warn the user because it would introduce race conditions (say they |
78 // compute a long-enough timeout, but then the call into the alarms interface | 48 // compute a long-enough timeout, but then the call into the alarms interface |
79 // gets delayed past the boundary). However, it's still worth warning about | 49 // gets delayed past the boundary). However, it's still worth warning about |
80 // relative delays that are shorter than we'll honor. | 50 // relative delays that are shorter than we'll honor. |
81 if (create_info.delay_in_minutes.get()) { | 51 if (create_info.delay_in_minutes.get()) { |
82 if (!ValidateDelay(*create_info.delay_in_minutes, | 52 if (*create_info.delay_in_minutes < kReleaseDelayMinimum) { |
83 extension, "Delay", error)) | 53 COMPILE_ASSERT(kReleaseDelayMinimum == 5, update_warning_message_below); |
84 return false; | 54 if (extension->location() == Extension::LOAD) |
55 warnings->push_back( | |
56 "Alarm delay is less than minimum of 5 minutes." | |
57 " In released .crx the alarm will fire in approximately" | |
58 " 5 minutes."); | |
59 else | |
60 warnings->push_back( | |
61 "Alarm delay is less than minimum of 5 minutes." | |
62 " The alarm will fire in approximately 5 minutes."); | |
Yoyo Zhou
2013/01/09 00:38:00
Alarms have names - can we include those in the wa
Jeffrey Yasskin
2013/01/09 01:04:45
Done.
| |
63 } | |
85 } | 64 } |
86 if (create_info.period_in_minutes.get()) { | 65 if (create_info.period_in_minutes.get()) { |
87 if (!ValidateDelay(*create_info.period_in_minutes, | 66 if (*create_info.period_in_minutes < kReleaseDelayMinimum) { |
88 extension, "Period", error)) | 67 COMPILE_ASSERT(kReleaseDelayMinimum == 5, update_warning_message_below); |
89 return false; | 68 if (extension->location() == Extension::LOAD) |
69 warnings->push_back( | |
70 "Alarm period is less than minimum of 5 minutes." | |
71 " In released .crx the alarm will fire approximately" | |
72 " every 5 minutes."); | |
73 else | |
74 warnings->push_back( | |
75 "Alarm period is less than minimum of 5 minutes." | |
76 " The alarm will fire approximately every 5 minutes."); | |
77 } | |
90 } | 78 } |
91 | 79 |
92 return true; | 80 return true; |
93 } | 81 } |
94 | 82 |
95 } // namespace | 83 } // namespace |
96 | 84 |
97 AlarmsCreateFunction::AlarmsCreateFunction() | 85 AlarmsCreateFunction::AlarmsCreateFunction() |
98 : now_(&base::Time::Now) { | 86 : now_(&base::Time::Now) { |
99 } | 87 } |
100 | 88 |
101 bool AlarmsCreateFunction::RunImpl() { | 89 bool AlarmsCreateFunction::RunImpl() { |
102 scoped_ptr<alarms::Create::Params> params( | 90 scoped_ptr<alarms::Create::Params> params( |
103 alarms::Create::Params::Create(*args_)); | 91 alarms::Create::Params::Create(*args_)); |
104 EXTENSION_FUNCTION_VALIDATE(params.get()); | 92 EXTENSION_FUNCTION_VALIDATE(params.get()); |
93 std::vector<std::string> warnings; | |
105 if (!ValidateAlarmCreateInfo( | 94 if (!ValidateAlarmCreateInfo( |
106 params->alarm_info, GetExtension(), &error_)) | 95 params->alarm_info, GetExtension(), &error_, &warnings)) |
107 return false; | 96 return false; |
97 for (std::vector<std::string>::const_iterator it = warnings.begin(); | |
98 it != warnings.end(); ++it) | |
99 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); | |
108 | 100 |
109 Alarm alarm(params->name.get() ? *params->name : kDefaultAlarmName, | 101 Alarm alarm(params->name.get() ? *params->name : kDefaultAlarmName, |
110 params->alarm_info, | 102 params->alarm_info, |
111 base::TimeDelta::FromMinutes( | 103 base::TimeDelta::FromMinutes( |
112 GetExtension()->location() == Extension::LOAD ? | 104 GetExtension()->location() == Extension::LOAD ? |
113 kDevDelayMinimum : kReleaseDelayMinimum), | 105 kDevDelayMinimum : kReleaseDelayMinimum), |
114 now_); | 106 now_); |
115 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( | 107 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( |
116 extension_id(), alarm); | 108 extension_id(), alarm); |
117 | 109 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 return true; | 162 return true; |
171 } | 163 } |
172 | 164 |
173 bool AlarmsClearAllFunction::RunImpl() { | 165 bool AlarmsClearAllFunction::RunImpl() { |
174 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( | 166 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( |
175 extension_id()); | 167 extension_id()); |
176 return true; | 168 return true; |
177 } | 169 } |
178 | 170 |
179 } // namespace extensions | 171 } // namespace extensions |
OLD | NEW |