File: update_service_stub.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (438 lines) | stat: -rw-r--r-- 18,423 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
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
// 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/app/server/posix/update_service_stub.h"

#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/version.h"
#include "chrome/updater/ipc/ipc_names.h"
#include "chrome/updater/ipc/ipc_security.h"
#include "chrome/updater/mojom/updater_service.mojom-forward.h"
#include "chrome/updater/registration_data.h"
#include "components/named_mojo_ipc_server/connection_info.h"
#include "components/named_mojo_ipc_server/endpoint_options.h"
#include "components/named_mojo_ipc_server/named_mojo_ipc_server.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace updater {
namespace {

// Helper functions for converting between mojom types and their native
// counterparts.
[[nodiscard]] updater::RegistrationRequest MakeRegistrationRequest(
    const mojom::RegistrationRequestPtr& mojom) {
  CHECK(mojom);

  updater::RegistrationRequest request;
  request.app_id = mojom->app_id;
  request.brand_code = mojom->brand_code;
  request.brand_path = mojom->brand_path;
  request.ap = mojom->ap;
  request.version = base::Version(mojom->version);
  request.existence_checker_path = mojom->existence_checker_path;
  if (mojom->version_path) {
    request.version_path = *mojom->version_path;
  }
  if (mojom->version_key) {
    request.version_key = *mojom->version_key;
  }
  if (mojom->ap_path) {
    request.ap_path = *mojom->ap_path;
  }
  if (mojom->ap_key) {
    request.ap_key = *mojom->ap_key;
  }
  if (mojom->install_id) {
    request.install_id = *mojom->install_id;
  }
  return request;
}

[[nodiscard]] mojom::AppStatePtr MakeMojoAppState(
    const updater::UpdateService::AppState& app_state) {
  return mojom::AppState::New(
      app_state.app_id, app_state.version.GetString(), app_state.ap,
      app_state.brand_code, app_state.brand_path, app_state.ecp,
      app_state.ap_path, app_state.ap_key, app_state.version_path,
      app_state.version_key, app_state.cohort);
}

[[nodiscard]] mojom::UpdateStatePtr MakeMojoUpdateState(
    const updater::UpdateService::UpdateState& update_state) {
  return mojom::UpdateState::New(
      update_state.app_id,
      static_cast<mojom::UpdateState::State>(update_state.state),
      update_state.next_version.GetString(), update_state.downloaded_bytes,
      update_state.total_bytes, update_state.install_progress,
      static_cast<mojom::UpdateService::ErrorCategory>(
          update_state.error_category),
      update_state.error_code, update_state.extra_code1,
      update_state.installer_text, update_state.installer_cmd_line);
}

// A thin wrapper around a StateChangeObserver remote to allow for refcounting.
class StateChangeObserverWrapper
    : public base::RefCountedThreadSafe<StateChangeObserverWrapper> {
 public:
  explicit StateChangeObserverWrapper(
      std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer)
      : observer_(std::move(observer)) {}

  void OnStateChange(const updater::UpdateService::UpdateState& update_state) {
    (*observer_)->OnStateChange(MakeMojoUpdateState(update_state));
  }

  void OnComplete(updater::UpdateService::Result result) {
    (*observer_)->OnComplete(static_cast<mojom::UpdateService::Result>(result));
  }

 private:
  friend class base::RefCountedThreadSafe<StateChangeObserverWrapper>;
  virtual ~StateChangeObserverWrapper() = default;
  std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer_;
};

// Binds a callback that forwards state change callbacks and the OnComplete
// callback to a StateChangeObserver.
[[nodiscard]] std::pair<
    base::RepeatingCallback<void(const UpdateService::UpdateState&)>,
    base::OnceCallback<void(UpdateService::Result)>>
MakeStateChangeObserverCallbacks(
    std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer) {
  auto wrapper =
      base::MakeRefCounted<StateChangeObserverWrapper>(std::move(observer));
  return {
      base::BindRepeating(&StateChangeObserverWrapper::OnStateChange, wrapper),
      base::BindOnce(&StateChangeObserverWrapper::OnComplete, wrapper)};
}

// UpdateServiceStubUntrusted only forwards certain safe calls to its underlying
// UpdateService.
class UpdateServiceStubUntrusted : public mojom::UpdateService {
 public:
  // `impl` must outlive `this`, since it `impl` is saved as a raw pointer.
  explicit UpdateServiceStubUntrusted(mojom::UpdateService* impl)
      : impl_(impl) {}
  UpdateServiceStubUntrusted(const UpdateServiceStubUntrusted&) = delete;
  UpdateServiceStubUntrusted& operator=(const UpdateServiceStubUntrusted&) =
      delete;

  // updater::mojom::UpdateService
  void GetVersion(GetVersionCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->GetVersion(std::move(callback));
  }

  void GetAppStates(GetAppStatesCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->GetAppStates(std::move(callback));
  }

  void Update(const std::string& app_id,
              const std::string& install_data_index,
              UpdateService::Priority priority,
              UpdateService::PolicySameVersionUpdate policy_same_version_update,
              bool do_update_check_only,
              const std::optional<std::string>& language,
              UpdateCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->Update(app_id, install_data_index, priority,
                  policy_same_version_update, do_update_check_only,
                  language.value_or(""), std::move(callback));
  }

