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 "chrome/common/chrome_utility_messages.h"
11 #include "components/component_updater/component_updater_service.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/utility_process_host.h"
14 #include "content/public/browser/utility_process_host_client.h"
15 #include "courgette/courgette.h"
16 #include "courgette/third_party/bsdiff.h"
17 #include "ipc/ipc_message_macros.h"
19 namespace component_updater
{
21 class PatchHost
: public content::UtilityProcessHostClient
{
23 PatchHost(base::Callback
<void(int result
)> callback
,
24 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
26 void StartProcess(scoped_ptr
<IPC::Message
> message
);
31 void OnPatchFinished(int result
);
33 // Overrides of content::UtilityProcessHostClient.
34 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
36 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
38 base::Callback
<void(int result
)> callback_
;
39 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
42 PatchHost::PatchHost(base::Callback
<void(int result
)> callback
,
43 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
44 : callback_(callback
), task_runner_(task_runner
) {
47 PatchHost::~PatchHost() {
50 void PatchHost::StartProcess(scoped_ptr
<IPC::Message
> message
) {
51 // The DeltaUpdateOpPatchHost is not responsible for deleting the
52 // UtilityProcessHost object.
53 content::UtilityProcessHost
* host
= content::UtilityProcessHost::Create(
54 this, base::MessageLoopProxy::current().get());
55 host
->DisableSandbox();
56 host
->Send(message
.release());
59 bool PatchHost::OnMessageReceived(const IPC::Message
& message
) {
61 IPC_BEGIN_MESSAGE_MAP(PatchHost
, message
)
62 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished
, OnPatchFinished
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
68 void PatchHost::OnPatchFinished(int result
) {
69 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, result
));
73 void PatchHost::OnProcessCrashed(int exit_code
) {
74 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, -1));
78 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
81 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
84 void ChromeOutOfProcessPatcher::Patch(
85 const std::string
& operation
,
86 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
87 base::FilePath
& input_abs_path
,
88 base::FilePath
& patch_abs_path
,
89 base::FilePath
& output_abs_path
,
90 base::Callback
<void(int result
)> callback
) {
91 host_
= new PatchHost(callback
, task_runner
);
92 scoped_ptr
<IPC::Message
> patch_message
;
93 if (operation
== kBsdiff
) {
94 patch_message
.reset(new ChromeUtilityMsg_PatchFileBsdiff(
95 input_abs_path
, patch_abs_path
, output_abs_path
));
96 } else if (operation
== kCourgette
) {
97 patch_message
.reset(new ChromeUtilityMsg_PatchFileCourgette(
98 input_abs_path
, patch_abs_path
, output_abs_path
));
103 content::BrowserThread::PostTask(
104 content::BrowserThread::IO
,
107 &PatchHost::StartProcess
, host_
, base::Passed(&patch_message
)));
110 } // namespace component_updater