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  if (task_runner_.get()) {
72    task_runner_->PostTask(FROM_HERE, base::Bind(callback_, result));
73    task_runner_ = NULL;
74  }
75}
76
77void PatchHost::OnProcessCrashed(int exit_code) {
78  if (task_runner_.get()) {
79    task_runner_->PostTask(FROM_HERE, base::Bind(callback_, -1));
80    task_runner_ = NULL;
81  }
82}
83
84ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
85}
86
87ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
88}
89
90void 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 == kBsdiff) {
100    patch_message.reset(new ChromeUtilityMsg_PatchFileBsdiff(
101        input_abs_path, patch_abs_path, output_abs_path));
102  } else if (operation == kCourgette) {
103    patch_message.reset(new ChromeUtilityMsg_PatchFileCourgette(
104        input_abs_path, patch_abs_path, output_abs_path));
105  } else {
106    NOTREACHED();
107  }
108
109  content::BrowserThread::PostTask(
110      content::BrowserThread::IO,
111      FROM_HERE,
112      base::Bind(
113          &PatchHost::StartProcess, host_, base::Passed(&patch_message)));
114}
115
116}  // namespace component_updater
117