File: timezone_resolver_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (449 lines) | stat: -rw-r--r-- 16,200 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/system/timezone_resolver_manager.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/constants/geolocation_access_level.h"
#include "ash/shell.h"
#include "ash/system/privacy_hub/privacy_hub_controller.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "chrome/browser/ash/net/delay_network_call.h"
#include "chrome/browser/ash/preferences/preferences.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/system/input_device_settings.h"
#include "chrome/browser/ash/system/timezone_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/geolocation/simple_geolocation_provider.h"
#include "chromeos/ash/components/install_attributes/install_attributes.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/session_manager/core/session_manager.h"

namespace ash {
namespace system {

namespace {

// This is the result of several methods calculating configured
// time zone resolve processes.
enum ServiceConfiguration {
  UNSPECIFIED = 0,   // Try another configuration source.
  SHOULD_START = 1,  // This source requires service Start.
  SHOULD_STOP = 2,   // This source requires service Stop.
};

// Starts or stops TimezoneResolver if required by
// SystemTimezoneAutomaticDetectionPolicy.
// Returns SHOULD_* if timezone resolver status is controlled by this policy.
ServiceConfiguration GetServiceConfigurationFromAutomaticDetectionPolicy() {
  PrefService* local_state = g_browser_process->local_state();
  const bool is_managed = local_state->IsManagedPreference(
      ::prefs::kSystemTimezoneAutomaticDetectionPolicy);
  if (!is_managed)
    return UNSPECIFIED;

  int policy_value =
      local_state->GetInteger(::prefs::kSystemTimezoneAutomaticDetectionPolicy);

  switch (policy_value) {
    case enterprise_management::SystemTimezoneProto::USERS_DECIDE:
      return UNSPECIFIED;
    case enterprise_management::SystemTimezoneProto::DISABLED:
      return SHOULD_STOP;
    case enterprise_management::SystemTimezoneProto::IP_ONLY:
      return SHOULD_START;
    case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS:
      return SHOULD_START;
    case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO:
      return SHOULD_START;
  }
  // Default for unknown policy value.
  NOTREACHED() << "Unrecognized policy value: " << policy_value;
}

// Stops TimezoneResolver if SystemTimezonePolicy is applied.
// Returns SHOULD_* if timezone resolver status is controlled by this policy.
ServiceConfiguration GetServiceConfigurationFromSystemTimezonePolicy() {
  if (!HasSystemTimezonePolicy())
    return UNSPECIFIED;

  return SHOULD_STOP;
}

// Starts or stops TimezoneResolver if required by policy.
// Returns true if timezone resolver status is controlled by policy.
ServiceConfiguration GetServiceConfigurationFromPolicy() {
  ServiceConfiguration result =
      GetServiceConfigurationFromSystemTimezonePolicy();

  if (result != UNSPECIFIED)
    return result;

  result = GetServiceConfigurationFromAutomaticDetectionPolicy();
  return result;
}

// Returns service configuration for the user.
ServiceConfiguration GetServiceConfigurationFromUserPrefs(
    const PrefService* user_prefs) {
  return TimeZoneResolverManager::TimeZoneResolveMethodFromInt(
             user_prefs->GetInteger(
                 ::prefs::kResolveTimezoneByGeolocationMethod)) ==
                 TimeZoneResolverManager::TimeZoneResolveMethod::DISABLED
             ? SHOULD_STOP
             : SHOULD_START;
}

// Returns service configuration for the signin screen.
ServiceConfiguration GetServiceConfigurationForSigninScreen() {
  using AccessLevel = GeolocationAccessLevel;

  const AccessLevel device_geolocation_permission =
      static_cast<AccessLevel>(g_browser_process->local_state()->GetInteger(
          prefs::kDeviceGeolocationAllowed));
  if (device_geolocation_permission == AccessLevel::kDisallowed) {
    return SHOULD_STOP;
  }

  const PrefService::Preference* device_pref =
      g_browser_process->local_state()->FindPreference(
          ::prefs::kResolveDeviceTimezoneByGeolocationMethod);
  if (!device_pref || device_pref->IsDefaultValue()) {
    // CfM devices default to static timezone.
    bool keyboard_driven_oobe =
        system::InputDeviceSettings::Get()->ForceKeyboardDrivenUINavigation();
    return keyboard_driven_oobe ? SHOULD_STOP : SHOULD_START;
  }

  // Do not start resolver if we are inside active user session.
  // If user preferences permit, it will be started on preferences
  // initialization.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kLoginUser))
    return SHOULD_STOP;

  return TimeZoneResolverManager::TimeZoneResolveMethodFromInt(
             device_pref->GetValue()->GetInt()) ==
                 TimeZoneResolverManager::TimeZoneResolveMethod::DISABLED
             ? SHOULD_STOP
             : SHOULD_START;
}

}  // anonymous namespace.

TimeZoneResolverManager::TimeZoneResolverManager(
    SimpleGeolocationProvider* geolocation_provider,
    session_manager::SessionManager* session_manager)
    : geolocation_provider_(geolocation_provider) {
  switch (g_browser_process->local_state()->GetInitializationStatus()) {
    case PrefService::INITIALIZATION_STATUS_SUCCESS:
    case PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE:
      local_state_initialized_ = true;
      break;
    default:
      local_state_initialized_ = false;
  }
  g_browser_process->local_state()->AddPrefInitObserver(
      base::BindOnce(&TimeZoneResolverManager::OnLocalStateInitialized,
                     weak_factory_.GetWeakPtr()));

  local_state_pref_change_registrar_.Init(g_browser_process->local_state());
  local_state_pref_change_registrar_.Add(
      ::prefs::kSystemTimezoneAutomaticDetectionPolicy,
      base::BindRepeating(&TimeZoneResolverManager::UpdateTimezoneResolver,
                          base::Unretained(this)));

  geolocation_provider_->AddObserver(this);
  session_observation_.Observe(session_manager);
}

TimeZoneResolverManager::~TimeZoneResolverManager() {
  geolocation_provider_->RemoveObserver(this);
  geolocation_provider_ = nullptr;
}

void TimeZoneResolverManager::SetPrimaryUserPrefs(PrefService* pref_service) {
  primary_user_prefs_ = pref_service;
}

bool TimeZoneResolverManager::ShouldSendWiFiGeolocationData() const {
  // Managed user case, check cloud policies for automatic time zone.
  if (IsTimeZoneResolutionPolicyControlled()) {
    switch (GetEffectiveAutomaticTimezoneManagementSetting()) {
      case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS:
        return true;
      case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO:
        return true;
      case enterprise_management::SystemTimezoneProto::USERS_DECIDE:
        // Same as regular user flow, continue processing below.
        break;
      default:
        return false;
    }
  }

  // Regular user case (also USERS_DECIDE case for managed).
  // primary_user_prefs_ indicates if the user has signed in or not.
  // Precise location is disabled on log-in screen.
  if (!primary_user_prefs_) {
    return false;
  }

  // User is logged in at this point.
  // Check that user location permission is granted for system services.
  if (static_cast<GeolocationAccessLevel>(primary_user_prefs_->GetInteger(
          ash::prefs::kUserGeolocationAccessLevel)) ==
      GeolocationAccessLevel::kDisallowed) {
    return false;
  }

  // Automatic time zone setting is a user configurable option, applying
  // the primary user's choice to the entire session.
  switch (GetEffectiveUserTimeZoneResolveMethod(primary_user_prefs_,
                                                /*check_policy=*/false)) {
    case TimeZoneResolveMethod::SEND_WIFI_ACCESS_POINTS:
      return true;
    case TimeZoneResolveMethod::SEND_ALL_LOCATION_INFO:
      return true;
    default:
      return false;
  }
}

bool TimeZoneResolverManager::ShouldSendCellularGeolocationData() const {
  // Managed user case, check cloud policies for automatic time zone.
  if (IsTimeZoneResolutionPolicyControlled()) {
    switch (GetEffectiveAutomaticTimezoneManagementSetting()) {
      case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO:
        return true;
      case enterprise_management::SystemTimezoneProto::USERS_DECIDE:
        // Same as regular user flow, continue processing below.
        break;
      default:
        return false;
    }
  }

  // Regular user case (also USERS_DECIDE case for managed).
  // primary_user_prefs_ indicates if the user has signed in or not.
  // Precise location is disabled on log-in screen.
  if (!primary_user_prefs_) {
    return false;
  }

  // User is logged in at this point.
  // Check that user location permission is granted for system services.
  if (static_cast<GeolocationAccessLevel>(primary_user_prefs_->GetInteger(
          ash::prefs::kUserGeolocationAccessLevel)) ==
      GeolocationAccessLevel::kDisallowed) {
    return false;
  }

  // Automatic time zone setting is a user configurable option, applying
  // the primary user's choice to the entire session.
  return GetEffectiveUserTimeZoneResolveMethod(primary_user_prefs_,
                                               /*check_policy=*/false) ==
         TimeZoneResolveMethod::SEND_ALL_LOCATION_INFO;
}

// static
int TimeZoneResolverManager::GetEffectiveAutomaticTimezoneManagementSetting() {
  // Regular users choose automatic time zone method themselves.
  if (!IsTimeZoneResolutionPolicyControlled()) {
    return enterprise_management::SystemTimezoneProto::USERS_DECIDE;
  }

  // Static time zone policy overrides automatic.
  if (HasSystemTimezonePolicy()) {
    return enterprise_management::SystemTimezoneProto::DISABLED;
  }

  int policy_value = g_browser_process->local_state()->GetInteger(
      ::prefs::kSystemTimezoneAutomaticDetectionPolicy);
  DCHECK(policy_value <= enterprise_management::SystemTimezoneProto::
                             AutomaticTimezoneDetectionType_MAX);

  return policy_value;
}

void TimeZoneResolverManager::OnUserProfileLoaded(const AccountId& account_id) {
  Profile* profile = ProfileHelper::Get()->GetProfileByAccountId(account_id);
  system::UpdateSystemTimezone(profile);

  auto* user_manager = user_manager::UserManager::Get();
  const auto* user = user_manager->FindUser(account_id);
  if (!user) {
    return;
  }

  // In Multi-Profile mode only primary user settings are in effect.
  if (user != user_manager->GetPrimaryUser()) {
    return;
  }

  if (!user_manager->IsUserLoggedIn()) {
    return;
  }

  // Timezone auto refresh is disabled for Guest and OffTheRecord
  // users, but enabled for Kiosk mode.
  if (user_manager->IsLoggedInAsGuest() || profile->IsOffTheRecord()) {
    GetResolver()->Stop();
    return;
  }
  UpdateTimezoneResolver();
}

void TimeZoneResolverManager::UpdateTimezoneResolver() {
  initialized_ = true;
  TimeZoneResolver* resolver = GetResolver();
  // Local state becomes initialized when policy data is loaded,
  // and we need policies to decide whether resolver can be started.
  if (!local_state_initialized_) {
    resolver->Stop();
    return;
  }
  if (TimeZoneResolverShouldBeRunning())
    resolver->Start();
  else
    resolver->Stop();

  // Observers must be notified whenever UpdateTimezoneResolver() is called.
  // This allows observers to listen for all relevant prefs updates.
  for (Observer& observer : observers_)
    observer.OnTimeZoneResolverUpdated();
}

void TimeZoneResolverManager::OnGeolocationPermissionChanged(bool enabled) {
  // New permission state will be retrieved from `geolocation_provider_`.
  UpdateTimezoneResolver();
}

void TimeZoneResolverManager::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void TimeZoneResolverManager::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

bool TimeZoneResolverManager::ShouldApplyResolvedTimezone() {
  return TimeZoneResolverShouldBeRunning();
}

bool TimeZoneResolverManager::TimeZoneResolverShouldBeRunning() {
  // System geolocation permission is required for automatic timezone
  // resolution.
  if (!geolocation_provider_->IsGeolocationUsageAllowedForSystem()) {
    return false;
  }

  // Once the permission is granted, it's all up to the time zone
  // configuration data.
  return TimeZoneResolverAllowedByTimeZoneConfigData();
}

bool TimeZoneResolverManager::TimeZoneResolverAllowedByTimeZoneConfigData() {
  ServiceConfiguration result = GetServiceConfigurationFromPolicy();

  if (result == UNSPECIFIED) {
    if (primary_user_prefs_) {
      result = GetServiceConfigurationFromUserPrefs(primary_user_prefs_);
    } else {
      // We are on a signin page.
      result = GetServiceConfigurationForSigninScreen();
    }
  }
  return result == SHOULD_START;
}

ash::TimeZoneResolver* TimeZoneResolverManager::GetResolver() {
  if (!timezone_resolver_.get()) {
    timezone_resolver_ = std::make_unique<ash::TimeZoneResolver>(
        this, geolocation_provider_,
        g_browser_process->shared_url_loader_factory(),
        base::BindRepeating(&ash::system::ApplyTimeZone),
        base::BindRepeating(&ash::DelayNetworkCall),
        g_browser_process->local_state());
  }
  return timezone_resolver_.get();
}

void TimeZoneResolverManager::OnLocalStateInitialized(bool initialized) {
  local_state_initialized_ = initialized;
  if (initialized_)
    UpdateTimezoneResolver();
}

// static
TimeZoneResolverManager::TimeZoneResolveMethod
TimeZoneResolverManager::TimeZoneResolveMethodFromInt(int value) {
  if (value < 0 ||
      value >= static_cast<int>(TimeZoneResolveMethod::METHODS_NUMBER)) {
    return TimeZoneResolveMethod::DISABLED;
  }

  const TimeZoneResolveMethod method =
      static_cast<TimeZoneResolveMethod>(value);

  if (FineGrainedTimeZoneDetectionEnabled())
    return method;

  if (method == TimeZoneResolveMethod::DISABLED)
    return TimeZoneResolveMethod::DISABLED;

  return TimeZoneResolveMethod::IP_ONLY;
}

// static
TimeZoneResolverManager::TimeZoneResolveMethod
TimeZoneResolverManager::GetEffectiveUserTimeZoneResolveMethod(
    const PrefService* user_prefs,
    bool check_policy) {
  if (check_policy) {
    int policy_value = GetEffectiveAutomaticTimezoneManagementSetting();
    switch (policy_value) {
      case enterprise_management::SystemTimezoneProto::USERS_DECIDE:
        // Follow user preference.
        break;
      case enterprise_management::SystemTimezoneProto::DISABLED:
        return TimeZoneResolveMethod::DISABLED;
      case enterprise_management::SystemTimezoneProto::IP_ONLY:
        return TimeZoneResolveMethod::IP_ONLY;
      case enterprise_management::SystemTimezoneProto::SEND_WIFI_ACCESS_POINTS:
        return TimeZoneResolveMethod::SEND_WIFI_ACCESS_POINTS;
      case enterprise_management::SystemTimezoneProto::SEND_ALL_LOCATION_INFO:
        return TimeZoneResolveMethod::SEND_ALL_LOCATION_INFO;
      default:
        NOTREACHED();
    }
  }
  if (user_prefs->GetBoolean(
          ::prefs::kResolveTimezoneByGeolocationMigratedToMethod)) {
    return TimeZoneResolveMethodFromInt(
        user_prefs->GetInteger(::prefs::kResolveTimezoneByGeolocationMethod));
  }
  return TimeZoneResolveMethod::IP_ONLY;
}

// static
bool TimeZoneResolverManager::IsTimeZoneResolutionPolicyControlled() {
  return GetServiceConfigurationFromPolicy() != UNSPECIFIED;
}

// static
bool TimeZoneResolverManager::IfServiceShouldBeRunningForSigninScreen() {
  return GetServiceConfigurationForSigninScreen() == SHOULD_START;
}

}  // namespace system
}  // namespace ash