 Chromium Code Reviews
 Chromium Code Reviews Issue 2705613003:
  Convert utility process zip creator IPC to mojo  (Closed)
    
  
    Issue 2705613003:
  Convert utility process zip creator IPC to mojo  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/file_manager/zip_file_creator.h" | 5 #include "chrome/browser/chromeos/file_manager/zip_file_creator.h" | 
| 6 | 6 | 
| 7 #include <utility> | 7 #include <utility> | 
| 8 | 8 | 
| 9 #include "base/bind.h" | 9 #include "base/bind.h" | 
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" | 
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/task_scheduler/post_task.h" | 11 #include "base/task_scheduler/post_task.h" | 
| 13 #include "chrome/common/chrome_utility_messages.h" | |
| 14 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" | 
| 15 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" | 
| 16 #include "content/public/browser/utility_process_host.h" | |
| 17 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" | 
| 18 | 15 | 
| 19 using content::BrowserThread; | |
| 20 using content::UtilityProcessHost; | |
| 21 | |
| 22 namespace { | 16 namespace { | 
| 23 | 17 | 
| 24 // Creates the destination zip file only if it does not already exist. | 18 // Creates the destination zip file only if it does not already exist. | 
| 25 base::File OpenFileHandleAsync(const base::FilePath& zip_path) { | 19 base::File OpenFileHandleAsync(const base::FilePath& zip_path) { | 
| 26 return base::File(zip_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 20 return base::File(zip_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE); | 
| 27 } | 21 } | 
| 28 | 22 | 
| 29 } // namespace | 23 } // namespace | 
| 30 | 24 | 
| 31 namespace file_manager { | 25 namespace file_manager { | 
| 32 | 26 | 
| 33 ZipFileCreator::ZipFileCreator( | 27 ZipFileCreator::ZipFileCreator( | 
| 34 const ResultCallback& callback, | 28 const ResultCallback& callback, | 
| 35 const base::FilePath& src_dir, | 29 const base::FilePath& src_dir, | 
| 36 const std::vector<base::FilePath>& src_relative_paths, | 30 const std::vector<base::FilePath>& src_relative_paths, | 
| 37 const base::FilePath& dest_file) | 31 const base::FilePath& dest_file) | 
| 38 : callback_(callback), | 32 : callback_(callback), | 
| 39 src_dir_(src_dir), | 33 src_dir_(src_dir), | 
| 40 src_relative_paths_(src_relative_paths), | 34 src_relative_paths_(src_relative_paths), | 
| 41 dest_file_(dest_file) { | 35 dest_file_(dest_file) { | 
| 42 DCHECK(!callback_.is_null()); | 36 DCHECK(!callback_.is_null()); | 
| 43 } | 37 } | 
| 44 | 38 | 
| 45 void ZipFileCreator::Start() { | 39 void ZipFileCreator::Start() { | 
| 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 40 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 47 | 41 | 
| 48 base::PostTaskWithTraitsAndReplyWithResult( | 42 base::PostTaskWithTraitsAndReplyWithResult( | 
| 49 FROM_HERE, base::TaskTraits().MayBlock(), | 43 FROM_HERE, base::TaskTraits().MayBlock(), | 
| 50 base::Bind(&OpenFileHandleAsync, dest_file_), | 44 base::Bind(&OpenFileHandleAsync, dest_file_), | 
| 51 base::Bind(&ZipFileCreator::OnOpenFileHandle, this)); | 45 base::Bind(&ZipFileCreator::CreateZipFile, this)); | 
| 52 } | 46 } | 
| 53 | 47 | 
| 54 ZipFileCreator::~ZipFileCreator() { | 48 ZipFileCreator::~ZipFileCreator() = default; | 
| 55 } | |
| 56 | 49 | 
| 57 bool ZipFileCreator::OnMessageReceived(const IPC::Message& message) { | 50 void ZipFileCreator::CreateZipFile(base::File file) { | 
| 58 bool handled = true; | |
| 59 IPC_BEGIN_MESSAGE_MAP(ZipFileCreator, message) | |
| 60 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Succeeded, | |
| 61 OnCreateZipFileSucceeded) | |
| 62 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_CreateZipFile_Failed, | |
| 63 OnCreateZipFileFailed) | |
| 64 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 65 IPC_END_MESSAGE_MAP() | |
| 66 return handled; | |
| 67 } | |
| 68 | |
| 69 void ZipFileCreator::OnProcessCrashed(int exit_code) { | |
| 70 ReportDone(false); | |
| 71 } | |
| 72 | |
| 73 void ZipFileCreator::OnOpenFileHandle(base::File file) { | |
| 74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 52 DCHECK(!utility_process_mojo_client_); | |
| 75 | 53 | 
| 76 if (!file.IsValid()) { | 54 if (!file.IsValid()) { | 
| 77 LOG(ERROR) << "Failed to create dest zip file " << dest_file_.value(); | 55 LOG(ERROR) << "Failed to create dest zip file " << dest_file_.value(); | 
| 78 ReportDone(false); | 56 ReportDone(false); | 
| 79 return; | 57 return; | 
| 80 } | 58 } | 
| 81 | 59 | 
| 82 BrowserThread::PostTask( | 60 utility_process_mojo_client_ = base::MakeUnique< | 
| 83 BrowserThread::IO, | 61 content::UtilityProcessMojoClient<chrome::mojom::ZipFileCreator>>( | 
| 84 FROM_HERE, | 62 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_ZIP_FILE_CREATOR_NAME)); | 
| 85 base::Bind( | 63 utility_process_mojo_client_->set_error_callback( | 
| 86 &ZipFileCreator::StartProcessOnIOThread, this, base::Passed(&file))); | 64 base::Bind(&ZipFileCreator::ReportDone, this, false)); | 
| 87 } | |
| 88 | 65 | 
| 89 void ZipFileCreator::StartProcessOnIOThread(base::File dest_file) { | 66 utility_process_mojo_client_->set_exposed_directory(src_dir_); | 
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 91 | 67 | 
| 92 base::FileDescriptor dest_fd(std::move(dest_file)); | 68 utility_process_mojo_client_->Start(); | 
| 93 | 69 | 
| 94 UtilityProcessHost* host = UtilityProcessHost::Create( | 70 utility_process_mojo_client_->service()->CreateZipFile( | 
| 95 this, BrowserThread::GetTaskRunnerForThread(BrowserThread::UI).get()); | 71 src_dir_, src_relative_paths_, std::move(file), | 
| 96 host->SetName( | 72 base::Bind(&ZipFileCreator::ReportDone, this)); | 
| 97 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_ZIP_FILE_CREATOR_NAME)); | |
| 98 host->SetExposedDir(src_dir_); | |
| 99 host->Send(new ChromeUtilityMsg_CreateZipFile(src_dir_, src_relative_paths_, | |
| 100 dest_fd)); | |
| 101 } | |
| 102 | |
| 103 void ZipFileCreator::OnCreateZipFileSucceeded() { | |
| 104 ReportDone(true); | |
| 105 } | |
| 106 | |
| 107 void ZipFileCreator::OnCreateZipFileFailed() { | |
| 108 ReportDone(false); | |
| 109 } | 73 } | 
| 110 | 74 | 
| 111 void ZipFileCreator::ReportDone(bool success) { | 75 void ZipFileCreator::ReportDone(bool success) { | 
| 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| 113 | 77 | 
| 114 // Guard against calling observer multiple times. | 78 // The current user of this class holds no reference to |this| so resetting | 
| 115 if (!callback_.is_null()) | 79 // the |utility_process_mojo_client_| here could release the last reference | 
| 116 base::ResetAndReturn(&callback_).Run(success); | 80 // and delete |this|. So save |callback_| before resetting the client. | 
| 
Noel Gordon
2017/03/01 03:22:47
sammc, tibell: I figure that mojo users might need
 | |
| 81 auto callback = base::ResetAndReturn(&callback_); | |
| 82 utility_process_mojo_client_.reset(); | |
| 83 callback.Run(success); | |
| 117 } | 84 } | 
| 118 | 85 | 
| 119 } // namespace file_manager | 86 } // namespace file_manager | 
| OLD | NEW |