Chromium Code Reviews| 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 kDevDelayMinimum = 0; | |
| 31 | 30 |
| 32 bool ValidateAlarmCreateInfo(const std::string& alarm_name, | 31 bool ValidateAlarmCreateInfo(const std::string& alarm_name, |
| 33 const alarms::AlarmCreateInfo& create_info, | 32 const alarms::AlarmCreateInfo& create_info, |
| 34 const Extension* extension, | 33 const Extension* extension, |
| 35 std::string* error, | 34 std::string* error, |
| 36 std::vector<std::string>* warnings) { | 35 std::vector<std::string>* warnings) { |
| 37 if (create_info.delay_in_minutes.get() && create_info.when.get()) { | 36 if (create_info.delay_in_minutes.get() && create_info.when.get()) { |
| 38 *error = kBothRelativeAndAbsoluteTime; | 37 *error = kBothRelativeAndAbsoluteTime; |
| 39 return false; | 38 return false; |
| 40 } | 39 } |
| 41 if (create_info.delay_in_minutes == NULL && create_info.when == NULL && | 40 if (create_info.delay_in_minutes == NULL && create_info.when == NULL && |
| 42 create_info.period_in_minutes == NULL) { | 41 create_info.period_in_minutes == NULL) { |
| 43 *error = kNoScheduledTime; | 42 *error = kNoScheduledTime; |
| 44 return false; | 43 return false; |
| 45 } | 44 } |
| 46 | 45 |
| 47 // Users can always use an absolute timeout to request an arbitrarily-short or | 46 // Users can always use an absolute timeout to request an arbitrarily-short or |
| 48 // negative delay. We won't honor the short timeout, but we can't check it | 47 // negative delay. We won't honor the short timeout, but we can't check it |
| 49 // and warn the user because it would introduce race conditions (say they | 48 // and warn the user because it would introduce race conditions (say they |
| 50 // compute a long-enough timeout, but then the call into the alarms interface | 49 // compute a long-enough timeout, but then the call into the alarms interface |
| 51 // gets delayed past the boundary). However, it's still worth warning about | 50 // gets delayed past the boundary). However, it's still worth warning about |
| 52 // relative delays that are shorter than we'll honor. | 51 // relative delays that are shorter than we'll honor. |
| 53 if (create_info.delay_in_minutes.get()) { | 52 if (create_info.delay_in_minutes.get()) { |
| 54 if (*create_info.delay_in_minutes < kReleaseDelayMinimum) { | 53 if (*create_info.delay_in_minutes < |
| 55 static_assert(kReleaseDelayMinimum == 1, | 54 alarms_api_constants::kReleaseDelayMinimum) { |
| 56 "warning message must be updated"); | |
|
asargent_no_longer_on_chrome
2016/06/10 00:07:33
optional: I see you moved these to a single assert
lazyboy
2016/06/10 20:01:31
I couldn't use static assert with variable declare
| |
| 57 if (Manifest::IsUnpackedLocation(extension->location())) | 55 if (Manifest::IsUnpackedLocation(extension->location())) |
| 58 warnings->push_back(ErrorUtils::FormatErrorMessage( | 56 warnings->push_back(ErrorUtils::FormatErrorMessage( |
| 59 "Alarm delay is less than minimum of 1 minutes." | 57 "Alarm delay is less than minimum of 1 minutes." |
| 60 " In released .crx, alarm \"*\" will fire in approximately" | 58 " In released .crx, alarm \"*\" will fire in approximately" |
| 61 " 1 minutes.", | 59 " 1 minutes.", |
| 62 alarm_name)); | 60 alarm_name)); |
| 63 else | 61 else |
| 64 warnings->push_back(ErrorUtils::FormatErrorMessage( | 62 warnings->push_back(ErrorUtils::FormatErrorMessage( |
| 65 "Alarm delay is less than minimum of 1 minutes." | 63 "Alarm delay is less than minimum of 1 minutes." |
| 66 " Alarm \"*\" will fire in approximately 1 minutes.", | 64 " Alarm \"*\" will fire in approximately 1 minutes.", |
| 67 alarm_name)); | 65 alarm_name)); |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 if (create_info.period_in_minutes.get()) { | 68 if (create_info.period_in_minutes.get()) { |
| 71 if (*create_info.period_in_minutes < kReleaseDelayMinimum) { | 69 if (*create_info.period_in_minutes < |
| 72 static_assert(kReleaseDelayMinimum == 1, | 70 alarms_api_constants::kReleaseDelayMinimum) { |
| 73 "warning message must be updated"); | |
| 74 if (Manifest::IsUnpackedLocation(extension->location())) | 71 if (Manifest::IsUnpackedLocation(extension->location())) |
| 75 warnings->push_back(ErrorUtils::FormatErrorMessage( | 72 warnings->push_back(ErrorUtils::FormatErrorMessage( |
| 76 "Alarm period is less than minimum of 1 minutes." | 73 "Alarm period is less than minimum of 1 minutes." |
| 77 " In released .crx, alarm \"*\" will fire approximately" | 74 " In released .crx, alarm \"*\" will fire approximately" |
| 78 " every 1 minutes.", | 75 " every 1 minutes.", |
| 79 alarm_name)); | 76 alarm_name)); |
| 80 else | 77 else |
| 81 warnings->push_back(ErrorUtils::FormatErrorMessage( | 78 warnings->push_back(ErrorUtils::FormatErrorMessage( |
| 82 "Alarm period is less than minimum of 1 minutes." | 79 "Alarm period is less than minimum of 1 minutes." |
| 83 " Alarm \"*\" will fire approximately every 1 minutes.", | 80 " Alarm \"*\" will fire approximately every 1 minutes.", |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 111 params->name.get() ? *params->name : kDefaultAlarmName; | 108 params->name.get() ? *params->name : kDefaultAlarmName; |
| 112 std::vector<std::string> warnings; | 109 std::vector<std::string> warnings; |
| 113 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(), | 110 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(), |
| 114 &error_, &warnings)) { | 111 &error_, &warnings)) { |
| 115 return false; | 112 return false; |
| 116 } | 113 } |
| 117 for (std::vector<std::string>::const_iterator it = warnings.begin(); | 114 for (std::vector<std::string>::const_iterator it = warnings.begin(); |
| 118 it != warnings.end(); ++it) | 115 it != warnings.end(); ++it) |
| 119 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); | 116 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); |
| 120 | 117 |
| 121 Alarm alarm(alarm_name, params->alarm_info, | 118 const int kSecondsPerMinute = 60; |
| 122 base::TimeDelta::FromMinutes( | 119 base::TimeDelta granularity = |
| 123 Manifest::IsUnpackedLocation(extension()->location()) | 120 base::TimeDelta::FromSecondsD( |
| 124 ? kDevDelayMinimum | 121 (Manifest::IsUnpackedLocation(extension()->location()) |
| 125 : kReleaseDelayMinimum), | 122 ? alarms_api_constants::kDevDelayMinimum |
| 126 clock_->Now()); | 123 : alarms_api_constants::kReleaseDelayMinimum)) * |
| 124 kSecondsPerMinute; | |
| 125 | |
| 126 Alarm alarm(alarm_name, params->alarm_info, granularity, clock_->Now()); | |
| 127 AlarmManager::Get(browser_context()) | 127 AlarmManager::Get(browser_context()) |
| 128 ->AddAlarm(extension_id(), alarm, | 128 ->AddAlarm(extension_id(), alarm, |
| 129 base::Bind(&AlarmsCreateFunction::Callback, this)); | 129 base::Bind(&AlarmsCreateFunction::Callback, this)); |
| 130 | 130 |
| 131 return true; | 131 return true; |
| 132 } | 132 } |
| 133 | 133 |
| 134 void AlarmsCreateFunction::Callback() { | 134 void AlarmsCreateFunction::Callback() { |
| 135 SendResponse(true); | 135 SendResponse(true); |
| 136 } | 136 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 base::Bind(&AlarmsClearAllFunction::Callback, this)); | 197 base::Bind(&AlarmsClearAllFunction::Callback, this)); |
| 198 return true; | 198 return true; |
| 199 } | 199 } |
| 200 | 200 |
| 201 void AlarmsClearAllFunction::Callback() { | 201 void AlarmsClearAllFunction::Callback() { |
| 202 SetResult(base::MakeUnique<base::FundamentalValue>(true)); | 202 SetResult(base::MakeUnique<base::FundamentalValue>(true)); |
| 203 SendResponse(true); | 203 SendResponse(true); |
| 204 } | 204 } |
| 205 | 205 |
| 206 } // namespace extensions | 206 } // namespace extensions |
| OLD | NEW |