File: install_service_work_item_impl.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (259 lines) | stat: -rw-r--r-- 9,692 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
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
// 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.

#ifndef CHROME_INSTALLER_UTIL_INSTALL_SERVICE_WORK_ITEM_IMPL_H_
#define CHROME_INSTALLER_UTIL_INSTALL_SERVICE_WORK_ITEM_IMPL_H_

#include <windows.h>

#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/strings/cstring_view.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_types.h"
#include "chrome/installer/util/work_item_list.h"

namespace base {

class CommandLine;

}  // namespace base

namespace installer {

// Helper class for the implementation of InstallServiceWorkItem.
class InstallServiceWorkItemImpl {
 public:
  struct ServiceConfig {
    ServiceConfig();
    ServiceConfig(uint32_t service_type,
                  uint32_t service_start_type,
                  uint32_t service_error_control,
                  const std::wstring& service_cmd_line,
                  const wchar_t* dependencies_multi_sz,
                  const std::wstring& service_display_name);

    ServiceConfig(const ServiceConfig& rhs);
    ServiceConfig& operator=(const ServiceConfig& rhs) = default;

    ServiceConfig(ServiceConfig&& rhs);
    ServiceConfig& operator=(ServiceConfig&& rhs) = default;

    ~ServiceConfig();

    friend bool operator==(const ServiceConfig& lhs, const ServiceConfig& rhs);

    bool is_valid = false;
    uint32_t type = SERVICE_NO_CHANGE;
    uint32_t start_type = SERVICE_NO_CHANGE;
    uint32_t error_control = SERVICE_NO_CHANGE;
    std::wstring cmd_line;
    std::vector<wchar_t> dependencies;
    std::wstring display_name;
  };

  InstallServiceWorkItemImpl(const std::wstring& service_name,
                             const std::wstring& display_name,
                             const std::wstring& description,
                             uint32_t start_type,
                             const base::CommandLine& service_cmd_line,
                             const base::CommandLine& com_service_cmd_line_args,
                             const std::wstring& registry_path,
                             const std::vector<GUID>& clsids,
                             const std::vector<GUID>& iids);

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

  ~InstallServiceWorkItemImpl();

  bool DoImpl();
  void RollbackImpl();
  bool DeleteServiceImpl();

  // Member functions that help with service installation or upgrades.

  // `MakeUpgradeServiceConfig()` returns a differential `ServiceConfig` that
  // facilitates passing it with minimal changes subsequently to
  // `ChangeServiceConfig()`. This way, `ChangeServiceConfig()` only asks the
  // Windows SCM to make changes where they differ from the existing service
  // configuration.
  ServiceConfig MakeUpgradeServiceConfig(const ServiceConfig& original_config);

  // Takes the `new_config` returned by `MakeUpgradeServiceConfig()` and
  // returns true if `new_config` does not match the default configuration.
  bool IsUpgradeNeeded(const ServiceConfig& new_config);

  bool ChangeServiceConfig(const ServiceConfig& config);
  bool DeleteCurrentService();

  // Helper functions for service install/upgrade/delete/rollback.
  bool OpenService();
  bool GetServiceConfig(ServiceConfig* config) const;

  // Stores in the registry a versioned service name generated by
  // GenerateVersionedServiceName().
  bool CreateAndSetServiceName() const;

  // Returns the versioned service name if one exists in the registry under the
  // named value service_name_. In other cases, it returns service_name_.
  std::wstring GetCurrentServiceName() const;

  // Returns a display name of the following format:
  // "Chrome Elevation Service (ChromeElevationService)"
  // or:
  // "Chrome Elevation Service (ChromeElevationService1d59511c58deaa8)"
  //
  // The "Chrome Elevation Service" fragment is the display_name_, and the
  // "ChromeElevationService1d59511c58deaa8" fragment is the versioned service
  // name returned from GetCurrentServiceName().
  std::wstring GetCurrentServiceDisplayName() const;

  // Copies and returns a vector containing a sequence of C-style strings
  // terminated with '\0\0'. Return an empty vector if the input is nullptr.
  static std::vector<wchar_t> MultiSzToVector(const wchar_t* multi_sz);

  // Returns true if a cursory check appears to indicate that the service
  // hosting `clsid` is installed.
  static bool IsComServiceInstalled(const GUID& clsid);

  // Returns the current name of the service as registered with the SCM.
  static std::wstring GetCurrentServiceName(base::wcstring_view service_name,
                                            base::wcstring_view registry_path);

