File: report_controller_initializer.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (442 lines) | stat: -rw-r--r-- 17,868 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 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/report_controller_initializer/report_controller_initializer.h"

#include <memory>
#include <string>
#include <unordered_set>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "chrome/browser/ash/login/startup_utils.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/settings/device_settings_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part_ash.h"
#include "chrome/browser/first_run/first_run.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/common/channel_info.h"
#include "chromeos/ash/components/report/device_metrics/use_case/real_psm_client_manager.h"
#include "chromeos/ash/components/report/proto/fresnel_service.pb.h"
#include "chromeos/ash/components/report/utils/time_utils.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/cros_settings_provider.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

namespace ash {

namespace {

// Path to file storing the last powerwash time, persisted over safe powerwash.
constexpr char kLastPowerwashTimePath[] =
    "/mnt/stateful_partition/unencrypted/preserve/last_powerwash_time";

// Number of minutes to wait before retrying
// reading the .oobe_completed file again.
constexpr base::TimeDelta kOobeReadFailedRetryDelay = base::Minutes(60);

// Number of times to retry before failing to report any device actives.
constexpr int kNumberOfRetriesBeforeFail = 120;

// Record the state transitions for the |ReportInitializer| class.
void RecordInitializerState(ReportControllerInitializer::State state) {
  base::UmaHistogramEnumeration("Ash.Report.InitializerState", state);
}

// Record minutes of startup delay before reporting.
void RecordStartupDelay(int delay_minutes) {
  base::UmaHistogramCustomCounts("Ash.Report.StartupDelay", delay_minutes,
                                 /*min=*/0,
                                 /*exclusive_max=*/60, /*buckets=*/61);
}

// Record whether oobe is completed.
void RecordIsOobeCompleted(bool is_complete) {
  base::UmaHistogramBoolean("Ash.Report.IsOobeCompleted", is_complete);
}

void RecordLastPowerwashTimeRead(bool success) {
  base::UmaHistogramBoolean("Ash.Report.IsLastPowerwashTimeRead", success);
}

// Record the device trusted status enum when checking policy trusted status.
void RecordTrustedStatus(CrosSettingsProvider::TrustedStatus status) {
  ReportControllerInitializer::TrustedStatus status_mapped;
  switch (status) {
    case CrosSettingsProvider::TrustedStatus::PERMANENTLY_UNTRUSTED:
      status_mapped =
          ReportControllerInitializer::TrustedStatus::kPermanentlyUntrusted;
      break;
    case CrosSettingsProvider::TrustedStatus::TEMPORARILY_UNTRUSTED:
      status_mapped =
          ReportControllerInitializer::TrustedStatus::kTemporarilyUntrusted;
      break;
    case CrosSettingsProvider::TrustedStatus::TRUSTED:
      status_mapped = ReportControllerInitializer::TrustedStatus::kTrusted;
      break;
  }

  base::UmaHistogramEnumeration("Ash.Report.TrustedStatus", status_mapped);
}

// Record the device market segment after oobe completed and segment is ready.
// @param market_segment Defined in fresnel_service.proto
void RecordMarketSegment(report::MarketSegment market_segment) {
  ReportControllerInitializer::MarketSegment market_segment_mapped;
  switch (market_segment) {
    case report::MarketSegment::MARKET_SEGMENT_UNSPECIFIED:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kUnspecified;
      break;
    case report::MarketSegment::MARKET_SEGMENT_UNKNOWN:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kUnspecified;
      break;
    case report::MarketSegment::MARKET_SEGMENT_CONSUMER:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kConsumer;
      break;
    case report::MarketSegment::MARKET_SEGMENT_ENTERPRISE_ENROLLED_BUT_UNKNOWN:
      market_segment_mapped = ReportControllerInitializer::MarketSegment::
          kEnterpriseEnrolledButUnknown;
      break;
    case report::MarketSegment::MARKET_SEGMENT_ENTERPRISE:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kEnterprise;
      break;
    case report::MarketSegment::MARKET_SEGMENT_EDUCATION:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kEducation;
      break;
    case report::MarketSegment::MARKET_SEGMENT_ENTERPRISE_DEMO:
      market_segment_mapped =
          ReportControllerInitializer::MarketSegment::kEnterpriseDemo;
      break;
  }

  base::UmaHistogramEnumeration("Ash.Report.MarketSegment",
                                market_segment_mapped);
}

// Determine market segment from the loaded ChromeOS device policies.
report::MarketSegment GetMarketSegment(
    policy::DeviceMode device_mode,
    policy::MarketSegment device_market_segment) {
  // Policy device modes that should be classified as not being set.
  const std::unordered_set<policy::DeviceMode> kDeviceModeNotSet{
      policy::DeviceMode::DEVICE_MODE_PENDING,
      policy::DeviceMode::DEVICE_MODE_NOT_SET};

  // Policy device modes that should be classified as consumer devices.
  const std::unordered_set<policy::DeviceMode> kDeviceModeConsumer{
      policy::DeviceMode::DEVICE_MODE_CONSUMER};

  // Policy device modes that should be classified as enterprise devices.
  const std::unordered_set<policy::DeviceMode> kDeviceModeEnterprise{
      policy::DeviceMode::DEVICE_MODE_ENTERPRISE};

  // Policy device modes that should be classified as demo devices.
  const std::unordered_set<policy::DeviceMode> kDeviceModeDemoEnterprise{
      policy::DeviceMode::DEVICE_MODE_DEMO};

  // Determine Fresnel market segment using the retrieved device policy
  // |device_mode| and |device_market_segment|.
  if (kDeviceModeNotSet.count(device_mode)) {
    return report::MARKET_SEGMENT_UNKNOWN;
  }

  if (kDeviceModeConsumer.count(device_mode)) {
    return report::MARKET_SEGMENT_CONSUMER;
  }

  if (kDeviceModeDemoEnterprise.count(device_mode)) {
    return report::MARKET_SEGMENT_ENTERPRISE_DEMO;
  }

  if (kDeviceModeEnterprise.count(device_mode)) {
    if (device_market_segment == policy::MarketSegment::ENTERPRISE) {
      return report::MARKET_SEGMENT_ENTERPRISE;
    }

    if (device_market_segment == policy::MarketSegment::EDUCATION) {
      return report::MARKET_SEGMENT_EDUCATION;
    }

    return report::MARKET_SEGMENT_ENTERPRISE_ENROLLED_BUT_UNKNOWN;
  }

  return report::MARKET_SEGMENT_UNKNOWN;
}

// Reads the last powerwash time from preserved files. If the device is new
// or the last powerwash time file does not exist, it will return UnixEpoch.
base::Time ReadLastPowerwashTime() {
  // Retrieve the last modified time of the powerwash time file.
  base::FilePath last_powerwash_file(kLastPowerwashTimePath);
  base::File::Info info;
  if (!base::GetFileInfo(last_powerwash_file, &info)) {
    LOG(ERROR) << "Failed to get last powerwash file info.";
    return base::Time::UnixEpoch();
  }

  return info.last_modified;
}

}  // namespace

ReportControllerInitializer::ReportControllerInitializer() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  SetState(State::kWaitingForOwnership);

