OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/extensions/zip_file_creator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/file_util_proxy.h" |
| 11 #include "base/memory/scoped_handle.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/path_service.h" |
| 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/chrome_utility_messages.h" |
| 17 #include "chrome/common/extensions/extension_file_util.h" |
| 18 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/utility_process_host.h" |
| 21 #include "grit/generated_resources.h" |
| 22 |
| 23 using content::BrowserThread; |
| 24 using content::UtilityProcessHost; |
| 25 |
| 26 namespace extensions { |
| 27 |
| 28 ZipFileCreator::ZipFileCreator( |
| 29 Observer* observer, |
| 30 const FilePath& src_dir, |
| 31 const std::vector<FilePath>& src_relative_paths, |
| 32 const FilePath& dest_file) |
| 33 : thread_identifier_(BrowserThread::ID_COUNT), |
| 34 observer_(observer), |
| 35 src_dir_(src_dir), |
| 36 src_relative_paths_(src_relative_paths), |
| 37 dest_file_(dest_file) { |
| 38 } |
| 39 |
| 40 void ZipFileCreator::Start() { |
| 41 CHECK(BrowserThread::GetCurrentThreadIdentifier(&thread_identifier_)); |
| 42 BrowserThread::PostTask( |
| 43 BrowserThread::IO, FROM_HERE, |
| 44 base::Bind( |
| 45 &ZipFileCreator::StartProcessOnIOThread, |
| 46 this)); |
| 47 } |
| 48 |
| 49 ZipFileCreator::~ZipFileCreator() { |
| 50 } |
| 51 |
| 52 bool ZipFileCreator::OnMessageReceived(const IPC::Message& message) { |
| 53 bool handled = true; |
| 54 IPC_BEGIN_MESSAGE_MAP(ZipFileCreator, message) |
| 55 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Succeeded, |
| 56 OnCreateZipFileSucceeded) |
| 57 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Failed, |
| 58 OnCreateZipFileFailed) |
| 59 IPC_MESSAGE_UNHANDLED(handled = false) |
| 60 IPC_END_MESSAGE_MAP() |
| 61 return handled; |
| 62 } |
| 63 |
| 64 void ZipFileCreator::OnProcessCrashed(int exit_code) { |
| 65 // Don't report crashes if they happen after we got a response. |
| 66 if (got_response_) |
| 67 return; |
| 68 |
| 69 // Utility process crashed while trying to create the zip file. |
| 70 ReportDone(false); |
| 71 } |
| 72 |
| 73 void ZipFileCreator::StartProcessOnIOThread() { |
| 74 // Create the destination zip file only if it does not already exist. |
| 75 int flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 76 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 77 base::PlatformFile dest_file = |
| 78 base::CreatePlatformFile(dest_file_, flags, NULL, &error_code); |
| 79 |
| 80 if (error_code != base::PLATFORM_FILE_OK) { |
| 81 LOG(ERROR) << "Failed to create dest zip file " << dest_file_.value(); |
| 82 ReportDone(false); |
| 83 return; |
| 84 } |
| 85 |
| 86 base::FileDescriptor dest_fd; |
| 87 dest_fd.fd = dest_file; |
| 88 dest_fd.auto_close = true; |
| 89 |
| 90 UtilityProcessHost* host = UtilityProcessHost::Create( |
| 91 this, BrowserThread::GetMessageLoopProxyForThread(thread_identifier_)); |
| 92 host->Send(new ChromeUtilityMsg_CreateZipFile(src_dir_, src_relative_paths_, |
| 93 dest_fd)); |
| 94 } |
| 95 |
| 96 void ZipFileCreator::OnCreateZipFileSucceeded() { |
| 97 // Skip check for unittests. |
| 98 if (thread_identifier_ != BrowserThread::ID_COUNT) |
| 99 CHECK(BrowserThread::CurrentlyOn(thread_identifier_)); |
| 100 got_response_ = true; |
| 101 |
| 102 ReportDone(true); |
| 103 } |
| 104 |
| 105 void ZipFileCreator::OnCreateZipFileFailed() { |
| 106 // Skip check for unittests. |
| 107 if (thread_identifier_ != BrowserThread::ID_COUNT) |
| 108 CHECK(BrowserThread::CurrentlyOn(thread_identifier_)); |
| 109 got_response_ = true; |
| 110 ReportDone(false); |
| 111 } |
| 112 |
| 113 void ZipFileCreator::ReportDone(bool success) { |
| 114 observer_->OnZipDone(success); |
| 115 } |
| 116 |
| 117 } // namespace extensions |
OLD | NEW |