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/idle.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/browser/event_router.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
,
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 // KeyedService 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
, ReAddListener
);
107 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionInterval
);
108 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalBeforeListener
);
109 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalMaximum
);
110 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalMinimum
);
111 FRIEND_TEST_ALL_PREFIXES(IdleTest
, UnloadCleanup
);
113 typedef std::map
<const std::string
, IdleMonitor
> MonitorMap
;
115 IdleMonitor
* GetMonitor(const std::string
& extension_id
);
118 void UpdateIdleState();
119 void UpdateIdleStateCallback(int idle_time
);
123 IdleState last_state_
;
124 MonitorMap monitors_
;
126 base::RepeatingTimer
<IdleManager
> poll_timer_
;
127 base::WeakPtrFactory
<IdleManager
> weak_factory_
;
128 content::NotificationRegistrar registrar_
;
130 scoped_ptr
<IdleTimeProvider
> idle_time_provider_
;
131 scoped_ptr
<EventDelegate
> event_delegate_
;
133 base::ThreadChecker thread_checker_
;
135 DISALLOW_COPY_AND_ASSIGN(IdleManager
);
138 } // namespace extensions
140 #endif // CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_