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 #include "chrome/browser/chromeos/power/renderer_freezer.h"
7 #include <cstring> // needed for strlen()
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
16 const char kFreezerStatePath[] =
17 "/sys/fs/cgroup/freezer/chrome_renderers/freezer.state";
18 const char kFreezeCommand[] = "FROZEN";
19 const char kThawCommand[] = "THAWED";
23 void RendererFreezer::SuspendImminent() {
24 if (base::WriteFile(state_path_, kFreezeCommand, strlen(kFreezeCommand)) !=
25 static_cast<int>(strlen(kFreezeCommand))) {
26 PLOG(WARNING) << "Unable to freeze processes in the cgroup freezer.";
32 void RendererFreezer::SuspendDone(const base::TimeDelta& sleep_duration) {
36 if (base::WriteFile(state_path_, kThawCommand, strlen(kThawCommand)) !=
37 static_cast<int>(strlen(kThawCommand))) {
38 // We failed to write the thaw command and the renderers are still frozen.
39 // We are in big trouble because none of the tabs will be responsive so
40 // let's crash the browser instead.
41 PLOG(FATAL) << "Unable to thaw processes in the cgroup freezer.";
47 RendererFreezer::RendererFreezer()
48 : state_path_(base::FilePath(kFreezerStatePath)),
49 enabled_(base::PathExists(state_path_) &&
50 base::PathIsWritable(state_path_)),
53 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
55 LOG(WARNING) << "Cgroup freezer does not exist or is not writable. "
56 << "Processes will not be frozen during suspend.";
60 RendererFreezer::~RendererFreezer() {
62 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
65 } // namespace chromeos