OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ |
6 #define CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ |
7 | 7 |
| 8 #include <set> |
| 9 |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/cancelable_callback.h" | 11 #include "base/cancelable_callback.h" |
10 #include "base/macros.h" | 12 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/process/kill.h" |
13 #include "base/time/time.h" | 16 #include "base/time/time.h" |
14 #include "chromeos/chromeos_export.h" | 17 #include "chromeos/chromeos_export.h" |
15 #include "chromeos/dbus/power_manager_client.h" | 18 #include "chromeos/dbus/power_manager_client.h" |
| 19 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/render_process_host_observer.h" |
| 22 |
| 23 namespace content { |
| 24 class RenderProcessHost; |
| 25 } |
16 | 26 |
17 namespace chromeos { | 27 namespace chromeos { |
18 | 28 |
19 // Freezes the chrome renderers when the system is about to suspend and thaws | 29 // Freezes the chrome renderers when the system is about to suspend and thaws |
20 // them after the system fully resumes. This class registers itself as a | 30 // them after the system fully resumes. This class registers itself as a |
21 // PowerManagerClient::Observer on creation and unregisters itself on | 31 // PowerManagerClient::Observer on creation and unregisters itself on |
22 // destruction. | 32 // destruction. |
23 class CHROMEOS_EXPORT RendererFreezer : public PowerManagerClient::Observer { | 33 class CHROMEOS_EXPORT RendererFreezer |
| 34 : public PowerManagerClient::RenderProcessManagerDelegate, |
| 35 public content::NotificationObserver, |
| 36 public content::RenderProcessHostObserver { |
24 public: | 37 public: |
25 class Delegate { | 38 class Delegate { |
26 public: | 39 public: |
27 virtual ~Delegate() {} | 40 virtual ~Delegate() {} |
28 | 41 |
29 // Freezes the chrome renderers. Returns true if the operation was | 42 // If |frozen| is true, marks the renderer process |handle| to be frozen |
30 // successful. | 43 // when FreezeRenderers() is called; otherwise marks it to remain unfrozen. |
| 44 virtual void SetShouldFreezeRenderer(base::ProcessHandle handle, |
| 45 bool frozen) = 0; |
| 46 |
| 47 // Freezes the renderers marked for freezing by SetShouldFreezeRenderer(). |
| 48 // Returns true if the operation was successful. |
31 virtual bool FreezeRenderers() = 0; | 49 virtual bool FreezeRenderers() = 0; |
32 | 50 |
33 // Thaws the chrome renderers. Returns true if the operation was | 51 // Thaws the chrome renderers that were frozen by the call to |
34 // successful. | 52 // FreezeRenderers(). Returns true if the operation was successful. |
35 virtual bool ThawRenderers() = 0; | 53 virtual bool ThawRenderers() = 0; |
36 | 54 |
37 // Returns true iff the delegate is capable of freezing renderers. | 55 // Returns true iff the delegate is capable of freezing renderers. |
38 virtual bool CanFreezeRenderers() = 0; | 56 virtual bool CanFreezeRenderers() = 0; |
39 }; | 57 }; |
40 | 58 |
41 explicit RendererFreezer(scoped_ptr<Delegate> delegate); | 59 explicit RendererFreezer(scoped_ptr<Delegate> delegate); |
42 virtual ~RendererFreezer(); | 60 ~RendererFreezer() override; |
43 | 61 |
44 // PowerManagerClient::Observer implementation | 62 // PowerManagerClient::RenderProcessManagerDelegate implementation. |
45 virtual void SuspendImminent() override; | 63 void SuspendImminent() override; |
46 virtual void SuspendDone(const base::TimeDelta& sleep_duration) override; | 64 void SuspendDone() override; |
| 65 |
| 66 // content::NotificationObserver implementation. |
| 67 void Observe(int type, |
| 68 const content::NotificationSource& source, |
| 69 const content::NotificationDetails& details) override; |
| 70 |
| 71 // content::RenderProcessHostObserver overrides. |
| 72 void RenderProcessExited(content::RenderProcessHost* host, |
| 73 base::TerminationStatus status, |
| 74 int exit_code) override; |
| 75 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
47 | 76 |
48 private: | 77 private: |
49 // Called when all asynchronous work is complete and renderers can be frozen. | 78 // Called whenever a new renderer process is created. |
50 void OnReadyToSuspend(const base::Closure& power_manager_callback); | 79 void OnRenderProcessCreated(content::RenderProcessHost* rph); |
51 | 80 |
52 // Used to ensure that renderers do not get frozen if the suspend is canceled. | 81 // Tracks if the renderers are currently frozen. |
53 base::CancelableClosure suspend_readiness_callback_; | |
54 | |
55 bool frozen_; | 82 bool frozen_; |
56 | 83 |
| 84 // Delegate that takes care of actually freezing and thawing renderers for us. |
57 scoped_ptr<Delegate> delegate_; | 85 scoped_ptr<Delegate> delegate_; |
58 | 86 |
| 87 // Set that keeps track of the RenderProcessHosts for processes that are |
| 88 // hosting GCM extensions. |
| 89 std::set<int> gcm_extension_processes_; |
| 90 |
| 91 // Manages notification registrations. |
| 92 content::NotificationRegistrar registrar_; |
| 93 |
59 base::WeakPtrFactory<RendererFreezer> weak_factory_; | 94 base::WeakPtrFactory<RendererFreezer> weak_factory_; |
60 | 95 |
61 DISALLOW_COPY_AND_ASSIGN(RendererFreezer); | 96 DISALLOW_COPY_AND_ASSIGN(RendererFreezer); |
62 }; | 97 }; |
63 | 98 |
64 } // namespace chromeos | 99 } // namespace chromeos |
65 | 100 |
66 #endif // CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ | 101 #endif // CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_ |
OLD | NEW |