| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/webui/settings/chromeos/date_time_handler.h" | 5 #include "chrome/browser/ui/webui/settings/chromeos/date_time_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | 11 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
| 12 #include "chrome/browser/chromeos/set_time_dialog.h" |
| 12 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h" | 13 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h" |
| 13 #include "chrome/browser/chromeos/system/timezone_util.h" | 14 #include "chrome/browser/chromeos/system/timezone_util.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "chromeos/chromeos_switches.h" | 16 #include "chromeos/chromeos_switches.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/system_clock_client.h" |
| 16 #include "chromeos/settings/timezone_settings.h" | 19 #include "chromeos/settings/timezone_settings.h" |
| 17 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 21 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
| 19 #include "content/public/browser/web_ui_data_source.h" | 23 #include "content/public/browser/web_ui_data_source.h" |
| 20 | 24 |
| 21 namespace chromeos { | 25 namespace chromeos { |
| 22 namespace settings { | 26 namespace settings { |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| 26 // Returns whether the system time zone automatic detection policy is disabled | 30 // Returns whether the system time zone automatic detection policy is disabled |
| 27 // by a flag. | 31 // by a flag. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 return new DateTimeHandler; | 92 return new DateTimeHandler; |
| 89 } | 93 } |
| 90 | 94 |
| 91 void DateTimeHandler::RegisterMessages() { | 95 void DateTimeHandler::RegisterMessages() { |
| 92 web_ui()->RegisterMessageCallback( | 96 web_ui()->RegisterMessageCallback( |
| 93 "dateTimePageReady", base::Bind(&DateTimeHandler::HandleDateTimePageReady, | 97 "dateTimePageReady", base::Bind(&DateTimeHandler::HandleDateTimePageReady, |
| 94 base::Unretained(this))); | 98 base::Unretained(this))); |
| 95 web_ui()->RegisterMessageCallback( | 99 web_ui()->RegisterMessageCallback( |
| 96 "getTimeZones", | 100 "getTimeZones", |
| 97 base::Bind(&DateTimeHandler::HandleGetTimeZones, base::Unretained(this))); | 101 base::Bind(&DateTimeHandler::HandleGetTimeZones, base::Unretained(this))); |
| 102 web_ui()->RegisterMessageCallback( |
| 103 "showSetDateTimeUI", base::Bind(&DateTimeHandler::HandleShowSetDateTimeUI, |
| 104 base::Unretained(this))); |
| 98 } | 105 } |
| 99 | 106 |
| 100 void DateTimeHandler::OnJavascriptAllowed() { | 107 void DateTimeHandler::OnJavascriptAllowed() { |
| 108 SystemClockClient* system_clock_client = |
| 109 DBusThreadManager::Get()->GetSystemClockClient(); |
| 110 system_clock_client->AddObserver(this); |
| 111 SystemClockCanSetTimeChanged(system_clock_client->CanSetTime()); |
| 112 |
| 101 // The system time zone policy disables auto-detection entirely. (However, | 113 // The system time zone policy disables auto-detection entirely. (However, |
| 102 // the time zone policy does not override the user's time zone itself.) | 114 // the time zone policy does not override the user's time zone itself.) |
| 103 system_timezone_policy_subscription_ = | 115 system_timezone_policy_subscription_ = |
| 104 CrosSettings::Get()->AddSettingsObserver( | 116 CrosSettings::Get()->AddSettingsObserver( |
| 105 kSystemTimezonePolicy, | 117 kSystemTimezonePolicy, |
| 106 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, | 118 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, |
| 107 weak_ptr_factory_.GetWeakPtr())); | 119 weak_ptr_factory_.GetWeakPtr())); |
| 108 | 120 |
| 109 if (IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) | 121 if (IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) |
| 110 return; | 122 return; |
| 111 | 123 |
| 112 // The auto-detection policy can force auto-detection on or off. | 124 // The auto-detection policy can force auto-detection on or off. |
| 113 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); | 125 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 114 local_state_pref_change_registrar_.Add( | 126 local_state_pref_change_registrar_.Add( |
| 115 prefs::kSystemTimezoneAutomaticDetectionPolicy, | 127 prefs::kSystemTimezoneAutomaticDetectionPolicy, |
| 116 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, | 128 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, |
| 117 base::Unretained(this))); | 129 base::Unretained(this))); |
| 118 } | 130 } |
| 119 | 131 |
| 120 void DateTimeHandler::OnJavascriptDisallowed() { | 132 void DateTimeHandler::OnJavascriptDisallowed() { |
| 133 DBusThreadManager::Get()->GetSystemClockClient()->RemoveObserver(this); |
| 134 |
| 121 system_timezone_policy_subscription_.reset(); | 135 system_timezone_policy_subscription_.reset(); |
| 122 | 136 |
| 123 if (!IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) | 137 if (!IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) |
| 124 local_state_pref_change_registrar_.RemoveAll(); | 138 local_state_pref_change_registrar_.RemoveAll(); |
| 125 } | 139 } |
| 126 | 140 |
| 127 void DateTimeHandler::HandleDateTimePageReady(const base::ListValue* args) { | 141 void DateTimeHandler::HandleDateTimePageReady(const base::ListValue* args) { |
| 128 AllowJavascript(); | 142 AllowJavascript(); |
| 129 | 143 |
| 130 // Send the time zone automatic detection policy in case it changed after the | 144 // Send the time zone automatic detection policy in case it changed after the |
| 131 // handler was created. | 145 // handler was created. |
| 132 NotifyTimezoneAutomaticDetectionPolicy(); | 146 NotifyTimezoneAutomaticDetectionPolicy(); |
| 133 } | 147 } |
| 134 | 148 |
| 135 void DateTimeHandler::HandleGetTimeZones(const base::ListValue* args) { | 149 void DateTimeHandler::HandleGetTimeZones(const base::ListValue* args) { |
| 136 AllowJavascript(); | 150 AllowJavascript(); |
| 137 | 151 |
| 138 CHECK_EQ(1U, args->GetSize()); | 152 CHECK_EQ(1U, args->GetSize()); |
| 139 const base::Value* callback_id; | 153 const base::Value* callback_id; |
| 140 CHECK(args->Get(0, &callback_id)); | 154 CHECK(args->Get(0, &callback_id)); |
| 141 ResolveJavascriptCallback(*callback_id, *system::GetTimezoneList().release()); | 155 ResolveJavascriptCallback(*callback_id, *system::GetTimezoneList().release()); |
| 142 } | 156 } |
| 143 | 157 |
| 158 void DateTimeHandler::HandleShowSetDateTimeUI(const base::ListValue* args) { |
| 159 // Make sure the clock status hasn't changed since the button was clicked. |
| 160 if (!DBusThreadManager::Get()->GetSystemClockClient()->CanSetTime()) |
| 161 return; |
| 162 SetTimeDialog::ShowDialogInParent( |
| 163 web_ui()->GetWebContents()->GetTopLevelNativeWindow()); |
| 164 } |
| 165 |
| 144 void DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy() { | 166 void DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy() { |
| 145 bool managed = !IsTimezoneAutomaticDetectionUserEditable(); | 167 bool managed = !IsTimezoneAutomaticDetectionUserEditable(); |
| 146 bool force_enabled = managed && | 168 bool force_enabled = managed && |
| 147 g_browser_process->platform_part() | 169 g_browser_process->platform_part() |
| 148 ->GetTimezoneResolverManager() | 170 ->GetTimezoneResolverManager() |
| 149 ->ShouldApplyResolvedTimezone(); | 171 ->ShouldApplyResolvedTimezone(); |
| 150 | 172 |
| 151 CallJavascriptFunction("cr.webUIListenerCallback", | 173 CallJavascriptFunction("cr.webUIListenerCallback", |
| 152 base::StringValue("time-zone-auto-detect-policy"), | 174 base::StringValue("time-zone-auto-detect-policy"), |
| 153 base::FundamentalValue(managed), | 175 base::FundamentalValue(managed), |
| 154 base::FundamentalValue(force_enabled)); | 176 base::FundamentalValue(force_enabled)); |
| 155 } | 177 } |
| 156 | 178 |
| 179 void DateTimeHandler::SystemClockCanSetTimeChanged(bool can_set_time) { |
| 180 CallJavascriptFunction("cr.webUIListenerCallback", |
| 181 base::StringValue("can-set-date-time-changed"), |
| 182 base::FundamentalValue(can_set_time)); |
| 183 } |
| 184 |
| 157 } // namespace settings | 185 } // namespace settings |
| 158 } // namespace chromeos | 186 } // namespace chromeos |
| OLD | NEW |