1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_UPDATER_WIN_UTIL_H_
6 #define CHROME_UPDATER_WIN_UTIL_H_
7
8 #include <winerror.h>
9
10 #include <stdint.h>
11
12 #include <string>
13
14 #include "base/optional.h"
15 #include "base/strings/string16.h"
16 #include "base/win/atl.h"
17 #include "base/win/scoped_handle.h"
18 #include "base/win/windows_types.h"
19
20 namespace updater {
21
22 // Returns the last error as an HRESULT or E_FAIL if last error is NO_ERROR.
23 // This is not a drop in replacement for the HRESULT_FROM_WIN32 macro.
24 // The macro maps a NO_ERROR to S_OK, whereas the HRESULTFromLastError maps a
25 // NO_ERROR to E_FAIL.
26 HRESULT HRESULTFromLastError();
27
28 // Returns an HRESULT with a custom facility code representing an updater error.
29 template <typename Error>
HRESULTFromUpdaterError(Error error)30 HRESULT HRESULTFromUpdaterError(Error error) {
31 constexpr ULONG kCustomerBit = 0x20000000;
32 constexpr ULONG kFacilityOmaha = 67;
33 return HRESULT{ULONG{SEVERITY_ERROR} | kCustomerBit | (kFacilityOmaha << 16) |
34 ULONG{error}};
35 }
36
37 // Checks whether a process is running with the image |executable|. Returns true
38 // if a process is found.
39 bool IsProcessRunning(const wchar_t* executable);
40
41 // Waits until every running instance of |executable| is stopped.
42 // Returns true if every running processes are stopped.
43 bool WaitForProcessesStopped(const wchar_t* executable);
44
45 bool InitializeCOMSecurity();
46
47 // Gets the handle to the module containing the given executing address.
48 HMODULE GetModuleHandleFromAddress(void* address);
49
50 // Gets the handle to the currently executing module.
51 HMODULE GetCurrentModuleHandle();
52
53 // Creates a unique event name and stores it in the specified environment var.
54 HRESULT CreateUniqueEventInEnvironment(const base::string16& var_name,
55 bool is_machine,
56 HANDLE* unique_event);
57
58 // Obtains a unique event name from specified environment var and opens it.
59 HRESULT OpenUniqueEventFromEnvironment(const base::string16& var_name,
60 bool is_machine,
61 HANDLE* unique_event);
62
63 struct NamedObjectAttributes {
64 NamedObjectAttributes();
65 ~NamedObjectAttributes();
66 base::string16 name;
67 CSecurityAttributes sa;
68 };
69
70 // For machine and local system, the prefix would be "Global\G{obj_name}".
71 // For user, the prefix would be "Global\G{user_sid}{obj_name}".
72 // For machine objects, returns a security attributes that gives permissions to
73 // both Admins and SYSTEM. This allows for cases where SYSTEM creates the named
74 // object first. The default DACL for SYSTEM will not allow Admins access.
75 void GetNamedObjectAttributes(const base::char16* base_name,
76 bool is_machine,
77 NamedObjectAttributes* attr);
78
79 // Creates an event based on the provided attributes.
80 HRESULT CreateEvent(NamedObjectAttributes* event_attr, HANDLE* event_handle);
81
82 // Gets the security descriptor with the default DACL for the current process
83 // user. The owner is the current user, the group is the current primary group.
84 // Returns true and populates sec_attr on success, false on failure.
85 bool GetCurrentUserDefaultSecurityAttributes(CSecurityAttributes* sec_attr);
86
87 // Get security attributes containing a DACL that grant the ACCESS_MASK access
88 // to admins and system.
89 void GetAdminDaclSecurityAttributes(CSecurityAttributes* sec_attr,
90 ACCESS_MASK accessmask);
91
92 // Get security descriptor containing a DACL that grants the ACCESS_MASK access
93 // to admins and system.
94 void GetAdminDaclSecurityDescriptor(CSecurityDesc* sd, ACCESS_MASK accessmask);
95
96 // Returns the registry path for the Updater app id under the |Clients| subkey.
97 // The path does not include the registry root hive prefix.
98 base::string16 GetRegistryKeyClientsUpdater();
99
100 // Returns the registry path for the Updater app id under the |ClientState|
101 // subkey. The path does not include the registry root hive prefix.
102 base::string16 GetRegistryKeyClientStateUpdater();
103
104 // Returns a value in the [0, 100] range or -1 if the progress could not
105 // be computed.
106 int GetDownloadProgress(int64_t downloaded_bytes, int64_t total_bytes);
107
108 // Reads installer progress for |app_id| from registry. The installer progress
109 // is written by the application installer. Returns a value in the [0, 100]
110 // range or -1 if the install progress is not available.
111 int GetInstallerProgress(const std::string& app_id);
112
113 bool DeleteInstallerProgress(const std::string& app_id);
114
115 // Returns a logged on user token handle from the current session.
116 base::win::ScopedHandle GetUserTokenFromCurrentSessionId();
117
118 } // namespace updater
119
120 #endif // CHROME_UPDATER_WIN_UTIL_H_
121