Update activity log owners
[chromium-blink-merge.git] / components / update_client / component_patcher_operation.cc
blobe971533b40266ccd76c4f21bce2f4d068c0aeca8
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.
5 #include "components/update_client/component_patcher_operation.h"
7 #include <stdint.h>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/files/memory_mapped_file.h"
13 #include "base/location.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "components/update_client/component_patcher.h"
16 #include "components/update_client/update_client.h"
17 #include "courgette/courgette.h"
18 #include "courgette/third_party/bsdiff.h"
19 #include "crypto/secure_hash.h"
20 #include "crypto/sha2.h"
21 #include "crypto/signature_verifier.h"
23 using crypto::SecureHash;
25 namespace update_client {
27 namespace {
29 const char kOutput[] = "output";
30 const char kSha256[] = "sha256";
32 // The integer offset disambiguates between overlapping error ranges.
33 const int kCourgetteErrorOffset = 300;
34 const int kBsdiffErrorOffset = 600;
36 } // namespace
38 const char kOp[] = "op";
39 const char kBsdiff[] = "bsdiff";
40 const char kCourgette[] = "courgette";
41 const char kInput[] = "input";
42 const char kPatch[] = "patch";
44 DeltaUpdateOp* CreateDeltaUpdateOp(
45 const std::string& operation,
46 const scoped_refptr<OutOfProcessPatcher>& out_of_process_patcher) {
47 if (operation == "copy") {
48 return new DeltaUpdateOpCopy();
49 } else if (operation == "create") {
50 return new DeltaUpdateOpCreate();
51 } else if (operation == "bsdiff" || operation == "courgette") {
52 return new DeltaUpdateOpPatch(operation, out_of_process_patcher);
54 return NULL;
57 DeltaUpdateOp::DeltaUpdateOp() {
60 DeltaUpdateOp::~DeltaUpdateOp() {
63 void DeltaUpdateOp::Run(
64 const base::DictionaryValue* command_args,
65 const base::FilePath& input_dir,
66 const base::FilePath& unpack_dir,
67 const scoped_refptr<CrxInstaller>& installer,
68 const ComponentUnpacker::Callback& callback,
69 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
70 callback_ = callback;
71 task_runner_ = task_runner;
72 std::string output_rel_path;
73 if (!command_args->GetString(kOutput, &output_rel_path) ||
74 !command_args->GetString(kSha256, &output_sha256_)) {
75 DoneRunning(ComponentUnpacker::kDeltaBadCommands, 0);
76 return;
79 output_abs_path_ =
80 unpack_dir.Append(base::FilePath::FromUTF8Unsafe(output_rel_path));
81 ComponentUnpacker::Error parse_result =
82 DoParseArguments(command_args, input_dir, installer);
83 if (parse_result != ComponentUnpacker::kNone) {
84 DoneRunning(parse_result, 0);
85 return;
88 const base::FilePath parent = output_abs_path_.DirName();
89 if (!base::DirectoryExists(parent)) {
90 if (!base::CreateDirectory(parent)) {
91 DoneRunning(ComponentUnpacker::kIoError, 0);
92 return;
96 DoRun(base::Bind(&DeltaUpdateOp::DoneRunning,
97 scoped_refptr<DeltaUpdateOp>(this)));
100 void DeltaUpdateOp::DoneRunning(ComponentUnpacker::Error error,
101 int extended_error) {
102 if (error == ComponentUnpacker::kNone)
103 error = CheckHash();
104 task_runner_->PostTask(FROM_HERE,
105 base::Bind(callback_, error, extended_error));
106 callback_.Reset();
109 // Uses the hash as a checksum to confirm that the file now residing in the
110 // output directory probably has the contents it should.
111 ComponentUnpacker::Error DeltaUpdateOp::CheckHash() {
112 std::vector<uint8_t> expected_hash;
113 if (!base::HexStringToBytes(output_sha256_, &expected_hash) ||
114 expected_hash.size() != crypto::kSHA256Length)
115 return ComponentUnpacker::kDeltaVerificationFailure;
117 base::MemoryMappedFile output_file_mmapped;
118 if (!output_file_mmapped.Initialize(output_abs_path_))
119 return ComponentUnpacker::kDeltaVerificationFailure;
121 uint8_t actual_hash[crypto::kSHA256Length] = {0};
122 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256));
123 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length());
124 hasher->Finish(actual_hash, sizeof(actual_hash));
125 if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash)))
126 return ComponentUnpacker::kDeltaVerificationFailure;
128 return ComponentUnpacker::kNone;
131 scoped_refptr<base::SequencedTaskRunner> DeltaUpdateOp::GetTaskRunner() {
132 return task_runner_;
135 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {
138 DeltaUpdateOpCopy::~DeltaUpdateOpCopy() {
141 ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments(
142 const base::DictionaryValue* command_args,
143 const base::FilePath& input_dir,
144 const scoped_refptr<CrxInstaller>& installer) {
145 std::string input_rel_path;
146 if (!command_args->GetString(kInput, &input_rel_path))
147 return ComponentUnpacker::kDeltaBadCommands;
149 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
150 return ComponentUnpacker::kDeltaMissingExistingFile;
152 return ComponentUnpacker::kNone;
155 void DeltaUpdateOpCopy::DoRun(const ComponentUnpacker::Callback& callback) {
156 if (!base::CopyFile(input_abs_path_, output_abs_path_))
157 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
158 else
159 callback.Run(ComponentUnpacker::kNone, 0);
162 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {
165 DeltaUpdateOpCreate::~DeltaUpdateOpCreate() {
168 ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments(
169 const base::DictionaryValue* command_args,
170 const base::FilePath& input_dir,
171 const scoped_refptr<CrxInstaller>& installer) {
172 std::string patch_rel_path;
173 if (!command_args->GetString(kPatch, &patch_rel_path))
174 return ComponentUnpacker::kDeltaBadCommands;
176 patch_abs_path_ =
177 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
179 return ComponentUnpacker::kNone;
182 void DeltaUpdateOpCreate::DoRun(const ComponentUnpacker::Callback& callback) {
183 if (!base::Move(patch_abs_path_, output_abs_path_))
184 callback.Run(ComponentUnpacker::kDeltaOperationFailure, 0);
185 else
186 callback.Run(ComponentUnpacker::kNone, 0);
189 DeltaUpdateOpPatch::DeltaUpdateOpPatch(
190 const std::string& operation,
191 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher)
192 : operation_(operation), out_of_process_patcher_(out_of_process_patcher) {
193 DCHECK(operation == kBsdiff || operation == kCourgette);
196 DeltaUpdateOpPatch::~DeltaUpdateOpPatch() {
199 ComponentUnpacker::Error DeltaUpdateOpPatch::DoParseArguments(
200 const base::DictionaryValue* command_args,
201 const base::FilePath& input_dir,
202 const scoped_refptr<CrxInstaller>& installer) {
203 std::string patch_rel_path;
204 std::string input_rel_path;
205 if (!command_args->GetString(kPatch, &patch_rel_path) ||
206 !command_args->GetString(kInput, &input_rel_path))
207 return ComponentUnpacker::kDeltaBadCommands;
209 if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
210 return ComponentUnpacker::kDeltaMissingExistingFile;
212 patch_abs_path_ =
213 input_dir.Append(base::FilePath::FromUTF8Unsafe(patch_rel_path));
215 return ComponentUnpacker::kNone;
218 void DeltaUpdateOpPatch::DoRun(const ComponentUnpacker::Callback& callback) {
219 if (out_of_process_patcher_.get()) {
220 out_of_process_patcher_->Patch(
221 operation_, GetTaskRunner(), input_abs_path_, patch_abs_path_,
222 output_abs_path_,
223 base::Bind(&DeltaUpdateOpPatch::DonePatching, this, callback));
224 return;
227 if (operation_ == kBsdiff) {
228 DonePatching(callback,
229 courgette::ApplyBinaryPatch(input_abs_path_, patch_abs_path_,
230 output_abs_path_));
231 } else if (operation_ == kCourgette) {
232 DonePatching(callback, courgette::ApplyEnsemblePatch(
233 input_abs_path_.value().c_str(),
234 patch_abs_path_.value().c_str(),
235 output_abs_path_.value().c_str()));
236 } else {
237 NOTREACHED();
241 void DeltaUpdateOpPatch::DonePatching(
242 const ComponentUnpacker::Callback& callback,
243 int result) {
244 if (operation_ == kBsdiff) {
245 if (result == courgette::OK) {
246 callback.Run(ComponentUnpacker::kNone, 0);
247 } else {
248 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
249 result + kBsdiffErrorOffset);
251 } else if (operation_ == kCourgette) {
252 if (result == courgette::C_OK) {
253 callback.Run(ComponentUnpacker::kNone, 0);
254 } else {
255 callback.Run(ComponentUnpacker::kDeltaOperationFailure,
256 result + kCourgetteErrorOffset);
258 } else {
259 NOTREACHED();
263 } // namespace update_client