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_WORKER_H_ | |
17 #define OMAHA_GOOPDATE_WORKER_H_ | |
18 | |
19 #include <windows.h> | |
20 #include <atlstr.h> | |
21 #include "base/basictypes.h" | |
22 #include "omaha/base/program_instance.h" | |
23 #include "base/scoped_ptr.h" | |
24 #include "omaha/base/shutdown_callback.h" | |
25 #include "omaha/base/shutdown_handler.h" | |
26 #include "omaha/base/wtl_atlapp_wrapper.h" | |
27 #include "third_party/bar/shared_ptr.h" | |
28 | |
29 namespace omaha { | |
30 | |
31 namespace xml { | |
32 | |
33 class UpdateRequest; | |
34 class UpdateResponse; | |
35 | |
36 } // namespace xml | |
37 | |
38 class AppBundle; | |
39 class DownloadManagerInterface; | |
40 class InstallManagerInterface; | |
41 class Model; | |
42 class Package; | |
43 class Reactor; | |
44 | |
45 // Limited subset of Worker interface that the Model needs. | |
46 class WorkerModelInterface { | |
47 public: | |
48 virtual ~WorkerModelInterface() {} | |
49 virtual HRESULT CheckForUpdateAsync(AppBundle* app_bundle) = 0; | |
50 virtual HRESULT DownloadAsync(AppBundle* app_bundle) = 0; | |
51 virtual HRESULT DownloadAndInstallAsync(AppBundle* app_bundle) = 0; | |
52 virtual HRESULT UpdateAllAppsAsync(AppBundle* app_bundle) = 0; | |
53 virtual HRESULT DownloadPackageAsync(Package* package) = 0; | |
54 virtual HRESULT Stop(AppBundle* app_bundle) = 0; | |
55 virtual HRESULT Pause(AppBundle* app_bundle) = 0; | |
56 virtual HRESULT Resume(AppBundle* app_bundle) = 0; | |
57 virtual HRESULT GetPackage(const Package* package, const CString& dir) = 0; | |
58 virtual bool IsPackageAvailable(const Package* package) const = 0; | |
59 virtual HRESULT PurgeAppLowerVersions(const CString& app_id, | |
60 const CString& version) = 0; | |
61 virtual int Lock() = 0; | |
62 virtual int Unlock() = 0; | |
63 }; | |
64 | |
65 // Worker is a singleton. | |
66 class Worker : public WorkerModelInterface, public ShutdownCallback { | |
67 public: | |
68 // Instance, Initialize, and DeleteInstance methods below are not thread safe. | |
69 // The caller must initialize and cleanup the instance before going | |
70 // multithreaded. | |
71 | |
72 // Gets the singleton instance of the class. | |
73 static Worker& Instance(); | |
74 | |
75 // Initializes the instance. | |
76 HRESULT Initialize(bool is_machine); | |
77 | |
78 // Cleans up the class instance. | |
79 static void DeleteInstance(); | |
80 | |
81 HRESULT Run(); | |
82 | |
83 HRESULT Shutdown(); | |
84 HRESULT InitializeShutDownHandler(); | |
85 | |
86 // TODO(omaha): not clear how to make this an atomic operation. Consider | |
87 // making the model instance a bare pointer instead of smart pointer. | |
88 Model* model() { return model_.get(); } | |
89 const Model* model() const { return model_.get(); } | |
90 | |
91 // Initiates an update check for all apps in the bundle. | |
92 virtual HRESULT CheckForUpdateAsync(AppBundle* app_bundle); | |
93 | |
94 // Initiates download of files necessary to install all apps in the bundle. | |
95 virtual HRESULT DownloadAsync(AppBundle* app_bundle); | |
96 | |
97 // Initiates Download, if necessary, and install all app in the bundle. | |
98 virtual HRESULT DownloadAndInstallAsync(AppBundle* app_bundle); | |
99 | |
100 // Initiates an update of all registered apps and performs periodic tasks | |
101 // related to all apps. Primarily for use by Omaha's /ua client. Includes | |
102 // update check, download and install. | |
103 virtual HRESULT UpdateAllAppsAsync(AppBundle* app_bundle); | |
104 | |
105 // Initiates download of a package. | |
106 virtual HRESULT DownloadPackageAsync(Package* package); | |
107 | |
108 virtual HRESULT Stop(AppBundle* app_bundle); | |
109 virtual HRESULT Pause(AppBundle* app_bundle); | |
110 virtual HRESULT Resume(AppBundle* app_bundle); | |
111 | |
112 virtual HRESULT GetPackage(const Package*, const CString& dir); | |
113 | |
114 virtual bool IsPackageAvailable(const Package* package) const; | |
115 | |
116 virtual HRESULT PurgeAppLowerVersions(const CString& app_id, | |
117 const CString& version); | |
118 | |
119 // Locks and unlocks the server module by incrementing or decrementing | |
120 // the lock count of the module. | |
121 virtual int Lock(); | |
122 virtual int Unlock(); | |
123 | |
124 private: | |
125 Worker(); | |
126 ~Worker(); | |
127 | |
128 HRESULT DoRun(); | |
129 | |
130 // These functions execute code in the thread pool. They hold an outstanding | |
131 // reference to the application bundle to prevent the application bundle | |
132 // object from being deleted before the functions complete. | |
133 void CheckForUpdate(shared_ptr<AppBundle> app_bundle); | |
134 void Download(shared_ptr<AppBundle> app_bundle); | |
135 void DownloadAndInstall(shared_ptr<AppBundle> app_bundle); | |
136 void DownloadPackage(shared_ptr<AppBundle> app_bundle, Package* package); | |
137 void UpdateAllApps(shared_ptr<AppBundle> app_bundle); | |
138 | |
139 // These functions do the work for the corresponding functions but do not call | |
140 // CompleteAsyncCall(). | |
141 void CheckForUpdateHelper(AppBundle* app_bundle, bool* is_check_successful); | |
142 void DownloadAndInstallHelper(AppBundle* app_bundle); | |
143 | |
144 // Stops and destroys the Worker and its members. | |
145 // TODO(omaha): rename this as it overloads WorkerModelInterface::Stop. | |
146 void Stop(); | |
147 | |
148 bool EnsureSingleInstance(); | |
149 | |
150 void CollectAmbientUsageStats(); | |
151 | |
152 void DoPreUpdateCheck(AppBundle* app_bundle, | |
153 xml::UpdateRequest* update_request); | |
154 | |
155 HRESULT CacheOfflinePackages(AppBundle* app_bundle); | |
156 | |
157 HRESULT DoUpdateCheck(AppBundle* app_bundle, | |
158 const xml::UpdateRequest* update_request, | |
159 xml::UpdateResponse* update_response); | |
160 void DoPostUpdateCheck(AppBundle* app_bundle, | |
161 HRESULT update_check_result, | |
162 xml::UpdateResponse* update_response); | |
163 | |
164 HRESULT QueueDeferredFunctionCall0( | |
165 AppBundle* app_bundle, | |
166 void (Worker::*deferred_function)(shared_ptr<AppBundle>)); | |
167 | |
168 template <typename P1> | |
169 HRESULT QueueDeferredFunctionCall1( | |
170 AppBundle* app_bundle, | |
171 P1 p1, | |
172 void (Worker::*deferred_function)(shared_ptr<AppBundle>, P1)); | |
173 | |
174 void WriteEventLog(int event_type, | |
175 int event_id, | |
176 const CString& event_description, | |
177 const CString& event_text); | |
178 | |
179 bool is_machine_; | |
180 scoped_ptr<ProgramInstance> single_instance_; | |
181 scoped_ptr<Reactor> reactor_; | |
182 scoped_ptr<ShutdownHandler> shutdown_handler_; | |
183 scoped_ptr<Model> model_; | |
184 scoped_ptr<DownloadManagerInterface> download_manager_; | |
185 scoped_ptr<InstallManagerInterface> install_manager_; | |
186 | |
187 CMessageLoop message_loop_; | |
188 | |
189 static Worker* const kInvalidInstance; | |
190 static Worker* instance_; | |
191 | |
192 friend class WorkerTest; | |
193 | |
194 DISALLOW_COPY_AND_ASSIGN(Worker); | |
195 }; | |
196 | |
197 // For unittests, where creation and termination of Worker instances may be | |
198 // required, the implementation disables the dead reference detection. This | |
199 // forces an inline of the code below for unit tests, so different behavior | |
200 // can be achieved, even though the rest of the implementation compiles in | |
201 // a library. It is somehow brittle but good enough for now. | |
202 | |
203 #ifdef UNITTEST | |
204 __forceinline | |
205 #else | |
206 inline | |
207 #endif | |
208 void Worker::DeleteInstance() { | |
209 delete instance_; | |
210 #ifdef UNITTEST | |
211 instance_ = NULL; | |
212 #else | |
213 instance_ = kInvalidInstance; | |
214 #endif | |
215 } | |
216 | |
217 } // namespace omaha | |
218 | |
219 #endif // OMAHA_GOOPDATE_WORKER_H_ | |
OLD | NEW |