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/values.h" | 8 #include "base/values.h" |
8 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" | 9 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" |
9 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
10 #include "chrome/common/extensions/api/experimental.alarms.h" | 11 #include "chrome/common/extensions/api/experimental.alarms.h" |
11 #include "chrome/common/extensions/extension_error_utils.h" | 12 #include "chrome/common/extensions/extension_error_utils.h" |
12 | 13 |
13 namespace alarms = extensions::api::experimental_alarms; | 14 namespace alarms = extensions::api::experimental_alarms; |
14 | 15 |
15 namespace extensions { | 16 namespace extensions { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kDefaultAlarmName[] = ""; | 20 const char kDefaultAlarmName[] = ""; |
20 const char kAlarmNotFound[] = "No alarm named '*' exists."; | 21 const char kAlarmNotFound[] = "No alarm named '*' exists."; |
22 const char kDelayLessThanMinimum[] = "Delay is less than minimum of * minutes."; | |
23 const char kDelayIsNonInteger[] = "Delay is not an integer value."; | |
21 | 24 |
25 const int kReleaseDelayMinimum = 5; | |
26 const int kDevDelayMinimum = 0; | |
27 | |
28 bool ValidateDelayTime(double delay_in_minutes, const Extension* extension, | |
Matt Perry
2012/04/30 23:51:42
style: if you need to wrap, do 1 argument per line
Matt Tytel
2012/05/01 00:10:34
Done.
| |
29 std::string* error) { | |
30 double delay_minimum = kDevDelayMinimum; | |
31 if(extension->location() != Extension::LOAD) { | |
Matt Perry
2012/04/30 23:51:42
style: space before (
Matt Tytel
2012/05/01 00:10:34
Done.
| |
32 // In release mode we check for integer delay values and a stricter delay | |
33 // minimum. | |
34 if (delay_in_minutes != static_cast<int>(delay_in_minutes)) { | |
35 *error = kDelayIsNonInteger; | |
36 return false; | |
37 } | |
38 delay_minimum = kReleaseDelayMinimum; | |
39 } | |
40 | |
41 // Validate against our found delay minimum. | |
42 if (delay_in_minutes < delay_minimum) { | |
43 *error = ExtensionErrorUtils::FormatErrorMessage(kDelayLessThanMinimum, | |
44 base::DoubleToString(delay_minimum)); | |
45 return false; | |
46 } | |
47 return true; | |
22 } | 48 } |
23 | 49 |
50 } // namespace | |
Matt Perry
2012/04/30 23:51:42
style: 2 spaces before //
Matt Tytel
2012/05/01 00:10:34
Done.
| |
51 | |
24 bool AlarmsCreateFunction::RunImpl() { | 52 bool AlarmsCreateFunction::RunImpl() { |
25 scoped_ptr<alarms::Create::Params> params( | 53 scoped_ptr<alarms::Create::Params> params( |
26 alarms::Create::Params::Create(*args_)); | 54 alarms::Create::Params::Create(*args_)); |
27 EXTENSION_FUNCTION_VALIDATE(params.get()); | 55 EXTENSION_FUNCTION_VALIDATE(params.get()); |
28 | 56 |
57 double delay_in_minutes = params->alarm_info.delay_in_minutes; | |
58 if (!ValidateDelayTime(delay_in_minutes, GetExtension(), &error_)) | |
59 return false; | |
60 | |
29 linked_ptr<AlarmManager::Alarm> alarm(new AlarmManager::Alarm()); | 61 linked_ptr<AlarmManager::Alarm> alarm(new AlarmManager::Alarm()); |
30 alarm->name = params->name.get() ? *params->name : kDefaultAlarmName; | 62 alarm->name = params->name.get() ? *params->name : kDefaultAlarmName; |
31 alarm->delay_in_seconds = params->alarm_info.delay_in_seconds; | 63 alarm->delay_in_minutes = params->alarm_info.delay_in_minutes; |
32 alarm->repeating = params->alarm_info.repeating.get() ? | 64 alarm->repeating = params->alarm_info.repeating.get() ? |
33 *params->alarm_info.repeating : false; | 65 *params->alarm_info.repeating : false; |
34 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( | 66 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( |
35 extension_id(), alarm); | 67 extension_id(), alarm); |
36 | 68 |
37 return true; | 69 return true; |
38 } | 70 } |
39 | 71 |
40 bool AlarmsGetFunction::RunImpl() { | 72 bool AlarmsGetFunction::RunImpl() { |
41 scoped_ptr<alarms::Get::Params> params( | 73 scoped_ptr<alarms::Get::Params> params( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 return true; | 117 return true; |
86 } | 118 } |
87 | 119 |
88 bool AlarmsClearAllFunction::RunImpl() { | 120 bool AlarmsClearAllFunction::RunImpl() { |
89 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( | 121 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( |
90 extension_id()); | 122 extension_id()); |
91 return true; | 123 return true; |
92 } | 124 } |
93 | 125 |
94 } // namespace extensions | 126 } // namespace extensions |
OLD | NEW |