OLD | NEW |
| (Empty) |
1 // Copyright 2009-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 #include "omaha/goopdate/package.h" | |
17 #include "omaha/base/debug.h" | |
18 #include "omaha/base/logging.h" | |
19 #include "omaha/base/scoped_ptr_address.h" | |
20 #include "omaha/base/synchronized.h" | |
21 #include "omaha/base/time.h" | |
22 #include "omaha/base/utils.h" | |
23 #include "omaha/goopdate/model.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 Package::Package(AppVersion* app_version) | |
28 : ModelObject(app_version->model()), | |
29 app_version_(app_version), | |
30 expected_size_(0), | |
31 bytes_downloaded_(0), | |
32 bytes_total_(0), | |
33 next_download_retry_time_(0), | |
34 progress_sampler_(5 * kMsPerSec, // Max sample time range. | |
35 1 * kMsPerSec), // Min range for meaningful average. | |
36 is_downloading_(false) { | |
37 } | |
38 | |
39 Package::~Package() { | |
40 } | |
41 | |
42 AppVersion* Package::app_version() { | |
43 __mutexScope(model()->lock()); | |
44 return app_version_; | |
45 } | |
46 | |
47 const AppVersion* Package::app_version() const { | |
48 __mutexScope(model()->lock()); | |
49 return app_version_; | |
50 } | |
51 | |
52 STDMETHODIMP Package::get(BSTR dir) const { | |
53 return model()->GetPackage(this, CString(dir)); | |
54 } | |
55 | |
56 STDMETHODIMP Package::get_isAvailable(VARIANT_BOOL* is_available) const { | |
57 ASSERT1(is_available); | |
58 *is_available = model()->IsPackageAvailable(this); | |
59 return S_OK; | |
60 } | |
61 | |
62 STDMETHODIMP Package::get_filename(BSTR* filename_as_bstr) const { | |
63 __mutexScope(model()->lock()); | |
64 ASSERT1(filename_as_bstr); | |
65 *filename_as_bstr = CComBSTR(filename()).Detach(); | |
66 return S_OK; | |
67 } | |
68 | |
69 // status_text can be NULL. | |
70 // TODO(omaha): Change bytes and bytes_total to uint64. Any logging will | |
71 // need to use %llu. | |
72 void Package::OnProgress(int bytes, | |
73 int bytes_total, | |
74 int status, | |
75 const TCHAR* status_text) { | |
76 __mutexScope(model()->lock()); | |
77 | |
78 UNREFERENCED_PARAMETER(status); | |
79 UNREFERENCED_PARAMETER(status_text); | |
80 ASSERT1(status == WINHTTP_CALLBACK_STATUS_READ_COMPLETE || | |
81 status == WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER); | |
82 | |
83 CORE_LOG(L5, (_T("[Package::OnProgress][bytes %d][bytes_total %d][status %d]") | |
84 _T("[status_text '%s']"), | |
85 bytes, bytes_total, status, status_text)); | |
86 | |
87 // TODO(omaha): What do we do if the following condition - bytes_total | |
88 // is not what we expect - fails? Omaha 2 just divided bytes by bytes_total | |
89 // to come up with the percentage, ignoring size. It didn't have to deal with | |
90 // multiple downloads (much) and pre-determined total download size. | |
91 // As an example, App::GetDownloadProgress() needs to know the expected | |
92 // size before we've downloaded any bytes, but it might also want to know if | |
93 // we are going to download less bytes. This could happen, for example, if | |
94 // a proxy returned an HTML page instead. (I had to disable the first assert | |
95 // because it fails when the actual file does not match the expected size.) | |
96 // Even worse, what if the bytes_total has a different non-zero number on | |
97 // successive calls? | |
98 | |
99 // ASSERT1(bytes_total == 0 || | |
100 // bytes_total == static_cast<int>(expected_size_)); | |
101 ASSERT1(bytes <= bytes_total); | |
102 | |
103 bytes_downloaded_ = bytes; | |
104 bytes_total_ = bytes_total; | |
105 | |
106 progress_sampler_.AddSampleWithCurrentTimeStamp(bytes_downloaded_); | |
107 } | |
108 | |
109 void Package::OnRequestBegin() { | |
110 __mutexScope(model()->lock()); | |
111 next_download_retry_time_ = 0; | |
112 bytes_downloaded_ = 0; | |
113 bytes_total_ = 0; | |
114 progress_sampler_.Reset(); | |
115 } | |
116 | |
117 void Package::OnRequestRetryScheduled(time64 next_download_retry_time) { | |
118 __mutexScope(model()->lock()); | |
119 ASSERT1(next_download_retry_time >= GetCurrent100NSTime()); | |
120 next_download_retry_time_ = next_download_retry_time; | |
121 } | |
122 | |
123 void Package::SetFileInfo(const CString& filename, | |
124 uint64 size, | |
125 const CString& hash) { | |
126 __mutexScope(model()->lock()); | |
127 | |
128 ASSERT1(!filename.IsEmpty()); | |
129 ASSERT1(0 < size); | |
130 ASSERT1(!hash.IsEmpty()); | |
131 | |
132 filename_ = filename; | |
133 expected_size_ = size; | |
134 expected_hash_ = hash; | |
135 } | |
136 | |
137 CString Package::filename() const { | |
138 __mutexScope(model()->lock()); | |
139 ASSERT1(!filename_.IsEmpty()); | |
140 return filename_; | |
141 } | |
142 | |
143 uint64 Package::expected_size() const { | |
144 __mutexScope(model()->lock()); | |
145 return expected_size_; | |
146 } | |
147 | |
148 CString Package::expected_hash() const { | |
149 __mutexScope(model()->lock()); | |
150 ASSERT1(!expected_hash_.IsEmpty()); | |
151 return expected_hash_; | |
152 } | |
153 | |
154 uint64 Package::bytes_downloaded() const { | |
155 __mutexScope(model()->lock()); | |
156 return bytes_downloaded_; | |
157 } | |
158 | |
159 time64 Package::next_download_retry_time() const { | |
160 __mutexScope(model()->lock()); | |
161 return next_download_retry_time_; | |
162 } | |
163 | |
164 LONG Package::GetEstimatedRemainingDownloadTimeMs() const { | |
165 __mutexScope(model()->lock()); | |
166 | |
167 const LONG kUnknownRemainingTime = -1; | |
168 | |
169 if (bytes_total_ == 0) { // Don't know how many bytes to download. | |
170 return kUnknownRemainingTime; | |
171 } | |
172 | |
173 if (bytes_total_ == bytes_downloaded_) { | |
174 return 0; | |
175 } | |
176 | |
177 LONG time_remaining_ms = kUnknownRemainingTime; | |
178 int average_speed = progress_sampler_.GetAverageProgressPerMs(); | |
179 if (average_speed == ProgressSampler<int>::kUnknownProgressPerMs) { | |
180 return kUnknownRemainingTime; | |
181 } | |
182 | |
183 if (bytes_total_ >= bytes_downloaded_ && average_speed > 0) { | |
184 time_remaining_ms = static_cast<LONG>( | |
185 CeilingDivide(bytes_total_ - bytes_downloaded_, average_speed)); | |
186 } | |
187 | |
188 return time_remaining_ms; | |
189 } | |
190 | |
191 STDMETHODIMP PackageWrapper::get(BSTR dir) { | |
192 __mutexScope(model()->lock()); | |
193 return wrapped_obj()->get(dir); | |
194 } | |
195 | |
196 STDMETHODIMP PackageWrapper::get_isAvailable(VARIANT_BOOL* is_available) { | |
197 __mutexScope(model()->lock()); | |
198 return wrapped_obj()->get_isAvailable(is_available); | |
199 } | |
200 | |
201 STDMETHODIMP PackageWrapper::get_filename(BSTR* filename) { | |
202 __mutexScope(model()->lock()); | |
203 return wrapped_obj()->get_filename(filename); | |
204 } | |
205 | |
206 } // namespace omaha | |
OLD | NEW |