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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/threading/thread_checker.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/extensions/event_router.h"
17 #include "chrome/browser/idle.h"
18 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
28 namespace extensions {
30 typedef base::Callback<void(IdleState)> QueryStateCallback;
33 explicit IdleMonitor(IdleState initial_state);
40 class IdleManager : public content::NotificationObserver,
41 public EventRouter::Observer,
42 public BrowserContextKeyedService {
44 class IdleTimeProvider {
47 virtual ~IdleTimeProvider() {}
48 virtual void CalculateIdleState(int idle_threshold,
49 IdleCallback notify) = 0;
50 virtual void CalculateIdleTime(IdleTimeCallback notify) = 0;
51 virtual bool CheckIdleStateIsLocked() = 0;
54 DISALLOW_COPY_AND_ASSIGN(IdleTimeProvider);
60 virtual ~EventDelegate() {}
61 virtual void OnStateChanged(const std::string& extension_id,
62 IdleState new_state) = 0;
63 virtual void RegisterObserver(EventRouter::Observer* observer) = 0;
64 virtual void UnregisterObserver(EventRouter::Observer* observer) = 0;
67 DISALLOW_COPY_AND_ASSIGN(EventDelegate);
70 explicit IdleManager(Profile* profile);
71 virtual ~IdleManager();
75 // BrowserContextKeyedService implementation.
76 virtual void Shutdown() OVERRIDE;
78 // content::NotificationDelegate implementation.
79 virtual void Observe(int type,
80 const content::NotificationSource& source,
81 const content::NotificationDetails& details) OVERRIDE;
83 // EventRouter::Observer implementation.
84 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
85 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
87 void QueryState(int threshold, QueryStateCallback notify);
88 void SetThreshold(const std::string& extension_id, int threshold);
89 static base::StringValue* CreateIdleValue(IdleState idle_state);
91 // Override default event class. Callee assumes ownership. Used for testing.
92 void SetEventDelegateForTest(scoped_ptr<EventDelegate> event_delegate);
94 // Override default idle time calculations. Callee assumes ownership. Used
96 void SetIdleTimeProviderForTest(scoped_ptr<IdleTimeProvider> idle_provider);
99 FRIEND_TEST_ALL_PREFIXES(IdleTest, ActiveToIdle);
100 FRIEND_TEST_ALL_PREFIXES(IdleTest, ActiveToLocked);
101 FRIEND_TEST_ALL_PREFIXES(IdleTest, IdleToActive);
102 FRIEND_TEST_ALL_PREFIXES(IdleTest, IdleToLocked);
103 FRIEND_TEST_ALL_PREFIXES(IdleTest, LockedToActive);
104 FRIEND_TEST_ALL_PREFIXES(IdleTest, LockedToIdle);
105 FRIEND_TEST_ALL_PREFIXES(IdleTest, MultipleExtensions);
106 FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionInterval);
107 FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalBeforeListener);
108 FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalMaximum);
109 FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalMinimum);
110 FRIEND_TEST_ALL_PREFIXES(IdleTest, UnloadCleanup);
112 typedef std::map<const std::string, IdleMonitor> MonitorMap;
114 IdleMonitor* GetMonitor(const std::string& extension_id);
117 void UpdateIdleState();
118 void UpdateIdleStateCallback(int idle_time);
122 IdleState last_state_;
123 MonitorMap monitors_;
125 base::RepeatingTimer<IdleManager> poll_timer_;
126 base::WeakPtrFactory<IdleManager> weak_factory_;
127 content::NotificationRegistrar registrar_;
129 scoped_ptr<IdleTimeProvider> idle_time_provider_;
130 scoped_ptr<EventDelegate> event_delegate_;
132 base::ThreadChecker thread_checker_;
134 DISALLOW_COPY_AND_ASSIGN(IdleManager);
137 } // namespace extensions
139 #endif // CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_