BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / chromeos / power / extension_event_observer.h
blobb576992a6af0d3ef5ce17e788e93dabd5264450e
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_
8 #include <set>
9 #include <string>
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"
26 class Profile;
28 namespace extensions {
29 class ExtensionHost;
32 namespace chromeos {
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
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();
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);
56 private:
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;
100 private:
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
108 // suspend.
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_