Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / power / renderer_freezer.cc
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 #include "chrome/browser/chromeos/power/renderer_freezer.h"
6
7 #include <cstring>  // needed for strlen()
8
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12
13 namespace chromeos {
14
15 namespace {
16 const char kFreezerStatePath[] =
17     "/sys/fs/cgroup/freezer/chrome_renderers/freezer.state";
18 const char kFreezeCommand[] = "FROZEN";
19 const char kThawCommand[] = "THAWED";
20
21 }  // namespace
22
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.";
27   } else {
28     frozen_ = true;
29   }
30 }
31
32 void RendererFreezer::SuspendDone(const base::TimeDelta& sleep_duration) {
33   if (!frozen_)
34     return;
35
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.";
42   }
43
44   frozen_ = false;
45 }
46
47 RendererFreezer::RendererFreezer()
48     : state_path_(base::FilePath(kFreezerStatePath)),
49       enabled_(base::PathExists(state_path_) &&
50                base::PathIsWritable(state_path_)),
51       frozen_(false) {
52   if (enabled_) {
53     DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this);
54   } else {
55     LOG(WARNING) << "Cgroup freezer does not exist or is not writable. "
56                  << "Processes will not be frozen during suspend.";
57   }
58 }
59
60 RendererFreezer::~RendererFreezer() {
61   if (enabled_)
62     DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this);
63 }
64
65 }  // namespace chromeos