File: history_converter.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 (198 lines) | stat: -rw-r--r-- 8,586 bytes parent folder | download | duplicates (5)
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
// 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/ui/webui/ash/enterprise_reporting/history_converter.h"

#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/ui/webui/ash/enterprise_reporting/enterprise_reporting.mojom.h"
#include "chromeos/dbus/missive/history_tracker.h"
#include "components/reporting/proto/synced/health.pb.h"
#include "components/reporting/proto/synced/status.pb.h"
#include "components/reporting/util/status.h"

namespace ash::reporting {
namespace {
std::string StatusFromProto(const ::reporting::StatusProto& source) {
  ::reporting::Status status;
  status.RestoreFrom(source);
  return status.ToString();
}

void PopulateStorageQueueAction(
    const ::reporting::StorageQueueAction& source,
    enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "QueueAction";
  switch (source.action_case()) {
    case ::reporting::StorageQueueAction::ActionCase::kStorageEnqueue:
      dest.parameters.emplace_back(
          enterprise_reporting::mojom::ErpHistoryEventParameter::New(
              "EnqueueSeqId",
              base::NumberToString(source.storage_enqueue().sequencing_id())));
      break;
    case ::reporting::StorageQueueAction::ActionCase::kStorageDequeue:
      dest.parameters.emplace_back(
          enterprise_reporting::mojom::ErpHistoryEventParameter::New(
              "DequeueSeqId",
              base::NumberToString(source.storage_dequeue().sequencing_id())));
      dest.parameters.emplace_back(
          enterprise_reporting::mojom::ErpHistoryEventParameter::New(
              "DequeueCount",
              base::NumberToString(source.storage_dequeue().records_count())));
      break;
    default:
      dest.parameters.emplace_back(
          enterprise_reporting::mojom::ErpHistoryEventParameter::New(
              "UnknownAction", ""));
  }
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  dest.status = StatusFromProto(source.status());
}

void PopulateEnqueueRecord(const ::reporting::EnqueueRecordCall& source,
                           enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "Enqueue";
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Destination", ::reporting::Destination_Name(source.destination())));
  dest.status = StatusFromProto(source.status());
}

void PopulateFlushRecord(const ::reporting::FlushPriorityCall& source,
                         enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "Flush";
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  dest.status = StatusFromProto(source.status());
}

void PopulateConfirmRecord(const ::reporting::ConfirmRecordUploadCall& source,
                           enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "Confirm";
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Sequencing_id", base::NumberToString(source.sequencing_id())));
  if (source.force_confirm()) {
    dest.parameters.emplace_back(
        enterprise_reporting::mojom::ErpHistoryEventParameter::New(
            "Force_confirm", "True"));
  }
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  dest.status = StatusFromProto(source.status());
}

void PopulateUploadRecord(const ::reporting::UploadEncryptedRecordCall& source,
                          enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "Upload";
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Reason", source.upload_reason()));
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  for (const auto& item : source.items()) {
    switch (item.item_case()) {
      case ::reporting::UploadItem::ItemCase::kRecord:
        dest.parameters.emplace_back(
            enterprise_reporting::mojom::ErpHistoryEventParameter::New(
                "Record",
                base::StrCat({"seq=", base::NumberToString(
                                          item.record().sequencing_id())})));
        break;
      case ::reporting::UploadItem::ItemCase::kGap:
        dest.parameters.emplace_back(
            enterprise_reporting::mojom::ErpHistoryEventParameter::New(
                "Gap",
                base::StrCat(
                    {"seq=", base::NumberToString(item.gap().sequencing_id()),
                     " count=", base::NumberToString(item.gap().count())})));
        break;
      default:
        dest.parameters.emplace_back(
            enterprise_reporting::mojom::ErpHistoryEventParameter::New(
                "Unknown", ""));
    }
  }
  dest.status = StatusFromProto(source.status());
}

void PopulateBlockedRecord(const ::reporting::BlockedRecordCall& source,
                           enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "BlockedRecord";
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Priority", ::reporting::Priority_Name(source.priority())));
  dest.parameters.emplace_back(
      enterprise_reporting::mojom::ErpHistoryEventParameter::New(
          "Destination", ::reporting::Destination_Name(source.destination())));
}

void PopulateBlockedDestinationsUpdated(
    const ::reporting::BlockedDestinationsUpdatedCall& source,
    enterprise_reporting::mojom::ErpHistoryEvent& dest) {
  dest.call = "BlockedDestinations";
  for (const auto& item : source.destinations()) {
    dest.parameters.emplace_back(
        enterprise_reporting::mojom::ErpHistoryEventParameter::New(
            "Destination", ::reporting::Destination_Name(item)));
  }
}
}  // namespace

mojo::StructPtr<enterprise_reporting::mojom::ErpHistoryData> ConvertHistory(
    const ::reporting::ERPHealthData& data) {
  auto result = enterprise_reporting::mojom::ErpHistoryData::New();
  for (const auto& history : data.history()) {
    result->events.emplace_back(
        enterprise_reporting::mojom::ErpHistoryEvent::New());
    result->events.back()->time = history.timestamp_seconds();
    switch (history.record_case()) {
      case ::reporting::HealthDataHistory::RecordCase::kEnqueueRecordCall:
        PopulateEnqueueRecord(history.enqueue_record_call(),
                              *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::kFlushPriorityCall:
        PopulateFlushRecord(history.flush_priority_call(),
                            *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::
          kUploadEncryptedRecordCall:
        PopulateUploadRecord(history.upload_encrypted_record_call(),
                             *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::kConfirmRecordUploadCall:
        PopulateConfirmRecord(history.confirm_record_upload_call(),
                              *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::kStorageQueueAction:
        PopulateStorageQueueAction(history.storage_queue_action(),
                                   *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::kBlockedRecordCall:
        PopulateBlockedRecord(history.blocked_record_call(),
                              *result->events.back());
        break;
      case ::reporting::HealthDataHistory::RecordCase::
          kBlockedDestinationsUpdatedCall:
        PopulateBlockedDestinationsUpdated(
            history.blocked_destinations_updated_call(),
            *result->events.back());
        break;

      default:
        result->events.back()->call = "UNKNOWN";
        result->events.back()->status = "N/A";
    }
  }
  return result;
}
}  // namespace ash::reporting