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 15 matching lines...) Expand all Loading... |
26 Operation::Operation(base::WeakPtr<OperationManager> manager, | 26 Operation::Operation(base::WeakPtr<OperationManager> manager, |
27 const ExtensionId& extension_id, | 27 const ExtensionId& extension_id, |
28 const std::string& device_path) | 28 const std::string& device_path) |
29 : manager_(manager), | 29 : manager_(manager), |
30 extension_id_(extension_id), | 30 extension_id_(extension_id), |
31 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
32 device_path_(base::FilePath::FromUTF8Unsafe(device_path)), | 32 device_path_(base::FilePath::FromUTF8Unsafe(device_path)), |
33 #else | 33 #else |
34 device_path_(device_path), | 34 device_path_(device_path), |
35 #endif | 35 #endif |
| 36 #if defined(OS_LINUX) && !defined(CHROMEOS) |
| 37 image_file_(base::kInvalidPlatformFileValue), |
| 38 device_file_(base::kInvalidPlatformFileValue), |
| 39 #endif |
36 stage_(image_writer_api::STAGE_UNKNOWN), | 40 stage_(image_writer_api::STAGE_UNKNOWN), |
37 progress_(0) { | 41 progress_(0) { |
38 } | 42 } |
39 | 43 |
40 Operation::~Operation() {} | 44 Operation::~Operation() {} |
41 | 45 |
42 void Operation::Cancel() { | 46 void Operation::Cancel() { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
44 | 48 |
45 stage_ = image_writer_api::STAGE_NONE; | 49 stage_ = image_writer_api::STAGE_NONE; |
46 | 50 |
47 CleanUp(); | 51 CleanUp(); |
48 } | 52 } |
49 | 53 |
50 void Operation::Abort() { | 54 void Operation::Abort() { |
51 Error(error::kAborted); | 55 Error(error::kAborted); |
52 } | 56 } |
53 | 57 |
54 int Operation::GetProgress() { | 58 int Operation::GetProgress() { |
55 return progress_; | 59 return progress_; |
56 } | 60 } |
57 | 61 |
58 image_writer_api::Stage Operation::GetStage() { | 62 image_writer_api::Stage Operation::GetStage() { |
59 return stage_; | 63 return stage_; |
60 } | 64 } |
61 | 65 |
62 #if !defined(OS_CHROMEOS) | |
63 void Operation::SetUtilityClientForTesting( | |
64 scoped_refptr<ImageWriterUtilityClient> client) { | |
65 image_writer_client_ = client; | |
66 } | |
67 #endif | |
68 | |
69 void Operation::Start() { | 66 void Operation::Start() { |
70 #if defined(OS_CHROMEOS) | 67 #if defined(OS_CHROMEOS) |
71 if (!temp_dir_.CreateUniqueTempDirUnderPath( | 68 if (!temp_dir_.CreateUniqueTempDirUnderPath( |
72 base::FilePath(kChromeOSTempRoot))) { | 69 base::FilePath(kChromeOSTempRoot))) { |
73 #else | 70 #else |
74 if (!temp_dir_.CreateUniqueTempDir()) { | 71 if (!temp_dir_.CreateUniqueTempDir()) { |
75 #endif | 72 #endif |
76 Error(error::kTempDirError); | 73 Error(error::kTempDirError); |
77 return; | 74 return; |
78 } | 75 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 zip::ZipReader::EntryInfo* entry_info = zip_reader_.current_entry_info(); | 110 zip::ZipReader::EntryInfo* entry_info = zip_reader_.current_entry_info(); |
114 if (entry_info) { | 111 if (entry_info) { |
115 image_path_ = temp_dir_.path().Append(entry_info->file_path().BaseName()); | 112 image_path_ = temp_dir_.path().Append(entry_info->file_path().BaseName()); |
116 } else { | 113 } else { |
117 Error(error::kTempDirError); | 114 Error(error::kTempDirError); |
118 return; | 115 return; |
119 } | 116 } |
120 | 117 |
121 zip_reader_.ExtractCurrentEntryToFilePathAsync( | 118 zip_reader_.ExtractCurrentEntryToFilePathAsync( |
122 image_path_, | 119 image_path_, |
123 base::Bind(&Operation::CompleteAndContinue, this, continuation), | 120 base::Bind(&Operation::OnUnzipSuccess, this, continuation), |
124 base::Bind(&Operation::OnUnzipFailure, this), | 121 base::Bind(&Operation::OnUnzipFailure, this), |
125 base::Bind(&Operation::OnUnzipProgress, | 122 base::Bind(&Operation::OnUnzipProgress, |
126 this, | 123 this, |
127 zip_reader_.current_entry_info()->original_size())); | 124 zip_reader_.current_entry_info()->original_size())); |
128 } | 125 } |
129 | 126 |
130 void Operation::Finish() { | 127 void Operation::Finish() { |
131 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 128 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
132 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
133 BrowserThread::FILE, FROM_HERE, base::Bind(&Operation::Finish, this)); | 130 BrowserThread::FILE, FROM_HERE, base::Bind(&Operation::Finish, this)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (progress <= progress_) { | 174 if (progress <= progress_) { |
178 return; | 175 return; |
179 } | 176 } |
180 | 177 |
181 if (IsCancelled()) { | 178 if (IsCancelled()) { |
182 return; | 179 return; |
183 } | 180 } |
184 | 181 |
185 progress_ = progress; | 182 progress_ = progress; |
186 | 183 |
187 BrowserThread::PostTask(BrowserThread::UI, | 184 BrowserThread::PostTask( |
188 FROM_HERE, | 185 BrowserThread::UI, |
189 base::Bind(&OperationManager::OnProgress, | 186 FROM_HERE, |
190 manager_, | 187 base::Bind(&OperationManager::OnProgress, |
191 extension_id_, | 188 manager_, |
192 stage_, | 189 extension_id_, |
193 progress_)); | 190 stage_, |
| 191 progress_)); |
194 } | 192 } |
195 | 193 |
196 void Operation::SetStage(image_writer_api::Stage stage) { | 194 void Operation::SetStage(image_writer_api::Stage stage) { |
197 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 195 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
198 BrowserThread::PostTask( | 196 BrowserThread::PostTask( |
199 BrowserThread::FILE, | 197 BrowserThread::FILE, |
200 FROM_HERE, | 198 FROM_HERE, |
201 base::Bind(&Operation::SetStage, | 199 base::Bind(&Operation::SetStage, |
202 this, | 200 this, |
203 stage)); | 201 stage)); |
(...skipping 21 matching lines...) Expand all Loading... |
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
226 | 224 |
227 return stage_ == image_writer_api::STAGE_NONE; | 225 return stage_ == image_writer_api::STAGE_NONE; |
228 } | 226 } |
229 | 227 |
230 void Operation::AddCleanUpFunction(const base::Closure& callback) { | 228 void Operation::AddCleanUpFunction(const base::Closure& callback) { |
231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
232 cleanup_functions_.push_back(callback); | 230 cleanup_functions_.push_back(callback); |
233 } | 231 } |
234 | 232 |
235 void Operation::CompleteAndContinue(const base::Closure& continuation) { | |
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
237 SetProgress(kProgressComplete); | |
238 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | |
239 } | |
240 | |
241 #if !defined(OS_CHROMEOS) | |
242 void Operation::StartUtilityClient() { | |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
244 if (!image_writer_client_) { | |
245 image_writer_client_ = new ImageWriterUtilityClient(); | |
246 AddCleanUpFunction(base::Bind(&Operation::StopUtilityClient, this)); | |
247 } | |
248 } | |
249 | |
250 void Operation::StopUtilityClient() { | |
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
252 BrowserThread::PostTask( | |
253 BrowserThread::IO, | |
254 FROM_HERE, | |
255 base::Bind(&ImageWriterUtilityClient::Shutdown, image_writer_client_)); | |
256 } | |
257 | |
258 void Operation::WriteImageProgress(int64 total_bytes, int64 curr_bytes) { | |
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
260 if (IsCancelled()) { | |
261 return; | |
262 } | |
263 | |
264 int progress = kProgressComplete * curr_bytes / total_bytes; | |
265 | |
266 if (progress > GetProgress()) { | |
267 SetProgress(progress); | |
268 } | |
269 } | |
270 #endif | |
271 | |
272 void Operation::GetMD5SumOfFile( | 233 void Operation::GetMD5SumOfFile( |
273 const base::FilePath& file_path, | 234 const base::FilePath& file_path, |
274 int64 file_size, | 235 int64 file_size, |
275 int progress_offset, | 236 int progress_offset, |
276 int progress_scale, | 237 int progress_scale, |
277 const base::Callback<void(const std::string&)>& callback) { | 238 const base::Callback<void(const std::string&)>& callback) { |
278 if (IsCancelled()) { | 239 if (IsCancelled()) { |
279 return; | 240 return; |
280 } | 241 } |
281 | 242 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 // Skip closing the file. | 319 // Skip closing the file. |
359 return; | 320 return; |
360 } else { | 321 } else { |
361 // We didn't read the bytes we expected. | 322 // We didn't read the bytes we expected. |
362 Error(error::kHashReadError); | 323 Error(error::kHashReadError); |
363 } | 324 } |
364 } | 325 } |
365 base::ClosePlatformFile(file); | 326 base::ClosePlatformFile(file); |
366 } | 327 } |
367 | 328 |
| 329 void Operation::OnUnzipSuccess(const base::Closure& continuation) { |
| 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 331 SetProgress(kProgressComplete); |
| 332 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
| 333 } |
| 334 |
368 void Operation::OnUnzipFailure() { | 335 void Operation::OnUnzipFailure() { |
369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
370 Error(error::kUnzipGenericError); | 337 Error(error::kUnzipGenericError); |
371 } | 338 } |
372 | 339 |
373 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 340 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { |
374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
375 | 342 |
376 int progress_percent = kProgressComplete * progress_bytes / total_bytes; | 343 int progress_percent = 100 * progress_bytes / total_bytes; |
377 SetProgress(progress_percent); | 344 SetProgress(progress_percent); |
378 } | 345 } |
379 | 346 |
380 void Operation::CleanUp() { | 347 void Operation::CleanUp() { |
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
382 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 349 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
383 it != cleanup_functions_.end(); | 350 it != cleanup_functions_.end(); |
384 ++it) { | 351 ++it) { |
385 it->Run(); | 352 it->Run(); |
386 } | 353 } |
387 cleanup_functions_.clear(); | 354 cleanup_functions_.clear(); |
388 } | 355 } |
389 | 356 |
390 } // namespace image_writer | 357 } // namespace image_writer |
391 } // namespace extensions | 358 } // namespace extensions |
OLD | NEW |