component_patcher_operation_out_of_process.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "chrome/browser/component_updater/component_patcher_operation_out_of_process.h"
6
7#include <vector>
8
9#include "base/bind.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"
20
21namespace component_updater {
22
23class PatchHost : public content::UtilityProcessHostClient {
24 public:
25  PatchHost(base::Callback<void(int result)> callback,
26            scoped_refptr<base::SequencedTaskRunner> task_runner);
27
28  void StartProcess(scoped_ptr<IPC::Message> message);
29
30 private:
31  virtual ~PatchHost();
32
33  void OnPatchFinished(int result);
34
35  // Overrides of content::UtilityProcessHostClient.
36  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
37
38  virtual void OnProcessCrashed(int exit_code) OVERRIDE;
39
40  base::Callback<void(int result)> callback_;
41  scoped_refptr<base::SequencedTaskRunner> task_runner_;
42};
43
44PatchHost::PatchHost(base::Callback<void(int result)> callback,
45                     scoped_refptr<base::SequencedTaskRunner> task_runner)
46    : callback_(callback), task_runner_(task_runner) {
47}
48
49PatchHost::~PatchHost() {
50}
51
52void 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());
59}
60
61bool PatchHost::OnMessageReceived(const IPC::Message& message) {
62  bool handled = true;
63  IPC_BEGIN_MESSAGE_MAP(PatchHost, message)
64  IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished, OnPatchFinished)
65  IPC_MESSAGE_UNHANDLED(handled = false)
66  IPC_END_MESSAGE_MAP()
67  return handled;
68}
69
70void PatchHost::OnPatchFinished(int result) {
71  task_runner_->PostTask(FROM_HERE, base::Bind(callback_, result));
72  task_runner_ = NULL;
73}
74
75void PatchHost::OnProcessCrashed(int exit_code) {
76  task_runner_->PostTask(FROM_HERE, base::Bind(callback_, -1));
77  task_runner_ = NULL;
78}
79
80ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
81}
82
83ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
84}
85
86void 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));
101  } else {
102    NOTREACHED();
103  }
104
105  content::BrowserThread::PostTask(
106      content::BrowserThread::IO,
107      FROM_HERE,
108      base::Bind(
109          &PatchHost::StartProcess, host_, base::Passed(&patch_message)));
110}
111
112}  // namespace component_updater
113