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 "chrome/common/extensions/extension_error_utils.h" | 12 #include "chrome/common/extensions/extension_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[] = "Delay is less than minimum of * minutes."; | 22 const char kDelayLessThanMinimum[] = "Delay is less than minimum of * minutes."; |
23 const char kDelayIsNonInteger[] = "Delay is not an integer value."; | 23 const char kDelayIsNonInteger[] = "Delay is not an integer value."; |
24 | 24 const char kBothRelativeAndAbsoluteTime[] = |
25 "Cannot set both approxTimeToFire and delayInMinutes."; | |
Matt Perry
2012/06/11 22:32:44
error messages are out of date
Jeffrey Yasskin
2012/06/12 19:12:31
Oops, fixed.
| |
26 const char kNeitherRelativeNorAbsoluteTime[] = | |
27 "Must set one of approxTimeToFire and delayInMinutes."; | |
28 const char kRepeatingAlarmMustBeRelative[] = | |
29 "A repeating alarm must set delayInMinutes."; | |
25 const int kReleaseDelayMinimum = 5; | 30 const int kReleaseDelayMinimum = 5; |
26 const int kDevDelayMinimum = 0; | 31 const int kDevDelayMinimum = 0; |
27 | 32 |
28 bool ValidateDelayTime(double delay_in_minutes, | 33 bool ValidateDelay(double delay_in_minutes, |
29 const Extension* extension, | 34 const Extension* extension, |
30 std::string* error) { | 35 std::string* error) { |
31 double delay_minimum = kDevDelayMinimum; | 36 double delay_minimum = kDevDelayMinimum; |
32 if (extension->location() != Extension::LOAD) { | 37 if (extension->location() != Extension::LOAD) { |
33 // In release mode we check for integer delay values and a stricter delay | 38 // In release mode we check for integer delay values and a stricter delay |
34 // minimum. | 39 // minimum. |
35 if (delay_in_minutes != static_cast<int>(delay_in_minutes)) { | 40 if (delay_in_minutes != static_cast<int>(delay_in_minutes)) { |
36 *error = kDelayIsNonInteger; | 41 *error = kDelayIsNonInteger; |
37 return false; | 42 return false; |
38 } | 43 } |
39 delay_minimum = kReleaseDelayMinimum; | 44 delay_minimum = kReleaseDelayMinimum; |
40 } | 45 } |
41 | 46 |
42 // Validate against our found delay minimum. | 47 // Validate against our found delay minimum. |
43 if (delay_in_minutes < delay_minimum) { | 48 if (delay_in_minutes < delay_minimum) { |
44 *error = ExtensionErrorUtils::FormatErrorMessage(kDelayLessThanMinimum, | 49 *error = ExtensionErrorUtils::FormatErrorMessage( |
50 kDelayLessThanMinimum, | |
45 base::DoubleToString(delay_minimum)); | 51 base::DoubleToString(delay_minimum)); |
46 return false; | 52 return false; |
47 } | 53 } |
54 | |
55 return true; | |
56 } | |
57 | |
58 bool ValidateAlarmOneShotCreateInfo( | |
59 const alarms::AlarmOneShotCreateInfo& create_info, | |
60 const Extension* extension, | |
61 std::string* error) { | |
62 if (create_info.delay_in_minutes.get() && | |
63 create_info.time.get()) { | |
64 *error = kBothRelativeAndAbsoluteTime; | |
65 return false; | |
66 } | |
67 if (create_info.delay_in_minutes == NULL && | |
68 create_info.time == NULL) { | |
69 *error = kNeitherRelativeNorAbsoluteTime; | |
70 return false; | |
71 } | |
72 | |
73 // Users can always use an absolute timeout to request an arbitrarily-short or | |
74 // negative delay. We won't honor the short timeout, but we can't check it | |
75 // and warn the user because it would introduce race conditions (say they | |
76 // compute a long-enough timeout, but then the call into the alarms interface | |
77 // gets delayed past the boundary). However, it's still worth warning about | |
78 // relative delays that are shorter than we'll honor. | |
79 if (create_info.delay_in_minutes.get()) { | |
80 if (!ValidateDelay(*create_info.delay_in_minutes, | |
81 extension, error)) | |
82 return false; | |
83 } | |
84 | |
48 return true; | 85 return true; |
49 } | 86 } |
50 | 87 |
51 } // namespace | 88 } // namespace |
52 | 89 |
53 bool AlarmsCreateFunction::RunImpl() { | 90 AlarmsCreateOneShotFunction::AlarmsCreateOneShotFunction() |
54 scoped_ptr<alarms::Create::Params> params( | 91 : now_(&base::Time::Now) { |
55 alarms::Create::Params::Create(*args_)); | 92 } |
93 | |
94 bool AlarmsCreateOneShotFunction::RunImpl() { | |
95 scoped_ptr<alarms::CreateOneShot::Params> params( | |
96 alarms::CreateOneShot::Params::Create(*args_)); | |
56 EXTENSION_FUNCTION_VALIDATE(params.get()); | 97 EXTENSION_FUNCTION_VALIDATE(params.get()); |
57 | 98 if (!ValidateAlarmOneShotCreateInfo( |
58 double delay_in_minutes = params->alarm_info.delay_in_minutes; | 99 params->alarm_info, GetExtension(), &error_)) |
59 if (!ValidateDelayTime(delay_in_minutes, GetExtension(), &error_)) | |
60 return false; | 100 return false; |
61 | 101 |
62 linked_ptr<AlarmManager::Alarm> alarm(new AlarmManager::Alarm()); | 102 Alarm alarm(params->name.get() ? *params->name : kDefaultAlarmName, |
63 alarm->name = params->name.get() ? *params->name : kDefaultAlarmName; | 103 params->alarm_info, |
64 alarm->delay_in_minutes = params->alarm_info.delay_in_minutes; | 104 base::TimeDelta::FromMinutes( |
65 alarm->repeating = params->alarm_info.repeating.get() ? | 105 GetExtension()->location() == Extension::LOAD |
66 *params->alarm_info.repeating : false; | 106 ? kDevDelayMinimum : kReleaseDelayMinimum), |
Matt Perry
2012/06/11 22:32:44
chrome style is to put operators on the line prece
Jeffrey Yasskin
2012/06/12 19:12:31
Yay for style comments. :) Done.
| |
107 now_); | |
67 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( | 108 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( |
68 extension_id(), alarm); | 109 extension_id(), alarm); |
69 | 110 |
111 return true; | |
112 } | |
113 | |
114 AlarmsCreateRepeatingFunction::AlarmsCreateRepeatingFunction() | |
115 : now_(&base::Time::Now) { | |
116 } | |
117 | |
118 bool AlarmsCreateRepeatingFunction::RunImpl() { | |
119 scoped_ptr<alarms::CreateRepeating::Params> params( | |
120 alarms::CreateRepeating::Params::Create(*args_)); | |
121 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
122 if (!ValidateDelay(params->alarm_info.period_in_minutes, | |
123 GetExtension(), &error_)) | |
124 return false; | |
125 | |
126 Alarm alarm(params->name.get() ? *params->name : kDefaultAlarmName, | |
127 params->alarm_info, | |
128 base::TimeDelta::FromMinutes( | |
129 GetExtension()->location() == Extension::LOAD | |
130 ? kDevDelayMinimum : kReleaseDelayMinimum), | |
131 now_); | |
132 ExtensionSystem::Get(profile())->alarm_manager()->AddAlarm( | |
133 extension_id(), alarm); | |
134 | |
70 return true; | 135 return true; |
71 } | 136 } |
72 | 137 |
73 bool AlarmsGetFunction::RunImpl() { | 138 bool AlarmsGetFunction::RunImpl() { |
74 scoped_ptr<alarms::Get::Params> params( | 139 scoped_ptr<alarms::Get::Params> params( |
75 alarms::Get::Params::Create(*args_)); | 140 alarms::Get::Params::Create(*args_)); |
76 EXTENSION_FUNCTION_VALIDATE(params.get()); | 141 EXTENSION_FUNCTION_VALIDATE(params.get()); |
77 | 142 |
78 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; | 143 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; |
79 const AlarmManager::Alarm* alarm = | 144 const Alarm* alarm = |
80 ExtensionSystem::Get(profile())->alarm_manager()->GetAlarm( | 145 ExtensionSystem::Get(profile())->alarm_manager()->GetAlarm( |
81 extension_id(), name); | 146 extension_id(), name); |
82 | 147 |
83 if (!alarm) { | 148 if (!alarm) { |
84 error_ = ExtensionErrorUtils::FormatErrorMessage(kAlarmNotFound, name); | 149 error_ = ExtensionErrorUtils::FormatErrorMessage(kAlarmNotFound, name); |
85 return false; | 150 return false; |
86 } | 151 } |
87 | 152 |
88 result_.reset(alarms::Get::Result::Create(*alarm)); | 153 result_.reset(alarms::Get::Result::Create(*alarm->js_alarm)); |
89 return true; | 154 return true; |
90 } | 155 } |
91 | 156 |
92 bool AlarmsGetAllFunction::RunImpl() { | 157 bool AlarmsGetAllFunction::RunImpl() { |
93 const AlarmManager::AlarmList* alarms = | 158 const AlarmManager::AlarmList* alarms = |
94 ExtensionSystem::Get(profile())->alarm_manager()->GetAllAlarms( | 159 ExtensionSystem::Get(profile())->alarm_manager()->GetAllAlarms( |
95 extension_id()); | 160 extension_id()); |
96 if (alarms) { | 161 if (alarms) { |
97 result_.reset(alarms::GetAll::Result::Create(*alarms)); | 162 std::vector<linked_ptr<extensions::api::alarms::Alarm> > create_arg; |
163 create_arg.reserve(alarms->size()); | |
164 for (size_t i = 0, size = alarms->size(); i < size; ++i) { | |
165 create_arg.push_back((*alarms)[i].js_alarm); | |
166 } | |
167 result_.reset(alarms::GetAll::Result::Create(create_arg)); | |
98 } else { | 168 } else { |
99 result_.reset(new base::ListValue()); | 169 result_.reset(new base::ListValue()); |
100 } | 170 } |
101 return true; | 171 return true; |
102 } | 172 } |
103 | 173 |
104 bool AlarmsClearFunction::RunImpl() { | 174 bool AlarmsClearFunction::RunImpl() { |
105 scoped_ptr<alarms::Clear::Params> params( | 175 scoped_ptr<alarms::Clear::Params> params( |
106 alarms::Clear::Params::Create(*args_)); | 176 alarms::Clear::Params::Create(*args_)); |
107 EXTENSION_FUNCTION_VALIDATE(params.get()); | 177 EXTENSION_FUNCTION_VALIDATE(params.get()); |
(...skipping 10 matching lines...) Expand all Loading... | |
118 return true; | 188 return true; |
119 } | 189 } |
120 | 190 |
121 bool AlarmsClearAllFunction::RunImpl() { | 191 bool AlarmsClearAllFunction::RunImpl() { |
122 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( | 192 ExtensionSystem::Get(profile())->alarm_manager()->RemoveAllAlarms( |
123 extension_id()); | 193 extension_id()); |
124 return true; | 194 return true; |
125 } | 195 } |
126 | 196 |
127 } // namespace extensions | 197 } // namespace extensions |
OLD | NEW |