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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/idle/idle_api.h"
8 #include "base/callback.h"
9 #include "base/values.h"
10 #include "extensions/browser/api/idle/idle_api_constants.h"
11 #include "extensions/browser/api/idle/idle_manager.h"
12 #include "extensions/browser/api/idle/idle_manager_factory.h"
14 namespace extensions
{
18 // In seconds. Set >1 sec for security concerns.
19 const int kMinThreshold
= 15;
21 // Four hours, in seconds. Not set arbitrarily high for security concerns.
22 const int kMaxThreshold
= 4 * 60 * 60;
24 int ClampThreshold(int threshold
) {
25 if (threshold
< kMinThreshold
) {
26 threshold
= kMinThreshold
;
27 } else if (threshold
> kMaxThreshold
) {
28 threshold
= kMaxThreshold
;
36 ExtensionFunction::ResponseAction
IdleQueryStateFunction::Run() {
38 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(0, &threshold
));
39 threshold
= ClampThreshold(threshold
);
41 IdleManagerFactory::GetForBrowserContext(context_
)->QueryState(
42 threshold
, base::Bind(&IdleQueryStateFunction::IdleStateCallback
, this));
44 return RespondLater();
47 void IdleQueryStateFunction::IdleStateCallback(ui::IdleState state
) {
48 Respond(OneArgument(IdleManager::CreateIdleValue(state
)));
51 ExtensionFunction::ResponseAction
IdleSetDetectionIntervalFunction::Run() {
53 EXTENSION_FUNCTION_VALIDATE(args_
->GetInteger(0, &threshold
));
54 threshold
= ClampThreshold(threshold
);
56 IdleManagerFactory::GetForBrowserContext(context_
)
57 ->SetThreshold(extension_id(), threshold
);
59 return RespondNow(NoArguments());
62 } // namespace extensions