| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/component_updater/component_patcher_operation_out_of_pr
ocess.h" | 5 #include "chrome/browser/component_updater/component_patcher_operation_out_of_pr
ocess.h" |
| 6 | 6 |
| 7 #include <memory> |
| 8 #include <vector> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/callback.h" | 11 #include "base/callback.h" |
| 9 #include "base/files/file.h" | |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/sequenced_task_runner.h" | 13 #include "base/location.h" |
| 12 #include "base/strings/string16.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/common/chrome_utility_messages.h" |
| 13 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
| 18 #include "components/component_updater/component_updater_service.h" |
| 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/utility_process_host.h" |
| 21 #include "content/public/browser/utility_process_host_client.h" |
| 22 #include "courgette/courgette.h" |
| 23 #include "courgette/third_party/bsdiff/bsdiff.h" |
| 24 #include "ipc/ipc_message_macros.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 15 | 26 |
| 16 namespace component_updater { | 27 namespace component_updater { |
| 17 | 28 |
| 18 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() = default; | 29 class PatchHost : public content::UtilityProcessHostClient { |
| 30 public: |
| 31 PatchHost(base::Callback<void(int result)> callback, |
| 32 scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 19 | 33 |
| 20 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() = default; | 34 void StartProcess(std::unique_ptr<IPC::Message> message); |
| 35 |
| 36 private: |
| 37 ~PatchHost() override; |
| 38 |
| 39 void OnPatchFinished(int result); |
| 40 |
| 41 // Overrides of content::UtilityProcessHostClient. |
| 42 bool OnMessageReceived(const IPC::Message& message) override; |
| 43 |
| 44 void OnProcessCrashed(int exit_code) override; |
| 45 |
| 46 base::Callback<void(int result)> callback_; |
| 47 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 48 }; |
| 49 |
| 50 PatchHost::PatchHost(base::Callback<void(int result)> callback, |
| 51 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 52 : callback_(callback), task_runner_(task_runner) { |
| 53 } |
| 54 |
| 55 PatchHost::~PatchHost() { |
| 56 } |
| 57 |
| 58 void PatchHost::StartProcess(std::unique_ptr<IPC::Message> message) { |
| 59 // The DeltaUpdateOpPatchHost is not responsible for deleting the |
| 60 // UtilityProcessHost object. |
| 61 content::UtilityProcessHost* host = content::UtilityProcessHost::Create( |
| 62 this, base::ThreadTaskRunnerHandle::Get().get()); |
| 63 host->SetName(l10n_util::GetStringUTF16( |
| 64 IDS_UTILITY_PROCESS_COMPONENT_PATCHER_NAME)); |
| 65 host->Send(message.release()); |
| 66 } |
| 67 |
| 68 bool PatchHost::OnMessageReceived(const IPC::Message& message) { |
| 69 bool handled = true; |
| 70 IPC_BEGIN_MESSAGE_MAP(PatchHost, message) |
| 71 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished, OnPatchFinished) |
| 72 IPC_MESSAGE_UNHANDLED(handled = false) |
| 73 IPC_END_MESSAGE_MAP() |
| 74 return handled; |
| 75 } |
| 76 |
| 77 void PatchHost::OnPatchFinished(int result) { |
| 78 if (task_runner_.get()) { |
| 79 task_runner_->PostTask(FROM_HERE, base::Bind(callback_, result)); |
| 80 task_runner_ = NULL; |
| 81 } |
| 82 } |
| 83 |
| 84 void PatchHost::OnProcessCrashed(int exit_code) { |
| 85 if (task_runner_.get()) { |
| 86 task_runner_->PostTask(FROM_HERE, base::Bind(callback_, -1)); |
| 87 task_runner_ = NULL; |
| 88 } |
| 89 } |
| 90 |
| 91 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() { |
| 92 } |
| 93 |
| 94 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() { |
| 95 } |
| 21 | 96 |
| 22 void ChromeOutOfProcessPatcher::Patch( | 97 void ChromeOutOfProcessPatcher::Patch( |
| 23 const std::string& operation, | 98 const std::string& operation, |
| 24 scoped_refptr<base::SequencedTaskRunner> task_runner, | 99 scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 25 const base::FilePath& input_abs_path, | 100 const base::FilePath& input_abs_path, |
| 26 const base::FilePath& patch_abs_path, | 101 const base::FilePath& patch_abs_path, |
| 27 const base::FilePath& output_abs_path, | 102 const base::FilePath& output_abs_path, |
| 28 base::Callback<void(int result)> callback) { | 103 base::Callback<void(int result)> callback) { |
| 29 DCHECK(task_runner); | 104 host_ = new PatchHost(callback, task_runner); |
| 30 DCHECK(!input_abs_path.empty()); | 105 IPC::PlatformFileForTransit input = IPC::TakePlatformFileForTransit( |
| 31 DCHECK(!patch_abs_path.empty()); | 106 base::File( |
| 32 DCHECK(!output_abs_path.empty()); | 107 input_abs_path, base::File::FLAG_OPEN | base::File::FLAG_READ)); |
| 33 DCHECK(!utility_process_mojo_client_); | 108 IPC::PlatformFileForTransit patch = IPC::TakePlatformFileForTransit( |
| 34 | 109 base::File( |
| 35 task_runner_ = std::move(task_runner); | 110 patch_abs_path, base::File::FLAG_OPEN | base::File::FLAG_READ)); |
| 36 callback_ = callback; | 111 IPC::PlatformFileForTransit output = IPC::TakePlatformFileForTransit( |
| 37 | 112 base::File( |
| 38 base::File input_file(input_abs_path, | 113 output_abs_path, |
| 39 base::File::FLAG_OPEN | base::File::FLAG_READ); | 114 base::File::FLAG_CREATE | |
| 40 base::File patch_file(patch_abs_path, | 115 base::File::FLAG_WRITE | |
| 41 base::File::FLAG_OPEN | base::File::FLAG_READ); | 116 base::File::FLAG_EXCLUSIVE_WRITE)); |
| 42 base::File output_file(output_abs_path, base::File::FLAG_CREATE | | 117 std::unique_ptr<IPC::Message> patch_message; |
| 43 base::File::FLAG_WRITE | | |
| 44 base::File::FLAG_EXCLUSIVE_WRITE); | |
| 45 | |
| 46 const base::string16 utility_process_name = | |
| 47 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_COMPONENT_PATCHER_NAME); | |
| 48 | |
| 49 utility_process_mojo_client_.reset( | |
| 50 new content::UtilityProcessMojoClient<chrome::mojom::FilePatcher>( | |
| 51 utility_process_name)); | |
| 52 utility_process_mojo_client_->set_error_callback( | |
| 53 base::Bind(&ChromeOutOfProcessPatcher::PatchDone, this, -1)); | |
| 54 | |
| 55 utility_process_mojo_client_->Start(); // Start the utility process. | |
| 56 | |
| 57 if (operation == update_client::kBsdiff) { | 118 if (operation == update_client::kBsdiff) { |
| 58 utility_process_mojo_client_->service()->PatchFileBsdiff( | 119 patch_message.reset(new ChromeUtilityMsg_PatchFileBsdiff( |
| 59 std::move(input_file), std::move(patch_file), std::move(output_file), | 120 input, patch, output)); |
| 60 base::Bind(&ChromeOutOfProcessPatcher::PatchDone, this)); | |
| 61 } else if (operation == update_client::kCourgette) { | 121 } else if (operation == update_client::kCourgette) { |
| 62 utility_process_mojo_client_->service()->PatchFileCourgette( | 122 patch_message.reset(new ChromeUtilityMsg_PatchFileCourgette( |
| 63 std::move(input_file), std::move(patch_file), std::move(output_file), | 123 input, patch, output)); |
| 64 base::Bind(&ChromeOutOfProcessPatcher::PatchDone, this)); | |
| 65 } else { | 124 } else { |
| 66 NOTREACHED(); | 125 NOTREACHED(); |
| 67 } | 126 } |
| 68 } | |
| 69 | 127 |
| 70 void ChromeOutOfProcessPatcher::PatchDone(int result) { | 128 content::BrowserThread::PostTask( |
| 71 utility_process_mojo_client_.reset(); // Terminate the utility process. | 129 content::BrowserThread::IO, |
| 72 task_runner_->PostTask(FROM_HERE, base::Bind(callback_, result)); | 130 FROM_HERE, |
| 131 base::Bind( |
| 132 &PatchHost::StartProcess, host_, base::Passed(&patch_message))); |
| 73 } | 133 } |
| 74 | 134 |
| 75 } // namespace component_updater | 135 } // namespace component_updater |
| OLD | NEW |