Home | History | Annotate | Download | only in power
      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.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_
      6 #define CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/cancelable_callback.h"
     10 #include "base/macros.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/time/time.h"
     14 #include "chromeos/chromeos_export.h"
     15 #include "chromeos/dbus/power_manager_client.h"
     16 
     17 namespace chromeos {
     18 
     19 // 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
     21 // PowerManagerClient::Observer on creation and unregisters itself on
     22 // destruction.
     23 class CHROMEOS_EXPORT RendererFreezer : public PowerManagerClient::Observer {
     24  public:
     25   class Delegate {
     26    public:
     27     virtual ~Delegate() {}
     28 
     29     // Freezes the chrome renderers.  Returns true if the operation was
     30     // successful.
     31     virtual bool FreezeRenderers() = 0;
     32 
     33     // Thaws the chrome renderers.  Returns true if the operation was
     34     // successful.
     35     virtual bool ThawRenderers() = 0;
     36 
     37     // Returns true iff the delegate is capable of freezing renderers.
     38     virtual bool CanFreezeRenderers() = 0;
     39   };
     40 
     41   explicit RendererFreezer(scoped_ptr<Delegate> delegate);
     42   virtual ~RendererFreezer();
     43 
     44   // PowerManagerClient::Observer implementation
     45   virtual void SuspendImminent() OVERRIDE;
     46   virtual void SuspendDone(const base::TimeDelta& sleep_duration) OVERRIDE;
     47 
     48  private:
     49   // Called when all asynchronous work is complete and renderers can be frozen.
     50   void OnReadyToSuspend(const base::Closure& power_manager_callback);
     51 
     52   // Used to ensure that renderers do not get frozen if the suspend is canceled.
     53   base::CancelableClosure suspend_readiness_callback_;
     54 
     55   bool frozen_;
     56 
     57   scoped_ptr<Delegate> delegate_;
     58 
     59   base::WeakPtrFactory<RendererFreezer> weak_factory_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(RendererFreezer);
     62 };
     63 
     64 }  // namespace chromeos
     65 
     66 #endif  // CHROME_BROWSER_CHROMEOS_POWER_RENDERER_FREEZER_H_
     67