Index: chrome/browser/component_updater/component_patcher.cc |
=================================================================== |
--- chrome/browser/component_updater/component_patcher.cc (revision 0) |
+++ chrome/browser/component_updater/component_patcher.cc (revision 0) |
@@ -0,0 +1,258 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/component_updater/component_patcher.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/file_util.h" |
+#include "base/files/memory_mapped_file.h" |
+#include "base/json/json_file_value_serializer.h" |
+#include "base/memory/scoped_handle.h" |
+#include "base/path_service.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "chrome/browser/component_updater/component_patcher_internal.h" |
+#include "chrome/browser/component_updater/component_updater_service.h" |
+#include "chrome/common/extensions/extension_constants.h" |
+#include "crypto/secure_hash.h" |
+#include "crypto/sha2.h" |
+#include "crypto/signature_verifier.h" |
+#include "extensions/common/crx_file.h" |
+#include "third_party/zlib/google/zip.h" |
+ |
+using crypto::SecureHash; |
+ |
+namespace { |
+ |
+DeltaUpdateOp* CreateDeltaUpdateOp(base::ListValue* command) { |
+ std::string operation; |
+ if (!command->GetString(DeltaUpdateOp::kDeltaOperation, &operation)) |
+ return NULL; |
+ 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.
|
+ return new DeltaUpdateOpCopy(); |
+ } else if (!operation.compare("create")) { |
+ return new DeltaUpdateOpCreate(); |
+ } else if (!operation.compare("bsdiff")) { |
+ return new DeltaUpdateOpPatchBsdiff(); |
+ } else if (!operation.compare("courgette")) { |
+ return new DeltaUpdateOpPatchCourgette(); |
+ } |
+ return NULL; |
+} |
+ |
+// Deserialize the commands file (present in delta update packages). The top |
+// level must be a list. |
+base::ListValue* ReadCommands(const base::FilePath& unpack_path) { |
+ const base::FilePath commands = |
+ unpack_path.Append(FILE_PATH_LITERAL("commands.json")); |
+ if (!file_util::PathExists(commands)) |
+ return NULL; |
+ JSONFileValueSerializer serializer(commands); |
+ std::string error; |
+ scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); |
+ if (!root.get()) |
+ return NULL; |
+ if (!root->IsType(base::Value::TYPE_LIST)) |
+ return NULL; |
+ return static_cast<base::ListValue*>(root.release()); |
+} |
+ |
+} // namespace. |
+ |
+ |
+// The patching support is not cross-platform at the moment. |
+ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} |
+ |
+DeltaUpdateResult ComponentPatcherCrossPlatform::Patch( |
+ PatchType patch_type, |
+ const base::FilePath& input_file, |
+ const base::FilePath& patch_file, |
+ const base::FilePath& output_file, |
+ int* error) { |
+ return DELTA_OPERATION_FAILURE; |
+} |
+ |
+DeltaUpdateOp::~DeltaUpdateOp() {} |
+ |
+DeltaUpdateOp::DeltaUpdateOp() {} |
+ |
+DeltaUpdateResult DeltaUpdateOp::Run(base::ListValue* command_args, |
+ const base::FilePath& input_dir, |
+ const base::FilePath& unpack_dir, |
+ ComponentPatcher* patcher, |
+ ComponentInstaller* installer, |
+ int* error) { |
+ // Parse the arguments. |
+ std::string output_rel_path; |
+ if (!command_args->GetString(kDeltaOutputPath, &output_rel_path) || |
+ !command_args->GetString(kDeltaOutputSha256, &output_sha256_)) |
+ return DELTA_BAD_COMMANDS; |
+ output_abs_path_ = unpack_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(output_rel_path)); |
+ DeltaUpdateResult parse_result = DoParseArguments( |
+ command_args, input_dir, installer); |
+ if (parse_result != DELTA_OK) |
+ return parse_result; |
+ |
+ // Confirm that the output directory exists. |
+ const base::FilePath parent = output_abs_path_.DirName(); |
+ if (!file_util::DirectoryExists(parent)) { |
+ if (!file_util::CreateDirectory(parent)) |
+ return DELTA_IO_ERROR; |
+ } |
+ |
+ // Run the actual operation, and verify the output. |
+ DeltaUpdateResult run_result = DoRun(patcher, error); |
+ if (run_result != DELTA_OK) |
+ return run_result; |
+ return CheckHash(); |
+} |
+ |
+// Uses the hash as a checksum to confirm that the file now residing in the |
+// output directory probably has the contents it should. |
+DeltaUpdateResult DeltaUpdateOp::CheckHash() { |
+ std::vector<uint8> expected_hash(0); |
+ if (!base::HexStringToBytes(output_sha256_, &expected_hash) || |
+ expected_hash.size() != crypto::kSHA256Length) |
+ return DELTA_VERIFICATION_INPUT_ERROR; |
+ uint8 actual_hash[crypto::kSHA256Length] = {0}; |
+ base::MemoryMappedFile output_file_mmapped; |
+ if (!output_file_mmapped.Initialize(output_abs_path_)) |
+ return DELTA_VERIFICATION_INPUT_ERROR; |
+ const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256)); |
+ hasher->Update(output_file_mmapped.data(), output_file_mmapped.length()); |
+ hasher->Finish(actual_hash, sizeof(actual_hash)); |
+ if (memcmp(actual_hash, &expected_hash[0], crypto::kSHA256Length)) |
+ return DELTA_VERIFICATION_FAILURE; |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateOpCopy::DeltaUpdateOpCopy() {} |
+ |
+DeltaUpdateResult DeltaUpdateOpCopy::DoParseArguments( |
+ base::ListValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string input_rel_path; // The relative location for the existing file. |
+ 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.
|
+ !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return DELTA_BAD_COMMANDS; |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateResult DeltaUpdateOpCopy::DoRun(ComponentPatcher*, int* error) { |
+ *error = 0; |
+ if (!file_util::CopyFile(input_abs_path_, output_abs_path_)) |
+ return DELTA_OPERATION_FAILURE; |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateOpCreate::DeltaUpdateOpCreate() {} |
+ |
+DeltaUpdateResult DeltaUpdateOpCreate::DoParseArguments( |
+ base::ListValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; // The relative location for the new file. |
+ if (!command_args->GetString(kDeltaPatch, &patch_rel_path)) |
+ return DELTA_BAD_COMMANDS; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateResult DeltaUpdateOpCreate::DoRun(ComponentPatcher*, int* error) { |
+ *error = 0; |
+ if (!file_util::Move(patch_abs_path_, output_abs_path_)) |
+ return DELTA_OPERATION_FAILURE; |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {} |
+ |
+DeltaUpdateResult DeltaUpdateOpPatchBsdiff::DoParseArguments( |
+ base::ListValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; // The relative location for the patch file. |
+ std::string input_rel_path; // The relative location for the existing file. |
+ 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.
|
+ !command_args->GetString(kDeltaFrom, &input_rel_path) || |
+ !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return DELTA_BAD_COMMANDS; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateResult DeltaUpdateOpPatchBsdiff::DoRun(ComponentPatcher* patcher, |
+ int* error) { |
+ *error = 0; |
+ return patcher->Patch(PATCH_TYPE_BSDIFF, |
+ input_abs_path_, |
+ patch_abs_path_, |
+ output_abs_path_, |
+ error); |
+} |
+ |
+DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {} |
+ |
+DeltaUpdateResult DeltaUpdateOpPatchCourgette::DoParseArguments( |
+ base::ListValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; // The relative location for the patch file. |
+ std::string input_rel_path; // The relative location for the existing file. |
+ if (!command_args->GetString(kDeltaPatch, &patch_rel_path) || |
+ !command_args->GetString(kDeltaFrom, &input_rel_path) || |
+ !installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return DELTA_BAD_COMMANDS; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return DELTA_OK; |
+} |
+ |
+DeltaUpdateResult DeltaUpdateOpPatchCourgette::DoRun(ComponentPatcher* patcher, |
+ int* error) { |
+ *error = 0; |
+ return patcher->Patch(PATCH_TYPE_COURGETTE, |
+ input_abs_path_, |
+ patch_abs_path_, |
+ output_abs_path_, |
+ error); |
+} |
+ |
+// Takes the contents of a differential component update in input_dir |
+// and produces the contents of a full component update in unpack_dir |
+// using input_abs_path_ files that the installer knows about. |
+DeltaUpdateResult DifferentialUpdatePatch(const base::FilePath& input_dir, |
+ const base::FilePath& unpack_dir, |
+ ComponentPatcher* patcher, |
+ ComponentInstaller* installer, |
+ int* error) { |
+ scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); |
+ 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.
|
+ return DELTA_BAD_COMMANDS; |
+ } |
+ // Do the actual patching: recreate the contents of the full CRX. |
+ for (base::ValueVector::const_iterator command = commands->begin(), |
+ end = commands->end(); command != end; command++) { |
+ if (!(*command)->IsType(base::Value::TYPE_LIST)) { |
+ 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
|
+ } |
+ base::ListValue* command_args = static_cast<base::ListValue*>(*command); |
+ scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); |
+ if (!operation) { |
+ return DELTA_UNSUPPORTED_COMMAND; |
+ } |
+ DeltaUpdateResult result = operation->Run( |
+ command_args, input_dir, unpack_dir, patcher, installer, error); |
+ if (result != DELTA_OK) { |
+ return result; |
+ } |
+ } |
+ return DELTA_OK; |
+} |
+ |