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