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
" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "third_party/zlib/google/zip.h" | |
13 | 12 |
14 namespace extensions { | 13 namespace extensions { |
15 namespace image_writer { | 14 namespace image_writer { |
16 | 15 |
17 using content::BrowserThread; | 16 using content::BrowserThread; |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
21 const int kMD5BufferSize = 1024; | 20 const int kMD5BufferSize = 1024; |
22 | 21 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 void Operation::CleanUp() { | 171 void Operation::CleanUp() { |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
174 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
175 it != cleanup_functions_.end(); | 174 it != cleanup_functions_.end(); |
176 ++it) { | 175 ++it) { |
177 it->Run(); | 176 it->Run(); |
178 } | 177 } |
179 cleanup_functions_.clear(); | 178 cleanup_functions_.clear(); |
180 } | 179 } |
181 | 180 |
182 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_file) { | 181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
184 if (IsCancelled()) { | 183 if (IsCancelled()) { |
185 return; | 184 return; |
186 } | 185 } |
187 | 186 |
188 DVLOG(1) << "Starting unzip stage for " << zip_file->value(); | 187 DVLOG(1) << "Starting unzip stage for " << zip_path->value(); |
189 | 188 |
190 SetStage(image_writer_api::STAGE_UNZIP); | 189 SetStage(image_writer_api::STAGE_UNZIP); |
191 | 190 |
192 base::FilePath tmp_dir; | 191 base::FilePath tmp_dir; |
193 if (!base::CreateTemporaryDirInDir(zip_file->DirName(), | 192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), |
194 FILE_PATH_LITERAL("image_writer"), | 193 FILE_PATH_LITERAL("image_writer"), |
195 &tmp_dir)) { | 194 &tmp_dir)) { |
196 Error(error::kTempDirError); | 195 Error(error::kTempDirError); |
197 return; | 196 return; |
198 } | 197 } |
199 | 198 |
200 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); | 199 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); |
201 | 200 |
202 if (!zip::Unzip(*zip_file, tmp_dir)) { | 201 if (!base::CreateTemporaryFileInDir(tmp_dir, &image_path_)) { |
| 202 DLOG(ERROR) << "Failed create temporary unzip target in " |
| 203 << tmp_dir.value(); |
| 204 Error(error::kTempDirError); |
| 205 return; |
| 206 } |
| 207 |
| 208 if (!(zip_reader_.Open(*zip_path) && |
| 209 zip_reader_.AdvanceToNextEntry() && |
| 210 zip_reader_.OpenCurrentEntryInZip())) { |
| 211 DLOG(ERROR) << "Failed to open zip file."; |
203 Error(error::kUnzipGenericError); | 212 Error(error::kUnzipGenericError); |
204 return; | 213 return; |
205 } | 214 } |
206 | 215 |
207 base::FileEnumerator file_enumerator(tmp_dir, | 216 if (zip_reader_.HasMore()) { |
208 false, | 217 DLOG(ERROR) << "Image zip has more than one file."; |
209 base::FileEnumerator::FILES); | |
210 | |
211 scoped_ptr<base::FilePath> unzipped_file( | |
212 new base::FilePath(file_enumerator.Next())); | |
213 | |
214 if (unzipped_file->empty()) { | |
215 Error(error::kUnzipInvalidArchive); | 218 Error(error::kUnzipInvalidArchive); |
216 return; | 219 return; |
217 } | 220 } |
218 | 221 |
219 if (!file_enumerator.Next().empty()) { | 222 zip_reader_.ExtractCurrentEntryToFilePathAsync( |
220 Error(error::kUnzipInvalidArchive); | 223 image_path_, |
221 return; | 224 base::Bind(&Operation::OnUnzipSuccess, this), |
222 } | 225 base::Bind(&Operation::OnUnzipFailure, this), |
223 | 226 base::Bind(&Operation::OnUnzipProgress, |
224 DVLOG(1) << "Successfully unzipped as " << unzipped_file->value(); | 227 this, |
225 | 228 zip_reader_.current_entry_info()->original_size())); |
226 SetProgress(kProgressComplete); | |
227 | |
228 image_path_ = *unzipped_file; | |
229 | |
230 BrowserThread::PostTask( | |
231 BrowserThread::FILE, | |
232 FROM_HERE, | |
233 base::Bind(&Operation::WriteStart, | |
234 this)); | |
235 } | 229 } |
236 | 230 |
237 void Operation::GetMD5SumOfFile( | 231 void Operation::GetMD5SumOfFile( |
238 scoped_ptr<base::FilePath> file_path, | 232 scoped_ptr<base::FilePath> file_path, |
239 int64 file_size, | 233 int64 file_size, |
240 int progress_offset, | 234 int progress_offset, |
241 int progress_scale, | 235 int progress_scale, |
242 const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 236 const base::Callback<void(scoped_ptr<std::string>)>& callback) { |
243 if (IsCancelled()) { | 237 if (IsCancelled()) { |
244 return; | 238 return; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 scoped_ptr<std::string> hash( | 314 scoped_ptr<std::string> hash( |
321 new std::string(base::MD5DigestToBase16(digest))); | 315 new std::string(base::MD5DigestToBase16(digest))); |
322 callback.Run(hash.Pass()); | 316 callback.Run(hash.Pass()); |
323 } | 317 } |
324 } else { // len < 0 | 318 } else { // len < 0 |
325 reader->Close(); | 319 reader->Close(); |
326 Error(error::kHashReadError); | 320 Error(error::kHashReadError); |
327 } | 321 } |
328 } | 322 } |
329 | 323 |
| 324 void Operation::OnUnzipSuccess() { |
| 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 326 SetProgress(kProgressComplete); |
| 327 WriteStart(); |
| 328 } |
| 329 |
| 330 void Operation::OnUnzipFailure() { |
| 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 332 Error(error::kUnzipGenericError); |
| 333 } |
| 334 |
| 335 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 337 |
| 338 int progress_percent = 100 * progress_bytes / total_bytes; |
| 339 SetProgress(progress_percent); |
| 340 } |
| 341 |
330 } // namespace image_writer | 342 } // namespace image_writer |
331 } // namespace extensions | 343 } // namespace extensions |
OLD | NEW |