 private:
  class ScHandleTraits {
   public:
    using Handle = SC_HANDLE;

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

    static bool CloseHandle(SC_HANDLE handle) {
      return ::CloseServiceHandle(handle) != FALSE;
    }

    static bool IsHandleValid(SC_HANDLE handle) { return handle != nullptr; }

    static SC_HANDLE NullHandle() { return nullptr; }
  };

  using ScopedScHandle =
      base::win::GenericScopedHandle<ScHandleTraits,
                                     base::win::DummyVerifierTraits>;

  // This is the core functionality for installing the Windows Service itself.
  bool DoInstallService();

  // This is the core functionality for COM registration for the Service.
  bool DoComRegistration();

  // Returns the service description by querying the service.
  std::wstring GetCurrentServiceDescription() const;

  // Sets the service description displayed in the services control panel.
  void SetDescription();

  // Member functions that help with service installation or upgrades.
  bool InstallNewService();

  // Upgrades an existing service's configuration in-place. Returns true if the
  // service was already properly configured, or if it was successfully
  // upgraded; otherwise, returns false in case of any failure.
  // Side-effects of this function:
  //   * Saves the original service's config in |original_service_config_| if
  //     the new service configuration will be different.
  //     |original_service_config_| is used in rollback scenarios, specifically
  //     in ReinstallOriginalService() and RestoreOriginalServiceConfig().
  //   * Sets |rollback_existing_service_| to true if the service is
  //     successfully upgraded, which is used by RollbackImpl().
  bool UpgradeService();

  // Member functions that help with rollbacks.
  bool ReinstallOriginalService();
  bool RestoreOriginalServiceConfig();

  // Helper functions for service install/upgrade/delete/rollback.
  bool InstallService(const ServiceConfig& config);
  bool DeleteService(ScopedScHandle service) const;

  // Generates a versioned service name prefixed with service_name_ and suffixed
  // with the current system time in hexadecimal format.
  std::wstring GenerateVersionedServiceName() const;

  // Persists the given service name in the registry.
  bool SetServiceName(const std::wstring& service_name) const;

  // The COM registration is done using a contained WorkItemList.
  std::unique_ptr<WorkItemList> com_registration_work_items_;

  // The service name, or in the case of a conflict, the prefix for the service
  // name.
  const std::wstring service_name_;

  // The service name displayed to the user.
  const std::wstring display_name_;

  // The service description displayed in the services control panel.
  const std::wstring description_;

  // The service start options. This parameter is typically SERVICE_AUTO_START
  // or SERVICE_DEMAND_START.
  const uint32_t start_type_;

  // The desired service command line.
  const base::CommandLine service_cmd_line_;

  // The SCM will pass any switches specified in `com_service_cmd_line_args_` to
  // ServiceMain() during COM activation.
  const base::CommandLine com_service_cmd_line_args_;

  // The path under HKEY_LOCAL_MACHINE where the service persists information,
  // such as a versioned service name. For legacy reasons, this path is mapped
  // to the 32-bit view of the registry.
  const std::wstring registry_path_;

  // If COM CLSID/AppId registration is required, |clsids_| would be populated.
  const std::vector<GUID> clsids_;

  // If COM Interface/Typelib registration is required, |iids_| would be
  // populated.
  const std::vector<GUID> iids_;

  ScopedScHandle scm_;
  ScopedScHandle service_;

  // Rollback-specific data.

  // True if original_service_config_ and service_ are both valid, and the
  // former should be applied to the latter on rollback.
  bool rollback_existing_service_;

  // True if service_ represents a newly-installed service that is to be deleted
  // on rollback.
  bool rollback_new_service_;

  // The configuration of a pre-existing service on the machine that may have
  // been modified or deleted.
  ServiceConfig original_service_config_;

  // The service name prior to any modifications; may be either |service_name_|
  // or a value read from the registry.
  std::wstring original_service_name_;

  // True if a pre-existing service (named |original_service_name_|) could not
  // be deleted and still exists on rollback.
  bool original_service_still_exists_;

  FRIEND_TEST_ALL_PREFIXES(InstallServiceWorkItemTest,
                           Do_UpgradeChangedCmdLineStartTypeCOMArgs);
};

}  // namespace installer

#endif  // CHROME_INSTALLER_UTIL_INSTALL_SERVICE_WORK_ITEM_IMPL_H_