Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: chrome/browser/chromeos/power/extension_event_observer.h

Issue 823703004: Tracking push events for lucid sleep (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Observers are better. Now with tests! Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_
7
8 #include <set>
9 #include <string>
10
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"
25
26 class Profile;
27
28 namespace extensions {
29 class ExtensionHost;
30 }
31
32 namespace chromeos {
33
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 ininitiated by
Daniel Erat 2015/01/13 23:49:10 s/ininitiated/initiated/
Chirantan Ekbote 2015/01/15 05:06:35 Done.
37 // extensions while processing the push messages. This class is owned by
38 // WakeOnWifiManager.
39 class ExtensionEventObserver : public content::NotificationObserver,
40 public extensions::ProcessManagerObserver,
41 public extensions::ExtensionHostObserver,
42 public PowerManagerClient::Observer {
43 public:
44 class TestApi {
45 public:
46 ~TestApi();
47
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();
51
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);
55
56 private:
57 friend class ExtensionEventObserver;
58
59 explicit TestApi(base::WeakPtr<ExtensionEventObserver> parent);
60
61 base::WeakPtr<ExtensionEventObserver> parent_;
62
63 DISALLOW_COPY_AND_ASSIGN(TestApi);
64 };
65
66 ExtensionEventObserver();
67 ~ExtensionEventObserver() override;
68
69 scoped_ptr<TestApi> GetApiForTesting();
Daniel Erat 2015/01/13 23:49:10 rename to CreateTestApi to make it clearer that ea
Chirantan Ekbote 2015/01/15 05:06:35 Done.
70
71 // Called by the WakeOnWifiManager to determine if the ExtensionEventObserver
72 // should or should not delay the system suspend.
73 void ShouldDelaySuspend(bool should_delay);
Daniel Erat 2015/01/13 23:49:10 SetShouldDelaySuspend()? the current name makes it
Chirantan Ekbote 2015/01/15 05:06:35 Done.
74
75 // content::NotificationObserver override.
76 void Observe(int type,
77 const content::NotificationSource& source,
78 const content::NotificationDetails& details) override;
79
80 // extensions::ProcessManagerObserver overrides.
81 void OnBackgroundHostCreated(extensions::ExtensionHost* host) override;
82
83 // extensions::ExtensionHostObserver overrides.
84 void OnExtensionHostDestroyed(const extensions::ExtensionHost* host) override;
85 void OnExtensionMessageDispatched(const extensions::ExtensionHost* host,
86 const std::string& event_name,
87 int message_id) override;
88 void OnExtensionMessageAcked(const extensions::ExtensionHost* host,
89 int message_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;
94
95 // PowerManagerClient::Observer overrides.
96 void SuspendImminent() override;
97 void DarkSuspendImminent() override;
98 void SuspendDone(const base::TimeDelta& duration) override;
99
100 private:
101 friend class TestApi;
102
103 // Called when a new profile is created or destroyed.
104 void OnProfileAdded(Profile* profile);
105 void OnProfileDestroyed(Profile* profile);
106
107 // Called when the system is about to perform a regular suspend or a dark
108 // suspend.
109 void OnSuspendImminent(bool dark_suspend);
110
111 // Maybe report readiness to suspend to the PowerManagerClient.
Daniel Erat 2015/01/13 23:49:10 nit: explain what determines whether it's reported
Chirantan Ekbote 2015/01/15 05:06:35 Done.
112 void MaybeReportSuspendReadiness();
113
114 struct KeepaliveSources;
115 base::ScopedPtrHashMap<const extensions::ExtensionHost*, KeepaliveSources>
116 keepalive_sources_;
117
118 std::set<Profile*> active_profiles_;
119
120 bool should_delay_suspend_;
121 bool suspend_is_pending_;
122 int suspend_keepalive_count_;
123 base::Closure power_manager_callback_;
124 base::CancelableClosure suspend_readiness_callback_;
125
126 content::NotificationRegistrar registrar_;
127
128 base::WeakPtrFactory<ExtensionEventObserver> weak_factory_;
129
130 DISALLOW_COPY_AND_ASSIGN(ExtensionEventObserver);
131 };
132
133 } // namespace chromeos
134
135 #endif // CHROME_BROWSER_CHROMEOS_POWER_EXTENSION_EVENT_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698