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_DOWNLOAD_MANAGER_H_ | |
17 #define OMAHA_GOOPDATE_DOWNLOAD_MANAGER_H_ | |
18 | |
19 #include <windows.h> | |
20 #include <atlstr.h> | |
21 #include <vector> | |
22 #include "base/basictypes.h" | |
23 #include "base/scoped_ptr.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 class App; | |
28 struct ErrorContext; | |
29 class HttpClient; | |
30 struct Lockable; // TODO(omaha): make Lockable a class. | |
31 class NetworkRequest; | |
32 class Package; | |
33 class PackageCache; | |
34 | |
35 // Public interface for the DownloadManager. | |
36 class DownloadManagerInterface { | |
37 public: | |
38 virtual ~DownloadManagerInterface() {} | |
39 virtual HRESULT Initialize() = 0; | |
40 virtual HRESULT PurgeAppLowerVersions(const CString& app_id, | |
41 const CString& version) = 0; | |
42 virtual HRESULT CachePackage(const Package* package, | |
43 const CString* filename_path) = 0; | |
44 virtual HRESULT DownloadApp(App* app) = 0; | |
45 virtual HRESULT DownloadPackage(Package* package) = 0; | |
46 virtual HRESULT GetPackage(const Package* package, | |
47 const CString& dir) const = 0; | |
48 virtual bool IsPackageAvailable(const Package* package) const = 0; | |
49 virtual void Cancel(App* app) = 0; | |
50 virtual void CancelAll() = 0; | |
51 virtual bool IsBusy() const = 0; | |
52 }; | |
53 | |
54 class DownloadManager : public DownloadManagerInterface { | |
55 public: | |
56 explicit DownloadManager(bool is_machine); | |
57 virtual ~DownloadManager(); | |
58 | |
59 virtual HRESULT Initialize(); | |
60 | |
61 virtual HRESULT PurgeAppLowerVersions(const CString& app_id, | |
62 const CString& version); | |
63 | |
64 virtual HRESULT CachePackage(const Package* package, | |
65 const CString* filename_path); | |
66 | |
67 // Downloads the specified app and stores its packages in the package cache. | |
68 // | |
69 // This is a blocking call. All errors are reported through the return value. | |
70 // Callers may use GetMessageForError() to convert this error value to an | |
71 // error message. Progress is reported via the NetworkRequestCallback | |
72 // method on the Package objects. | |
73 virtual HRESULT DownloadApp(App* app); | |
74 | |
75 // Downloads the specified package and stores it in the package cache. | |
76 virtual HRESULT DownloadPackage(Package* package); | |
77 | |
78 // Retrieves a package from the cache, if the package is locally available. | |
79 virtual HRESULT GetPackage(const Package* package, const CString& dir) const; | |
80 | |
81 // Returns true if the specified package is in the package cache. | |
82 virtual bool IsPackageAvailable(const Package* package) const; | |
83 | |
84 // Cancels the download of specified app and makes DownloadApp return to the | |
85 // caller at some point in the future. Cancel can be called multiple times | |
86 // until the DownloadApp returns. | |
87 virtual void Cancel(App* app); | |
88 | |
89 // Cancels download of all apps currently downloading. | |
90 virtual void CancelAll(); | |
91 | |
92 // Returns true if applications are downloading. | |
93 virtual bool IsBusy() const; | |
94 | |
95 // Returns a formatted message for the specified error in given language. | |
96 static CString GetMessageForError(const ErrorContext& error_context, | |
97 const CString& language); | |
98 | |
99 private: | |
100 // Maintains per-app download state. | |
101 class State { | |
102 public: | |
103 State(App* app, NetworkRequest* network_request); | |
104 ~State(); | |
105 | |
106 App* app() const { return app_; } | |
107 | |
108 NetworkRequest* network_request() const; | |
109 | |
110 HRESULT CancelNetworkRequest(); | |
111 | |
112 private: | |
113 // Not owned by this object. | |
114 App* app_; | |
115 | |
116 scoped_ptr<NetworkRequest> network_request_; | |
117 | |
118 DISALLOW_EVIL_CONSTRUCTORS(State); | |
119 }; | |
120 | |
121 // Creates a download state corresponding to the app. The state object is | |
122 // owned by the download manager. A pointer to the state object is returned | |
123 // to the caller. | |
124 HRESULT CreateStateForApp(App* app, State** state); | |
125 | |
126 HRESULT DeleteStateForApp(App* app); | |
127 | |
128 HRESULT DoDownloadPackage(Package* package, State* state); | |
129 | |
130 bool is_machine() const; | |
131 | |
132 CString package_cache_root() const; | |
133 | |
134 PackageCache* package_cache(); | |
135 const PackageCache* package_cache() const; | |
136 | |
137 const Lockable& lock() const; | |
138 | |
139 // Returns the full path to a unique filename. | |
140 static HRESULT BuildUniqueFileName(const CString& filename, | |
141 CString* unique_filename); | |
142 | |
143 // Locks shared instance state for concurrent downloads. This lock is | |
144 // owned by this class. | |
145 mutable Lockable* volatile lock_; | |
146 | |
147 bool is_machine_; | |
148 | |
149 // The root of the package_cache. | |
150 CString package_cache_root_; | |
151 | |
152 std::vector<State*> download_state_; | |
153 | |
154 scoped_ptr<PackageCache> package_cache_; | |
155 | |
156 friend class DownloadManagerTest; | |
157 DISALLOW_EVIL_CONSTRUCTORS(DownloadManager); | |
158 }; | |
159 | |
160 } // namespace omaha | |
161 | |
162 #endif // OMAHA_GOOPDATE_DOWNLOAD_MANAGER_H_ | |
OLD | NEW |