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

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: run git cl format and git cl lint 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 DeltaUpdateOp* CreateDeltaUpdateOp(
126 } 39 const std::string& operation,
127 40 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher) {
128 DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation) {
129 if (operation == "copy") { 41 if (operation == "copy") {
130 return new DeltaUpdateOpCopy(); 42 return new DeltaUpdateOpCopy();
131 } else if (operation == "create") { 43 } else if (operation == "create") {
132 return new DeltaUpdateOpCreate(); 44 return new DeltaUpdateOpCreate();
133 } else if (operation == "bsdiff") { 45 } else if (operation == "bsdiff" || operation == "courgette") {
134 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy(new BsdiffTraits()); 46 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 } 47 }
140 return NULL; 48 return NULL;
141 } 49 }
142 50
143 DeltaUpdateOp* CreateDeltaUpdateOp(const base::DictionaryValue& command) { 51 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 } 52 }
152 53
153 DeltaUpdateOp::~DeltaUpdateOp() { 54 DeltaUpdateOp::~DeltaUpdateOp() {
154 } 55 }
155 56
156 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args, 57 void DeltaUpdateOp::Run(const base::DictionaryValue* command_args,
157 const base::FilePath& input_dir, 58 const base::FilePath& input_dir,
158 const base::FilePath& unpack_dir, 59 const base::FilePath& unpack_dir,
159 ComponentInstaller* installer, 60 ComponentInstaller* installer,
160 bool in_process,
161 const ComponentUnpacker::Callback& callback, 61 const ComponentUnpacker::Callback& callback,
162 scoped_refptr<base::SequencedTaskRunner> task_runner) { 62 scoped_refptr<base::SequencedTaskRunner> task_runner) {
163 callback_ = callback; 63 callback_ = callback;
164 in_process_ = in_process;
165 task_runner_ = task_runner; 64 task_runner_ = task_runner;
166 std::string output_rel_path; 65 std::string output_rel_path;
167 if (!command_args->GetString(kOutput, &output_rel_path) || 66 if (!command_args->GetString(kOutput, &output_rel_path) ||
168 !command_args->GetString(kSha256, &output_sha256_)) { 67 !command_args->GetString(kSha256, &output_sha256_)) {
169 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0); 68 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0);
170 return; 69 return;
171 } 70 }
172 71
173 output_abs_path_ = 72 output_abs_path_ =
174 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path)); 73 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}; 114 uint8 actual_hash[crypto::kSHA256Length] = {0};
216 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); 115 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256));
217 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); 116 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length());
218 hasher->Finish(actual_hash, sizeof(actual_hash)); 117 hasher->Finish(actual_hash, sizeof(actual_hash));
219 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash))) 118 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash)))
220 return ComponentUnpacker::kDeltaVerificationFailure; 119 return ComponentUnpacker::kDeltaVerificationFailure;
221 120
222 return ComponentUnpacker::kNone; 121 return ComponentUnpacker::kNone;
223 } 122 }
224 123
225 bool DeltaUpdateOp::InProcess() {
226 return in_process_;
227 }
228
229 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() { 124 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() {
230 return task_runner_; 125 return task_runner_;
231 } 126 }
232 127
233 DeltaUpdateOpCopy::DeltaUpdateOpCopy() { 128 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
234 } 129 }
235 130
236 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() { 131 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
237 } 132 }
238 133
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 return ComponentUnpacker::kNone; 172 return ComponentUnpacker::kNone;
278 } 173 }
279 174
280 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) { 175 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) {
281 if (!base::Move(patch_abs_path_, output_abs_path_)) 176 if (!base::Move(patch_abs_path_, output_abs_path_))
282 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0); 177 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
283 else 178 else
284 callback.Run(ComponentUnpacker::kNone, 0); 179 callback.Run(ComponentUnpacker::kNone, 0);
285 } 180 }
286 181
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( 182 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
349 scoped_ptr<DeltaUpdateOpPatchStrategy> strategy) { 183 const std::string& operation,
350 strategy_ = strategy.Pass(); 184 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher)
185 : operation_(operation), out_of_process_patcher_(out_of_process_patcher) {
186 DCHECK(operation == kBsdiff || operation == kCourgette);
351 } 187 }
352 188
353 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() { 189 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
354 } 190 }
355 191
356 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments( 192 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments(
357 const base::DictionaryValue* command_args, 193 const base::DictionaryValue* command_args,
358 const base::FilePath& input_dir, 194 const base::FilePath& input_dir,
359 ComponentInstaller* installer) { 195 ComponentInstaller* installer) {
360 std::string patch_rel_path; 196 std::string patch_rel_path;
361 std::string input_rel_path; 197 std::string input_rel_path;
362 if (!command_args->GetString(kPatch, &patch_rel_path) || 198 if (!command_args->GetString(kPatch, &patch_rel_path) ||
363 !command_args->GetString(kInput, &input_rel_path)) 199 !command_args->GetString(kInput, &input_rel_path))
364 return ComponentUnpacker::kDeltaBadCommands; 200 return ComponentUnpacker::kDeltaBadCommands;
365 201
366 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) 202 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
367 return ComponentUnpacker::kDeltaMissingExistingFile; 203 return ComponentUnpacker::kDeltaMissingExistingFile;
368 204
369 patch_abs_path_ = 205 patch_abs_path_ =
370 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path)); 206 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
371 207
372 return ComponentUnpacker::kNone; 208 return ComponentUnpacker::kNone;
373 } 209 }
374 210
375 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) { 211 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) {
376 callback_ = callback; 212 if (out_of_process_patcher_.get()) {
377 if (!InProcess()) { 213 out_of_process_patcher_->Patch(
378 host_ = new DeltaUpdateOpPatchHost(scoped_refptr<DeltaUpdateOpPatch>(this), 214 operation_,
379 GetTaskRunner()); 215 GetTaskRunner(),
380 content::BrowserThread::PostTask( 216 input_abs_path_,
381 content::BrowserThread::IO, 217 patch_abs_path_,
382 FROM_HERE, 218 output_abs_path_,
383 base::Bind(&DeltaUpdateOpPatchHost::StartProcess, 219 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; 220 return;
389 } 221 }
390 const int result = strategy_->Patch(input_abs_path_, 222
391 patch_abs_path_, 223 if (operation_ == kBsdiff) {
392 output_abs_path_); 224 DonePatching(callback,
393 if (result == strategy_->GetSuccessCode()) 225 courgette::ApplyBinaryPatch(
394 DonePatching(ComponentUnpacker::kNone, 0); 226 input_abs_path_, patch_abs_path_, output_abs_path_));
395 else 227 } else if (operation_ == kCourgette) {
396 DonePatching(ComponentUnpacker::kDeltaOperationFailure, result); 228 DonePatching(
229 callback,
230 courgette::ApplyEnsemblePatch(input_abs_path_.value().c_str(),
231 patch_abs_path_.value().c_str(),
232 output_abs_path_.value().c_str()));
233 } else {
234 NOTREACHED();
235 }
397 } 236 }
398 237
399 void DeltaUpdateOpPatch::DonePatching(ComponentUnpacker::Error error, 238 void DeltaUpdateOpPatch::DonePatching(
400 int error_code) { 239 const ComponentUnpacker::Callback& callback,
401 host_ = NULL; 240 int result) {
402 if (error != ComponentUnpacker::kNone) { 241 if (operation_ == kBsdiff) {
403 error_code += strategy_->GetErrorOffset(); 242 if (result == courgette::OK) {
243 callback.Run(ComponentUnpacker::kNone, 0);
244 } else {
245 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
246 result + kBsdiffErrorOffset);
247 }
248 } else if (operation_ == kCourgette) {
249 if (result == courgette::C_OK) {
250 callback.Run(ComponentUnpacker::kNone, 0);
251 } else {
252 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
253 result + kCourgetteErrorOffset);
254 }
255 } else {
256 NOTREACHED();
404 } 257 }
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 } 258 }
410 259
411 } // namespace component_updater 260 } // namespace component_updater
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698