 Chromium Code Reviews
 Chromium Code Reviews Issue 61643015:
  Adds imageWriterPrivate support for Windows  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 61643015:
  Adds imageWriterPrivate support for Windows  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 base::Bind(&Operation::SetProgress, | 87 base::Bind(&Operation::SetProgress, | 
| 91 this, | 88 this, | 
| 92 progress)); | 89 progress)); | 
| 93 return; | 90 return; | 
| 94 } | 91 } | 
| 95 | 92 | 
| 96 if (IsCancelled()) { | 93 if (IsCancelled()) { | 
| 97 return; | 94 return; | 
| 98 } | 95 } | 
| 99 | 96 | 
| 100 progress_ = progress; | 97 if (progress > progress_) { | 
| 98 progress_ = progress; | |
| 101 | 99 | 
| 102 BrowserThread::PostTask( | 100 BrowserThread::PostTask(BrowserThread::UI, | 
| 103 BrowserThread::UI, | 101 FROM_HERE, | 
| 104 FROM_HERE, | 102 base::Bind(&OperationManager::OnProgress, | 
| 105 base::Bind(&OperationManager::OnProgress, | 103 manager_, | 
| 106 manager_, | 104 extension_id_, | 
| 107 extension_id_, | 105 stage_, | 
| 108 stage_, | 106 progress_)); | 
| 109 progress_)); | 107 } | 
| 110 } | 108 } | 
| 111 | 109 | 
| 112 void Operation::SetStage(image_writer_api::Stage stage) { | 110 void Operation::SetStage(image_writer_api::Stage stage) { | 
| 113 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 111 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 
| 114 BrowserThread::PostTask( | 112 BrowserThread::PostTask( | 
| 115 BrowserThread::FILE, | 113 BrowserThread::FILE, | 
| 116 FROM_HERE, | 114 FROM_HERE, | 
| 117 base::Bind(&Operation::SetStage, | 115 base::Bind(&Operation::SetStage, | 
| 118 this, | 116 this, | 
| 119 stage)); | 117 stage)); | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 137 progress_)); | 135 progress_)); | 
| 138 } | 136 } | 
| 139 | 137 | 
| 140 void Operation::Finish() { | 138 void Operation::Finish() { | 
| 141 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 139 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 
| 142 BrowserThread::PostTask(BrowserThread::FILE, | 140 BrowserThread::PostTask(BrowserThread::FILE, | 
| 143 FROM_HERE, | 141 FROM_HERE, | 
| 144 base::Bind(&Operation::Finish, this)); | 142 base::Bind(&Operation::Finish, this)); | 
| 145 return; | 143 return; | 
| 146 } | 144 } | 
| 147 DVLOG(1) << "Write operation complete."; | |
| 148 | 145 | 
| 149 CleanUp(); | 146 CleanUp(); | 
| 150 | 147 | 
| 151 BrowserThread::PostTask( | 148 BrowserThread::PostTask( | 
| 152 BrowserThread::UI, | 149 BrowserThread::UI, | 
| 153 FROM_HERE, | 150 FROM_HERE, | 
| 154 base::Bind(&OperationManager::OnComplete, | 151 base::Bind(&OperationManager::OnComplete, | 
| 155 manager_, | 152 manager_, | 
| 156 extension_id_)); | 153 extension_id_)); | 
| 157 } | 154 } | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 171 void Operation::CleanUp() { | 168 void Operation::CleanUp() { | 
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 170 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 
| 174 it != cleanup_functions_.end(); | 171 it != cleanup_functions_.end(); | 
| 175 ++it) { | 172 ++it) { | 
| 176 it->Run(); | 173 it->Run(); | 
| 177 } | 174 } | 
| 178 cleanup_functions_.clear(); | 175 cleanup_functions_.clear(); | 
| 179 } | 176 } | 
| 180 | 177 | 
| 178 void Operation::StartImageWriterClient() { | |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 180 if (!image_writer_client_) { | |
| 181 image_writer_client_ = new ImageWriterClient(); | |
| 182 | |
| 183 AddCleanUpFunction(base::Bind(&Operation::StopImageWriterClient, this)); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void Operation::StopImageWriterClient() { | |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 189 BrowserThread::PostTask( | |
| 190 BrowserThread::IO, | |
| 191 FROM_HERE, | |
| 192 base::Bind(&ImageWriterClient::Shutdown, image_writer_client_)); | |
| 193 } | |
| 194 | |
| 195 void Operation::WriteImageProgress(int64 total_bytes, int64 curr_bytes) { | |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 197 if (IsCancelled()) { | |
| 198 return; | |
| 199 } | |
| 200 | |
| 201 int progress = kProgressComplete * curr_bytes / total_bytes; | |
| 
Lei Zhang
2014/02/15 00:26:59
Can this overflow? Same with line 338.
 
Drew Haven
2014/02/15 01:23:58
My understanding is that kProgressComplete * curr_
 
Lei Zhang
2014/02/15 01:41:50
Do you trust the utility process to send you a val
 
Jorge Lucangeli Obes
2014/02/15 01:43:19
I would add CHECKS for both values to be positive,
 | |
| 202 | |
| 203 if (progress > GetProgress()) { | |
| 204 SetProgress(progress); | |
| 205 } | |
| 206 } | |
| 207 | |
| 181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { | 208 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { | 
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 183 if (IsCancelled()) { | 210 if (IsCancelled()) { | 
| 184 return; | 211 return; | 
| 185 } | 212 } | 
| 186 | 213 | 
| 187 DVLOG(1) << "Starting unzip stage for " << zip_path->value(); | |
| 188 | |
| 189 SetStage(image_writer_api::STAGE_UNZIP); | 214 SetStage(image_writer_api::STAGE_UNZIP); | 
| 190 | 215 | 
| 191 base::FilePath tmp_dir; | 216 base::FilePath tmp_dir; | 
| 192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), | 217 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), | 
| 193 FILE_PATH_LITERAL("image_writer"), | 218 FILE_PATH_LITERAL("image_writer"), | 
| 194 &tmp_dir)) { | 219 &tmp_dir)) { | 
| 195 Error(error::kTempDirError); | 220 Error(error::kTempDirError); | 
| 196 return; | 221 return; | 
| 197 } | 222 } | 
| 198 | 223 | 
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 } | 353 } | 
| 329 | 354 | 
| 330 void Operation::OnUnzipFailure() { | 355 void Operation::OnUnzipFailure() { | 
| 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 332 Error(error::kUnzipGenericError); | 357 Error(error::kUnzipGenericError); | 
| 333 } | 358 } | 
| 334 | 359 | 
| 335 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 360 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 337 | 362 | 
| 338 int progress_percent = 100 * progress_bytes / total_bytes; | 363 int progress_percent = kProgressComplete * progress_bytes / total_bytes; | 
| 339 SetProgress(progress_percent); | 364 SetProgress(progress_percent); | 
| 340 } | 365 } | 
| 341 | 366 | 
| 342 } // namespace image_writer | 367 } // namespace image_writer | 
| 343 } // namespace extensions | 368 } // namespace extensions | 
| OLD | NEW |