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" |
10 #include "chrome/browser/extensions/extension_service.h" | |
9 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
12 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/common/extensions/api/experimental.alarms.h" | 13 #include "chrome/common/extensions/api/experimental.alarms.h" |
11 #include "chrome/common/extensions/extension_error_utils.h" | 14 #include "chrome/common/extensions/extension_error_utils.h" |
12 | 15 |
13 namespace alarms = extensions::api::experimental_alarms; | 16 namespace alarms = extensions::api::experimental_alarms; |
14 | 17 |
15 namespace extensions { | 18 namespace extensions { |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 const char kDefaultAlarmName[] = ""; | 22 const char kDefaultAlarmName[] = ""; |
20 const char kAlarmNotFound[] = "No alarm named '*' exists."; | 23 const char kAlarmNotFound[] = "No alarm named '*' exists."; |
24 const char kDelayLessThanMinimum[] = "Delay is less than minimum of * minutes."; | |
25 const char kDelayIsNonInteger[] = "Delay is not an integer value."; | |
21 | 26 |
27 const int kReleaseDelayMinimum = 5; | |
28 const double kDevDelayMinimum = 0.1; | |
Matt Perry
2012/04/25 19:11:06
Let's allow 0 minute delays for dev mode. That'll
Matt Tytel
2012/04/30 23:36:21
Done.
| |
29 | |
30 } | |
31 | |
32 bool AlarmsCreateFunction::ValidateDelayTime(double delay_in_minutes) { | |
Matt Perry
2012/04/25 19:11:06
nit: let's make this a global function in this fil
Matt Tytel
2012/04/30 23:36:21
Done.
| |
33 double delay_minimum = kDevDelayMinimum; | |
34 ExtensionService* extension_service = profile()->GetExtensionService(); | |
35 if (!extension_service) { | |
36 // In testing, only check if the delay is non negative. | |
37 delay_minimum = 0; | |
38 } | |
39 else if(extension_service->GetExtensionById(extension_id(), false)-> | |
Matt Perry
2012/04/25 19:11:06
AlarmCreateFunction has a GetExtension() method. J
Matt Tytel
2012/04/30 23:36:21
Done.
| |
40 location() != Extension::LOAD) { | |
41 // In release mode we check for integer delay values and a stricter delay | |
42 // minimum. | |
43 if (delay_in_minutes != static_cast<int>(delay_in_minutes)) { | |
44 error_ = kDelayIsNonInteger; | |
45 return false; | |
46 } | |
47 delay_minimum = kReleaseDelayMinimum; | |
48 } | |
49 | |
50 // Validate against our found delay minimum. | |
51 if (delay_in_minutes < delay_minimum) { | |
52 error_ = ExtensionErrorUtils::FormatErrorMessage(kDelayLessThanMinimum, | |
53 base::DoubleToString(delay_minimum)); | |
54 return false; | |
55 } | |
56 return true; | |
22 } | 57 } |
23 | 58 |
24 bool AlarmsCreateFunction::RunImpl() { | 59 bool AlarmsCreateFunction::RunImpl() { |
25 scoped_ptr<alarms::Create::Params> params( | 60 scoped_ptr<alarms::Create::Params> params( |
26 alarms::Create::Params::Create(*args_)); | 61 alarms::Create::Params::Create(*args_)); |
27 EXTENSION_FUNCTION_VALIDATE(params.get()); | 62 EXTENSION_FUNCTION_VALIDATE(params.get()); |
28 | 63 |
64 if (!ValidateDelayTime(params->alarm_info.delay_in_minutes)) | |
65 return false; | |
66 | |
29 linked_ptr<AlarmManager::Alarm> alarm(new AlarmManager::Alarm()); | 67 linked_ptr<AlarmManager::Alarm> alarm(new AlarmManager::Alarm()); |
30 alarm->name = params->name.get() ? *params->name : kDefaultAlarmName; | 68 alarm->name = params->name.get() ? *params->name : kDefaultAlarmName; |
31 alarm->delay_in_seconds = params->alarm_info.delay_in_seconds; | 69 alarm->delay_in_minutes = params->alarm_info.delay_in_minutes; |
32 alarm->repeating = params->alarm_info.repeating.get() ? | 70 alarm->repeating = params->alarm_info.repeating.get() ? |
33 *params->alarm_info.repeating : false; | 71 *params->alarm_info.repeating : false; |
34 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( | 72 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( |
35 extension_id(), alarm); | 73 extension_id(), alarm); |
36 | 74 |
37 return true; | 75 return true; |
38 } | 76 } |
39 | 77 |
40 bool AlarmsGetFunction::RunImpl() { | 78 bool AlarmsGetFunction::RunImpl() { |
41 scoped_ptr<alarms::Get::Params> params( | 79 scoped_ptr<alarms::Get::Params> params( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
85 return true; | 123 return true; |
86 } | 124 } |
87 | 125 |
88 bool AlarmsClearAllFunction::RunImpl() { | 126 bool AlarmsClearAllFunction::RunImpl() { |
89 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( | 127 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( |
90 extension_id()); | 128 extension_id()); |
91 return true; | 129 return true; |
92 } | 130 } |
93 | 131 |
94 } // namespace extensions | 132 } // namespace extensions |
OLD | NEW |