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/scoped_observer.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/idle.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_registry_observer.h"
28 } // namespace content
30 namespace extensions
{
31 class ExtensionRegistry
;
33 typedef base::Callback
<void(IdleState
)> QueryStateCallback
;
36 explicit IdleMonitor(IdleState initial_state
);
43 class IdleManager
: public ExtensionRegistryObserver
,
44 public EventRouter::Observer
,
47 class IdleTimeProvider
{
50 virtual ~IdleTimeProvider() {}
51 virtual void CalculateIdleState(int idle_threshold
,
52 IdleCallback notify
) = 0;
53 virtual void CalculateIdleTime(IdleTimeCallback notify
) = 0;
54 virtual bool CheckIdleStateIsLocked() = 0;
57 DISALLOW_COPY_AND_ASSIGN(IdleTimeProvider
);
63 virtual ~EventDelegate() {}
64 virtual void OnStateChanged(const std::string
& extension_id
,
65 IdleState new_state
) = 0;
66 virtual void RegisterObserver(EventRouter::Observer
* observer
) = 0;
67 virtual void UnregisterObserver(EventRouter::Observer
* observer
) = 0;
70 DISALLOW_COPY_AND_ASSIGN(EventDelegate
);
73 explicit IdleManager(content::BrowserContext
* context
);
74 ~IdleManager() override
;
78 // KeyedService implementation.
79 void Shutdown() override
;
81 // ExtensionRegistryObserver implementation.
82 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
83 const Extension
* extension
,
84 UnloadedExtensionInfo::Reason reason
) override
;
86 // EventRouter::Observer implementation.
87 void OnListenerAdded(const EventListenerInfo
& details
) override
;
88 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
90 void QueryState(int threshold
, QueryStateCallback notify
);
91 void SetThreshold(const std::string
& extension_id
, int threshold
);
92 static base::StringValue
* CreateIdleValue(IdleState idle_state
);
94 // Override default event class. Callee assumes ownership. Used for testing.
95 void SetEventDelegateForTest(scoped_ptr
<EventDelegate
> event_delegate
);
97 // Override default idle time calculations. Callee assumes ownership. Used
99 void SetIdleTimeProviderForTest(scoped_ptr
<IdleTimeProvider
> idle_provider
);
102 FRIEND_TEST_ALL_PREFIXES(IdleTest
, ActiveToIdle
);
103 FRIEND_TEST_ALL_PREFIXES(IdleTest
, ActiveToLocked
);
104 FRIEND_TEST_ALL_PREFIXES(IdleTest
, IdleToActive
);
105 FRIEND_TEST_ALL_PREFIXES(IdleTest
, IdleToLocked
);
106 FRIEND_TEST_ALL_PREFIXES(IdleTest
, LockedToActive
);
107 FRIEND_TEST_ALL_PREFIXES(IdleTest
, LockedToIdle
);
108 FRIEND_TEST_ALL_PREFIXES(IdleTest
, MultipleExtensions
);
109 FRIEND_TEST_ALL_PREFIXES(IdleTest
, ReAddListener
);
110 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionInterval
);
111 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalBeforeListener
);
112 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalMaximum
);
113 FRIEND_TEST_ALL_PREFIXES(IdleTest
, SetDetectionIntervalMinimum
);
114 FRIEND_TEST_ALL_PREFIXES(IdleTest
, UnloadCleanup
);
116 typedef std::map
<const std::string
, IdleMonitor
> MonitorMap
;
118 IdleMonitor
* GetMonitor(const std::string
& extension_id
);
121 void UpdateIdleState();
122 void UpdateIdleStateCallback(int idle_time
);
124 content::BrowserContext
* const context_
;
126 IdleState last_state_
;
127 MonitorMap monitors_
;
129 base::RepeatingTimer
<IdleManager
> poll_timer_
;
131 scoped_ptr
<IdleTimeProvider
> idle_time_provider_
;
132 scoped_ptr
<EventDelegate
> event_delegate_
;
134 base::ThreadChecker thread_checker_
;
136 // Listen to extension unloaded notification.
137 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
138 extension_registry_observer_
;
140 base::WeakPtrFactory
<IdleManager
> weak_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(IdleManager
);
145 } // namespace extensions
147 #endif // CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_