OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
(...skipping 18 matching lines...) Expand all Loading... |
29 const ExtensionId& extension_id, | 29 const ExtensionId& extension_id, |
30 const std::string& storage_unit_id) | 30 const std::string& storage_unit_id) |
31 : manager_(manager), | 31 : manager_(manager), |
32 extension_id_(extension_id), | 32 extension_id_(extension_id), |
33 storage_unit_id_(storage_unit_id), | 33 storage_unit_id_(storage_unit_id), |
34 verify_write_(true), | 34 verify_write_(true), |
35 stage_(image_writer_api::STAGE_UNKNOWN), | 35 stage_(image_writer_api::STAGE_UNKNOWN), |
36 progress_(0) { | 36 progress_(0) { |
37 } | 37 } |
38 | 38 |
39 Operation::~Operation() { | 39 Operation::~Operation() {} |
40 } | |
41 | 40 |
42 void Operation::Cancel() { | 41 void Operation::Cancel() { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
44 | 43 |
45 DVLOG(1) << "Cancelling image writing operation for ext: " << extension_id_; | |
46 | |
47 stage_ = image_writer_api::STAGE_NONE; | 44 stage_ = image_writer_api::STAGE_NONE; |
48 | 45 |
49 CleanUp(); | 46 CleanUp(); |
50 } | 47 } |
51 | 48 |
52 void Operation::Abort() { | 49 void Operation::Abort() { |
53 Error(error::kAborted); | 50 Error(error::kAborted); |
54 } | 51 } |
55 | 52 |
56 int Operation::GetProgress() { | 53 int Operation::GetProgress() { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 progress_)); | 134 progress_)); |
138 } | 135 } |
139 | 136 |
140 void Operation::Finish() { | 137 void Operation::Finish() { |
141 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 138 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
142 BrowserThread::PostTask(BrowserThread::FILE, | 139 BrowserThread::PostTask(BrowserThread::FILE, |
143 FROM_HERE, | 140 FROM_HERE, |
144 base::Bind(&Operation::Finish, this)); | 141 base::Bind(&Operation::Finish, this)); |
145 return; | 142 return; |
146 } | 143 } |
147 DVLOG(1) << "Write operation complete."; | |
148 | 144 |
149 CleanUp(); | 145 CleanUp(); |
150 | 146 |
151 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
152 BrowserThread::UI, | 148 BrowserThread::UI, |
153 FROM_HERE, | 149 FROM_HERE, |
154 base::Bind(&OperationManager::OnComplete, | 150 base::Bind(&OperationManager::OnComplete, |
155 manager_, | 151 manager_, |
156 extension_id_)); | 152 extension_id_)); |
157 } | 153 } |
(...skipping 13 matching lines...) Expand all Loading... |
171 void Operation::CleanUp() { | 167 void Operation::CleanUp() { |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 169 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
174 it != cleanup_functions_.end(); | 170 it != cleanup_functions_.end(); |
175 ++it) { | 171 ++it) { |
176 it->Run(); | 172 it->Run(); |
177 } | 173 } |
178 cleanup_functions_.clear(); | 174 cleanup_functions_.clear(); |
179 } | 175 } |
180 | 176 |
| 177 void Operation::StartImageWriterClient() { |
| 178 if (!image_writer_client_) { |
| 179 image_writer_client_ = new ImageWriterClient(); |
| 180 |
| 181 AddCleanUpFunction(base::Bind(&Operation::StopImageWriterClient, this)); |
| 182 } |
| 183 } |
| 184 |
| 185 void Operation::StopImageWriterClient() { |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 187 |
| 188 BrowserThread::PostTask( |
| 189 BrowserThread::IO, |
| 190 FROM_HERE, |
| 191 base::Bind(&ImageWriterClient::Shutdown, image_writer_client_)); |
| 192 } |
| 193 |
| 194 void Operation::WriteImageProgress(int64 total_bytes, int64 curr_bytes) { |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 196 if (IsCancelled()) { |
| 197 return; |
| 198 } |
| 199 |
| 200 int progress = kProgressComplete * curr_bytes / total_bytes; |
| 201 |
| 202 if (progress > GetProgress()) { |
| 203 SetProgress(progress); |
| 204 } |
| 205 } |
| 206 |
181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { | 207 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
183 if (IsCancelled()) { | 209 if (IsCancelled()) { |
184 return; | 210 return; |
185 } | 211 } |
186 | 212 |
187 DVLOG(1) << "Starting unzip stage for " << zip_path->value(); | |
188 | |
189 SetStage(image_writer_api::STAGE_UNZIP); | 213 SetStage(image_writer_api::STAGE_UNZIP); |
190 | 214 |
191 base::FilePath tmp_dir; | 215 base::FilePath tmp_dir; |
192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), | 216 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), |
193 FILE_PATH_LITERAL("image_writer"), | 217 FILE_PATH_LITERAL("image_writer"), |
194 &tmp_dir)) { | 218 &tmp_dir)) { |
195 Error(error::kTempDirError); | 219 Error(error::kTempDirError); |
196 return; | 220 return; |
197 } | 221 } |
198 | 222 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 } | 352 } |
329 | 353 |
330 void Operation::OnUnzipFailure() { | 354 void Operation::OnUnzipFailure() { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
332 Error(error::kUnzipGenericError); | 356 Error(error::kUnzipGenericError); |
333 } | 357 } |
334 | 358 |
335 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 359 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
337 | 361 |
338 int progress_percent = 100 * progress_bytes / total_bytes; | 362 int progress_percent = kProgressComplete * progress_bytes / total_bytes; |
339 SetProgress(progress_percent); | 363 if (progress_percent > GetProgress()) |
| 364 SetProgress(progress_percent); |
340 } | 365 } |
341 | 366 |
342 } // namespace image_writer | 367 } // namespace image_writer |
343 } // namespace extensions | 368 } // namespace extensions |
OLD | NEW |