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. 4 5#include "chrome/browser/extensions/api/idle/idle_api.h" 6 7#include "base/bind.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" 12 13namespace { 14 15const int kMinThreshold = 15; // In seconds. Set >1 sec for security concerns. 16const int kMaxThreshold = 4*60*60; // Four hours, in seconds. Not set 17 // arbitrarily high for security concerns. 18 19int ClampThreshold(int threshold) { 20 if (threshold < kMinThreshold) { 21 threshold = kMinThreshold; 22 } else if (threshold > kMaxThreshold) { 23 threshold = kMaxThreshold; 24 } 25 26 return threshold; 27} 28 29} // namespace 30 31namespace extensions { 32 33bool IdleQueryStateFunction::RunImpl() { 34 int threshold; 35 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold)); 36 threshold = ClampThreshold(threshold); 37 38 IdleManagerFactory::GetForProfile(profile())->QueryState( 39 threshold, 40 base::Bind(&IdleQueryStateFunction::IdleStateCallback, this)); 41 42 // Don't send the response, it'll be sent by our callback 43 return true; 44} 45 46void IdleQueryStateFunction::IdleStateCallback(IdleState state) { 47 SetResult(IdleManager::CreateIdleValue(state)); 48 SendResponse(true); 49} 50 51bool IdleSetDetectionIntervalFunction::RunImpl() { 52 int threshold; 53 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold)); 54 threshold = ClampThreshold(threshold); 55 56 IdleManagerFactory::GetForProfile(profile())->SetThreshold(extension_id(), 57 threshold); 58 59 return true; 60} 61 62} // namespace extensions 63