  void CheckForUpdate(
      const std::string& app_id,
      UpdateService::Priority priority,
      UpdateService::PolicySameVersionUpdate policy_same_version_update,
      const std::optional<std::string>& language,
      UpdateCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->CheckForUpdate(app_id, priority, policy_same_version_update,
                          language.value_or(""), std::move(callback));
  }

  void RunPeriodicTasks(RunPeriodicTasksCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->RunPeriodicTasks(std::move(callback));
  }

  void FetchPolicies(policy::PolicyFetchReason reason,
                     FetchPoliciesCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->FetchPolicies(reason, std::move(callback));
  }

  void UpdateAll(UpdateAllCallback callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    impl_->UpdateAll(std::move(callback));
  }

  // The rest of updater::mojom::UpdateService is rejected.
  void RegisterApp(mojom::RegistrationRequestPtr request,
                   RegisterAppCallback callback) override {
    VLOG(1) << __func__ << " rejected (untrusted caller)";
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    std::move(callback).Run(kErrorPermissionDenied);
  }

  void Install(mojom::RegistrationRequestPtr registration,
               const std::string& client_install_data,
               const std::string& install_data_index,
               UpdateService::Priority priority,
               const std::optional<std::string>& language,
               InstallCallback callback) override {
    VLOG(1) << __func__ << " rejected (untrusted caller)";
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    mojo::Remote<mojom::StateChangeObserver> observer;
    std::move(callback).Run(observer.BindNewPipeAndPassReceiver());
    observer->OnComplete(mojom::UpdateService_Result::kPermissionDenied);
  }

  void CancelInstalls(const std::string& app_id) override {
    VLOG(1) << __func__ << " rejected (untrusted caller)";
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  }

  void RunInstaller(const std::string& app_id,
                    const ::base::FilePath& installer_path,
                    const std::string& install_args,
                    const std::string& install_data,
                    const std::string& install_settings,
                    const std::optional<std::string>& language,
                    RunInstallerCallback callback) override {
    VLOG(1) << __func__ << " rejected (untrusted caller)";
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    mojo::Remote<mojom::StateChangeObserver> observer;
    std::move(callback).Run(observer.BindNewPipeAndPassReceiver());
    observer->OnComplete(mojom::UpdateService_Result::kPermissionDenied);
  }

 private:
  void OnClientDisconnected();

