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