Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation.cc

Issue 92873003: Adds asynchronous unzip functions to ZipReader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes callback types and removes unnecessary message posting. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //
satorux1 2013/12/13 07:55:09 remove this
Drew Haven 2013/12/13 18:36:11 Done.
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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 void Operation::CleanUp() { 171 void Operation::CleanUp() {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); 173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin();
174 it != cleanup_functions_.end(); 174 it != cleanup_functions_.end();
175 ++it) { 175 ++it) {
176 it->Run(); 176 it->Run();
177 } 177 }
178 cleanup_functions_.clear(); 178 cleanup_functions_.clear();
179 } 179 }
180 180
181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_file) { 181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
183 if (IsCancelled()) { 183 if (IsCancelled()) {
184 return; 184 return;
185 } 185 }
186 186
187 DVLOG(1) << "Starting unzip stage for " << zip_file->value(); 187 DVLOG(1) << "Starting unzip stage for " << zip_path->value();
188 188
189 SetStage(image_writer_api::STAGE_UNZIP); 189 SetStage(image_writer_api::STAGE_UNZIP);
190 190
191 base::FilePath tmp_dir; 191 base::FilePath tmp_dir;
192 if (!base::CreateTemporaryDirInDir(zip_file->DirName(), 192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(),
193 FILE_PATH_LITERAL("image_writer"), 193 FILE_PATH_LITERAL("image_writer"),
194 &tmp_dir)) { 194 &tmp_dir)) {
195 DLOG(ERROR) << "Failed to create temporary directory.";
195 Error(error::kTempDir); 196 Error(error::kTempDir);
196 return; 197 return;
197 } 198 }
198 199
199 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); 200 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir));
200 201
201 if (!zip::Unzip(*zip_file, tmp_dir)) { 202 if (!base::CreateTemporaryFileInDir(tmp_dir, &image_path_)) {
203 DLOG(ERROR) << "Failed create temporary unzip target in "
204 << tmp_dir.value();
205 Error(error::kTempDir);
206 return;
207 }
208
209 if (!(zip_reader_.Open(*zip_path) &&
210 zip_reader_.AdvanceToNextEntry() &&
211 zip_reader_.OpenCurrentEntryInZip())) {
212 DLOG(ERROR) << "Failed to open zip file.";
202 Error(error::kUnzip); 213 Error(error::kUnzip);
203 return; 214 return;
204 } 215 }
205 216
206 base::FileEnumerator file_enumerator(tmp_dir, 217 zip_reader_.ExtractCurrentEntryToFilePathAsync(
207 false, 218 image_path_,
208 base::FileEnumerator::FILES); 219 base::Bind(&Operation::OnUnzipSuccess, this),
209 220 base::Bind(&Operation::OnUnzipFailure, this),
210 scoped_ptr<base::FilePath> unzipped_file( 221 base::Bind(&Operation::OnUnzipProgress,
211 new base::FilePath(file_enumerator.Next())); 222 this,
212 223 zip_reader_.current_entry_info()->original_size()));
213 if (unzipped_file->empty()) {
214 Error(error::kEmptyUnzip);
215 return;
216 }
217
218 if (!file_enumerator.Next().empty()) {
219 Error(error::kMultiFileZip);
220 return;
221 }
222
223 DVLOG(1) << "Successfully unzipped as " << unzipped_file->value();
224
225 SetProgress(kProgressComplete);
226
227 image_path_ = *unzipped_file;
228
229 BrowserThread::PostTask(
230 BrowserThread::FILE,
231 FROM_HERE,
232 base::Bind(&Operation::WriteStart,
233 this));
234 } 224 }
235 225
236 void Operation::GetMD5SumOfFile( 226 void Operation::GetMD5SumOfFile(
237 scoped_ptr<base::FilePath> file_path, 227 scoped_ptr<base::FilePath> file_path,
238 int64 file_size, 228 int64 file_size,
239 int progress_offset, 229 int progress_offset,
240 int progress_scale, 230 int progress_scale,
241 const base::Callback<void(scoped_ptr<std::string>)>& callback) { 231 const base::Callback<void(scoped_ptr<std::string>)>& callback) {
242 if (IsCancelled()) { 232 if (IsCancelled()) {
243 return; 233 return;
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 scoped_ptr<std::string> hash( 309 scoped_ptr<std::string> hash(
320 new std::string(base::MD5DigestToBase16(digest))); 310 new std::string(base::MD5DigestToBase16(digest)));
321 callback.Run(hash.Pass()); 311 callback.Run(hash.Pass());
322 } 312 }
323 } else { // len < 0 313 } else { // len < 0
324 reader->Close(); 314 reader->Close();
325 Error(error::kReadImage); 315 Error(error::kReadImage);
326 } 316 }
327 } 317 }
328 318
319 void Operation::OnUnzipSuccess() {
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
321 SetProgress(kProgressComplete);
322
323 if (zip_reader_.HasMore()) {
324 Error(error::kMultiFileZip);
325 } else {
326 WriteStart();
327 }
328 }
329
330 void Operation::OnUnzipFailure() {
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
332 Error(error::kUnzip);
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
329 } // namespace image_writer 342 } // namespace image_writer
330 } // namespace extensions 343 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698