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 "chrome/grit/generated_resources.h"
14 #include "components/component_updater/component_updater_service.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "content/public/browser/utility_process_host_client.h"
18 #include "courgette/courgette.h"
19 #include "courgette/third_party/bsdiff.h"
20 #include "ipc/ipc_message_macros.h"
21 #include "ui/base/l10n/l10n_util.h"
23 namespace component_updater
{
25 class PatchHost
: public content::UtilityProcessHostClient
{
27 PatchHost(base::Callback
<void(int result
)> callback
,
28 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
30 void StartProcess(scoped_ptr
<IPC::Message
> message
);
33 ~PatchHost() override
;
35 void OnPatchFinished(int result
);
37 // Overrides of content::UtilityProcessHostClient.
38 bool OnMessageReceived(const IPC::Message
& message
) override
;
40 void OnProcessCrashed(int exit_code
) override
;
42 base::Callback
<void(int result
)> callback_
;
43 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
46 PatchHost::PatchHost(base::Callback
<void(int result
)> callback
,
47 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
48 : callback_(callback
), task_runner_(task_runner
) {
51 PatchHost::~PatchHost() {
54 void PatchHost::StartProcess(scoped_ptr
<IPC::Message
> message
) {
55 // The DeltaUpdateOpPatchHost is not responsible for deleting the
56 // UtilityProcessHost object.
57 content::UtilityProcessHost
* host
= content::UtilityProcessHost::Create(
58 this, base::MessageLoopProxy::current().get());
59 host
->SetName(l10n_util::GetStringUTF16(
60 IDS_UTILITY_PROCESS_COMPONENT_PATCHER_NAME
));
61 host
->DisableSandbox();
62 host
->Send(message
.release());
65 bool PatchHost::OnMessageReceived(const IPC::Message
& message
) {
67 IPC_BEGIN_MESSAGE_MAP(PatchHost
, message
)
68 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished
, OnPatchFinished
)
69 IPC_MESSAGE_UNHANDLED(handled
= false)
74 void PatchHost::OnPatchFinished(int result
) {
75 if (task_runner_
.get()) {
76 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, result
));
81 void PatchHost::OnProcessCrashed(int exit_code
) {
82 if (task_runner_
.get()) {
83 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
, -1));
88 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
91 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
94 void ChromeOutOfProcessPatcher::Patch(
95 const std::string
& operation
,
96 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
97 const base::FilePath
& input_abs_path
,
98 const base::FilePath
& patch_abs_path
,
99 const base::FilePath
& output_abs_path
,
100 base::Callback
<void(int result
)> callback
) {
101 host_
= new PatchHost(callback
, task_runner
);
102 scoped_ptr
<IPC::Message
> patch_message
;
103 if (operation
== update_client::kBsdiff
) {
104 patch_message
.reset(new ChromeUtilityMsg_PatchFileBsdiff(
105 input_abs_path
, patch_abs_path
, output_abs_path
));
106 } else if (operation
== update_client::kCourgette
) {
107 patch_message
.reset(new ChromeUtilityMsg_PatchFileCourgette(
108 input_abs_path
, patch_abs_path
, output_abs_path
));
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO
,
117 &PatchHost::StartProcess
, host_
, base::Passed(&patch_message
)));
120 } // namespace component_updater