OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/settings/shutdown_policy_handler.h" | 5 #include "chrome/browser/chromeos/settings/shutdown_policy_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "chromeos/settings/cros_settings_names.h" | 9 #include "chromeos/settings/cros_settings_names.h" |
10 #include "chromeos/settings/cros_settings_provider.h" | 10 #include "chromeos/settings/cros_settings_provider.h" |
11 | 11 |
12 namespace chromeos { | 12 namespace chromeos { |
13 | 13 |
14 ShutdownPolicyHandler::ShutdownPolicyHandler(CrosSettings* cros_settings, | 14 ShutdownPolicyHandler::ShutdownPolicyHandler(CrosSettings* cros_settings, |
15 Delegate* delegate) | 15 Delegate* delegate) |
16 : cros_settings_(cros_settings), delegate_(delegate), weak_factory_(this) { | 16 : cros_settings_(cros_settings), delegate_(delegate), weak_factory_(this) { |
17 if (delegate_) { | 17 DCHECK(cros_settings_); |
18 shutdown_policy_subscription_ = cros_settings_->AddSettingsObserver( | 18 DCHECK(delegate_); |
19 kRebootOnShutdown, | 19 // Notify the delegate if the setting changes. |
20 base::Bind(&ShutdownPolicyHandler::OnShutdownPolicyChanged, | 20 shutdown_policy_subscription_ = cros_settings_->AddSettingsObserver( |
21 weak_factory_.GetWeakPtr())); | 21 kRebootOnShutdown, |
22 } | 22 base::Bind(&ShutdownPolicyHandler::CheckIfRebootOnShutdown, |
| 23 weak_factory_.GetWeakPtr())); |
23 } | 24 } |
24 | 25 |
25 ShutdownPolicyHandler::~ShutdownPolicyHandler() {} | 26 ShutdownPolicyHandler::~ShutdownPolicyHandler() {} |
26 | 27 |
27 void ShutdownPolicyHandler::Shutdown() { | 28 void ShutdownPolicyHandler::CheckIfRebootOnShutdown() { |
28 shutdown_policy_subscription_.reset(); | 29 // Only notify the delegate after trusted settings are loaded. |
29 delegate_ = nullptr; | |
30 } | |
31 | |
32 void ShutdownPolicyHandler::CallDelegate(bool reboot_on_shutdown) { | |
33 if (delegate_) | |
34 delegate_->OnShutdownPolicyChanged(reboot_on_shutdown); | |
35 } | |
36 | |
37 void ShutdownPolicyHandler::OnShutdownPolicyChanged() { | |
38 CheckIfRebootOnShutdown(base::Bind(&ShutdownPolicyHandler::CallDelegate, | |
39 weak_factory_.GetWeakPtr())); | |
40 } | |
41 | |
42 void ShutdownPolicyHandler::CheckIfRebootOnShutdown( | |
43 const RebootOnShutdownCallback& callback) { | |
44 CrosSettingsProvider::TrustedStatus status = | 30 CrosSettingsProvider::TrustedStatus status = |
45 cros_settings_->PrepareTrustedValues( | 31 cros_settings_->PrepareTrustedValues( |
46 base::Bind(&ShutdownPolicyHandler::CheckIfRebootOnShutdown, | 32 base::Bind(&ShutdownPolicyHandler::CheckIfRebootOnShutdown, |
47 weak_factory_.GetWeakPtr(), callback)); | 33 weak_factory_.GetWeakPtr())); |
48 if (status != CrosSettingsProvider::TRUSTED) | 34 if (status != CrosSettingsProvider::TRUSTED) |
49 return; | 35 return; |
50 | 36 |
51 // Get the updated policy. | 37 // Get the updated policy. |
52 bool reboot_on_shutdown = false; | 38 bool reboot_on_shutdown = false; |
53 cros_settings_->GetBoolean(kRebootOnShutdown, &reboot_on_shutdown); | 39 cros_settings_->GetBoolean(kRebootOnShutdown, &reboot_on_shutdown); |
54 callback.Run(reboot_on_shutdown); | 40 delegate_->OnShutdownPolicyChanged(reboot_on_shutdown); |
55 } | 41 } |
56 | 42 |
57 } // namespace chromeos | 43 } // namespace chromeos |
OLD | NEW |