| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-date-time-page' is the settings page containing date and time | 7 * 'settings-date-time-page' is the settings page containing date and time |
| 8 * settings. | 8 * settings. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 cr.exportPath('settings'); | 11 cr.exportPath('settings'); |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Possible values of the system time-zone auto-detection policy. Must stay in | 14 * Describes the status of the auto-detect policy. |
| 15 * sync with AutomaticTimezoneDetectionType in chrome_device_policy.proto. | |
| 16 * @enum {number} | 15 * @enum {number} |
| 17 * @const | |
| 18 */ | 16 */ |
| 19 settings.AutomaticTimezoneDetectionPolicy = { | 17 settings.TimeZoneAutoDetectPolicy = { |
| 20 USERS_DECIDE: 0, | 18 NONE: 0, |
| 21 DISABLED: 1, | 19 FORCED_ON: 1, |
| 22 IP_ONLY: 2, | 20 FORCED_OFF: 2, |
| 23 SEND_WIFI_ACCESS_POINTS: 3, | |
| 24 }; | 21 }; |
| 25 | 22 |
| 26 Polymer({ | 23 Polymer({ |
| 27 is: 'settings-date-time-page', | 24 is: 'settings-date-time-page', |
| 28 | 25 |
| 29 behaviors: [PrefsBehavior], | 26 behaviors: [PrefsBehavior, WebUIListenerBehavior], |
| 30 | 27 |
| 31 properties: { | 28 properties: { |
| 32 /** Preferences state. */ | 29 /** Preferences state. */ |
| 33 prefs: { | 30 prefs: { |
| 34 type: Object, | 31 type: Object, |
| 35 notify: true, | 32 notify: true, |
| 36 }, | 33 }, |
| 37 | 34 |
| 38 /** | 35 /** |
| 39 * If the time zone is managed at the system level, the user cannot control | 36 * The time zone auto-detect policy. |
| 40 * any time zone settings. | 37 * @private {settings.TimeZoneAutoDetectPolicy} |
| 41 * @private | |
| 42 */ | 38 */ |
| 43 systemTimeZoneManaged_: { | 39 timeZoneAutoDetectPolicy_: { |
| 44 type: Boolean, | 40 type: Boolean, |
| 45 value: function() { | 41 value: function() { |
| 46 return loadTimeData.getBoolean('systemTimeZoneManaged'); | 42 if (!loadTimeData.valueExists('timeZoneAutoDetectValueFromPolicy')) |
| 43 return settings.TimeZoneAutoDetectPolicy.NONE; |
| 44 return loadTimeData.getBoolean('timeZoneAutoDetectValueFromPolicy') ? |
| 45 settings.TimeZoneAutoDetectPolicy.FORCED_ON : |
| 46 settings.TimeZoneAutoDetectPolicy.FORCED_OFF; |
| 47 }, | 47 }, |
| 48 }, | 48 }, |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * If the time zone auto-detection setting is managed at the system level, | 51 * Whether a policy controls the time zone auto-detect setting. |
| 52 * the user cannot override the setting unless the policy is USERS_DECIDE. | |
| 53 * @private | 52 * @private |
| 54 */ | 53 */ |
| 55 systemTimeZoneDetectionManaged_: { | 54 hasTimeZoneAutoDetectPolicy_: { |
| 56 type: Boolean, | 55 type: Boolean, |
| 57 value: function() { | 56 computed: |
| 58 return loadTimeData.getBoolean( | 57 'computeHasTimeZoneAutoDetectPolicy_(timeZoneAutoDetectPolicy_)', |
| 59 'systemTimeZoneDetectionManaged'); | |
| 60 }, | |
| 61 }, | 58 }, |
| 62 | 59 |
| 63 /** | 60 /** |
| 64 * Value of the system time zone auto-detection policy, or null if system | 61 * The effective time zone auto-detect setting. |
| 65 * time zone auto-detection is not managed. | 62 * @private |
| 66 * @private {?settings.AutomaticTimezoneDetectionPolicy} | |
| 67 */ | 63 */ |
| 68 systemTimeZoneDetectionPolicyValue_: { | 64 timeZoneAutoDetect_: { |
| 69 type: Number, | 65 type: Boolean, |
| 66 computed: 'computeTimeZoneAutoDetect_(' + |
| 67 'timeZoneAutoDetectPolicy_,' + |
| 68 'prefs.settings.resolve_timezone_by_geolocation.value)', |
| 69 }, |
| 70 |
| 71 /** |
| 72 * A fake preference to provide cr-policy-pref-indicator with policy info. |
| 73 * @private {!chrome.settingsPrivate.PrefObject|undefined} |
| 74 */ |
| 75 fakeTimeZonePolicyPref_: Object, |
| 76 |
| 77 /** |
| 78 * Initialized with the current time zone so the menu displays the |
| 79 * correct value. The full option list is fetched lazily if necessary by |
| 80 * maybeGetTimeZoneList_. |
| 81 * @private {!DropdownMenuOptionList} |
| 82 */ |
| 83 timeZoneList_: { |
| 84 type: Array, |
| 70 value: function() { | 85 value: function() { |
| 71 return loadTimeData.valueExists('systemTimeZoneDetectionPolicyValue') ? | 86 return [{ |
| 72 /** @type {settings.AutomaticTimezoneDetectionPolicy} */( | 87 name: loadTimeData.getString('timeZoneName'), |
| 73 loadTimeData.getInteger('systemTimeZoneDetectionPolicyValue')) : | 88 value: loadTimeData.getString('timeZoneID'), |
| 74 null; | 89 }]; |
| 75 }, | 90 }, |
| 76 }, | 91 }, |
| 77 }, | 92 }, |
| 78 | 93 |
| 79 observers: [ | 94 observers: [ |
| 80 // TODO(michaelpg): Implement a BrowserProxy to listen for policy changes. | 95 'maybeGetTimeZoneList_(' + |
| 81 'updateTimeZoneDetectionCheckbox_(' + | 96 'prefs.cros.system.timezone.value, timeZoneAutoDetect_)', |
| 82 'prefs.settings.resolve_timezone_by_geolocation.value,' + | |
| 83 'systemTimeZoneManaged_,' + | |
| 84 'systemTimeZoneDetectionManaged_,' + | |
| 85 'systemTimeZoneDetectionPolicyValue_)', | |
| 86 ], | 97 ], |
| 87 | 98 |
| 99 attached: function() { |
| 100 this.addWebUIListener( |
| 101 'time-zone-auto-detect-policy', |
| 102 this.onTimeZoneAutoDetectPolicyChanged_.bind(this)); |
| 103 chrome.send('dateTimePageReady'); |
| 104 |
| 105 this.maybeGetTimeZoneList_(); |
| 106 }, |
| 107 |
| 88 /** | 108 /** |
| 89 * Processes all the time zone preferences and policy interactions in one | 109 * @param {boolean} managed Whether the auto-detect setting is controlled. |
| 90 * observer function instead of complicating the HTML with computed bindings. | 110 * @param {boolean} valueFromPolicy The value of the auto-detect setting |
| 91 * @param {boolean} userPrefValue | 111 * forced by policy. |
| 92 * @param {boolean} systemTimeZoneManaged | 112 * @private |
| 93 * @param {boolean} systemTimeZoneDetectionManaged | |
| 94 * @param {?settings.AutomaticTimezoneDetectionPolicy} | |
| 95 * systemTimeZoneDetectionPolicyValue | |
| 96 */ | 113 */ |
| 97 updateTimeZoneDetectionCheckbox_(userPrefValue, | 114 onTimeZoneAutoDetectPolicyChanged_: function(managed, valueFromPolicy) { |
| 98 systemTimeZoneManaged, | 115 if (!managed) { |
| 99 systemTimeZoneDetectionManaged, | 116 this.timeZoneAutoDetectPolicy_ = settings.TimeZoneAutoDetectPolicy.NONE; |
| 100 systemTimeZoneDetectionPolicyValue) { | 117 this.fakeTimeZonePolicyPref_ = undefined; |
| 101 var checkbox = this.$.timeZoneDetectionCheckbox; | |
| 102 | |
| 103 // Time zone auto-detection is disabled when the time zone is managed. | |
| 104 if (systemTimeZoneManaged) { | |
| 105 checkbox.disabled = true; | |
| 106 checkbox.checked = false; | |
| 107 return; | 118 return; |
| 108 } | 119 } |
| 109 | 120 |
| 110 // The time zone auto-detection policy may force-disable auto-detection. | 121 this.timeZoneAutoDetectPolicy_ = valueFromPolicy ? |
| 111 if (systemTimeZoneDetectionManaged && | 122 settings.TimeZoneAutoDetectPolicy.FORCED_ON : |
| 112 systemTimeZoneDetectionPolicyValue != | 123 settings.TimeZoneAutoDetectPolicy.FORCED_OFF; |
| 113 settings.AutomaticTimezoneDetectionPolicy.USERS_DECIDE) { | 124 |
| 114 checkbox.disabled = true; | 125 if (!this.fakeTimeZonePolicyPref_) { |
| 115 checkbox.checked = | 126 this.fakeTimeZonePolicyPref_ = { |
| 116 systemTimeZoneDetectionPolicyValue != | 127 key: 'fakeTimeZonePref', |
| 117 settings.AutomaticTimezoneDetectionPolicy.DISABLED; | 128 type: chrome.settingsPrivate.PrefType.NUMBER, |
| 129 value: 0, |
| 130 policySource: chrome.settingsPrivate.PolicySource.DEVICE_POLICY, |
| 131 policyEnforcement: chrome.settingsPrivate.PolicyEnforcement.ENFORCED, |
| 132 }; |
| 133 } |
| 134 }, |
| 135 |
| 136 /** |
| 137 * @param {!Event} e |
| 138 * @private |
| 139 */ |
| 140 onTimeZoneAutoDetectCheckboxChange_: function(e) { |
| 141 this.setPrefValue( |
| 142 'settings.resolve_timezone_by_geolocation', e.target.checked); |
| 143 }, |
| 144 |
| 145 /** |
| 146 * @param {settings.TimeZoneAutoDetectPolicy} timeZoneAutoDetectPolicy |
| 147 * @return {boolean} |
| 148 * @private |
| 149 */ |
| 150 computeHasTimeZoneAutoDetectPolicy_: function(timeZoneAutoDetectPolicy) { |
| 151 return timeZoneAutoDetectPolicy != settings.TimeZoneAutoDetectPolicy.NONE; |
| 152 }, |
| 153 |
| 154 /** |
| 155 * @param {settings.TimeZoneAutoDetectPolicy} timeZoneAutoDetectPolicy |
| 156 * @param {boolean} prefValue Value of the geolocation pref. |
| 157 * @return {boolean} Whether time zone auto-detect is enabled. |
| 158 * @private |
| 159 */ |
| 160 computeTimeZoneAutoDetect_: function(timeZoneAutoDetectPolicy, prefValue) { |
| 161 switch (timeZoneAutoDetectPolicy) { |
| 162 case settings.TimeZoneAutoDetectPolicy.NONE: |
| 163 return prefValue; |
| 164 case settings.TimeZoneAutoDetectPolicy.FORCED_ON: |
| 165 return true; |
| 166 case settings.TimeZoneAutoDetectPolicy.FORCED_OFF: |
| 167 return false; |
| 168 default: |
| 169 assertNotReached(); |
| 170 } |
| 171 }, |
| 172 |
| 173 /** |
| 174 * Fetches the list of time zones if necessary. |
| 175 * @private |
| 176 */ |
| 177 maybeGetTimeZoneList_: function() { |
| 178 // Only fetch the list once. |
| 179 if (this.timeZoneList_.length > 1 || !CrSettingsPrefs.isInitialized) |
| 180 return; |
| 181 |
| 182 // If auto-detect is enabled, we only need the current time zone. |
| 183 if (this.timeZoneAutoDetect_ && |
| 184 this.getPref('cros.system.timezone').value == |
| 185 this.timeZoneList_[0].value) { |
| 118 return; | 186 return; |
| 119 } | 187 } |
| 120 | 188 |
| 121 // If there is no policy, or the policy is USERS_DECIDE, the pref is used. | 189 cr.sendWithPromise('getTimeZones').then(this.setTimeZoneList_.bind(this)); |
| 122 checkbox.disabled = false; | |
| 123 checkbox.checked = userPrefValue; | |
| 124 }, | 190 }, |
| 125 | 191 |
| 126 /** @param {!Event} e */ | 192 /** |
| 127 onTimeZoneDetectionCheckboxChange_: function(e) { | 193 * Converts the C++ response into an array of menu options. |
| 128 this.setPrefValue( | 194 * @param {!Array<!Array<string>>} timeZones C++ time zones response. |
| 129 'settings.resolve_timezone_by_geolocation', e.target.checked); | 195 * @private |
| 196 */ |
| 197 setTimeZoneList_: function(timeZones) { |
| 198 this.timeZoneList_ = timeZones.map(function(timeZonePair) { |
| 199 return { |
| 200 name: timeZonePair[1], |
| 201 value: timeZonePair[0], |
| 202 }; |
| 203 }); |
| 130 }, | 204 }, |
| 131 }); | 205 }); |
| OLD | NEW |