| OLD | NEW |
| (Empty) |
| 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_IDLE_API_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_IDLE_API_H_ | |
| 7 | |
| 8 #include "chrome/browser/idle.h" | |
| 9 #include "chrome/browser/extensions/extension_function.h" | |
| 10 | |
| 11 class Profile; | |
| 12 | |
| 13 // Event router class for events related to the idle API. | |
| 14 class ExtensionIdleEventRouter { | |
| 15 public: | |
| 16 static void OnIdleStateChange(Profile* profile, | |
| 17 IdleState idleState); | |
| 18 private: | |
| 19 DISALLOW_COPY_AND_ASSIGN(ExtensionIdleEventRouter); | |
| 20 }; | |
| 21 | |
| 22 // Implementation of the chrome.idle.queryState API. | |
| 23 class ExtensionIdleQueryStateFunction : public AsyncExtensionFunction { | |
| 24 public: | |
| 25 DECLARE_EXTENSION_FUNCTION_NAME("idle.queryState") | |
| 26 | |
| 27 protected: | |
| 28 virtual ~ExtensionIdleQueryStateFunction() {} | |
| 29 | |
| 30 // ExtensionFunction: | |
| 31 virtual bool RunImpl() OVERRIDE; | |
| 32 | |
| 33 private: | |
| 34 void IdleStateCallback(int threshold, IdleState state); | |
| 35 }; | |
| 36 | |
| 37 // Class used for caching answers from CalculateIdleState. | |
| 38 class ExtensionIdleCache { | |
| 39 public: | |
| 40 static IdleState CalculateIdleState(int threshold); | |
| 41 static void UpdateCache(int threshold, IdleState state); | |
| 42 | |
| 43 private: | |
| 44 FRIEND_TEST_ALL_PREFIXES(ExtensionIdleApiTest, CacheTest); | |
| 45 | |
| 46 struct CacheData { | |
| 47 // Latest moment in history after which we are certain that there was some | |
| 48 // activity. If we are querying for a threshold beyond this moment, we know | |
| 49 // the result will be active. | |
| 50 double latest_known_active; | |
| 51 // [idle_interval_start, idle_interval_end] is the latest interval in | |
| 52 // history for which we are certain there was no activity. | |
| 53 double idle_interval_start; | |
| 54 double idle_interval_end; | |
| 55 // Set iff the last recored query result was IDLE_STATE_LOCKED. Equals the | |
| 56 // moment the result was recorded in cache. | |
| 57 double latest_locked; | |
| 58 }; | |
| 59 | |
| 60 // We assume moment is increasing with every call to one of these two methods. | |
| 61 | |
| 62 // Tries to determine the idle state based on results of previous queries. | |
| 63 // |threshold| is time span in seconds from now we consider in calculating | |
| 64 // state. | |
| 65 // |moment| is the moment in time this method was called (should be equal to | |
| 66 // now, except in tests). | |
| 67 // Returns calculated state, or IDLE_STATE_UNKNOWN if the state could not be | |
| 68 // determined. | |
| 69 static IdleState CalculateState(int threshold, double moment); | |
| 70 | |
| 71 // Updates cached data with the latest query result. | |
| 72 // |threshold| is threshold parameter of the query. | |
| 73 // |state| is result of the query. | |
| 74 // |moment| is moment in time this method is called. | |
| 75 static void Update(int threshold, IdleState state, double moment); | |
| 76 | |
| 77 static int get_min_threshold(); | |
| 78 static double get_throttle_interval(); | |
| 79 | |
| 80 static CacheData cached_data; | |
| 81 }; | |
| 82 | |
| 83 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_IDLE_API_H_ | |
| OLD | NEW |