OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/files/memory_mapped_file.h" | |
12 #include "base/json/json_file_value_serializer.h" | |
13 #include "base/memory/scoped_handle.h" | |
14 #include "base/path_service.h" | |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "chrome/browser/component_updater/component_patcher_internal.h" | |
17 #include "chrome/browser/component_updater/component_updater_service.h" | |
18 #include "chrome/common/extensions/extension_constants.h" | |
19 #include "crypto/secure_hash.h" | |
20 #include "crypto/sha2.h" | |
21 #include "crypto/signature_verifier.h" | |
22 #include "extensions/common/crx_file.h" | |
23 #include "third_party/zlib/google/zip.h" | |
24 | |
25 using crypto::SecureHash; | |
26 | |
27 namespace { | |
28 | |
29 DeltaUpdateOp* CreateDeltaUpdateOp(base::ListValue* command) { | |
30 std::string operation; | |
31 if (!command->GetString(DeltaUpdateOp::kDeltaOperation, &operation)) | |
32 return NULL; | |
33 if (!operation.compare("copy")) { | |
cpu_(ooo_6.6-7.5)
2013/05/29 20:06:44
this style is hard to read can we do if (operation
waffles
2013/06/13 20:55:04
Done.
| |
34 return new DeltaUpdateOpCopy(); | |
35 } else if (!operation.compare("create")) { | |
36 return new DeltaUpdateOpCreate(); | |
37 } else if (!operation.compare("bsdiff")) { | |
38 return new DeltaUpdateOpPatchBsdiff(); | |
39 } else if (!operation.compare("courgette")) { | |
40 return new DeltaUpdateOpPatchCourgette(); | |
41 } | |
42 return NULL; | |
43 } | |
44 | |
45 // Deserialize the commands file (present in delta update packages). The top | |
46 // level must be a list. | |
47 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { | |
48 const base::FilePath commands = | |
49 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); | |
50 if (!file_util::PathExists(commands)) | |
51 return NULL; | |
52 JSONFileValueSerializer serializer(commands); | |
53 std::string error; | |
54 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); | |
55 if (!root.get()) | |
56 return NULL; | |
57 if (!root->IsType(base::Value::TYPE_LIST)) | |
58 return NULL; | |
59 return static_cast<base::ListValue*>(root.release()); | |
60 } | |
61 | |
62 } // namespace. | |
63 | |
64 | |
65 // The patching support is not cross-platform at the moment. | |
66 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} | |
67 | |
68 DeltaUpdateResult ComponentPatcherCrossPlatform::Patch( | |
69 PatchType patch_type, | |
70 const base::FilePath& input_file, | |
71 const base::FilePath& patch_file, | |
72 const base::FilePath& output_file, | |
73 int* error) { | |
74 return DELTA_OPERATION_FAILURE; | |
75 } | |
76 | |
77 DeltaUpdateOp::~DeltaUpdateOp() {} | |
78 | |
79 DeltaUpdateOp::DeltaUpdateOp() {} | |
80 | |
81 DeltaUpdateResult DeltaUpdateOp::Run(base::ListValue* command_args, | |
82 const base::FilePath& input_dir, | |
83 const base::FilePath& unpack_dir, | |
84 ComponentPatcher* patcher, | |
85 ComponentInstaller* installer, | |
86 int* error) { | |
87 // Parse the arguments. | |
88 std::string output_rel_path; | |
89 if (!command_args->GetString(kDeltaOutputPath, &output_rel_path) || | |
90 !command_args->GetString(kDeltaOutputSha256, &output_sha256_)) | |
91 return DELTA_BAD_COMMANDS; | |
92 output_abs_path_ = unpack_dir.Append( | |
93 base::FilePath::FromUTF8Unsafe(output_rel_path)); | |
94 DeltaUpdateResult parse_result = DoParseArguments( | |
95 command_args, input_dir, installer); | |
96 if (parse_result != DELTA_OK) | |
97 return parse_result; | |
98 | |
99 // Confirm that the output directory exists. | |
100 const base::FilePath parent = output_abs_path_.DirName(); | |
101 if (!file_util::DirectoryExists(parent)) { | |
102 if (!file_util::CreateDirectory(parent)) | |
103 return DELTA_IO_ERROR; | |
104 } | |
105 | |
106 // Run the actual operation, and verify the output. | |
107 DeltaUpdateResult run_result = DoRun(patcher, error); | |
108 if (run_result != DELTA_OK) | |
109 return run_result; | |
110 return CheckHash(); | |
111 } | |
112 | |
113 // Uses the hash as a checksum to confirm that the file now residing in the | |
114 // output directory probably has the contents it should. | |
115 DeltaUpdateResult DeltaUpdateOp::CheckHash() { | |
116 std::vector<uint8> expected_hash(0); | |
117 if (!base::HexStringToBytes(output_sha256_, &expected_hash) || | |
118 expected_hash.size() != crypto::kSHA256Length) | |
119 return DELTA_VERIFICATION_INPUT_ERROR; | |
120 uint8 actual_hash[crypto::kSHA256Length] = {0}; | |
121 base::MemoryMappedFile output_file_mmapped; | |
122 if (!output_file_mmapped.Initialize(output_abs_path_)) | |
123 return DELTA_VERIFICATION_INPUT_ERROR; | |
124 const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); | |
125 hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); | |
126 hasher->Finish(actual_hash, sizeof(actual_hash)); | |
127 if (memcmp(actual_hash, &expected_hash[0], crypto::kSHA256Length)) | |
128 return DELTA_VERIFICATION_FAILURE; | |
129 return DELTA_OK; | |
130 } | |
131 | |
132 DeltaUpdateOpCopy::DeltaUpdateOpCopy() {} | |
133 | |
134 DeltaUpdateResult DeltaUpdateOpCopy::DoParseArguments( | |
135 base::ListValue* command_args, | |
136 const base::FilePath& input_dir, | |
137 ComponentInstaller* installer) { | |
138 std::string input_rel_path; // The relative location for the existing file. | |
139 if (!command_args->GetString(kDeltaFrom, &input_rel_path) || | |
cpu_(ooo_6.6-7.5)
2013/05/29 20:06:44
In the same vein of error handling (see my comment
waffles
2013/06/13 20:55:04
Done.
| |
140 !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | |
141 return DELTA_BAD_COMMANDS; | |
142 return DELTA_OK; | |
143 } | |
144 | |
145 DeltaUpdateResult DeltaUpdateOpCopy::DoRun(ComponentPatcher*, int* error) { | |
146 *error = 0; | |
147 if (!file_util::CopyFile(input_abs_path_, output_abs_path_)) | |
148 return DELTA_OPERATION_FAILURE; | |
149 return DELTA_OK; | |
150 } | |
151 | |
152 DeltaUpdateOpCreate::DeltaUpdateOpCreate() {} | |
153 | |
154 DeltaUpdateResult DeltaUpdateOpCreate::DoParseArguments( | |
155 base::ListValue* command_args, | |
156 const base::FilePath& input_dir, | |
157 ComponentInstaller* installer) { | |
158 std::string patch_rel_path; // The relative location for the new file. | |
159 if (!command_args->GetString(kDeltaPatch, &patch_rel_path)) | |
160 return DELTA_BAD_COMMANDS; | |
161 patch_abs_path_ = input_dir.Append( | |
162 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | |
163 return DELTA_OK; | |
164 } | |
165 | |
166 DeltaUpdateResult DeltaUpdateOpCreate::DoRun(ComponentPatcher*, int* error) { | |
167 *error = 0; | |
168 if (!file_util::Move(patch_abs_path_, output_abs_path_)) | |
169 return DELTA_OPERATION_FAILURE; | |
170 return DELTA_OK; | |
171 } | |
172 | |
173 DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {} | |
174 | |
175 DeltaUpdateResult DeltaUpdateOpPatchBsdiff::DoParseArguments( | |
176 base::ListValue* command_args, | |
177 const base::FilePath& input_dir, | |
178 ComponentInstaller* installer) { | |
179 std::string patch_rel_path; // The relative location for the patch file. | |
180 std::string input_rel_path; // The relative location for the existing file. | |
181 if (!command_args->GetString(kDeltaPatch, &patch_rel_path) || | |
cpu_(ooo_6.6-7.5)
2013/05/29 20:06:44
comment of line 179 and 180 seem unnecessary.
waffles
2013/06/13 20:55:04
Done.
| |
182 !command_args->GetString(kDeltaFrom, &input_rel_path) || | |
183 !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | |
184 return DELTA_BAD_COMMANDS; | |
185 patch_abs_path_ = input_dir.Append( | |
186 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | |
187 return DELTA_OK; | |
188 } | |
189 | |
190 DeltaUpdateResult DeltaUpdateOpPatchBsdiff::DoRun(ComponentPatcher* patcher, | |
191 int* error) { | |
192 *error = 0; | |
193 return patcher->Patch(PATCH_TYPE_BSDIFF, | |
194 input_abs_path_, | |
195 patch_abs_path_, | |
196 output_abs_path_, | |
197 error); | |
198 } | |
199 | |
200 DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {} | |
201 | |
202 DeltaUpdateResult DeltaUpdateOpPatchCourgette::DoParseArguments( | |
203 base::ListValue* command_args, | |
204 const base::FilePath& input_dir, | |
205 ComponentInstaller* installer) { | |
206 std::string patch_rel_path; // The relative location for the patch file. | |
207 std::string input_rel_path; // The relative location for the existing file. | |
208 if (!command_args->GetString(kDeltaPatch, &patch_rel_path) || | |
209 !command_args->GetString(kDeltaFrom, &input_rel_path) || | |
210 !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) | |
211 return DELTA_BAD_COMMANDS; | |
212 patch_abs_path_ = input_dir.Append( | |
213 base::FilePath::FromUTF8Unsafe(patch_rel_path)); | |
214 return DELTA_OK; | |
215 } | |
216 | |
217 DeltaUpdateResult DeltaUpdateOpPatchCourgette::DoRun(ComponentPatcher* patcher, | |
218 int* error) { | |
219 *error = 0; | |
220 return patcher->Patch(PATCH_TYPE_COURGETTE, | |
221 input_abs_path_, | |
222 patch_abs_path_, | |
223 output_abs_path_, | |
224 error); | |
225 } | |
226 | |
227 // Takes the contents of a differential component update in input_dir | |
228 // and produces the contents of a full component update in unpack_dir | |
229 // using input_abs_path_ files that the installer knows about. | |
230 DeltaUpdateResult DifferentialUpdatePatch(const base::FilePath& input_dir, | |
231 const base::FilePath& unpack_dir, | |
232 ComponentPatcher* patcher, | |
233 ComponentInstaller* installer, | |
234 int* error) { | |
235 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); | |
236 if (!commands.get()) { | |
cpu_(ooo_6.6-7.5)
2013/05/29 20:06:44
not sure if we like braces for single line ifs aga
waffles
2013/06/13 20:55:04
Done with great fear.
| |
237 return DELTA_BAD_COMMANDS; | |
238 } | |
239 // Do the actual patching: recreate the contents of the full CRX. | |
240 for (base::ValueVector::const_iterator command = commands->begin(), | |
241 end = commands->end(); command != end; command++) { | |
242 if (!(*command)->IsType(base::Value::TYPE_LIST)) { | |
243 return DELTA_BAD_COMMANDS; | |
cpu_(ooo_6.6-7.5)
2013/05/29 20:06:44
note that here and and in 248 we return without se
waffles
2013/06/13 20:55:04
We would like the specific error code of bsdiff or
| |
244 } | |
245 base::ListValue* command_args = static_cast<base::ListValue*>(*command); | |
246 scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); | |
247 if (!operation) { | |
248 return DELTA_UNSUPPORTED_COMMAND; | |
249 } | |
250 DeltaUpdateResult result = operation->Run( | |
251 command_args, input_dir, unpack_dir, patcher, installer, error); | |
252 if (result != DELTA_OK) { | |
253 return result; | |
254 } | |
255 } | |
256 return DELTA_OK; | |
257 } | |
258 | |
OLD | NEW |