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
);
33 void OnPatchFinished(int result
);
35 // Overrides of content::UtilityProcessHostClient.
36 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
38 virtual 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 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, result
));
75 void PatchHost::OnProcessCrashed(int exit_code
) {
76 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, -1));
80 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
83 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
86 void ChromeOutOfProcessPatcher::Patch(
87 const std::string
& operation
,
88 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
89 const base::FilePath
& input_abs_path
,
90 const base::FilePath
& patch_abs_path
,
91 const base::FilePath
& output_abs_path
,
92 base::Callback
<void(int result
)> callback
) {
93 host_
= new PatchHost(callback
, task_runner
);
94 scoped_ptr
<IPC::Message
> patch_message
;
95 if (operation
== kBsdiff
) {
96 patch_message
.reset(new ChromeUtilityMsg_PatchFileBsdiff(
97 input_abs_path
, patch_abs_path
, output_abs_path
));
98 } else if (operation
== kCourgette
) {
99 patch_message
.reset(new ChromeUtilityMsg_PatchFileCourgette(
100 input_abs_path
, patch_abs_path
, output_abs_path
));
105 content::BrowserThread::PostTask(
106 content::BrowserThread::IO
,
109 &PatchHost::StartProcess
, host_
, base::Passed(&patch_message
)));
112 } // namespace component_updater