| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/values.h" |
| 8 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 9 #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/system/timezone_resolver_manager.h" |
| 10 #include "chrome/browser/chromeos/system/timezone_util.h" | 13 #include "chrome/browser/chromeos/system/timezone_util.h" |
| 11 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 12 #include "chromeos/chromeos_switches.h" | 15 #include "chromeos/chromeos_switches.h" |
| 16 #include "chromeos/settings/timezone_settings.h" |
| 13 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
| 14 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 15 #include "content/public/browser/web_ui_data_source.h" | 19 #include "content/public/browser/web_ui_data_source.h" |
| 16 | 20 |
| 17 namespace chromeos { | 21 namespace chromeos { |
| 18 namespace settings { | 22 namespace settings { |
| 19 | 23 |
| 20 namespace { | 24 namespace { |
| 21 | 25 |
| 26 // Returns whether the system time zone automatic detection policy is disabled |
| 27 // by a flag. |
| 28 bool IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled() { |
| 29 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 30 switches::kDisableSystemTimezoneAutomaticDetectionPolicy); |
| 31 } |
| 32 |
| 22 // Returns whether the system's automatic time zone detection setting is | 33 // Returns whether the system's automatic time zone detection setting is |
| 23 // managed, which may override the user's setting. | 34 // managed, which may override the user's setting. |
| 24 bool IsSystemTimezoneAutomaticDetectionManaged() { | 35 bool IsSystemTimezoneAutomaticDetectionManaged() { |
| 25 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 36 if (IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) |
| 26 switches::kDisableSystemTimezoneAutomaticDetectionPolicy)) { | |
| 27 return false; | 37 return false; |
| 28 } | 38 |
| 29 return g_browser_process->local_state()->IsManagedPreference( | 39 return g_browser_process->local_state()->IsManagedPreference( |
| 30 prefs::kSystemTimezoneAutomaticDetectionPolicy); | 40 prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| 31 } | 41 } |
| 32 | 42 |
| 33 // Returns the system's current automatic time zone detection policy value, | 43 // Returns the system's automatic time zone detection policy value, which |
| 34 // which corresponds to the SystemTimezoneProto's AutomaticTimezoneDetectionType | 44 // corresponds to the SystemTimezoneProto's AutomaticTimezoneDetectionType |
| 35 // enum and determines whether the user's setting will be overridden. | 45 // enum and determines whether the user's setting will be overridden. |
| 36 int GetSystemTimezoneAutomaticDetectionPolicyValue() { | 46 int GetSystemTimezoneAutomaticDetectionPolicyValue() { |
| 37 DCHECK(IsSystemTimezoneAutomaticDetectionManaged()); | 47 DCHECK(IsSystemTimezoneAutomaticDetectionManaged()); |
| 38 | 48 |
| 39 return g_browser_process->local_state()->GetInteger( | 49 return g_browser_process->local_state()->GetInteger( |
| 40 prefs::kSystemTimezoneAutomaticDetectionPolicy); | 50 prefs::kSystemTimezoneAutomaticDetectionPolicy); |
| 41 } | 51 } |
| 42 | 52 |
| 53 // Returns whether the user can set the automatic detection setting, based on |
| 54 // flags and policies. |
| 55 bool IsTimezoneAutomaticDetectionUserEditable() { |
| 56 if (system::HasSystemTimezonePolicy()) |
| 57 return false; |
| 58 |
| 59 if (IsSystemTimezoneAutomaticDetectionManaged()) { |
| 60 return GetSystemTimezoneAutomaticDetectionPolicyValue() == |
| 61 enterprise_management::SystemTimezoneProto::USERS_DECIDE; |
| 62 } |
| 63 |
| 64 return true; |
| 65 } |
| 66 |
| 43 } // namespace | 67 } // namespace |
| 44 | 68 |
| 45 DateTimeHandler::DateTimeHandler() {} | 69 DateTimeHandler::DateTimeHandler() : weak_ptr_factory_(this) {} |
| 46 | 70 |
| 47 DateTimeHandler::~DateTimeHandler() {} | 71 DateTimeHandler::~DateTimeHandler() {} |
| 48 | 72 |
| 49 DateTimeHandler* DateTimeHandler::Create( | 73 DateTimeHandler* DateTimeHandler::Create( |
| 50 content::WebUIDataSource* html_source) { | 74 content::WebUIDataSource* html_source) { |
| 51 html_source->AddBoolean("systemTimeZoneManaged", | 75 // Set the initial time zone to show. |
| 52 chromeos::system::HasSystemTimezonePolicy()); | 76 html_source->AddString("timeZoneName", system::GetCurrentTimezoneName()); |
| 77 html_source->AddString( |
| 78 "timeZoneID", |
| 79 system::TimezoneSettings::GetInstance()->GetCurrentTimezoneID()); |
| 53 | 80 |
| 54 bool system_time_zone_automatic_detection_managed = | 81 if (!IsTimezoneAutomaticDetectionUserEditable()) { |
| 55 IsSystemTimezoneAutomaticDetectionManaged(); | 82 html_source->AddBoolean("timeZoneAutoDetectValueFromPolicy", |
| 56 html_source->AddBoolean("systemTimeZoneDetectionManaged", | 83 g_browser_process->platform_part() |
| 57 system_time_zone_automatic_detection_managed); | 84 ->GetTimezoneResolverManager() |
| 58 if (system_time_zone_automatic_detection_managed) { | 85 ->ShouldApplyResolvedTimezone()); |
| 59 html_source->AddInteger("systemTimeZoneDetectionPolicyValue", | |
| 60 GetSystemTimezoneAutomaticDetectionPolicyValue()); | |
| 61 } | 86 } |
| 62 | 87 |
| 63 return new DateTimeHandler; | 88 return new DateTimeHandler; |
| 64 } | 89 } |
| 65 | 90 |
| 66 void DateTimeHandler::RegisterMessages() { | 91 void DateTimeHandler::RegisterMessages() { |
| 67 // TODO(michaelpg): Add time zone message handlers. | 92 web_ui()->RegisterMessageCallback( |
| 93 "dateTimePageReady", base::Bind(&DateTimeHandler::HandleDateTimePageReady, |
| 94 base::Unretained(this))); |
| 95 web_ui()->RegisterMessageCallback( |
| 96 "getTimeZones", |
| 97 base::Bind(&DateTimeHandler::HandleGetTimeZones, base::Unretained(this))); |
| 68 } | 98 } |
| 69 | 99 |
| 70 void DateTimeHandler::OnJavascriptAllowed() { | 100 void DateTimeHandler::OnJavascriptAllowed() { |
| 71 // TODO(michaelpg): Add policy observers. | 101 // The system time zone policy disables auto-detection entirely. (However, |
| 102 // the time zone policy does not override the user's time zone itself.) |
| 103 system_timezone_policy_subscription_ = |
| 104 CrosSettings::Get()->AddSettingsObserver( |
| 105 kSystemTimezonePolicy, |
| 106 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, |
| 107 weak_ptr_factory_.GetWeakPtr())); |
| 108 |
| 109 if (IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) |
| 110 return; |
| 111 |
| 112 // The auto-detection policy can force auto-detection on or off. |
| 113 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); |
| 114 local_state_pref_change_registrar_.Add( |
| 115 prefs::kSystemTimezoneAutomaticDetectionPolicy, |
| 116 base::Bind(&DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy, |
| 117 base::Unretained(this))); |
| 72 } | 118 } |
| 73 | 119 |
| 74 void DateTimeHandler::OnJavascriptDisallowed() { | 120 void DateTimeHandler::OnJavascriptDisallowed() { |
| 75 // TODO(michaelpg): Remove policy observers. | 121 system_timezone_policy_subscription_.reset(); |
| 122 |
| 123 if (!IsSystemTimezoneAutomaticDetectionPolicyFlagDisabled()) |
| 124 local_state_pref_change_registrar_.RemoveAll(); |
| 125 } |
| 126 |
| 127 void DateTimeHandler::HandleDateTimePageReady(const base::ListValue* args) { |
| 128 AllowJavascript(); |
| 129 |
| 130 // Send the time zone automatic detection policy in case it changed after the |
| 131 // handler was created. |
| 132 NotifyTimezoneAutomaticDetectionPolicy(); |
| 133 } |
| 134 |
| 135 void DateTimeHandler::HandleGetTimeZones(const base::ListValue* args) { |
| 136 AllowJavascript(); |
| 137 |
| 138 CHECK_EQ(1U, args->GetSize()); |
| 139 const base::Value* callback_id; |
| 140 CHECK(args->Get(0, &callback_id)); |
| 141 ResolveJavascriptCallback(*callback_id, *system::GetTimezoneList().release()); |
| 142 } |
| 143 |
| 144 void DateTimeHandler::NotifyTimezoneAutomaticDetectionPolicy() { |
| 145 bool managed = !IsTimezoneAutomaticDetectionUserEditable(); |
| 146 bool force_enabled = managed && |
| 147 g_browser_process->platform_part() |
| 148 ->GetTimezoneResolverManager() |
| 149 ->ShouldApplyResolvedTimezone(); |
| 150 |
| 151 CallJavascriptFunction("cr.webUIListenerCallback", |
| 152 base::StringValue("time-zone-auto-detect-policy"), |
| 153 base::FundamentalValue(managed), |
| 154 base::FundamentalValue(force_enabled)); |
| 76 } | 155 } |
| 77 | 156 |
| 78 } // namespace settings | 157 } // namespace settings |
| 79 } // namespace chromeos | 158 } // namespace chromeos |
| OLD | NEW |