File: upstart_client.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 (217 lines) | stat: -rw-r--r-- 7,748 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
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/upstart/upstart_client.h"

#include <utility>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/dbus/upstart/fake_upstart_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"

namespace ash {

namespace {

constexpr char kUpstartServiceName[] = "com.ubuntu.Upstart";
constexpr char kUpstartJobInterface[] = "com.ubuntu.Upstart0_6.Job";
constexpr char kStartMethod[] = "Start";
constexpr char kRestartMethod[] = "Restart";
constexpr char kStopMethod[] = "Stop";

constexpr char kUpstartJobsPath[] = "/com/ubuntu/Upstart/jobs/";
constexpr char kMediaAnalyticsJob[] = "rtanalytics";

UpstartClient* g_instance = nullptr;

class UpstartClientImpl : public UpstartClient {
 public:
  explicit UpstartClientImpl(dbus::Bus* bus) : bus_(bus) {}

  UpstartClientImpl(const UpstartClientImpl&) = delete;
  UpstartClientImpl& operator=(const UpstartClientImpl&) = delete;

  ~UpstartClientImpl() override = default;

  // UpstartClient overrides:
  void StartJob(const std::string& job,
                const std::vector<std::string>& upstart_env,
                chromeos::VoidDBusMethodCallback callback) override {
    CallJobMethod(job, kStartMethod, upstart_env,
                  base::BindOnce(&UpstartClientImpl::OnVoidMethod,
                                 weak_ptr_factory_.GetWeakPtr(), job, "start",
                                 std::move(callback)));
  }

  void StartJobWithTimeout(const std::string& job,
                           const std::vector<std::string>& upstart_env,
                           chromeos::VoidDBusMethodCallback callback,
                           int timeout_ms) override {
    CallJobMethod(job, kStartMethod, upstart_env,
                  base::BindOnce(&UpstartClientImpl::OnVoidMethod,
                                 weak_ptr_factory_.GetWeakPtr(), job, "start",
                                 std::move(callback)),
                  timeout_ms);
  }

  void StartJobWithErrorDetails(
      const std::string& job,
      const std::vector<std::string>& upstart_env,
      StartJobWithErrorDetailsCallback callback) override {
    CallJobMethod(
        job, kStartMethod, upstart_env,
        base::BindOnce(&UpstartClientImpl::OnStartJobWithErrorDetails,
                       weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  }

  void StopJob(const std::string& job,
               const std::vector<std::string>& upstart_env,
               chromeos::VoidDBusMethodCallback callback) override {
    CallJobMethod(job, kStopMethod, upstart_env,
                  base::BindOnce(&UpstartClientImpl::OnVoidMethod,
                                 weak_ptr_factory_.GetWeakPtr(), job, "stop",
                                 std::move(callback)));
  }

  void StartMediaAnalytics(const std::vector<std::string>& upstart_env,
                           chromeos::VoidDBusMethodCallback callback) override {
    StartJob(kMediaAnalyticsJob, upstart_env, std::move(callback));
  }

  void RestartMediaAnalytics(
      chromeos::VoidDBusMethodCallback callback) override {
    CallJobMethod(
        kMediaAnalyticsJob, kRestartMethod, {},
        base::BindOnce(&UpstartClientImpl::OnVoidMethod,
                       weak_ptr_factory_.GetWeakPtr(), kMediaAnalyticsJob,
                       "restart", std::move(callback)));
  }

  using UpstartClient::StopJob;

  void StopMediaAnalytics() override {
    StopJob(kMediaAnalyticsJob, {}, base::DoNothing());
  }

  void StopMediaAnalytics(chromeos::VoidDBusMethodCallback callback) override {
    StopJob(kMediaAnalyticsJob, {}, std::move(callback));
  }

 private:
  void CallJobMethod(const std::string& job,
                     const std::string& method,
                     const std::vector<std::string>& upstart_env,
                     dbus::ObjectProxy::ResponseOrErrorCallback callback,
                     int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) {
    dbus::ObjectProxy* job_proxy = bus_->GetObjectProxy(
        kUpstartServiceName, dbus::ObjectPath(kUpstartJobsPath + job));
    dbus::MethodCall method_call(kUpstartJobInterface, method);
    dbus::MessageWriter writer(&method_call);
    writer.AppendArrayOfStrings(upstart_env);
    writer.AppendBool(true /* wait for response */);
    job_proxy->CallMethodWithErrorResponse(&method_call, timeout_ms,
                                           std::move(callback));
  }

  void OnVoidMethod(const std::string& job_for_logging,
                    const std::string& action_for_logging,
                    chromeos::VoidDBusMethodCallback callback,
                    dbus::Response* response,
                    dbus::ErrorResponse* error_response) {
    if (!response)
      LogError(job_for_logging, action_for_logging, error_response);
    std::move(callback).Run(response);
  }

  void OnStartJobWithErrorDetails(StartJobWithErrorDetailsCallback callback,
                                  dbus::Response* response,
                                  dbus::ErrorResponse* error_response) {
    std::optional<std::string> error_name;
    std::optional<std::string> error_message;
    if (!response && error_response) {
      // Error response may contain the error message as string.
      error_name = error_response->GetErrorName();
      dbus::MessageReader reader(error_response);
      std::string message;
      if (reader.PopString(&message))
        error_message = std::move(message);
    }
    std::move(callback).Run(response, std::move(error_name),
                            std::move(error_message));
  }

  void LogError(const std::string& job_for_logging,
                const std::string& action_for_logging,
                dbus::ErrorResponse* error_response) {
    std::string error_name;
    std::string error_message;
    if (error_response) {
      // Error response may contain the error message as string.
      error_name = error_response->GetErrorName();
      dbus::MessageReader reader(error_response);
      reader.PopString(&error_message);
    } else {
      // Method call failed without returning an error response. D-Bus itself is
      // not working for whatever reason.
      error_name = "unknown error";
    }
    LOG(ERROR) << "Failed to " << action_for_logging << " " << job_for_logging
               << ": " << error_name << ": " << error_message;
  }

  raw_ptr<dbus::Bus> bus_ = nullptr;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<UpstartClientImpl> weak_ptr_factory_{this};
};

}  // namespace

// static
const char UpstartClient::kAlreadyStartedError[] =
    "com.ubuntu.Upstart0_6.Error.AlreadyStarted";

UpstartClient::UpstartClient() {
  DCHECK(!g_instance);
  g_instance = this;
}

UpstartClient::~UpstartClient() {
  DCHECK_EQ(this, g_instance);
  g_instance = nullptr;
}

// static
void UpstartClient::Initialize(dbus::Bus* bus) {
  DCHECK(bus);
  new UpstartClientImpl(bus);
}

// static
void UpstartClient::InitializeFake() {
  // Do not create a new fake if it was initialized early in a browser test (to
  // allow test properties to be set).
  if (!FakeUpstartClient::Get())
    new FakeUpstartClient();
}

// static
void UpstartClient::Shutdown() {
  DCHECK(g_instance);
  delete g_instance;
}

// static
UpstartClient* UpstartClient::Get() {
  return g_instance;
}

}  // namespace ash