#ifndef CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_
#define CHROME_BROWSER_EXTENSIONS_API_IDLE_IDLE_MANAGER_H_
#include <map>
#include <string>
#include "base/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "base/timer/timer.h"
#include "chrome/browser/idle.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "extensions/browser/event_router.h"
namespace base {
class StringValue;
}
class Profile;
namespace extensions {
typedef base::Callback<void(IdleState)> QueryStateCallback;
struct IdleMonitor {
explicit IdleMonitor(IdleState initial_state);
IdleState last_state;
int listeners;
int threshold;
};
class IdleManager : public content::NotificationObserver,
public EventRouter::Observer,
public KeyedService {
public:
class IdleTimeProvider {
public:
IdleTimeProvider() {}
virtual ~IdleTimeProvider() {}
virtual void CalculateIdleState(int idle_threshold,
IdleCallback notify) = 0;
virtual void CalculateIdleTime(IdleTimeCallback notify) = 0;
virtual bool CheckIdleStateIsLocked() = 0;
private:
DISALLOW_COPY_AND_ASSIGN(IdleTimeProvider);
};
class EventDelegate {
public:
EventDelegate() {}
virtual ~EventDelegate() {}
virtual void OnStateChanged(const std::string& extension_id,
IdleState new_state) = 0;
virtual void RegisterObserver(EventRouter::Observer* observer) = 0;
virtual void UnregisterObserver(EventRouter::Observer* observer) = 0;
private:
DISALLOW_COPY_AND_ASSIGN(EventDelegate);
};
explicit IdleManager(Profile* profile);
virtual ~IdleManager();
void Init();
virtual void Shutdown() OVERRIDE;
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
void QueryState(int threshold, QueryStateCallback notify);
void SetThreshold(const std::string& extension_id, int threshold);
static base::StringValue* CreateIdleValue(IdleState idle_state);
void SetEventDelegateForTest(scoped_ptr<EventDelegate> event_delegate);
void SetIdleTimeProviderForTest(scoped_ptr<IdleTimeProvider> idle_provider);
private:
FRIEND_TEST_ALL_PREFIXES(IdleTest, ActiveToIdle);
FRIEND_TEST_ALL_PREFIXES(IdleTest, ActiveToLocked);
FRIEND_TEST_ALL_PREFIXES(IdleTest, IdleToActive);
FRIEND_TEST_ALL_PREFIXES(IdleTest, IdleToLocked);
FRIEND_TEST_ALL_PREFIXES(IdleTest, LockedToActive);
FRIEND_TEST_ALL_PREFIXES(IdleTest, LockedToIdle);
FRIEND_TEST_ALL_PREFIXES(IdleTest, MultipleExtensions);
FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionInterval);
FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalBeforeListener);
FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalMaximum);
FRIEND_TEST_ALL_PREFIXES(IdleTest, SetDetectionIntervalMinimum);
FRIEND_TEST_ALL_PREFIXES(IdleTest, UnloadCleanup);
typedef std::map<const std::string, IdleMonitor> MonitorMap;
IdleMonitor* GetMonitor(const std::string& extension_id);
void StartPolling();
void StopPolling();
void UpdateIdleState();
void UpdateIdleStateCallback(int idle_time);
Profile* profile_;
IdleState last_state_;
MonitorMap monitors_;
base::RepeatingTimer<IdleManager> poll_timer_;
base::WeakPtrFactory<IdleManager> weak_factory_;
content::NotificationRegistrar registrar_;
scoped_ptr<IdleTimeProvider> idle_time_provider_;
scoped_ptr<EventDelegate> event_delegate_;
base::ThreadChecker thread_checker_;
DISALLOW_COPY_AND_ASSIGN(IdleManager);
};
}
#endif