Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / component_patcher_operation_out_of_process.cc
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 "chrome/browser/component_updater/component_updater_service.h"
11 #include "chrome/common/chrome_utility_messages.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"
18
19 namespace component_updater {
20
21 class PatchHost : public content::UtilityProcessHostClient {
22  public:
23   PatchHost(base::Callback<void(int result)> callback,
24             scoped_refptr<base::SequencedTaskRunner> task_runner);
25
26   void StartProcess(scoped_ptr<IPC::Message> message);
27
28  private:
29   virtual ~PatchHost();
30
31   void OnPatchFinished(int result);
32
33   // Overrides of content::UtilityProcessHostClient.
34   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
35
36   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
37
38   base::Callback<void(int result)> callback_;
39   scoped_refptr<base::SequencedTaskRunner> task_runner_;
40 };
41
42 PatchHost::PatchHost(base::Callback<void(int result)> callback,
43                      scoped_refptr<base::SequencedTaskRunner> task_runner)
44     : callback_(callback), task_runner_(task_runner) {
45 }
46
47 PatchHost::~PatchHost() {
48 }
49
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());
57 }
58
59 bool PatchHost::OnMessageReceived(const IPC::Message& message) {
60   bool handled = true;
61   IPC_BEGIN_MESSAGE_MAP(PatchHost, message)
62   IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Finished, OnPatchFinished)
63   IPC_MESSAGE_UNHANDLED(handled = false)
64   IPC_END_MESSAGE_MAP()
65   return handled;
66 }
67
68 void PatchHost::OnPatchFinished(int result) {
69   task_runner_->PostTask(FROM_HERE, base::Bind(callback_, result));
70   task_runner_ = NULL;
71 }
72
73 void PatchHost::OnProcessCrashed(int exit_code) {
74   task_runner_->PostTask(FROM_HERE, base::Bind(callback_, -1));
75   task_runner_ = NULL;
76 }
77
78 ChromeOutOfProcessPatcher::ChromeOutOfProcessPatcher() {
79 }
80
81 ChromeOutOfProcessPatcher::~ChromeOutOfProcessPatcher() {
82 }
83
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));
99   } else {
100     NOTREACHED();
101   }
102
103   content::BrowserThread::PostTask(
104       content::BrowserThread::IO,
105       FROM_HERE,
106       base::Bind(
107           &PatchHost::StartProcess, host_, base::Passed(&patch_message)));
108 }
109
110 }  // namespace component_updater