File: protocol_serializer_json.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (260 lines) | stat: -rw-r--r-- 8,522 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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/protocol_serializer_json.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "components/update_client/activity_data_service.h"

namespace update_client {

std::string ProtocolSerializerJSON::Serialize(
    const protocol_request::Request& request) const {
  base::Value::Dict root_node;
  base::Value::Dict request_node;
  request_node.Set("protocol", request.protocol_version);
  request_node.Set("ismachine", request.is_machine);
  request_node.Set("dedup", "cr");
  request_node.Set("acceptformat", "crx3,download,puff,run,xz,zucc");
  if (!request.additional_attributes.empty()) {
    for (const auto& [name, value] : request.additional_attributes) {
      request_node.Set(name, value);
    }
  }
  request_node.Set("sessionid", request.session_id);
  request_node.Set("requestid", request.request_id);
  request_node.Set("@updater", request.updatername);
  request_node.Set("prodversion", request.prodversion);
  request_node.Set("updaterversion", request.updaterversion);
  request_node.Set("@os", request.operating_system);
  request_node.Set("arch", request.arch);
  request_node.Set("nacl_arch", request.nacl_arch);
#if BUILDFLAG(IS_WIN)
  if (request.is_wow64) {
    request_node.Set("wow64", request.is_wow64);
  }
#endif  // BUILDFLAG(IS_WIN)
  if (!request.updaterchannel.empty()) {
    request_node.Set("updaterchannel", request.updaterchannel);
  }
  if (!request.prodchannel.empty()) {
    request_node.Set("prodchannel", request.prodchannel);
  }
  if (!request.dlpref.empty()) {
    request_node.Set("dlpref", request.dlpref);
  }
  if (request.domain_joined) {
    request_node.Set("domainjoined", *request.domain_joined);
  }

  // HW platform information.
  base::Value::Dict hw_node;
  hw_node.Set("physmemory", static_cast<int>(request.hw.physmemory));
  hw_node.Set("sse", request.hw.sse);
  hw_node.Set("sse2", request.hw.sse2);
  hw_node.Set("sse3", request.hw.sse3);
  hw_node.Set("sse41", request.hw.sse41);
  hw_node.Set("sse42", request.hw.sse42);
  hw_node.Set("ssse3", request.hw.ssse3);
  hw_node.Set("avx", request.hw.avx);
  request_node.Set("hw", std::move(hw_node));

  // OS version and platform information.
  base::Value::Dict os_node;
  os_node.Set("platform", request.os.platform);
  os_node.Set("arch", request.os.arch);
  if (!request.os.version.empty()) {
    os_node.Set("version", request.os.version);
  }
  if (!request.os.service_pack.empty()) {
    os_node.Set("sp", request.os.service_pack);
  }
  request_node.Set("os", std::move(os_node));

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  if (request.updater) {
    const auto& updater = *request.updater;
    base::Value::Dict updater_node;
    updater_node.Set("name", updater.name);
    updater_node.Set("ismachine", updater.is_machine);
    updater_node.Set("autoupdatecheckenabled",
                     updater.autoupdate_check_enabled);
    updater_node.Set("updatepolicy", updater.update_policy);
    if (!updater.version.empty()) {
      updater_node.Set("version", updater.version);
    }
    if (updater.last_checked) {
      updater_node.Set("lastchecked", *updater.last_checked);
    }
    if (updater.last_started) {
      updater_node.Set("laststarted", *updater.last_started);
    }
    request_node.Set("updaters", std::move(updater_node));
  }
#endif

  base::Value::List app_nodes;
  for (const auto& app : request.apps) {
    base::Value::Dict app_node;
    app_node.Set("appid", app.app_id);
    app_node.Set("version", app.version);
    if (!app.ap.empty()) {
      app_node.Set("ap", app.ap);
    }
    if (!app.brand_code.empty()) {
      app_node.Set("brand", app.brand_code);
    }
    if (!app.lang.empty()) {
      app_node.Set("lang", app.lang);
    }
    if (app.install_date != kDateUnknown) {
      app_node.Set("installdate", app.install_date);
    }
    if (!app.install_id.empty()) {
      app_node.Set("iid", app.install_id);
    }
    if (!app.install_source.empty()) {
      app_node.Set("installsource", app.install_source);
    }
    if (!app.install_location.empty()) {
      app_node.Set("installedby", app.install_location);
    }
    // TODO(crbug.com/40145897): Test that this is never sent to the server if
    // the machine is not enterprise managed.
    if (!app.release_channel.empty()) {
      app_node.Set("release_channel", app.release_channel);
    }
    if (!app.cohort.empty()) {
      app_node.Set("cohort", app.cohort);
    }
    if (!app.cohort_name.empty()) {
      app_node.Set("cohortname", app.cohort_name);
    }
    if (!app.cohort_hint.empty()) {
      app_node.Set("cohorthint", app.cohort_hint);
    }
    if (app.enabled) {
      app_node.Set("enabled", *app.enabled);
    }

    if (!app.cached_hashes.empty()) {
      base::Value::List hash_list;
      for (const auto& hash : app.cached_hashes) {
        base::Value::Dict node;
        node.Set("sha256", hash);
        hash_list.Append(std::move(node));
      }
      app_node.Set("cached_items", std::move(hash_list));
    }

    if (app.disabled_reasons && !app.disabled_reasons->empty()) {
      base::Value::List disabled_nodes;
      for (const int disabled_reason : *app.disabled_reasons) {
        base::Value::Dict disabled_node;
        disabled_node.Set("reason", disabled_reason);
        disabled_nodes.Append(std::move(disabled_node));
      }
      app_node.Set("disabled", std::move(disabled_nodes));
    }

    for (const auto& [name, value] : app.installer_attributes) {
      app_node.Set(name, value);
    }

    if (app.update_check) {
      base::Value::Dict update_check_node;
      if (app.update_check->is_update_disabled) {
        update_check_node.Set("updatedisabled", true);
      }
      if (app.update_check->rollback_allowed) {
        update_check_node.Set("rollback_allowed", true);
      }
      if (app.update_check->same_version_update_allowed) {
        update_check_node.Set("sameversionupdate", true);
      }
      if (!app.update_check->target_version_prefix.empty()) {
        update_check_node.Set("targetversionprefix",
                              app.update_check->target_version_prefix);
      }
      app_node.Set("updatecheck", std::move(update_check_node));
    }

    if (!app.data.empty()) {
      base::Value::List data_nodes;
      for (const auto& data : app.data) {
        base::Value::Dict data_node;

        data_node.Set("name", data.name);
        if (data.name == "install") {
          data_node.Set("index", data.install_data_index);
        } else if (data.name == "untrusted") {
          data_node.Set("#text", data.untrusted_data);
        }

        data_nodes.Append(std::move(data_node));
      }

      if (!data_nodes.empty()) {
        app_node.Set("data", std::move(data_nodes));
      }
    }

    if (app.ping) {
      const auto& ping = *app.ping;
      base::Value::Dict ping_node;
      if (!ping.ping_freshness.empty()) {
        ping_node.Set("ping_freshness", ping.ping_freshness);
      }

      // Output "ad" or "a" only if the this app has been seen 'active'.
      if (ping.date_last_active) {
        ping_node.Set("ad", *ping.date_last_active);
      } else if (ping.days_since_last_active_ping) {
        ping_node.Set("a", *ping.days_since_last_active_ping);
      }

      // Output "rd" if valid or "r" as a last resort roll call metric.
      if (ping.date_last_roll_call) {
        ping_node.Set("rd", *ping.date_last_roll_call);
      } else {
        ping_node.Set("r", ping.days_since_last_roll_call);
      }
      app_node.Set("ping", std::move(ping_node));
    }

    if (app.events) {
      base::Value::List event_nodes;
      for (const auto& event : *app.events) {
        CHECK(!event.empty());
        event_nodes.Append(event.Clone());
      }
      app_node.Set("events", std::move(event_nodes));
    }

    app_nodes.Append(std::move(app_node));
  }

  if (!app_nodes.empty()) {
    request_node.Set("apps", std::move(app_nodes));
  }

  root_node.Set("request", std::move(request_node));

  std::string msg;
  return base::JSONWriter::WriteWithOptions(
             root_node, base::JSONWriter::OPTIONS_OMIT_DOUBLE_TYPE_PRESERVATION,
             &msg)
             ? msg
             : std::string();
}

}  // namespace update_client