File: protocol_parser_xml.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • 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 (323 lines) | stat: -rw-r--r-- 9,672 bytes parent folder | download | duplicates (4)
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
// Copyright 2022 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/updater/win/protocol_parser_xml.h"

// clang-format off
#include <objbase.h>
#include <msxml2.h>
// clang-format on
#include <wrl/client.h>

#include <cstdint>
#include <string>

#include "base/check_op.h"
#include "base/containers/flat_map.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions_win.h"
#include "base/strings/sys_string_conversions.h"
#include "base/version.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_variant.h"
#include "url/gurl.h"

namespace updater {
namespace {

bool ReadAttribute(IXMLDOMNode* node,
                   const std::wstring& attribute_name,
                   BSTR* value) {
  Microsoft::WRL::ComPtr<IXMLDOMNamedNodeMap> attributes;
  HRESULT hr = node->get_attributes(&attributes);
  if (FAILED(hr) || !attributes) {
    return false;
  }

  Microsoft::WRL::ComPtr<IXMLDOMNode> attribute_node;
  base::win::ScopedVariant node_value;
  base::win::ScopedBstr attr_name(attribute_name);

  hr = attributes->getNamedItem(attr_name.Get(), &attribute_node);
  if (FAILED(hr) || !attribute_node) {
    return false;
  }

  hr = attribute_node->get_nodeValue(node_value.Receive());
  if (FAILED(hr) || node_value.type() == VT_EMPTY) {
    return false;
  }

  CHECK_EQ(node_value.type(), VT_BSTR);
  VARIANT released_variant = node_value.Release();
  *value = V_BSTR(&released_variant);
  return true;
}

bool ReadStringAttribute(IXMLDOMNode* node,
                         const std::wstring& attribute_name,
                         std::string* value) {
  base::win::ScopedBstr node_value;
  if (!ReadAttribute(node, attribute_name, node_value.Receive())) {
    return false;
  }

  *value = base::SysWideToUTF8(node_value.Get());
  return true;
}

}  // namespace

bool ProtocolParserXML::ParseAction(IXMLDOMNode* node, Results* results) {
  std::string event;
  if (!ReadStringAttribute(node, L"event", &event)) {
    VLOG(1) << "Missing `event` attribute in <action>";
    return false;
  }

  if (event == "install") {
    if (!results->apps.back().manifest.run.empty()) {
      // Only parse the first install action and ignore the rest.
      return true;
    }

    if (!ReadStringAttribute(node, L"run",
                             &results->apps.back().manifest.run) ||
        results->apps.back().manifest.run.empty()) {
      VLOG(1) << "Missing `run` attribute in <action>";
      return false;
    }

    ReadStringAttribute(node, L"arguments",
                        &results->apps.back().manifest.arguments);
    return true;
  } else if (event == "postinstall") {
    // The "postinstall" event is handled by `AppInstall`. The common
    // `postinstall` scenario where the installer provides a launch command is
    // handled by `AppInstall` by running the launch command and exiting in the
    // interactive install case.
    // Other `postinstall` actions such as "reboot", "restart browser", and
    // "post install url" are obsolete, since no one currently uses these
    // actions.
    return true;
  } else {
    VLOG(1) << "Unsupported `event` type in <action>: %s", event.c_str();
    return false;
  }
}

bool ProtocolParserXML::ParseActions(IXMLDOMNode* node, Results* results) {
  return ParseChildren(
      {
          {L"action", &ProtocolParserXML::ParseAction},
      },
      node, results);
}

bool ProtocolParserXML::ParseApp(IXMLDOMNode* node, Results* results) {
  App result;
  if (!ReadStringAttribute(node, L"appid", &result.app_id)) {
    VLOG(1) << "Missing `appid` attribute in <app>";
    return false;
  }

  results->apps.push_back(result);

  return ParseChildren(
      {
          {L"data", &ProtocolParserXML::ParseData},
          {L"updatecheck", &ProtocolParserXML::ParseUpdateCheck},
      },
      node, results);
}

bool ProtocolParserXML::ParseData(IXMLDOMNode* node, Results* results) {
  Data data;
  if (!ReadStringAttribute(node, L"index", &data.install_data_index)) {
    VLOG(1) << "Missing `index` attribute in <data>";
    return false;
  }

  base::win::ScopedBstr text;
  HRESULT hr = node->get_text(text.Receive());
  if (FAILED(hr)) {
    VLOG(1) << "Failed to get text from <data>: " << hr;
    return false;
  }
  data.text = base::SysWideToUTF8(text.Get());

  results->apps.back().data.push_back(data);
  return true;
}

bool ProtocolParserXML::ParseManifest(IXMLDOMNode* node, Results* results) {
  std::string version;
  if (ReadStringAttribute(node, L"version", &version) &&
      base::Version(version).IsValid()) {
    results->apps.back().manifest.version = version;
  }

  return ParseChildren(
      {
          {L"actions", &ProtocolParserXML::ParseActions},
      },
      node, results);
}

bool ProtocolParserXML::ParseResponse(IXMLDOMNode* node, Results* results) {
  std::string protocol_version;
  if (!ReadStringAttribute(node, L"protocol", &protocol_version) ||
      protocol_version != "3.0") {
    VLOG(1) << "Unsupported protocol version: %s", protocol_version.c_str();
    return false;
  }

  return ParseChildren(
      {
          {L"app", &ProtocolParserXML::ParseApp},
          {L"systemrequirements", &ProtocolParserXML::ParseSystemRequirements},
      },
      node, results);
}

bool ProtocolParserXML::ParseSystemRequirements(IXMLDOMNode* node,
                                                Results* results) {
  std::string platform;
  if (ReadStringAttribute(node, L"platform", &platform)) {
    results->system_requirements.platform = platform;
  }

  std::string arch;
  if (ReadStringAttribute(node, L"arch", &arch)) {
    results->system_requirements.arch = arch;
  }

  std::string min_os_version;
  if (ReadStringAttribute(node, L"min_os_version", &min_os_version)) {
    results->system_requirements.min_os_version = min_os_version;
  }

  return true;
}

bool ProtocolParserXML::ParseUpdateCheck(IXMLDOMNode* node, Results* results) {
  const ElementHandlerMap child_handlers = {
      {L"manifest", &ProtocolParserXML::ParseManifest},
  };
  return ParseChildren(child_handlers, node, results);
}

bool ProtocolParserXML::ParseElement(const ElementHandlerMap& handler_map,
                                     IXMLDOMNode* node,
                                     Results* results) {
  base::win::ScopedBstr basename;

  if (FAILED(node->get_baseName(basename.Receive()))) {
    VLOG(1) << "Failed to get name for a DOM element.";
    return false;
  }

  for (const auto& [name, handler] : handler_map) {
    if (name == basename.Get()) {
      return (this->*handler)(node, results);
    }
  }

  return true;  // Ignore unknown elements.
}

bool ProtocolParserXML::ParseChildren(const ElementHandlerMap& handler_map,
                                      IXMLDOMNode* node,
                                      Results* results) {
  Microsoft::WRL::ComPtr<IXMLDOMNodeList> children_list;

  HRESULT hr = node->get_childNodes(&children_list);
  if (FAILED(hr)) {
    VLOG(1) << "Failed to get child elements: " << hr;
    return false;
  }

  long num_children = 0;
  hr = children_list->get_length(&num_children);
  if (FAILED(hr)) {
    VLOG(1) << "Failed to get the number of child elements: " << hr;
    return false;
  }

  for (long i = 0; i < num_children; ++i) {
    Microsoft::WRL::ComPtr<IXMLDOMNode> child_node;
    hr = children_list->get_item(i, &child_node);
    if (FAILED(hr)) {
      VLOG(1) << "Failed to get " << i << "-th child element: " << hr;
      return false;
    }

    DOMNodeType type = NODE_INVALID;
    hr = child_node->get_nodeType(&type);
    if (FAILED(hr)) {
      VLOG(1) << "Failed to get " << i << "-th child element type: " << hr;
      return false;
    }

    if (type == NODE_ELEMENT &&
        !ParseElement(handler_map, child_node.Get(), results)) {
      return false;
    }
  }

  return true;
}

std::optional<ProtocolParserXML::Results> ProtocolParserXML::DoParse(
    const std::string& response_xml) {
  Microsoft::WRL::ComPtr<IXMLDOMDocument> xmldoc;
  HRESULT hr = ::CoCreateInstance(CLSID_DOMDocument30, nullptr, CLSCTX_ALL,
                                  IID_IXMLDOMDocument, &xmldoc);
  if (FAILED(hr)) {
    VLOG(1) << "IXMLDOMDocument.CoCreateInstance failed: " << hr;
    return std::nullopt;
  }
  hr = xmldoc->put_resolveExternals(VARIANT_FALSE);
  if (FAILED(hr)) {
    VLOG(1) << "IXMLDOMDocument.put_resolveExternals failed: " << hr;
    return std::nullopt;
  }

  VARIANT_BOOL is_successful(VARIANT_FALSE);
  auto xml_bstr =
      base::win::ScopedBstr(base::SysUTF8ToWide(response_xml).c_str());
  hr = xmldoc->loadXML(xml_bstr.Get(), &is_successful);
  if (FAILED(hr)) {
    VLOG(1) << "Load manifest failed: " << hr;
    return std::nullopt;
  }

  Microsoft::WRL::ComPtr<IXMLDOMElement> root_node;
  hr = xmldoc->get_documentElement(&root_node);
  if (FAILED(hr) || !root_node) {
    VLOG(1) << "Load manifest failed: " << hr;
    return std::nullopt;
  }

  ProtocolParserXML::Results results;
  if (!ParseElement({{L"response", &ProtocolParserXML::ParseResponse}},
                    root_node.Get(), &results)) {
    return std::nullopt;
  }
  return results;
}

ProtocolParserXML::App::App() = default;
ProtocolParserXML::App::App(const App&) = default;
ProtocolParserXML::App::~App() = default;
ProtocolParserXML::Results::Results() = default;
ProtocolParserXML::Results::Results(const Results&) = default;
ProtocolParserXML::Results::~Results() = default;

std::optional<ProtocolParserXML::Results> ProtocolParserXML::Parse(
    const std::string& xml) {
  return ProtocolParserXML().DoParse(xml);
}

}  // namespace updater