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