  // Adds observer for device ownership status changes in this class.
  device_settings_observation_.Observe(DeviceSettingsService::Get());

  OwnershipStatusChanged();
}

ReportControllerInitializer::~ReportControllerInitializer() = default;

void ReportControllerInitializer::SetState(State state) {
  state_ = state;
  RecordInitializerState(state_);
}

report::MarketSegment ReportControllerInitializer::GetMarketSegmentForTesting(
    const policy::DeviceMode& device_mode,
    const policy::MarketSegment& device_market_segment) {
  return GetMarketSegment(device_mode, device_market_segment);
}

void ReportControllerInitializer::OwnershipStatusChanged() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (state_ != State::kWaitingForOwnership) {
    LOG(ERROR) << "Invalid state - expected to be waiting for ownership.";
    return;
  }

  // Device should only get ownership taken at most once on a browser start up.
  if (ash::DeviceSettingsService::Get()->GetOwnershipStatus() !=
      ash::DeviceSettingsService::OwnershipStatus::kOwnershipTaken) {
    LOG(ERROR) << "Ownership status is not taken yet, returning early.";
    return;
  }

  SetState(State::kWaitingForStartupDelay);

  // Retrieve chrome first run sentinel time.
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(&first_run::GetFirstRunSentinelCreationTime),
      base::BindOnce(
          &ReportControllerInitializer::OnFirstRunSentinelCreationTimeRead,
          weak_factory_.GetWeakPtr()));
}