  raw_ptr<mojom::UpdateService> impl_;
  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace

UpdateServiceStub::UpdateServiceStub(scoped_refptr<updater::UpdateService> impl,
                                     UpdaterScope scope,
                                     base::RepeatingClosure task_start_listener,
                                     base::RepeatingClosure task_end_listener)
    : UpdateServiceStub(impl,
                        scope,
                        task_start_listener,
                        task_end_listener,
                        base::RepeatingClosure()) {}

UpdateServiceStub::~UpdateServiceStub() = default;

void UpdateServiceStub::GetVersion(GetVersionCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->GetVersion(
      base::BindOnce(
          [](GetVersionCallback callback, const base::Version& version) {
            std::move(callback).Run(version.GetString());
          },
          std::move(callback))
          .Then(task_end_listener_));
}

void UpdateServiceStub::FetchPolicies(policy::PolicyFetchReason reason,
                                      FetchPoliciesCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->FetchPolicies(reason, std::move(callback).Then(task_end_listener_));
}

void UpdateServiceStub::RegisterApp(mojom::RegistrationRequestPtr request,
                                    RegisterAppCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->RegisterApp(MakeRegistrationRequest(request),
                     std::move(callback).Then(task_end_listener_));
}

void UpdateServiceStub::GetAppStates(GetAppStatesCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->GetAppStates(
      base::BindOnce(
          [](const std::vector<updater::UpdateService::AppState>& app_states) {
            std::vector<mojom::AppStatePtr> app_states_mojom;
            std::ranges::transform(app_states,
                                   std::back_inserter(app_states_mojom),
                                   &MakeMojoAppState);
            return app_states_mojom;
          })
          .Then(std::move(callback))
          .Then(task_end_listener_));
}

void UpdateServiceStub::RunPeriodicTasks(RunPeriodicTasksCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->RunPeriodicTasks(std::move(callback).Then(task_end_listener_));
}

void UpdateServiceStub::UpdateAll(UpdateAllCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer =
      std::make_unique<mojo::Remote<mojom::StateChangeObserver>>();
  std::move(callback).Run(observer->BindNewPipeAndPassReceiver());

  auto [state_change_callback, on_complete_callback] =
      MakeStateChangeObserverCallbacks(std::move(observer));
  impl_->UpdateAll(std::move(state_change_callback),
                   std::move(on_complete_callback).Then(task_end_listener_));
}

void UpdateServiceStub::Update(
    const std::string& app_id,
    const std::string& install_data_index,
    UpdateService::Priority priority,
    UpdateService::PolicySameVersionUpdate policy_same_version_update,
    bool do_update_check_only,
    const std::optional<std::string>& language,
    UpdateCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  auto observer = std::make_unique<mojo::Remote<mojom::StateChangeObserver>>();
  std::move(callback).Run(observer->BindNewPipeAndPassReceiver());

  auto [state_change_callback, on_complete_callback] =
      MakeStateChangeObserverCallbacks(std::move(observer));
  if (do_update_check_only) {
    impl_->CheckForUpdate(
        app_id, static_cast<updater::UpdateService::Priority>(priority),
        static_cast<updater::UpdateService::PolicySameVersionUpdate>(
            policy_same_version_update),
        language.value_or(""), state_change_callback,
        std::move(on_complete_callback).Then(task_end_listener_));
  } else {
    impl_->Update(app_id, install_data_index,
                  static_cast<updater::UpdateService::Priority>(priority),
                  static_cast<updater::UpdateService::PolicySameVersionUpdate>(
                      policy_same_version_update),
                  language.value_or(""), state_change_callback,
                  std::move(on_complete_callback).Then(task_end_listener_));
  }
}

void UpdateServiceStub::Install(mojom::RegistrationRequestPtr registration,
                                const std::string& client_install_data,
                                const std::string& install_data_index,
                                UpdateService::Priority priority,
                                const std::optional<std::string>& language,
                                InstallCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer =
      std::make_unique<mojo::Remote<mojom::StateChangeObserver>>();
  std::move(callback).Run(observer->BindNewPipeAndPassReceiver());

  auto [state_change_callback, on_complete_callback] =
      MakeStateChangeObserverCallbacks(std::move(observer));
  impl_->Install(MakeRegistrationRequest(registration), client_install_data,
                 install_data_index,
                 static_cast<updater::UpdateService::Priority>(priority),
                 language.value_or(""), std::move(state_change_callback),
                 std::move(on_complete_callback).Then(task_end_listener_));
}

void UpdateServiceStub::CancelInstalls(const std::string& app_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  impl_->CancelInstalls(app_id);
  task_end_listener_.Run();
}

void UpdateServiceStub::RunInstaller(const std::string& app_id,
                                     const ::base::FilePath& installer_path,
                                     const std::string& install_args,
                                     const std::string& install_data,
                                     const std::string& install_settings,
                                     const std::optional<std::string>& language,
                                     RunInstallerCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  std::unique_ptr<mojo::Remote<mojom::StateChangeObserver>> observer =
      std::make_unique<mojo::Remote<mojom::StateChangeObserver>>();
  std::move(callback).Run(observer->BindNewPipeAndPassReceiver());

  auto [state_change_callback, on_complete_callback] =
      MakeStateChangeObserverCallbacks(std::move(observer));
  impl_->RunInstaller(app_id, installer_path, install_args, install_data,
                      install_settings, language.value_or(""),
                      std::move(state_change_callback),
                      std::move(on_complete_callback).Then(task_end_listener_));
}

void UpdateServiceStub::CheckForUpdate(
    const std::string& app_id,
    UpdateService::Priority priority,
    UpdateService::PolicySameVersionUpdate policy_same_version_update,
    const std::optional<std::string>& language,
    UpdateCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  task_start_listener_.Run();
  auto observer = std::make_unique<mojo::Remote<mojom::StateChangeObserver>>();
  std::move(callback).Run(observer->BindNewPipeAndPassReceiver());
  auto [state_change_callback, on_complete_callback] =
      MakeStateChangeObserverCallbacks(std::move(observer));
  impl_->CheckForUpdate(
      app_id, static_cast<updater::UpdateService::Priority>(priority),
      static_cast<updater::UpdateService::PolicySameVersionUpdate>(
          policy_same_version_update),
      language.value_or(""), state_change_callback,
      std::move(on_complete_callback).Then(task_end_listener_));
}

UpdateServiceStub::UpdateServiceStub(
    scoped_refptr<updater::UpdateService> impl,
    UpdaterScope scope,
    base::RepeatingClosure task_start_listener,
    base::RepeatingClosure task_end_listener,
    base::RepeatingClosure endpoint_created_listener_for_testing)
    : filter_(std::make_unique<UpdateServiceStubUntrusted>(this)),
      server_({GetUpdateServiceServerName(scope),
               named_mojo_ipc_server::EndpointOptions::kUseIsolatedConnection},
              base::BindRepeating(base::BindRepeating(
                  [](mojom::UpdateService* interface,
                     mojom::UpdateService* filter,
                     const named_mojo_ipc_server::ConnectionInfo& info) {
                    return IsConnectionTrusted(info) ? interface : filter;
                  },
                  this,
                  filter_.get()))),
      impl_(impl),
      task_start_listener_(task_start_listener),
      task_end_listener_(task_end_listener) {
  server_.set_disconnect_handler(base::BindRepeating(
      [] { VLOG(1) << "UpdateService client disconnected."; }));
  if (endpoint_created_listener_for_testing) {
    server_.set_on_server_endpoint_created_callback_for_testing(  // IN-TEST
        endpoint_created_listener_for_testing);
  }
  server_.StartServer();
}

}  // namespace updater