| OLD | NEW | 
|---|
|  | 1 // | 
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 2 // Copyright 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 4 // found in the LICENSE file. | 
| 4 | 5 | 
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" | 
| 6 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" | 
| 7 #include "base/threading/worker_pool.h" | 8 #include "base/threading/worker_pool.h" | 
| 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 
| 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 
| 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
     " | 11 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
     " | 
| 11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" | 
| 12 #include "third_party/zlib/google/zip.h" |  | 
| 13 | 13 | 
| 14 namespace extensions { | 14 namespace extensions { | 
| 15 namespace image_writer { | 15 namespace image_writer { | 
| 16 | 16 | 
| 17 using content::BrowserThread; | 17 using content::BrowserThread; | 
| 18 | 18 | 
| 19 namespace { | 19 namespace { | 
| 20 | 20 | 
| 21 const int kMD5BufferSize = 1024; | 21 const int kMD5BufferSize = 1024; | 
| 22 | 22 | 
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 161 void Operation::CleanUp() { | 161 void Operation::CleanUp() { | 
| 162   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 162   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 163   for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 163   for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 
| 164        it != cleanup_functions_.end(); | 164        it != cleanup_functions_.end(); | 
| 165        ++it) { | 165        ++it) { | 
| 166     it->Run(); | 166     it->Run(); | 
| 167   } | 167   } | 
| 168   cleanup_functions_.clear(); | 168   cleanup_functions_.clear(); | 
| 169 } | 169 } | 
| 170 | 170 | 
| 171 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_file) { | 171 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { | 
| 172   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 172   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
| 173   if (IsCancelled()) { | 173   if (IsCancelled()) { | 
| 174     return; | 174     return; | 
| 175   } | 175   } | 
| 176 | 176 | 
| 177   DVLOG(1) << "Starting unzip stage for " << zip_file->value(); | 177   DVLOG(1) << "Starting unzip stage for " << zip_path->value(); | 
| 178 | 178 | 
| 179   SetStage(image_writer_api::STAGE_UNZIP); | 179   SetStage(image_writer_api::STAGE_UNZIP); | 
| 180 | 180 | 
| 181   base::FilePath tmp_dir; | 181   base::FilePath tmp_dir; | 
| 182   if (!file_util::CreateTemporaryDirInDir(zip_file->DirName(), | 182   if (!file_util::CreateTemporaryDirInDir(zip_path->DirName(), | 
| 183                                           FILE_PATH_LITERAL("image_writer"), | 183                                           FILE_PATH_LITERAL("image_writer_"), | 
| 184                                           &tmp_dir)) { | 184                                           &tmp_dir)) { | 
|  | 185     DLOG(ERROR) << "Failed to create temporary directory."; | 
| 185     Error(error::kTempDir); | 186     Error(error::kTempDir); | 
| 186     return; | 187     return; | 
| 187   } | 188   } | 
| 188 | 189 | 
| 189   AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); | 190   AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); | 
| 190 | 191 | 
| 191   if (!zip::Unzip(*zip_file, tmp_dir)) { | 192   if (!file_util::CreateTemporaryFileInDir(tmp_dir, &image_path_)) { | 
|  | 193     DLOG(ERROR) << "Failed create temporary unzip target in " | 
|  | 194                 << tmp_dir.value(); | 
|  | 195     Error(error::kTempDir); | 
|  | 196     return; | 
|  | 197   } | 
|  | 198 | 
|  | 199   if (!(zip_reader_.Open(*zip_path) && | 
|  | 200         zip_reader_.AdvanceToNextEntry() && | 
|  | 201         zip_reader_.OpenCurrentEntryInZip())) { | 
|  | 202     DLOG(ERROR) << "Failed to open zip file."; | 
| 192     Error(error::kUnzip); | 203     Error(error::kUnzip); | 
| 193     return; | 204     return; | 
| 194   } | 205   } | 
| 195 | 206 | 
| 196   base::FileEnumerator file_enumerator(tmp_dir, | 207   zip_reader_.ExtractCurrentEntryToFilePathAsync( | 
| 197                                        false, | 208       image_path_, | 
| 198                                        base::FileEnumerator::FILES); | 209       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 
| 199 | 210       this); | 
| 200   scoped_ptr<base::FilePath> unzipped_file( |  | 
| 201       new base::FilePath(file_enumerator.Next())); |  | 
| 202 |  | 
| 203   if (unzipped_file->empty()) { |  | 
| 204     Error(error::kEmptyUnzip); |  | 
| 205     return; |  | 
| 206   } |  | 
| 207 |  | 
| 208   if (!file_enumerator.Next().empty()) { |  | 
| 209     Error(error::kMultiFileZip); |  | 
| 210     return; |  | 
| 211   } |  | 
| 212 |  | 
| 213   DVLOG(1) << "Successfully unzipped as " << unzipped_file->value(); |  | 
| 214 |  | 
| 215   SetProgress(kProgressComplete); |  | 
| 216 |  | 
| 217   image_path_ = *unzipped_file; |  | 
| 218 |  | 
| 219   BrowserThread::PostTask( |  | 
| 220       BrowserThread::FILE, |  | 
| 221       FROM_HERE, |  | 
| 222       base::Bind(&Operation::WriteStart, |  | 
| 223                  this)); |  | 
| 224 } | 211 } | 
| 225 | 212 | 
| 226 void Operation::GetMD5SumOfFile( | 213 void Operation::GetMD5SumOfFile( | 
| 227     scoped_ptr<base::FilePath> file_path, | 214     scoped_ptr<base::FilePath> file_path, | 
| 228     int64 file_size, | 215     int64 file_size, | 
| 229     int progress_offset, | 216     int progress_offset, | 
| 230     int progress_scale, | 217     int progress_scale, | 
| 231     const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 218     const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 
| 232   if (IsCancelled()) { | 219   if (IsCancelled()) { | 
| 233     return; | 220     return; | 
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 309       scoped_ptr<std::string> hash( | 296       scoped_ptr<std::string> hash( | 
| 310           new std::string(base::MD5DigestToBase16(digest))); | 297           new std::string(base::MD5DigestToBase16(digest))); | 
| 311       callback.Run(hash.Pass()); | 298       callback.Run(hash.Pass()); | 
| 312     } | 299     } | 
| 313   } else {  // len < 0 | 300   } else {  // len < 0 | 
| 314     reader->Close(); | 301     reader->Close(); | 
| 315     Error(error::kReadImage); | 302     Error(error::kReadImage); | 
| 316   } | 303   } | 
| 317 } | 304 } | 
| 318 | 305 | 
|  | 306 void Operation::OnUnzipProgress(int progress) { | 
|  | 307   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
|  | 308   SetProgress(progress); | 
|  | 309 } | 
|  | 310 | 
|  | 311 void Operation::OnUnzipFailed() { | 
|  | 312   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
|  | 313   Error(error::kUnzip); | 
|  | 314 } | 
|  | 315 | 
|  | 316 void Operation::OnUnzipSuccess() { | 
|  | 317   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 
|  | 318   SetProgress(kProgressComplete); | 
|  | 319 | 
|  | 320   if (zip_reader_.HasMore()) { | 
|  | 321     Error(error::kMultiFileZip); | 
|  | 322   } else { | 
|  | 323     WriteStart(); | 
|  | 324   } | 
|  | 325 } | 
|  | 326 | 
| 319 }  // namespace image_writer | 327 }  // namespace image_writer | 
| 320 }  // namespace extensions | 328 }  // namespace extensions | 
| OLD | NEW | 
|---|