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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_
6 #define CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_
10 #include "base/callback.h"
11 #include "base/cancelable_callback.h"
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/process/kill.h"
16 #include "base/time/time.h"
17 #include "chromeos/chromeos_export.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"
24 class RenderProcessHost
;
31 // Freezes the chrome renderers when the system is about to suspend and thaws
32 // them after the system fully resumes. This class registers itself as a
33 // PowerManagerClient::Observer on creation and unregisters itself on
35 class CHROMEOS_EXPORT RendererFreezer
36 : public PowerManagerClient::RenderProcessManagerDelegate
,
37 public content::NotificationObserver
,
38 public content::RenderProcessHostObserver
{
42 typedef base::Callback
<void(bool)> ResultCallback
;
44 virtual ~Delegate() {}
46 // If |frozen| is true, marks the renderer process |handle| to be frozen
47 // when FreezeRenderers() is called; otherwise marks it to remain unfrozen.
48 // Performs the operation asynchronously on the FILE thread.
49 virtual void SetShouldFreezeRenderer(base::ProcessHandle handle
,
52 // Freezes the renderers marked for freezing by SetShouldFreezeRenderer().
53 // Performs the operation asynchronously on the FILE thread.
54 virtual void FreezeRenderers() = 0;
56 // Thaws the chrome renderers that were frozen by the call to
57 // FreezeRenderers(). Performs the operation asynchronously on the FILE
58 // thread and runs |callback| with the result on the UI thread.
59 virtual void ThawRenderers(ResultCallback callback
) = 0;
61 // Asynchronously checks on the FILE thread if the delegate can freeze
62 // renderers and runs |callback| on the UI thread with the result.
63 virtual void CheckCanFreezeRenderers(ResultCallback callback
) = 0;
66 explicit RendererFreezer(scoped_ptr
<Delegate
> delegate
);
67 ~RendererFreezer() override
;
69 // PowerManagerClient::RenderProcessManagerDelegate implementation.
70 void SuspendImminent() override
;
71 void SuspendDone() override
;
73 // content::NotificationObserver implementation.
74 void Observe(int type
,
75 const content::NotificationSource
& source
,
76 const content::NotificationDetails
& details
) override
;
78 // content::RenderProcessHostObserver overrides.
79 void RenderProcessExited(content::RenderProcessHost
* host
,
80 base::TerminationStatus status
,
81 int exit_code
) override
;
82 void RenderProcessHostDestroyed(content::RenderProcessHost
* host
) override
;
85 // Called after checking if the delegate is capable of freezing renderers.
86 void OnCheckCanFreezeRenderersComplete(bool can_freeze
);
88 // Called after thawing the renderers has completed.
89 void OnThawRenderersComplete(bool success
);
91 // Called whenever the screen locker is shown or hidden.
92 void OnScreenLockStateChanged(chromeos::ScreenLocker
* locker
, bool is_locked
);
94 // Called whenever a new renderer process is created.
95 void OnRenderProcessCreated(content::RenderProcessHost
* rph
);
97 // Delegate that takes care of actually freezing and thawing renderers for us.
98 scoped_ptr
<Delegate
> delegate_
;
100 // Set that keeps track of the RenderProcessHosts for processes that are
101 // hosting GCM extensions.
102 std::set
<int> gcm_extension_processes_
;
104 // Manages notification registrations.
105 content::NotificationRegistrar registrar_
;
107 base::WeakPtrFactory
<RendererFreezer
> weak_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(RendererFreezer
);
112 } // namespace chromeos
114 #endif // CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_