File: install_event_log_uploader_base.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 (104 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 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/policy/reporting/install_event_log_uploader_base.h"

#include <algorithm>

#include "ash/constants/ash_switches.h"
#include "base/command_line.h"

namespace policy {

namespace {

// The backoff time starts at |kMinRetryBackoffMs| and doubles after each upload
// failure until it reaches |kMaxRetryBackoffMs|, from which point on it remains
// constant. The backoff is reset to |kMinRetryBackoffMs| after the next
// successful upload or if the upload request is canceled.
const int kMinRetryBackoffMs = 10 * 1000;            // 10 seconds
const int kMaxRetryBackoffMs = 24 * 60 * 60 * 1000;  // 24 hours

// If install-log-fast-upload-for-tests flag is enabled, do not increase retry
// backoff.
bool FastUploadForTestsEnabled() {
  return base::CommandLine::ForCurrentProcess()->HasSwitch(
      ash::switches::kInstallLogFastUploadForTests);
}

}  // namespace

InstallEventLogUploaderBase::InstallEventLogUploaderBase(
    CloudPolicyClient* client,
    Profile* profile)
    : client_(client),
      profile_(profile),
      retry_backoff_ms_(kMinRetryBackoffMs) {
  DCHECK(client_);
  client_->AddObserver(this);
}

InstallEventLogUploaderBase::InstallEventLogUploaderBase(Profile* profile)
    : client_(nullptr),
      profile_(profile),
      retry_backoff_ms_(kMinRetryBackoffMs) {}

InstallEventLogUploaderBase::~InstallEventLogUploaderBase() {
  if (client_)
    client_->RemoveObserver(this);
}

void InstallEventLogUploaderBase::RequestUpload() {
  CheckDelegateSet();
  if (upload_requested_)
    return;

  upload_requested_ = true;

  // If the client is set - ensure that it is also registered.
  // Otherwise start Serialization.
  if ((client_ && client_->is_registered()) || !client_) {
    StartSerialization();
  }
}

void InstallEventLogUploaderBase::CancelUpload() {
  CancelClientUpload();
  upload_requested_ = false;
  retry_backoff_ms_ = kMinRetryBackoffMs;
}

void InstallEventLogUploaderBase::OnRegistrationStateChanged(
    CloudPolicyClient* client) {
  DCHECK(client_);
  if (!upload_requested_)
    return;

  if (client->is_registered()) {
    StartSerialization();
  } else {
    CancelUpload();
    RequestUpload();
  }
}

void InstallEventLogUploaderBase::OnUploadDone(
    CloudPolicyClient::Result result) {
  // TODO(b/256553070): Do not crash if the client is unregistered.
  CHECK(!result.IsClientNotRegisteredError());

  if (result.IsSuccess()) {
    upload_requested_ = false;
    retry_backoff_ms_ = kMinRetryBackoffMs;
    OnUploadSuccess();
    return;
  }

  PostTaskForStartSerialization();
  if (FastUploadForTestsEnabled())
    return;
  retry_backoff_ms_ = std::min(retry_backoff_ms_ << 1, kMaxRetryBackoffMs);
}

}  // namespace policy