void ReportControllerInitializer::OnFirstRunSentinelCreationTimeRead(
    base::Time first_chrome_run_time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(state_, State::kWaitingForStartupDelay);

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(
          &ReportControllerInitializer::CheckOobeCompleted,
          weak_factory_.GetWeakPtr(),
          base::BindRepeating(&StartupUtils::GetTimeSinceOobeFlagFileCreation)),
      DetermineStartUpDelay(first_chrome_run_time));
}

base::TimeDelta ReportControllerInitializer::DetermineStartUpDelay(
    base::Time chrome_first_run_ts) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(state_, State::kWaitingForStartupDelay);

  // Wait at least 1 hour from the first chrome run sentinel file creation
  // time. This creation time is used as an indicator of when the device last
  // reset (powerwash/recovery/RMA). PSM servers can take 1 hour after CheckIn
  // to return the correct response for CheckMembership requests, since the PSM
  // servers need to update their cache.
  //
  // This delay avoids the scenario where a device checks in, powerwashes, and
  // on device start up, gets the wrong check membership response.
  base::TimeDelta delay_on_first_chrome_run;
  base::Time current_ts = base::Time::Now();
  if (current_ts < (chrome_first_run_ts + base::Hours(1))) {
    delay_on_first_chrome_run =
        chrome_first_run_ts + base::Hours(1) - current_ts;
  }

  RecordStartupDelay(delay_on_first_chrome_run.InMinutes());
  return delay_on_first_chrome_run;
}

void ReportControllerInitializer::CheckOobeCompleted(
    base::RepeatingCallback<base::TimeDelta()> check_oobe_completed_callback) {
  SetState(State::kWaitingForOobeCompleted);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // We block if the oobe completed file is not written.
  // ChromeOS devices should go through oobe to be considered a real device.
  // The ActivateDate is also only set after oobe is written.
  if (retry_oobe_completed_count_ >= kNumberOfRetriesBeforeFail) {
    LOG(ERROR) << "Retry failed - .oobe_completed file was not written for "
               << "1 minute after retrying 120 times. "
               << "There was a 60 minute wait between each retry and spanned "
               << "5 days.";
    RecordIsOobeCompleted(false);
    return;
  }

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(check_oobe_completed_callback),
      base::BindOnce(&ReportControllerInitializer::OnOobeFileWritten,
                     weak_factory_.GetWeakPtr(),
                     check_oobe_completed_callback));
}

void ReportControllerInitializer::OnOobeFileWritten(
    base::RepeatingCallback<base::TimeDelta()> check_oobe_completed_callback,
    base::TimeDelta time_since_oobe_file_written) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(state_, State::kWaitingForOobeCompleted);

  // If the OOBE completed file isn't created yet,
  // time_since_oobe_file_written returns base::TimeDelta().
  if (time_since_oobe_file_written == base::TimeDelta() ||
      time_since_oobe_file_written < base::Minutes(1)) {
    ++retry_oobe_completed_count_;

    LOG(ERROR) << "Time since oobe file created was less than 1 minute. "
               << "Wait and retry again after 1 minute to ensure that "
               << "the ActivateDate VPD field is set. "
               << "TimeDelta since oobe flag file was created = "
               << time_since_oobe_file_written
               << ". Retry count = " << retry_oobe_completed_count_;

    RecordIsOobeCompleted(false);

    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce(&ReportControllerInitializer::CheckOobeCompleted,
                       weak_factory_.GetWeakPtr(),
                       std::move(check_oobe_completed_callback)),
        kOobeReadFailedRetryDelay);

    return;
  }

  RecordIsOobeCompleted(true);

  CheckTrustedStatus();
}

