Home | History | Annotate | Download | only in idle
      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 
     13 namespace {
     14 
     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.
     18 
     19 int 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 
     31 namespace extensions {
     32 
     33 bool IdleQueryStateFunction::RunAsync() {
     34   int threshold;
     35   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
     36   threshold = ClampThreshold(threshold);
     37 
     38   IdleManagerFactory::GetForProfile(GetProfile())->QueryState(
     39       threshold, base::Bind(&IdleQueryStateFunction::IdleStateCallback, this));
     40 
     41   // Don't send the response, it'll be sent by our callback
     42   return true;
     43 }
     44 
     45 void IdleQueryStateFunction::IdleStateCallback(IdleState state) {
     46   SetResult(IdleManager::CreateIdleValue(state));
     47   SendResponse(true);
     48 }
     49 
     50 bool IdleSetDetectionIntervalFunction::RunSync() {
     51   int threshold;
     52   EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold));
     53   threshold = ClampThreshold(threshold);
     54 
     55   IdleManagerFactory::GetForProfile(GetProfile())
     56       ->SetThreshold(extension_id(), threshold);
     57 
     58   return true;
     59 }
     60 
     61 }  // namespace extensions
     62