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 "chrome/common/chrome_utility_messages.h"
13 #include "components/component_updater/component_updater_service.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/utility_process_host.h"
16 #include "content/public/browser/utility_process_host_client.h"
17 #include "courgette/courgette.h"
18 #include "courgette/third_party/bsdiff.h"
19 #include "ipc/ipc_message_macros.h"
21 namespace component_updater
{
23 class PatchHost
: public content::UtilityProcessHostClient
{
25 PatchHost(base::Callback
<void(int result
)> callback
,
26 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
28 void StartProcess(scoped_ptr
<IPC::Message
> message
);
31 ~PatchHost() override
;
33 void OnPatchFinished(int result
);
35 // Overrides of content::UtilityProcessHostClient.
36 bool OnMessageReceived(const IPC::Message
& message
) override
;
38 void OnProcessCrashed(int exit_code
) override
;
40 base::Callback
<void(int result
)> callback_
;
41 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
44 PatchHost::PatchHost(base::Callback
<void(int result
)> callback
,
45 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
46 : callback_(callback
), task_runner_(task_runner
) {
49 PatchHost::~PatchHost() {
52 void PatchHost::StartProcess(scoped_ptr
<IPC::Message
> message
) {
53 // The DeltaUpdateOpPatchHost is not responsible for deleting the
54 // UtilityProcessHost object.
55 content::UtilityProcessHost
* host
= content::UtilityProcessHost::Create(
56 this, base::MessageLoopProxy::current().get());
57 host
->DisableSandbox();
58 host
->Send(message
.release());
61 bool PatchHost::OnMessageReceived(const IPC::Message
& message
) {
63 IPC_BEGIN_MESSAGE_MAP(PatchHost
, message
)
64 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished
, OnPatchFinished
)
65 IPC_MESSAGE_UNHANDLED(handled
= false)
70 void PatchHost::OnPatchFinished(int result
) {
71 if (task_runner_
.get()) {
72 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, result
));
77 void PatchHost::OnProcessCrashed(int exit_code
) {
78 if (task_runner_
.get()) {
79 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, -1));
84 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
87 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
90 void ChromeOutOfProcessPatcher::Patch(
91 const std::string
& operation
,
92 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
93 const base::FilePath
& input_abs_path
,
94 const base::FilePath
& patch_abs_path
,
95 const base::FilePath
& output_abs_path
,
96 base::Callback
<void(int result
)> callback
) {
97 host_
= new PatchHost(callback
, task_runner
);
98 scoped_ptr
<IPC::Message
> patch_message
;
99 if (operation
== update_client::kBsdiff
) {
100 patch_message
.reset(new ChromeUtilityMsg_PatchFileBsdiff(
101 input_abs_path
, patch_abs_path
, output_abs_path
));
102 } else if (operation
== update_client::kCourgette
) {
103 patch_message
.reset(new ChromeUtilityMsg_PatchFileCourgette(
104 input_abs_path
, patch_abs_path
, output_abs_path
));
109 content::BrowserThread::PostTask(
110 content::BrowserThread::IO
,
113 &PatchHost::StartProcess
, host_
, base::Passed(&patch_message
)));
116 } // namespace component_updater