Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: chrome/browser/component_updater/component_patcher_operation.cc

Issue 420503002: Componentize component_updater: Decouple in-process DeltaUpdateOp from out-of-process version. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/component_updater/component_patcher_operation.h" 5 #include "chrome/browser/component_updater/component_patcher_operation.h"
6 6
7 #include <string>
8 #include <vector> 7 #include <vector>
9 8
10 #include "base/bind.h" 9 #include "base/bind.h"
11 #include "base/file_util.h" 10 #include "base/file_util.h"
12 #include "base/files/memory_mapped_file.h" 11 #include "base/files/memory_mapped_file.h"
13 #include "base/json/json_file_value_serializer.h" 12 #include "base/location.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
16 #include "chrome/browser/component_updater/component_patcher.h" 14 #include "chrome/browser/component_updater/component_patcher.h"
17 #include "chrome/browser/component_updater/component_updater_service.h" 15 #include "chrome/browser/component_updater/component_updater_service.h"
18 #include "chrome/common/chrome_utility_messages.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/utility_process_host.h"
21 #include "courgette/courgette.h" 16 #include "courgette/courgette.h"
22 #include "courgette/third_party/bsdiff.h" 17 #include "courgette/third_party/bsdiff.h"
23 #include "crypto/secure_hash.h" 18 #include "crypto/secure_hash.h"
24 #include "crypto/sha2.h" 19 #include "crypto/sha2.h"
25 #include "crypto/signature_verifier.h" 20 #include "crypto/signature_verifier.h"
26 #include "extensions/common/crx_file.h" 21 #include "extensions/common/crx_file.h"
27 #include "ipc/ipc_message_macros.h"
28 22
29 using crypto::SecureHash; 23 using crypto::SecureHash;
30 24
31 namespace component_updater { 25 namespace component_updater {
32 26
33 namespace { 27 namespace {
34 28
35 const char kInput[] = "input";
36 const char kOp[] = "op";
37 const char kOutput[] = "output"; 29 const char kOutput[] = "output";
38 const char kPatch[] = "patch";
39 const char kSha256[] = "sha256"; 30 const char kSha256[] = "sha256";
40 31
41 // The integer offset disambiguates between overlapping error ranges. 32 // The integer offset disambiguates between overlapping error ranges.
42 const int kCourgetteErrorOffset = 300; 33 const int kCourgetteErrorOffset = 300;
43 const int kBsdiffErrorOffset = 600; 34 const int kBsdiffErrorOffset = 600;
44 35
45 class CourgetteTraits : public DeltaUpdateOpPatchStrategy {
46 public:
47 virtual int GetErrorOffset() const OVERRIDE;
48 virtual int GetSuccessCode() const OVERRIDE;
49 virtual scoped_ptr<IPC::Message> GetPatchMessage(
50 base::FilePath input_abs_path,
51 base::FilePath patch_abs_path,
52 base::FilePath output_abs_path) OVERRIDE;
53 virtual int Patch(base::FilePath input_abs_path,
54 base::FilePath patch_abs_path,
55 base::FilePath output_abs_path) OVERRIDE;
56 };
57
58 int CourgetteTraits::GetErrorOffset() const {
59 return kCourgetteErrorOffset;
60 }
61
62 int CourgetteTraits::GetSuccessCode() const {
63 return courgette::C_OK;
64 }
65
66 scoped_ptr<IPC::Message> CourgetteTraits::GetPatchMessage(
67 base::FilePath input_abs_path,
68 base::FilePath patch_abs_path,
69 base::FilePath output_abs_path) {
70 return scoped_ptr<IPC::Message>(
71 new ChromeUtilityMsg_PatchFileCourgette(input_abs_path,
72 patch_abs_path,
73 output_abs_path));
74 }
75
76 int CourgetteTraits::Patch(base::FilePath input_abs_path,
77 base::FilePath patch_abs_path,
78 base::FilePath output_abs_path) {
79 return courgette::ApplyEnsemblePatch(input_abs_path.value().c_str(),
80 patch_abs_path.value().c_str(),
81 output_abs_path.value().c_str());
82 }
83
84 class BsdiffTraits : public DeltaUpdateOpPatchStrategy {
85 public:
86 virtual int GetErrorOffset() const OVERRIDE;
87 virtual int GetSuccessCode() const OVERRIDE;
88 virtual scoped_ptr<IPC::Message> GetPatchMessage(
89 base::FilePath input_abs_path,
90 base::FilePath patch_abs_path,
91 base::FilePath output_abs_path) OVERRIDE;
92 virtual int Patch(base::FilePath input_abs_path,
93 base::FilePath patch_abs_path,
94 base::FilePath output_abs_path) OVERRIDE;
95 };
96
97 int BsdiffTraits::GetErrorOffset() const {
98 return kBsdiffErrorOffset;
99 }
100
101 int BsdiffTraits::GetSuccessCode() const {
102 return courgette::OK;
103 }
104
105 scoped_ptr<IPC::Message> BsdiffTraits::GetPatchMessage(
106 base::FilePath input_abs_path,
107 base::FilePath patch_abs_path,
108 base::FilePath output_abs_path) {
109 return scoped_ptr<IPC::Message>(
110 new ChromeUtilityMsg_PatchFileBsdiff(input_abs_path,
111 patch_abs_path,
112 output_abs_path));
113 }
114
115 int BsdiffTraits::Patch(base::FilePath input_abs_path,
116 base::FilePath patch_abs_path,
117 base::FilePath output_abs_path) {
118 return courgette::ApplyBinaryPatch(input_abs_path,
119 patch_abs_path,
120 output_abs_path);
121 }
122
123 } // namespace 36 } // namespace
124 37
125 DeltaUpdateOpPatchStrategy::~DeltaUpdateOpPatchStrategy() { 38 const char kOp[] = "op";
126 } 39 const char kBsdiff[] = "bsdiff";
40 const char kCourgette[] = "courgette";
41 const char kInput[] = "input";
42 const char kPatch[] = "patch";
127 43
128 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation) { 44 DeltaUpdateOp* CreateDeltaUpdateOp(
45 const std::string& operation,
46 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher) {
129 if (operation == "copy") { 47 if (operation == "copy") {
130 return new DeltaUpdateOpCopy(); 48 return new DeltaUpdateOpCopy();
131 } else if (operation == "create") { 49 } else if (operation == "create") {
132 return new DeltaUpdateOpCreate(); 50 return new DeltaUpdateOpCreate();
133 } else if (operation == "bsdiff") { 51 } else if (operation == "bsdiff" || operation == "courgette") {
134 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new BsdiffTraits()); 52 return new DeltaUpdateOpPatch(operation, out_of_process_patcher);
135 return new DeltaUpdateOpPatch(strategy.Pass());
136 } else if (operation == "courgette") {
137 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new CourgetteTraits());
138 return new DeltaUpdateOpPatch(strategy.Pass());
139 } 53 }
140 return NULL; 54 return NULL;
141 } 55 }
142 56
143 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command) { 57 DeltaUpdateOp::DeltaUpdateOp() {
144 std::string operation;
145 if (!command.GetString(kOp, &operation))
146 return NULL;
147 return CreateDeltaUpdateOp(operation);
148 }
149
150 DeltaUpdateOp::DeltaUpdateOp() : in_process_(false) {
151 } 58 }
152 59
153 DeltaUpdateOp::~DeltaUpdateOp() { 60 DeltaUpdateOp::~DeltaUpdateOp() {
154 } 61 }
155 62
156 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args, 63 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args,
157 const base::FilePath& input_dir, 64 const base::FilePath& input_dir,
158 const base::FilePath& unpack_dir, 65 const base::FilePath& unpack_dir,
159 ComponentInstaller* installer, 66 ComponentInstaller* installer,
160 bool in_process,
161 const ComponentUnpacker::Callback& callback, 67 const ComponentUnpacker::Callback& callback,
162 scoped_refptr<base::SequencedTaskRunner> task_runner) { 68 scoped_refptr<base::SequencedTaskRunner> task_runner) {
163 callback_ = callback; 69 callback_ = callback;
164 in_process_ = in_process;
165 task_runner_ = task_runner; 70 task_runner_ = task_runner;
166 std::string output_rel_path; 71 std::string output_rel_path;
167 if (!command_args->GetString(kOutput, &output_rel_path) || 72 if (!command_args->GetString(kOutput, &output_rel_path) ||
168 !command_args->GetString(kSha256, &output_sha256_)) { 73 !command_args->GetString(kSha256, &output_sha256_)) {
169 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0); 74 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0);
170 return; 75 return;
171 } 76 }
172 77
173 output_abs_path_ = 78 output_abs_path_ =
174 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); 79 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 uint8 actual_hash[crypto::kSHA256Length] = {0}; 120 uint8 actual_hash[crypto::kSHA256Length] = {0};
216 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); 121 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256));
217 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); 122 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length());
218 hasher->Finish(actual_hash, sizeof(actual_hash)); 123 hasher->Finish(actual_hash, sizeof(actual_hash));
219 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) 124 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash)))
220 return ComponentUnpacker::kDeltaVerificationFailure; 125 return ComponentUnpacker::kDeltaVerificationFailure;
221 126
222 return ComponentUnpacker::kNone; 127 return ComponentUnpacker::kNone;
223 } 128 }
224 129
225 bool DeltaUpdateOp::InProcess() {
226 return in_process_;
227 }
228
229 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() { 130 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() {
230 return task_runner_; 131 return task_runner_;
231 } 132 }
232 133
233 DeltaUpdateOpCopy::DeltaUpdateOpCopy() { 134 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
234 } 135 }
235 136
236 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { 137 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
237 } 138 }
238 139
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return ComponentUnpacker::kNone; 178 return ComponentUnpacker::kNone;
278 } 179 }
279 180
280 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) { 181 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) {
281 if (!base::Move(patch_abs_path_, output_abs_path_)) 182 if (!base::Move(patch_abs_path_, output_abs_path_))
282 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); 183 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
283 else 184 else
284 callback.Run(ComponentUnpacker::kNone, 0); 185 callback.Run(ComponentUnpacker::kNone, 0);
285 } 186 }
286 187
287 DeltaUpdateOpPatchHost::DeltaUpdateOpPatchHost(
288 scoped_refptr<DeltaUpdateOpPatch> patcher,
289 scoped_refptr<base::SequencedTaskRunner> task_runner)
290 : patcher_(patcher), task_runner_(task_runner) {
291 }
292
293 DeltaUpdateOpPatchHost::~DeltaUpdateOpPatchHost() {
294 }
295
296 void DeltaUpdateOpPatchHost::StartProcess(scoped_ptr<IPC::Message> message) {
297 // The DeltaUpdateOpPatchHost is not responsible for deleting the
298 // UtilityProcessHost object.
299 content::UtilityProcessHost* host = content::UtilityProcessHost::Create(
300 this, base::MessageLoopProxy::current().get());
301 host->DisableSandbox();
302 host->Send(message.release());
303 }
304
305 bool DeltaUpdateOpPatchHost::OnMessageReceived(const IPC::Message& message) {
306 bool handled = true;
307 IPC_BEGIN_MESSAGE_MAP(DeltaUpdateOpPatchHost, message)
308 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Succeeded,
309 OnPatchSucceeded)
310 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PatchFile_Failed,
311 OnPatchFailed)
312 IPC_MESSAGE_UNHANDLED(handled = false)
313 IPC_END_MESSAGE_MAP()
314 return handled;
315 }
316
317 void DeltaUpdateOpPatchHost::OnPatchSucceeded() {
318 task_runner_->PostTask(FROM_HERE,
319 base::Bind(&DeltaUpdateOpPatch::DonePatching,
320 patcher_,
321 ComponentUnpacker::kNone,
322 0));
323 task_runner_ = NULL;
324 patcher_ = NULL;
325 }
326
327 void DeltaUpdateOpPatchHost::OnPatchFailed(int error_code) {
328 task_runner_->PostTask(FROM_HERE,
329 base::Bind(&DeltaUpdateOpPatch::DonePatching,
330 patcher_,
331 ComponentUnpacker::kDeltaOperationFailure,
332 error_code));
333 task_runner_ = NULL;
334 patcher_ = NULL;
335 }
336
337 void DeltaUpdateOpPatchHost::OnProcessCrashed(int exit_code) {
338 task_runner_->PostTask(
339 FROM_HERE,
340 base::Bind(&DeltaUpdateOpPatch::DonePatching,
341 patcher_,
342 ComponentUnpacker::kDeltaPatchProcessFailure,
343 exit_code));
344 task_runner_ = NULL;
345 patcher_ = NULL;
346 }
347
348 DeltaUpdateOpPatch::DeltaUpdateOpPatch( 188 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
349 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy) { 189 const std::string& operation,
350 strategy_ = strategy.Pass(); 190 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher)
191 : operation_(operation), out_of_process_patcher_(out_of_process_patcher) {
192 DCHECK(operation == kBsdiff || operation == kCourgette);
351 } 193 }
352 194
353 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() { 195 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
354 } 196 }
355 197
356 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments( 198 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments(
357 const base::DictionaryValue* command_args, 199 const base::DictionaryValue* command_args,
358 const base::FilePath& input_dir, 200 const base::FilePath& input_dir,
359 ComponentInstaller* installer) { 201 ComponentInstaller* installer) {
360 std::string patch_rel_path; 202 std::string patch_rel_path;
361 std::string input_rel_path; 203 std::string input_rel_path;
362 if (!command_args->GetString(kPatch, &patch_rel_path) || 204 if (!command_args->GetString(kPatch, &patch_rel_path) ||
363 !command_args->GetString(kInput, &input_rel_path)) 205 !command_args->GetString(kInput, &input_rel_path))
364 return ComponentUnpacker::kDeltaBadCommands; 206 return ComponentUnpacker::kDeltaBadCommands;
365 207
366 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) 208 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
367 return ComponentUnpacker::kDeltaMissingExistingFile; 209 return ComponentUnpacker::kDeltaMissingExistingFile;
368 210
369 patch_abs_path_ = 211 patch_abs_path_ =
370 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); 212 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
371 213
372 return ComponentUnpacker::kNone; 214 return ComponentUnpacker::kNone;
373 } 215 }
374 216
375 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) { 217 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) {
376 callback_ = callback; 218 if (out_of_process_patcher_.get()) {
377 if (!InProcess()) { 219 out_of_process_patcher_->Patch(
378 host_ = new DeltaUpdateOpPatchHost(scoped_refptr<DeltaUpdateOpPatch>(this), 220 operation_,
379 GetTaskRunner()); 221 GetTaskRunner(),
380 content::BrowserThread::PostTask( 222 input_abs_path_,
381 content::BrowserThread::IO, 223 patch_abs_path_,
382 FROM_HERE, 224 output_abs_path_,
383 base::Bind(&DeltaUpdateOpPatchHost::StartProcess, 225 base::Bind(&DeltaUpdateOpPatch::DonePatching, this, callback));
384 host_,
385 base::Passed(strategy_->GetPatchMessage(input_abs_path_,
386 patch_abs_path_,
387 output_abs_path_))));
388 return; 226 return;
389 } 227 }
390 const int result = strategy_->Patch(input_abs_path_, 228
391 patch_abs_path_, 229 if (operation_ == kBsdiff) {
392 output_abs_path_); 230 DonePatching(callback,
393 if (result == strategy_->GetSuccessCode()) 231 courgette::ApplyBinaryPatch(
394 DonePatching(ComponentUnpacker::kNone, 0); 232 input_abs_path_, patch_abs_path_, output_abs_path_));
395 else 233 } else if (operation_ == kCourgette) {
396 DonePatching(ComponentUnpacker::kDeltaOperationFailure, result); 234 DonePatching(
235 callback,
236 courgette::ApplyEnsemblePatch(input_abs_path_.value().c_str(),
237 patch_abs_path_.value().c_str(),
238 output_abs_path_.value().c_str()));
239 } else {
240 NOTREACHED();
241 }
397 } 242 }
398 243
399 void DeltaUpdateOpPatch::DonePatching(ComponentUnpacker::Error error, 244 void DeltaUpdateOpPatch::DonePatching(
400 int error_code) { 245 const ComponentUnpacker::Callback& callback,
401 host_ = NULL; 246 int result) {
402 if (error != ComponentUnpacker::kNone) { 247 if (operation_ == kBsdiff) {
403 error_code += strategy_->GetErrorOffset(); 248 if (result == courgette::OK) {
249 callback.Run(ComponentUnpacker::kNone, 0);
250 } else {
251 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
252 result + kBsdiffErrorOffset);
253 }
254 } else if (operation_ == kCourgette) {
255 if (result == courgette::C_OK) {
256 callback.Run(ComponentUnpacker::kNone, 0);
257 } else {
258 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
259 result + kCourgetteErrorOffset);
260 }
261 } else {
262 NOTREACHED();
404 } 263 }
405 callback_.Run(error, error_code);
406 // The callback is no longer needed - it is best to release it in case it
407 // contains a reference to this object.
408 callback_.Reset();
409 } 264 }
410 265
411 } // namespace component_updater 266 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698