1 // Copyright 2015 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_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "chromeos/dbus/power_manager_client.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_source.h"
23 #include "extensions/browser/extension_host_observer.h"
24 #include "extensions/browser/process_manager_observer.h"
28 namespace extensions
{
34 // This class listens for extension events that should potentially keep the
35 // system awake while they are being processed. Examples include push messages
36 // that arrive from Google's GCM servers and network requests initiated by
37 // extensions while processing the push messages. This class is owned by
39 class ExtensionEventObserver
: public content::NotificationObserver
,
40 public extensions::ProcessManagerObserver
,
41 public extensions::ExtensionHostObserver
,
42 public PowerManagerClient::Observer
{
48 // Runs |suspend_readiness_callback_| if it is non-null and then resets it.
49 // Returns true iff it actually ran the callback.
50 bool MaybeRunSuspendReadinessCallback();
52 // Returns true if the ExtensionEventObserver will delay suspend attempts
53 // for |host| if host has pending push messages or network requests.
54 bool WillDelaySuspendForExtensionHost(extensions::ExtensionHost
* host
);
57 friend class ExtensionEventObserver
;
59 explicit TestApi(base::WeakPtr
<ExtensionEventObserver
> parent
);
61 base::WeakPtr
<ExtensionEventObserver
> parent_
;
63 DISALLOW_COPY_AND_ASSIGN(TestApi
);
66 ExtensionEventObserver();
67 ~ExtensionEventObserver() override
;
69 scoped_ptr
<TestApi
> CreateTestApi();
71 // Called by the WakeOnWifiManager to control whether the
72 // ExtensionEventObserver should or should not delay the system suspend.
73 void SetShouldDelaySuspend(bool should_delay
);
75 // content::NotificationObserver override.
76 void Observe(int type
,
77 const content::NotificationSource
& source
,
78 const content::NotificationDetails
& details
) override
;
80 // extensions::ProcessManagerObserver overrides.
81 void OnBackgroundHostCreated(extensions::ExtensionHost
* host
) override
;
83 // extensions::ExtensionHostObserver overrides.
84 void OnExtensionHostDestroyed(const extensions::ExtensionHost
* host
) override
;
85 void OnBackgroundEventDispatched(const extensions::ExtensionHost
* host
,
86 const std::string
& event_name
,
87 int event_id
) override
;
88 void OnBackgroundEventAcked(const extensions::ExtensionHost
* host
,
89 int event_id
) override
;
90 void OnNetworkRequestStarted(const extensions::ExtensionHost
* host
,
91 uint64 request_id
) override
;
92 void OnNetworkRequestDone(const extensions::ExtensionHost
* host
,
93 uint64 request_id
) override
;
95 // PowerManagerClient::Observer overrides.
96 void SuspendImminent() override
;
97 void DarkSuspendImminent() override
;
98 void SuspendDone(const base::TimeDelta
& duration
) override
;
101 friend class TestApi
;
103 // Called when a new profile is created or destroyed.
104 void OnProfileAdded(Profile
* profile
);
105 void OnProfileDestroyed(Profile
* profile
);
107 // Called when the system is about to perform a regular suspend or a dark
109 void OnSuspendImminent(bool dark_suspend
);
111 // Reports readiness to suspend to the PowerManagerClient if a suspend is
112 // pending and there are no outstanding events keeping the system awake.
113 void MaybeReportSuspendReadiness();
115 struct KeepaliveSources
;
116 base::ScopedPtrHashMap
<const extensions::ExtensionHost
*,
117 scoped_ptr
<KeepaliveSources
>> keepalive_sources_
;
119 std::set
<Profile
*> active_profiles_
;
121 bool should_delay_suspend_
;
122 bool suspend_is_pending_
;
123 int suspend_keepalive_count_
;
124 base::Closure power_manager_callback_
;
125 base::CancelableClosure suspend_readiness_callback_
;
127 content::NotificationRegistrar registrar_
;
129 base::WeakPtrFactory
<ExtensionEventObserver
> weak_factory_
;
131 DISALLOW_COPY_AND_ASSIGN(ExtensionEventObserver
);
134 } // namespace chromeos
136 #endif // CHROME_BROWSER_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_