Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: extensions/browser/api/alarms/alarms_api.cc

Issue 2051573003: Perform alarm's period limit check while reading alarm info from StateStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase @tott Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"); 55 if (Manifest::IsUnpackedLocation(extension->location())) {
57 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 alarms_api_constants::kWarningMinimumDevDelay, alarm_name));
60 " In released .crx, alarm \"*\" will fire in approximately" 58 } else {
61 " 1 minutes.",
62 alarm_name));
63 else
64 warnings->push_back(ErrorUtils::FormatErrorMessage( 59 warnings->push_back(ErrorUtils::FormatErrorMessage(
65 "Alarm delay is less than minimum of 1 minutes." 60 alarms_api_constants::kWarningMinimumReleaseDelay, alarm_name));
66 " Alarm \"*\" will fire in approximately 1 minutes.", 61 }
67 alarm_name));
68 } 62 }
69 } 63 }
70 if (create_info.period_in_minutes.get()) { 64 if (create_info.period_in_minutes.get()) {
71 if (*create_info.period_in_minutes < kReleaseDelayMinimum) { 65 if (*create_info.period_in_minutes <
72 static_assert(kReleaseDelayMinimum == 1, 66 alarms_api_constants::kReleaseDelayMinimum) {
73 "warning message must be updated"); 67 if (Manifest::IsUnpackedLocation(extension->location())) {
74 if (Manifest::IsUnpackedLocation(extension->location()))
75 warnings->push_back(ErrorUtils::FormatErrorMessage( 68 warnings->push_back(ErrorUtils::FormatErrorMessage(
76 "Alarm period is less than minimum of 1 minutes." 69 alarms_api_constants::kWarningMinimumDevPeriod, alarm_name));
77 " In released .crx, alarm \"*\" will fire approximately" 70 } else {
78 " every 1 minutes.",
79 alarm_name));
80 else
81 warnings->push_back(ErrorUtils::FormatErrorMessage( 71 warnings->push_back(ErrorUtils::FormatErrorMessage(
82 "Alarm period is less than minimum of 1 minutes." 72 alarms_api_constants::kWarningMinimumReleasePeriod, alarm_name));
83 " Alarm \"*\" will fire approximately every 1 minutes.", 73 }
84 alarm_name));
85 } 74 }
86 } 75 }
87 76
88 return true; 77 return true;
89 } 78 }
90 79
91 } // namespace 80 } // namespace
92 81
93 AlarmsCreateFunction::AlarmsCreateFunction() 82 AlarmsCreateFunction::AlarmsCreateFunction()
94 : clock_(new base::DefaultClock()), owns_clock_(true) { 83 : clock_(new base::DefaultClock()), owns_clock_(true) {
(...skipping 16 matching lines...) Expand all
111 params->name.get() ? *params->name : kDefaultAlarmName; 100 params->name.get() ? *params->name : kDefaultAlarmName;
112 std::vector<std::string> warnings; 101 std::vector<std::string> warnings;
113 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(), 102 if (!ValidateAlarmCreateInfo(alarm_name, params->alarm_info, extension(),
114 &error_, &warnings)) { 103 &error_, &warnings)) {
115 return false; 104 return false;
116 } 105 }
117 for (std::vector<std::string>::const_iterator it = warnings.begin(); 106 for (std::vector<std::string>::const_iterator it = warnings.begin();
118 it != warnings.end(); ++it) 107 it != warnings.end(); ++it)
119 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it); 108 WriteToConsole(content::CONSOLE_MESSAGE_LEVEL_WARNING, *it);
120 109
121 Alarm alarm(alarm_name, params->alarm_info, 110 const int kSecondsPerMinute = 60;
122 base::TimeDelta::FromMinutes( 111 base::TimeDelta granularity =
123 Manifest::IsUnpackedLocation(extension()->location()) 112 base::TimeDelta::FromSecondsD(
124 ? kDevDelayMinimum 113 (Manifest::IsUnpackedLocation(extension()->location())
125 : kReleaseDelayMinimum), 114 ? alarms_api_constants::kDevDelayMinimum
126 clock_->Now()); 115 : alarms_api_constants::kReleaseDelayMinimum)) *
116 kSecondsPerMinute;
117
118 Alarm alarm(alarm_name, params->alarm_info, granularity, clock_->Now());
127 AlarmManager::Get(browser_context()) 119 AlarmManager::Get(browser_context())
128 ->AddAlarm(extension_id(), alarm, 120 ->AddAlarm(extension_id(), alarm,
129 base::Bind(&AlarmsCreateFunction::Callback, this)); 121 base::Bind(&AlarmsCreateFunction::Callback, this));
130 122
131 return true; 123 return true;
132 } 124 }
133 125
134 void AlarmsCreateFunction::Callback() { 126 void AlarmsCreateFunction::Callback() {
135 SendResponse(true); 127 SendResponse(true);
136 } 128 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 base::Bind(&AlarmsClearAllFunction::Callback, this)); 189 base::Bind(&AlarmsClearAllFunction::Callback, this));
198 return true; 190 return true;
199 } 191 }
200 192
201 void AlarmsClearAllFunction::Callback() { 193 void AlarmsClearAllFunction::Callback() {
202 SetResult(base::MakeUnique<base::FundamentalValue>(true)); 194 SetResult(base::MakeUnique<base::FundamentalValue>(true));
203 SendResponse(true); 195 SendResponse(true);
204 } 196 }
205 197
206 } // namespace extensions 198 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/alarms/alarm_manager.cc ('k') | extensions/browser/api/alarms/alarms_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698