void ReportControllerInitializer::CheckTrustedStatus() {
  SetState(State::kWaitingForDeviceSettingsTrusted);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(state_, State::kWaitingForDeviceSettingsTrusted);

  // Device is owned, confirm the settings can be trusted.
  CrosSettingsProvider::TrustedStatus status =
      CrosSettings::Get()->PrepareTrustedValues(
          base::BindOnce(&ReportControllerInitializer::CheckTrustedStatus,
                         weak_factory_.GetWeakPtr()));

  // Record histogram that indicates the status of the device policies.
  RecordTrustedStatus(status);

  if (status == CrosSettingsProvider::TEMPORARILY_UNTRUSTED ||
      status == CrosSettingsProvider::PERMANENTLY_UNTRUSTED) {
    // When status is TEMPORARILY_UNTRUSTED, PrepareTrustedValues method takes
    // ownership of the start report controller callback.
    // It will retry later when the TRUSTED status becomes available.
    //
    // When status is PERMANENTLY_UNTRUSTED, client assumes this status is final
    // until browser restarts. Client does not proceed without signature
    // verification, so retry is not attempted. This status may be caused
    // if the policy proto blob fails the signature check.
    LOG(ERROR) << "CrosSettings status is not trusted yet.";
    return;
  }

  SetState(State::kWaitingForLastPowerwashTime);

  // Retrieve last powerwash time, if file exists.
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(&ReadLastPowerwashTime),
      base::BindOnce(&ReportControllerInitializer::OnLastPowerwashTimeRead,
                     weak_factory_.GetWeakPtr()));
}

void ReportControllerInitializer::OnLastPowerwashTimeRead(
    base::Time last_powerwash_gmt) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_EQ(state_, State::kWaitingForLastPowerwashTime);

  // Default values before handling last powerwash time.
  // Variable is based off GMT YYYY-WW just like ActivateDate VPD field.
  std::string last_powerwash_week;
  // Handle the last powerwash time received after read attempt.
  if (last_powerwash_gmt.is_null() ||
      last_powerwash_gmt == base::Time::UnixEpoch()) {
    RecordLastPowerwashTimeRead(false);
  } else {
    last_powerwash_week =
        report::utils::ConvertTimeToISO8601String(last_powerwash_gmt);
    RecordLastPowerwashTimeRead(true);
  }

  // OOBE is completed, so we can safely calculate the device market
  // segment.
  report::MarketSegment device_market_segment =
      GetMarketSegment(g_browser_process->platform_part()
                           ->browser_policy_connector_ash()
                           ->GetDeviceMode(),
                       g_browser_process->platform_part()
                           ->browser_policy_connector_ash()
                           ->GetEnterpriseMarketSegment());

  // Record histogram after oobe is completed and the policies are in trusted
  // status. At this point, the device market segment is known and assigned.
  RecordMarketSegment(device_market_segment);

  SetState(State::kReportControllerInitialized);

  // At this step we have checked for 3 conditions.
  // 1. The device is owned.
  // 2. OOBE is completed and .oobe_completed file is written > 1 minute ago.
  // 3. CrosSettingsProvider::TRUSTED: device policies are loaded and trusted.
  report_controller_ = std::make_unique<report::ReportController>(
      ash::report::device_metrics::ChromeDeviceMetadataParameters{
          chrome::GetChannel() /* chromeos_channel */, device_market_segment,
          last_powerwash_week},
      g_browser_process->local_state(),
      g_browser_process->system_network_context_manager()
          ->GetSharedURLLoaderFactory(),
      std::make_unique<ash::report::device_metrics::PsmClientManager>(
          std::make_unique<
              report::device_metrics::RealPsmClientManagerDelegate>()));
}

}  // namespace ash