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
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/component_patcher_operation_out_of_process.h"
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/location.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "chrome/common/chrome_utility_messages.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/component_updater/component_updater_service.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/utility_process_host.h"
20 #include "content/public/browser/utility_process_host_client.h"
21 #include "courgette/courgette.h"
22 #include "courgette/third_party/bsdiff.h"
23 #include "ipc/ipc_message_macros.h"
24 #include "ui/base/l10n/l10n_util.h"
26 namespace component_updater
{
28 class PatchHost
: public content::UtilityProcessHostClient
{
30 PatchHost(base::Callback
<void(int result
)> callback
,
31 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
33 void StartProcess(scoped_ptr
<IPC::Message
> message
);
36 ~PatchHost() override
;
38 void OnPatchFinished(int result
);
40 // Overrides of content::UtilityProcessHostClient.
41 bool OnMessageReceived(const IPC::Message
& message
) override
;
43 void OnProcessCrashed(int exit_code
) override
;
45 base::Callback
<void(int result
)> callback_
;
46 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
49 PatchHost::PatchHost(base::Callback
<void(int result
)> callback
,
50 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
51 : callback_(callback
), task_runner_(task_runner
) {
54 PatchHost::~PatchHost() {
57 void PatchHost::StartProcess(scoped_ptr
<IPC::Message
> message
) {
58 // The DeltaUpdateOpPatchHost is not responsible for deleting the
59 // UtilityProcessHost object.
60 content::UtilityProcessHost
* host
= content::UtilityProcessHost::Create(
61 this, base::ThreadTaskRunnerHandle::Get().get());
62 host
->SetName(l10n_util::GetStringUTF16(
63 IDS_UTILITY_PROCESS_COMPONENT_PATCHER_NAME
));
64 host
->DisableSandbox();
65 host
->Send(message
.release());
68 bool PatchHost::OnMessageReceived(const IPC::Message
& message
) {
70 IPC_BEGIN_MESSAGE_MAP(PatchHost
, message
)
71 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished
, OnPatchFinished
)
72 IPC_MESSAGE_UNHANDLED(handled
= false)
77 void PatchHost::OnPatchFinished(int result
) {
78 if (task_runner_
.get()) {
79 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, result
));
84 void PatchHost::OnProcessCrashed(int exit_code
) {
85 if (task_runner_
.get()) {
86 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, -1));
91 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
94 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
97 void ChromeOutOfProcessPatcher::Patch(
98 const std::string
& operation
,
99 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
100 const base::FilePath
& input_abs_path
,
101 const base::FilePath
& patch_abs_path
,
102 const base::FilePath
& output_abs_path
,
103 base::Callback
<void(int result
)> callback
) {
104 host_
= new PatchHost(callback
, task_runner
);
105 scoped_ptr
<IPC::Message
> patch_message
;
106 if (operation
== update_client::kBsdiff
) {
107 patch_message
.reset(new ChromeUtilityMsg_PatchFileBsdiff(
108 input_abs_path
, patch_abs_path
, output_abs_path
));
109 } else if (operation
== update_client::kCourgette
) {
110 patch_message
.reset(new ChromeUtilityMsg_PatchFileCourgette(
111 input_abs_path
, patch_abs_path
, output_abs_path
));
116 content::BrowserThread::PostTask(
117 content::BrowserThread::IO
,
120 &PatchHost::StartProcess
, host_
, base::Passed(&patch_message
)));
123 } // namespace component_updater