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/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/time/clock.h" | 8 #include "base/time/clock.h" |
9 #include "base/time/default_clock.h" | 9 #include "base/time/default_clock.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 } | 100 } |
101 | 101 |
102 bool AlarmsCreateFunction::RunImpl() { | 102 bool AlarmsCreateFunction::RunImpl() { |
103 scoped_ptr<alarms::Create::Params> params( | 103 scoped_ptr<alarms::Create::Params> params( |
104 alarms::Create::Params::Create(*args_)); | 104 alarms::Create::Params::Create(*args_)); |
105 EXTENSION_FUNCTION_VALIDATE(params.get()); | 105 EXTENSION_FUNCTION_VALIDATE(params.get()); |
106 const std::string& alarm_name = | 106 const std::string& alarm_name = |
107 params->name.get() ? *params->name : kDefaultAlarmName; | 107 params->name.get() ? *params->name : kDefaultAlarmName; |
108 std::vector<std::string> warnings; | 108 std::vector<std::string> warnings; |
109 if (!ValidateAlarmCreateInfo( | 109 if (!ValidateAlarmCreateInfo( |
110 alarm_name, params->alarm_info, GetExtension(), &error_, &warnings)) | 110 alarm_name, params->alarm_info, GetExtension(), &error_, &warnings)) { |
111 return false; | 111 return false; |
| 112 } |
112 for (std::vector<std::string>::const_iterator it = warnings.begin(); | 113 for (std::vector<std::string>::const_iterator it = warnings.begin(); |
113 it != warnings.end(); ++it) | 114 it != warnings.end(); ++it) |
114 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); | 115 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); |
115 | 116 |
116 Alarm alarm(alarm_name, | 117 Alarm alarm(alarm_name, |
117 params->alarm_info, | 118 params->alarm_info, |
118 base::TimeDelta::FromMinutes( | 119 base::TimeDelta::FromMinutes( |
119 Manifest::IsUnpackedLocation(GetExtension()->location()) ? | 120 Manifest::IsUnpackedLocation(GetExtension()->location()) ? |
120 kDevDelayMinimum : kReleaseDelayMinimum), | 121 kDevDelayMinimum : kReleaseDelayMinimum), |
121 clock_->Now()); | 122 clock_->Now()); |
122 AlarmManager::Get(profile())->AddAlarm(extension_id(), alarm); | 123 AlarmManager::Get(profile())->AddAlarm(extension_id(), alarm, base::Bind( |
| 124 &AlarmsCreateFunction::Callback, this)); |
123 | 125 |
124 return true; | 126 return true; |
125 } | 127 } |
126 | 128 |
| 129 void AlarmsCreateFunction::Callback() { |
| 130 SendResponse(true); |
| 131 } |
| 132 |
127 bool AlarmsGetFunction::RunImpl() { | 133 bool AlarmsGetFunction::RunImpl() { |
128 scoped_ptr<alarms::Get::Params> params(alarms::Get::Params::Create(*args_)); | 134 scoped_ptr<alarms::Get::Params> params(alarms::Get::Params::Create(*args_)); |
129 EXTENSION_FUNCTION_VALIDATE(params.get()); | 135 EXTENSION_FUNCTION_VALIDATE(params.get()); |
130 | 136 |
131 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; | 137 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; |
132 const Alarm* alarm = | 138 AlarmManager::Get(profile())->GetAlarm(extension_id(), name, base::Bind( |
133 AlarmManager::Get(profile())->GetAlarm(extension_id(), name); | 139 &AlarmsGetFunction::Callback, this, name)); |
134 | 140 |
135 if (!alarm) { | |
136 error_ = ErrorUtils::FormatErrorMessage(kAlarmNotFound, name); | |
137 return false; | |
138 } | |
139 | |
140 results_ = alarms::Get::Results::Create(*alarm->js_alarm); | |
141 return true; | 141 return true; |
142 } | 142 } |
143 | 143 |
| 144 void AlarmsGetFunction::Callback( |
| 145 const std::string& name, extensions::Alarm* alarm) { |
| 146 if (alarm) { |
| 147 results_ = alarms::Get::Results::Create(*alarm->js_alarm); |
| 148 SendResponse(true); |
| 149 } else { |
| 150 error_ = ErrorUtils::FormatErrorMessage(kAlarmNotFound, name); |
| 151 SendResponse(false); |
| 152 } |
| 153 } |
| 154 |
144 bool AlarmsGetAllFunction::RunImpl() { | 155 bool AlarmsGetAllFunction::RunImpl() { |
145 const AlarmManager::AlarmList* alarms = | 156 AlarmManager::Get(profile())->GetAllAlarms(extension_id(), base::Bind( |
146 AlarmManager::Get(profile())->GetAllAlarms(extension_id()); | 157 &AlarmsGetAllFunction::Callback, this)); |
| 158 return true; |
| 159 } |
| 160 |
| 161 void AlarmsGetAllFunction::Callback(const AlarmList* alarms) { |
147 if (alarms) { | 162 if (alarms) { |
148 std::vector<linked_ptr<extensions::api::alarms::Alarm> > create_arg; | 163 std::vector<linked_ptr<extensions::api::alarms::Alarm> > create_arg; |
149 create_arg.reserve(alarms->size()); | 164 create_arg.reserve(alarms->size()); |
150 for (size_t i = 0, size = alarms->size(); i < size; ++i) { | 165 for (size_t i = 0, size = alarms->size(); i < size; ++i) { |
151 create_arg.push_back((*alarms)[i].js_alarm); | 166 create_arg.push_back((*alarms)[i].js_alarm); |
152 } | 167 } |
153 results_ = alarms::GetAll::Results::Create(create_arg); | 168 results_ = alarms::GetAll::Results::Create(create_arg); |
154 } else { | 169 } else { |
155 SetResult(new base::ListValue()); | 170 SetResult(new base::ListValue()); |
156 } | 171 } |
157 return true; | 172 SendResponse(true); |
158 } | 173 } |
159 | 174 |
160 bool AlarmsClearFunction::RunImpl() { | 175 bool AlarmsClearFunction::RunImpl() { |
161 scoped_ptr<alarms::Clear::Params> params( | 176 scoped_ptr<alarms::Clear::Params> params( |
162 alarms::Clear::Params::Create(*args_)); | 177 alarms::Clear::Params::Create(*args_)); |
163 EXTENSION_FUNCTION_VALIDATE(params.get()); | 178 EXTENSION_FUNCTION_VALIDATE(params.get()); |
164 | 179 |
165 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; | 180 std::string name = params->name.get() ? *params->name : kDefaultAlarmName; |
166 bool success = AlarmManager::Get(profile())->RemoveAlarm(extension_id(), | 181 AlarmManager::Get(profile())->RemoveAlarm(extension_id(), name, base::Bind( |
167 name); | 182 &AlarmsClearFunction::Callback, this, name)); |
168 | |
169 if (!success) { | |
170 error_ = ErrorUtils::FormatErrorMessage(kAlarmNotFound, name); | |
171 return false; | |
172 } | |
173 | 183 |
174 return true; | 184 return true; |
175 } | 185 } |
176 | 186 |
| 187 void AlarmsClearFunction::Callback(const std::string& name, bool success) { |
| 188 if (!success) |
| 189 error_ = ErrorUtils::FormatErrorMessage(kAlarmNotFound, name); |
| 190 |
| 191 SendResponse(success); |
| 192 } |
| 193 |
177 bool AlarmsClearAllFunction::RunImpl() { | 194 bool AlarmsClearAllFunction::RunImpl() { |
178 AlarmManager::Get(profile())->RemoveAllAlarms(extension_id()); | 195 AlarmManager::Get(profile())->RemoveAllAlarms(extension_id(), base::Bind( |
| 196 &AlarmsClearAllFunction::Callback, this)); |
179 return true; | 197 return true; |
180 } | 198 } |
181 | 199 |
| 200 void AlarmsClearAllFunction::Callback() { |
| 201 SendResponse(true); |
| 202 } |
| 203 |
182 } // namespace extensions | 204 } // namespace extensions |
OLD | NEW |