OLD | NEW |
| (Empty) |
1 // Copyright 2007-2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #ifndef OMAHA_GOOPDATE_APP_MANAGER_H_ | |
17 #define OMAHA_GOOPDATE_APP_MANAGER_H_ | |
18 | |
19 #include <windows.h> | |
20 #include <atlstr.h> | |
21 #include <vector> | |
22 #include "base/basictypes.h" | |
23 #include "omaha/base/synchronized.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 class App; | |
28 class RegKey; | |
29 | |
30 typedef std::vector<CString> AppIdVector; | |
31 | |
32 // Manages the persistence of application state in the registry. | |
33 // All functions that operate on model objects assume the call is protected by | |
34 // the model lock. | |
35 // All public functions hold a registry access lock for the duration of registry | |
36 // accesses in that function. Unless otherwise noted, read operations may return | |
37 // inconsistent/unstable state in some cases. Examples include: | |
38 // * The app installer is running and modifying the registry (not all installers | |
39 // acquire this lock before modifying the registry). | |
40 // * Omaha is in the process of installing an app, and the read occurred between | |
41 // registry operations (i.e. after WritePreInstallData() but before | |
42 // WriteAppPersistentData(). | |
43 // If your operation absolutely needs consistent/stable state, use the functions | |
44 // that ensure this. | |
45 // All write functions assume that the lock returned by | |
46 // GetRegistryStableStateLock() is held. Reads do not require this lock to be | |
47 // held. | |
48 class AppManager { | |
49 public: | |
50 // These values are a public API. Do not remove or move existing values. | |
51 enum InstallerResult { | |
52 INSTALLER_RESULT_SUCCESS = 0, | |
53 INSTALLER_RESULT_FAILED_CUSTOM_ERROR = 1, | |
54 INSTALLER_RESULT_FAILED_MSI_ERROR = 2, | |
55 INSTALLER_RESULT_FAILED_SYSTEM_ERROR = 3, | |
56 INSTALLER_RESULT_EXIT_CODE = 4, | |
57 INSTALLER_RESULT_DEFAULT = INSTALLER_RESULT_EXIT_CODE, | |
58 INSTALLER_RESULT_MAX, | |
59 }; | |
60 | |
61 static HRESULT CreateInstance(bool is_machine); | |
62 static void DeleteInstance(); | |
63 | |
64 static AppManager* Instance(); | |
65 | |
66 // Reads the "pv" value from Google\Update\Clients\{app_guid}, and is used by | |
67 // the Update3WebControl. This method does not take any locks, and is not | |
68 // recommended for use in any other scenario. | |
69 static HRESULT ReadAppVersionNoLock(bool is_machine, const GUID& app_guid, | |
70 CString* version); | |
71 | |
72 bool IsAppRegistered(const GUID& app_guid) const; | |
73 bool IsAppUninstalled(const GUID& app_guid) const; | |
74 | |
75 // Adds all registered products to bundle. | |
76 HRESULT GetRegisteredApps(AppIdVector* app_ids) const; | |
77 | |
78 // Adds all uninstalled products to bundle. | |
79 HRESULT GetUninstalledApps(AppIdVector* app_ids) const; | |
80 | |
81 // Adds all OEM installed apps that user has accepted EULA either explicitly | |
82 // or implicitly. | |
83 HRESULT GetOemInstalledAndEulaAcceptedApps(AppIdVector* app_ids) const; | |
84 | |
85 // TODO(omaha3): Consider moving these two RegistrationUpdateHook functions | |
86 // out of this class. Instead, AppManager should expose a function to obtain | |
87 // the hook for each app. | |
88 | |
89 // CoCreates and runs the HookClsid for app_id. | |
90 HRESULT RunRegistrationUpdateHook(const CString& app_id) const; | |
91 | |
92 // CoCreates and runs HookClsids for registered products. | |
93 HRESULT RunAllRegistrationUpdateHooks() const; | |
94 | |
95 // Populates the app object with the persisted state stored in the registry. | |
96 HRESULT ReadAppPersistentData(App* app); | |
97 | |
98 // Populates the app object with the install time diff based on the install | |
99 // time stored in the registry. | |
100 // If the app is registered or has pv value, app's install time diff will be | |
101 // calculated based on InstallTime value from the registry, or 0 if the value | |
102 // is not there. For other cases, the install time diff will be -1 day. | |
103 void ReadAppInstallTimeDiff(App* app); | |
104 | |
105 // Populates the app object with the persisted state for an uninstalled app | |
106 // stored in the registry. | |
107 HRESULT ReadUninstalledAppPersistentData(App* app); | |
108 | |
109 // Sets dynamic install parameters that the installer or app may use. | |
110 // Call this method before calling the installer. | |
111 HRESULT WritePreInstallData(const App& app); | |
112 | |
113 // Reads Installer Result API values the installer may have written to the | |
114 // registry. Clears all values after reading. | |
115 void ReadInstallerResultApiValues(const GUID& app_guid, | |
116 InstallerResult* installer_result, | |
117 DWORD* installer_error, | |
118 DWORD* installer_extra_code1, | |
119 CString* installer_result_uistring, | |
120 CString* installer_success_launch_cmd); | |
121 | |
122 // Clears the Installer Result API values from the registry. | |
123 void ClearInstallerResultApiValues(const GUID& app_guid); | |
124 | |
125 // Reads the values the app wrote to the Clients key and stores them in the | |
126 // app object. Replaces existing values. | |
127 HRESULT ReadInstallerRegistrationValues(App* app); | |
128 | |
129 // Updates relevant values of the app object in the registry after a | |
130 // successful update check, which is either a "noupdate" response or an update | |
131 // available even if it will not be applied. | |
132 void PersistSuccessfulUpdateCheckResponse(const App& app, | |
133 bool is_update_available); | |
134 | |
135 // Persists relevant values of the app object in the registry after a | |
136 // successful install. | |
137 void PersistSuccessfulInstall(const App& app); | |
138 | |
139 // Copies product version and language from client key to client state key. | |
140 // Returns S_OK when the client key does not exist. | |
141 HRESULT SynchronizeClientState(const GUID& app_guid); | |
142 | |
143 // Updates application state after an update check request has been | |
144 // successfully sent to the server. | |
145 HRESULT PersistUpdateCheckSuccessfullySent( | |
146 const App& app, | |
147 int elapsed_seconds_since_day_start); | |
148 | |
149 // TODO(omaha3): Most of these methods should be eliminated or moved (i.e. to | |
150 // App) since we only want to write the registry in one or two functions. | |
151 // Can't make them all private in the meantime because unit tests use them. | |
152 | |
153 // Clears the OEM-installed flag for the apps. | |
154 void ClearOemInstalled(const AppIdVector& app_ids); | |
155 | |
156 // Obtains usage stats information from the stored information about update | |
157 // available events for the app. | |
158 void ReadUpdateAvailableStats(const GUID& app_guid, | |
159 DWORD* update_responses, | |
160 DWORD64* time_since_first_response_ms); | |
161 | |
162 // Removes the ClientState and ClientStateMedium keys for the application. | |
163 HRESULT RemoveClientState(const GUID& app_guid); | |
164 | |
165 // Returns a reference to the lock that ensures the registry is in a stable | |
166 // state (i.e. no app is being installed). Acquire this lock before calling | |
167 // read functions if you require a consistent/stable snapshot of the system | |
168 // (for example, to determine whether Omaha should install). Because this | |
169 // lock is held throughout app install, the Lock() call could block for | |
170 // seconds or more. | |
171 Lockable& GetRegistryStableStateLock() { return registry_stable_state_lock_; } | |
172 | |
173 // Gets the time since InstallTime was written. Returns 0 if InstallTime | |
174 // could not be read. This could occur if the app is not already installed or | |
175 // there is no valid install time in the registry, which can occur for apps | |
176 // installed before installtime was implemented. | |
177 uint32 GetInstallTimeDiffSec(const GUID& app_guid) const; | |
178 | |
179 #if 0 | |
180 HRESULT RegisterProduct(const GUID& product_guid, | |
181 const CString& product_name); | |
182 HRESULT UnregisterProduct(const GUID& product_guid); | |
183 #endif | |
184 | |
185 bool IsAppRegistered(const CString& app_id) const; | |
186 bool IsAppUninstalled(const CString& app_id) const; | |
187 bool IsAppOemInstalledAndEulaAccepted(const CString& app_id) const; | |
188 | |
189 private: | |
190 explicit AppManager(bool is_machine); | |
191 ~AppManager() {} | |
192 | |
193 bool InitializeRegistryLock(); | |
194 | |
195 CString GetClientKeyName(const GUID& app_guid) const; | |
196 CString GetClientStateKeyName(const GUID& app_guid) const; | |
197 CString GetClientStateMediumKeyName(const GUID& app_guid) const; | |
198 | |
199 // Opens the app's Client key for read access. | |
200 HRESULT OpenClientKey(const GUID& app_guid, RegKey* client_key) const; | |
201 // Opens the app's ClientState key with the specified access. | |
202 HRESULT OpenClientStateKey(const GUID& app_guid, | |
203 REGSAM sam_desired, | |
204 RegKey* client_state_key) const; | |
205 // Creates the app's ClientState key. | |
206 HRESULT CreateClientStateKey(const GUID& app_guid, RegKey* client_state_key); | |
207 | |
208 // Write the TT Token with what the server returned. | |
209 HRESULT SetTTToken(const App& app); | |
210 | |
211 // Stores information about the update available event for the app. | |
212 // Call each time an update is available. | |
213 void UpdateUpdateAvailableStats(const GUID& app_guid); | |
214 | |
215 HRESULT ClearInstallationId(const App& app); | |
216 | |
217 // Writes the day start time when last active ping/roll call happened to | |
218 // registry. | |
219 void SetLastPingDayStartTime(const App& app, | |
220 int elapsed_seconds_since_day_start); | |
221 | |
222 bool IsRegistryStableStateLockedByCaller() const { | |
223 return ::GetCurrentThreadId() == registry_stable_state_lock_.GetOwner(); | |
224 } | |
225 | |
226 const bool is_machine_; | |
227 | |
228 // Locks. | |
229 // If it is going to be acquired, registry_stable_state_lock_ should always be | |
230 // acquired before registry_access_lock_. | |
231 // registry_access_lock_ is only ever acquired by this class and app | |
232 // installers. | |
233 | |
234 // Ensures that each function's access is on a stable snapshot of the | |
235 // registry, excluding values modified by the installer. | |
236 GLock registry_access_lock_; | |
237 | |
238 // Ensures the registry is in a stable state (i.e. all apps are fully | |
239 // installed and no installer is running that might be modifying the | |
240 // registry.) Uninstalls are still an issue unless the app uninstaller informs | |
241 // Omaha that it is uninstalling the app. | |
242 LLock registry_stable_state_lock_; | |
243 | |
244 static AppManager* instance_; | |
245 | |
246 friend class RunRegistrationUpdateHooksFunc; | |
247 friend class AppManagerTestBase; | |
248 | |
249 DISALLOW_COPY_AND_ASSIGN(AppManager); | |
250 }; | |
251 | |
252 } // namespace omaha | |
253 | |
254 #endif // OMAHA_GOOPDATE_APP_MANAGER_H_ | |
OLD | NEW |