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