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 "chrome/browser/extensions/api/idle/idle_api.h"
8 #include "base/callback.h"
9 #include "chrome/browser/extensions/api/idle/idle_api_constants.h"
10 #include "chrome/browser/extensions/api/idle/idle_manager.h"
11 #include "chrome/browser/extensions/api/idle/idle_manager_factory.h"
15 const int kMinThreshold = 15; // In seconds. Set >1 sec for security concerns.
16 const int kMaxThreshold = 4*60*60; // Four hours, in seconds. Not set
17 // arbitrarily high for security concerns.
19 int ClampThreshold(int threshold) {
20 if (threshold < kMinThreshold) {
21 threshold = kMinThreshold;
22 } else if (threshold > kMaxThreshold) {
23 threshold = kMaxThreshold;
31 namespace extensions {
33 bool IdleQueryStateFunction::RunAsync() {
35 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
36 threshold = ClampThreshold(threshold);
38 IdleManagerFactory::GetForProfile(GetProfile())->QueryState(
39 threshold, base::Bind(&IdleQueryStateFunction::IdleStateCallback, this));
41 // Don't send the response, it'll be sent by our callback
45 void IdleQueryStateFunction::IdleStateCallback(IdleState state) {
46 SetResult(IdleManager::CreateIdleValue(state));
50 bool IdleSetDetectionIntervalFunction::RunSync() {
52 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
53 threshold = ClampThreshold(threshold);
55 IdleManagerFactory::GetForProfile(GetProfile())
56 ->SetThreshold(extension_id(), threshold);
61 } // namespace extensions