| 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 "extensions/browser/api/alarms/alarms_api.h" | 5 #include "extensions/browser/api/alarms/alarms_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
| 12 #include "base/time/default_clock.h" | 12 #include "base/time/default_clock.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "extensions/browser/api/alarms/alarm_manager.h" | 14 #include "extensions/browser/api/alarms/alarm_manager.h" |
| 15 #include "extensions/browser/api/alarms/alarms_api_constants.h" |
| 15 #include "extensions/common/api/alarms.h" | 16 #include "extensions/common/api/alarms.h" |
| 16 #include "extensions/common/error_utils.h" | 17 #include "extensions/common/error_utils.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace alarms = api::alarms; | 21 namespace alarms = api::alarms; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 const char kDefaultAlarmName[] = ""; | 25 const char kDefaultAlarmName[] = ""; |
| 25 const char kBothRelativeAndAbsoluteTime[] = | 26 const char kBothRelativeAndAbsoluteTime[] = |
| 26 "Cannot set both when and delayInMinutes."; | 27 "Cannot set both when and delayInMinutes."; |
| 27 const char kNoScheduledTime[] = | 28 const char kNoScheduledTime[] = |
| 28 "Must set at least one of when, delayInMinutes, or periodInMinutes."; | 29 "Must set at least one of when, delayInMinutes, or periodInMinutes."; |
| 29 const int kReleaseDelayMinimum = 1; | 30 const int kReleaseDelayMinimum = 1; |
| 30 const int kDevDelayMinimum = 0; | |
| 31 | 31 |
| 32 bool ValidateAlarmCreateInfo(const std::string& alarm_name, | 32 bool ValidateAlarmCreateInfo(const std::string& alarm_name, |
| 33 const alarms::AlarmCreateInfo& create_info, | 33 const alarms::AlarmCreateInfo& create_info, |
| 34 const Extension* extension, | 34 const Extension* extension, |
| 35 std::string* error, | 35 std::string* error, |
| 36 std::vector<std::string>* warnings) { | 36 std::vector<std::string>* warnings) { |
| 37 if (create_info.delay_in_minutes.get() && create_info.when.get()) { | 37 if (create_info.delay_in_minutes.get() && create_info.when.get()) { |
| 38 *error = kBothRelativeAndAbsoluteTime; | 38 *error = kBothRelativeAndAbsoluteTime; |
| 39 return false; | 39 return false; |
| 40 } | 40 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 params->name.get() ? *params->name : kDefaultAlarmName; | 111 params->name.get() ? *params->name : kDefaultAlarmName; |
| 112 std::vector<std::string> warnings; | 112 std::vector<std::string> warnings; |
| 113 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(), | 113 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(), |
| 114 &error_, &warnings)) { | 114 &error_, &warnings)) { |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 for (std::vector<std::string>::const_iterator it = warnings.begin(); | 117 for (std::vector<std::string>::const_iterator it = warnings.begin(); |
| 118 it != warnings.end(); ++it) | 118 it != warnings.end(); ++it) |
| 119 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); | 119 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); |
| 120 | 120 |
| 121 Alarm alarm(alarm_name, params->alarm_info, | 121 const int kSecondsPerMinute = 60; |
| 122 base::TimeDelta::FromMinutes( | 122 base::TimeDelta granularity = |
| 123 Manifest::IsUnpackedLocation(extension()->location()) | 123 base::TimeDelta::FromSecondsD( |
| 124 ? kDevDelayMinimum | 124 (Manifest::IsUnpackedLocation(extension()->location()) |
| 125 : kReleaseDelayMinimum), | 125 ? alarms_api_constants::kDevDelayMinimum |
| 126 clock_->Now()); | 126 : kReleaseDelayMinimum)) * |
| 127 kSecondsPerMinute; |
| 128 |
| 129 Alarm alarm(alarm_name, params->alarm_info, granularity, clock_->Now()); |
| 127 AlarmManager::Get(browser_context()) | 130 AlarmManager::Get(browser_context()) |
| 128 ->AddAlarm(extension_id(), alarm, | 131 ->AddAlarm(extension_id(), alarm, |
| 129 base::Bind(&AlarmsCreateFunction::Callback, this)); | 132 base::Bind(&AlarmsCreateFunction::Callback, this)); |
| 130 | 133 |
| 131 return true; | 134 return true; |
| 132 } | 135 } |
| 133 | 136 |
| 134 void AlarmsCreateFunction::Callback() { | 137 void AlarmsCreateFunction::Callback() { |
| 135 SendResponse(true); | 138 SendResponse(true); |
| 136 } | 139 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 base::Bind(&AlarmsClearAllFunction::Callback, this)); | 200 base::Bind(&AlarmsClearAllFunction::Callback, this)); |
| 198 return true; | 201 return true; |
| 199 } | 202 } |
| 200 | 203 |
| 201 void AlarmsClearAllFunction::Callback() { | 204 void AlarmsClearAllFunction::Callback() { |
| 202 SetResult(base::MakeUnique<base::FundamentalValue>(true)); | 205 SetResult(base::MakeUnique<base::FundamentalValue>(true)); |
| 203 SendResponse(true); | 206 SendResponse(true); |
| 204 } | 207 } |
| 205 | 208 |
| 206 } // namespace extensions | 209 } // namespace extensions |
| OLD | NEW |