File: fake_upstart_client.h

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 (115 lines) | stat: -rw-r--r-- 4,222 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef CHROMEOS_ASH_COMPONENTS_DBUS_UPSTART_FAKE_UPSTART_CLIENT_H_
#define CHROMEOS_ASH_COMPONENTS_DBUS_UPSTART_FAKE_UPSTART_CLIENT_H_

#include "base/component_export.h"
#include "base/functional/callback.h"
#include "chromeos/ash/components/dbus/upstart/upstart_client.h"

namespace ash {

class COMPONENT_EXPORT(UPSTART_CLIENT) FakeUpstartClient
    : public UpstartClient {
 public:
  struct StartJobResult {
    StartJobResult(bool success,
                   std::optional<std::string> error_name = std::nullopt,
                   std::optional<std::string> error_message = std::nullopt);
    ~StartJobResult();

    bool success;
    std::optional<std::string> error_name;
    std::optional<std::string> error_message;
  };

  using StartJobCallback = base::RepeatingCallback<StartJobResult(
      const std::string& job,
      const std::vector<std::string>& upstart_env)>;
  using StopJobCallback = base::RepeatingCallback<bool(
      const std::string& job,
      const std::vector<std::string>& upstart_env)>;

  FakeUpstartClient();

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

  ~FakeUpstartClient() override;

  // Returns the fake global instance if initialized. May return null.
  static FakeUpstartClient* Get();

  // UpstartClient overrides:
  void StartJob(const std::string& job,
                const std::vector<std::string>& upstart_env,
                chromeos::VoidDBusMethodCallback callback) override;
  void StartJobWithTimeout(const std::string& job,
                           const std::vector<std::string>& upstart_env,
                           chromeos::VoidDBusMethodCallback callback,
                           int timeout_ms) override;
  void StartJobWithErrorDetails(
      const std::string& job,
      const std::vector<std::string>& upstart_env,
      StartJobWithErrorDetailsCallback callback) override;
  void StopJob(const std::string& job,
               const std::vector<std::string>& upstart_env,
               chromeos::VoidDBusMethodCallback callback) override;
  void StartMediaAnalytics(const std::vector<std::string>& upstart_env,
                           chromeos::VoidDBusMethodCallback callback) override;
  void RestartMediaAnalytics(
      chromeos::VoidDBusMethodCallback callback) override;
  void StopMediaAnalytics() override;
  void StopMediaAnalytics(chromeos::VoidDBusMethodCallback callback) override;

  void set_start_job_cb(const StartJobCallback& cb) { start_job_cb_ = cb; }
  void set_stop_job_cb(const StopJobCallback& cb) { stop_job_cb_ = cb; }

  enum class UpstartOperationType { START, STOP };

  struct UpstartOperation {
    UpstartOperation(const std::string& name,
                     const std::vector<std::string>& env,
                     UpstartOperationType type);
    UpstartOperation(const UpstartOperation& other);
    UpstartOperation& operator=(const UpstartOperation&);

    ~UpstartOperation();

    std::string name;
    std::vector<std::string> env;
    UpstartOperationType type;
  };

  // Starts recording all Upstart start/stop operations made via
  // FakeUpstartClient using StartJob* or StopJob methods.
  void StartRecordingUpstartOperations();

  // Getter for |upstart_operations_|.
  std::vector<UpstartOperation> upstart_operations() {
    return upstart_operations_;
  }

  // Returns the history of all the Upstart operations recorded for a given job.
  std::vector<UpstartOperation> GetRecordedUpstartOperationsForJob(
      const std::string& name);

 private:
  // Callback to decide the result of StartJob() / StartJobWithErrorDetails().
  StartJobCallback start_job_cb_;

  // Callback to decide the result of StopJob().
  StopJobCallback stop_job_cb_;

  // Stores all Upstart start/stop operations recorded, ordered by the timing.
  std::vector<UpstartOperation> upstart_operations_;

  // A flag indicating whether to record Upstart operations.
  bool is_recording_ = false;
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_DBUS_UPSTART_FAKE_UPSTART_CLIENT_H_