| 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 #include "chrome/browser/chromeos/power/renderer_freezer.h" | 5 #include "chrome/browser/chromeos/power/renderer_freezer.h" |
| 6 | 6 |
| 7 #include <cstring> // needed for strlen() | 7 #include <cstring> // needed for strlen() |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 | 14 |
| 13 namespace chromeos { | 15 namespace chromeos { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 const char kFreezerStatePath[] = | 18 const char kFreezerStatePath[] = |
| 17 "/sys/fs/cgroup/freezer/chrome_renderers/freezer.state"; | 19 "/sys/fs/cgroup/freezer/chrome_renderers/freezer.state"; |
| 18 const char kFreezeCommand[] = "FROZEN"; | 20 const char kFreezeCommand[] = "FROZEN"; |
| 19 const char kThawCommand[] = "THAWED"; | 21 const char kThawCommand[] = "THAWED"; |
| 20 | 22 |
| 21 } // namespace | 23 } // namespace |
| 22 | 24 |
| 23 void RendererFreezer::SuspendImminent() { | 25 void RendererFreezer::SuspendImminent() { |
| 24 if (base::WriteFile(state_path_, kFreezeCommand, strlen(kFreezeCommand)) != | 26 suspend_readiness_callback_ = DBusThreadManager::Get() |
| 25 static_cast<int>(strlen(kFreezeCommand))) { | 27 ->GetPowerManagerClient() |
| 26 PLOG(WARNING) << "Unable to freeze processes in the cgroup freezer."; | 28 ->GetSuspendReadinessCallback(); |
| 27 } else { | 29 |
| 28 frozen_ = true; | 30 base::MessageLoop::current()->PostTask( |
| 29 } | 31 FROM_HERE, |
| 32 base::Bind(&RendererFreezer::OnReadyToSuspend, |
| 33 weak_factory_.GetWeakPtr())); |
| 30 } | 34 } |
| 31 | 35 |
| 32 void RendererFreezer::SuspendDone(const base::TimeDelta& sleep_duration) { | 36 void RendererFreezer::SuspendDone(const base::TimeDelta& sleep_duration) { |
| 33 if (!frozen_) | 37 if (!frozen_) |
| 34 return; | 38 return; |
| 35 | 39 |
| 36 if (base::WriteFile(state_path_, kThawCommand, strlen(kThawCommand)) != | 40 if (base::WriteFile(state_path_, kThawCommand, strlen(kThawCommand)) != |
| 37 static_cast<int>(strlen(kThawCommand))) { | 41 static_cast<int>(strlen(kThawCommand))) { |
| 38 // We failed to write the thaw command and the renderers are still frozen. | 42 // 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 | 43 // We are in big trouble because none of the tabs will be responsive so |
| 40 // let's crash the browser instead. | 44 // let's crash the browser instead. |
| 41 PLOG(FATAL) << "Unable to thaw processes in the cgroup freezer."; | 45 PLOG(FATAL) << "Unable to thaw processes in the cgroup freezer."; |
| 42 } | 46 } |
| 43 | 47 |
| 44 frozen_ = false; | 48 frozen_ = false; |
| 45 } | 49 } |
| 46 | 50 |
| 51 void RendererFreezer::OnReadyToSuspend() { |
| 52 if (base::WriteFile(state_path_, kFreezeCommand, strlen(kFreezeCommand)) != |
| 53 static_cast<int>(strlen(kFreezeCommand))) { |
| 54 PLOG(WARNING) << "Unable to freeze processes in the cgroup freezer."; |
| 55 } else { |
| 56 frozen_ = true; |
| 57 } |
| 58 |
| 59 DCHECK(!suspend_readiness_callback_.is_null()); |
| 60 suspend_readiness_callback_.Run(); |
| 61 suspend_readiness_callback_.Reset(); |
| 62 } |
| 63 |
| 47 RendererFreezer::RendererFreezer() | 64 RendererFreezer::RendererFreezer() |
| 48 : state_path_(base::FilePath(kFreezerStatePath)), | 65 : state_path_(base::FilePath(kFreezerStatePath)), |
| 49 enabled_(base::PathExists(state_path_) && | 66 enabled_(base::PathExists(state_path_) && |
| 50 base::PathIsWritable(state_path_)), | 67 base::PathIsWritable(state_path_)), |
| 51 frozen_(false) { | 68 frozen_(false), |
| 69 weak_factory_(this) { |
| 52 if (enabled_) { | 70 if (enabled_) { |
| 53 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); | 71 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(this); |
| 54 } else { | 72 } else { |
| 55 LOG(WARNING) << "Cgroup freezer does not exist or is not writable. " | 73 LOG(WARNING) << "Cgroup freezer does not exist or is not writable. " |
| 56 << "Processes will not be frozen during suspend."; | 74 << "Processes will not be frozen during suspend."; |
| 57 } | 75 } |
| 58 } | 76 } |
| 59 | 77 |
| 60 RendererFreezer::~RendererFreezer() { | 78 RendererFreezer::~RendererFreezer() { |
| 61 if (enabled_) | 79 if (enabled_) |
| 62 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); | 80 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(this); |
| 63 } | 81 } |
| 64 | 82 |
| 65 } // namespace chromeos | 83 } // namespace chromeos |
| OLD | NEW |