Index: chrome/browser/component_updater/component_patcher.cc |
diff --git a/chrome/browser/component_updater/component_patcher.cc b/chrome/browser/component_updater/component_patcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ad75bf8502e133e60c2e93bc8426e7724f41694d |
--- /dev/null |
+++ b/chrome/browser/component_updater/component_patcher.cc |
@@ -0,0 +1,265 @@ |
+// 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 { |
+ |
+const char kInput[] = "input"; |
+const char kOp[] = "op"; |
+const char kOutput[] = "output"; |
+const char kPatch[] = "patch"; |
+const char kSha256[] = "sha256"; |
+ |
+DeltaUpdateOp* CreateDeltaUpdateOp(base::DictionaryValue* command) { |
+ std::string operation; |
+ if (!command->GetString(kOp, &operation)) |
+ return NULL; |
+ if (operation == "copy") |
+ return new DeltaUpdateOpCopy(); |
+ else if (operation == "create") |
+ return new DeltaUpdateOpCreate(); |
+ else if (operation == "bsdiff") |
+ return new DeltaUpdateOpPatchBsdiff(); |
+ else if (operation == "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; |
robertshield
2013/06/17 17:47:00
error isn't used, pass NULL to Deserialize instead
Sorin Jianu
2013/06/17 22:21:09
Done.
|
+ 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()); |
robertshield
2013/06/17 17:47:00
Could shorten the last five lines to:
return (roo
Sorin Jianu
2013/06/17 22:21:09
I prefer to keep it as is instead of the terse exp
robertshield
2013/06/18 13:27:07
How about a compromise:
if (!root.get()) || !root
Sorin Jianu
2013/06/18 17:00:11
Thank you Robert. Given the choice, I decided to f
|
+} |
+ |
+} // namespace. |
+ |
+ |
+// The patching support is not cross-platform at the moment. |
+ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} |
+ |
+ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch( |
+ PatchType patch_type, |
+ const base::FilePath& input_file, |
+ const base::FilePath& patch_file, |
+ const base::FilePath& output_file, |
+ int* error) { |
+ return ComponentUnpacker::kDeltaOperationFailure; |
cpu_(ooo_6.6-7.5)
2013/06/14 23:25:28
return kDeltaOperationUnsuported ?
waffles
2013/06/15 00:01:37
Done; will be in the next patch set.
|
+} |
+ |
+DeltaUpdateOp::~DeltaUpdateOp() {} |
+ |
+DeltaUpdateOp::DeltaUpdateOp() {} |
robertshield
2013/06/17 17:47:00
This is a relatively unconventional file structure
Sorin Jianu
2013/06/18 03:05:14
Done. Introduced component_patcher_operation.[h|cc
|
+ |
+ComponentUnpacker::Error DeltaUpdateOp::Run(base::DictionaryValue* command_args, |
+ const base::FilePath& input_dir, |
+ const base::FilePath& unpack_dir, |
+ ComponentPatcher* patcher, |
+ ComponentInstaller* installer, |
+ int* error) { |
cpu_(ooo_6.6-7.5)
2013/06/17 20:27:01
After trying to like the int* error (and its frien
Sorin Jianu
2013/06/17 22:21:09
The component patcher can give us valuable error i
|
+ std::string output_rel_path; |
+ if (!command_args->GetString(kOutput, &output_rel_path) || |
+ !command_args->GetString(kSha256, &output_sha256_)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ output_abs_path_ = unpack_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(output_rel_path)); |
+ ComponentUnpacker::Error parse_result = DoParseArguments( |
+ command_args, input_dir, installer); |
+ if (parse_result != ComponentUnpacker::kNone) |
+ return parse_result; |
+ |
+ const base::FilePath parent = output_abs_path_.DirName(); |
+ if (!file_util::DirectoryExists(parent)) { |
+ if (!file_util::CreateDirectory(parent)) |
+ return ComponentUnpacker::kIoError; |
+ } |
+ |
+ ComponentUnpacker::Error run_result = DoRun(patcher, error); |
+ if (run_result != ComponentUnpacker::kNone) |
+ 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. |
+ComponentUnpacker::Error DeltaUpdateOp::CheckHash() { |
+ std::vector<uint8> expected_hash(0); |
robertshield
2013/06/17 17:47:00
remove the (0), it is redundant
Sorin Jianu
2013/06/17 22:21:09
Done.
|
+ if (!base::HexStringToBytes(output_sha256_, &expected_hash) || |
+ expected_hash.size() != crypto::kSHA256Length) |
+ return ComponentUnpacker::kDeltaVerificationFailure; |
robertshield
2013/06/17 17:47:00
nit: I find this code a little too dense, and woul
Sorin Jianu
2013/06/17 22:21:09
Done.
|
+ uint8 actual_hash[crypto::kSHA256Length] = {0}; |
+ base::MemoryMappedFile output_file_mmapped; |
+ if (!output_file_mmapped.Initialize(output_abs_path_)) |
+ return ComponentUnpacker::kDeltaVerificationFailure; |
+ 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)) |
robertshield
2013/06/17 17:47:00
crypto::kSHA256Length -> arraysize(actual_hash)
Sorin Jianu
2013/06/17 22:21:09
replaced it with sizeof since we are doing a byte-
|
+ return ComponentUnpacker::kDeltaVerificationFailure; |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+DeltaUpdateOpCopy::DeltaUpdateOpCopy() {} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments( |
+ base::DictionaryValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string input_rel_path; |
+ if (!command_args->GetString(kInput, &input_rel_path)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return ComponentUnpacker::kDeltaMissingExistingFile; |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpCopy::DoRun(ComponentPatcher*, |
+ int* error) { |
+ *error = 0; |
+ if (!file_util::CopyFile(input_abs_path_, output_abs_path_)) |
+ return ComponentUnpacker::kDeltaOperationFailure; |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+DeltaUpdateOpCreate::DeltaUpdateOpCreate() {} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments( |
+ base::DictionaryValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; |
+ if (!command_args->GetString(kPatch, &patch_rel_path)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpCreate::DoRun(ComponentPatcher*, |
+ int* error) { |
+ *error = 0; |
+ if (!file_util::Move(patch_abs_path_, output_abs_path_)) |
+ return ComponentUnpacker::kDeltaOperationFailure; |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoParseArguments( |
+ base::DictionaryValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; |
+ std::string input_rel_path; |
+ if (!command_args->GetString(kPatch, &patch_rel_path) || |
+ !command_args->GetString(kInput, &input_rel_path)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return ComponentUnpacker::kDeltaMissingExistingFile; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoRun( |
+ ComponentPatcher* patcher, |
+ int* error) { |
+ *error = 0; |
+ return patcher->Patch(kPatchTypeBsdiff, |
+ input_abs_path_, |
+ patch_abs_path_, |
+ output_abs_path_, |
+ error); |
+} |
+ |
+DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoParseArguments( |
+ base::DictionaryValue* command_args, |
+ const base::FilePath& input_dir, |
+ ComponentInstaller* installer) { |
+ std::string patch_rel_path; |
+ std::string input_rel_path; |
+ if (!command_args->GetString(kPatch, &patch_rel_path) || |
+ !command_args->GetString(kInput, &input_rel_path)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_)) |
+ return ComponentUnpacker::kDeltaMissingExistingFile; |
+ patch_abs_path_ = input_dir.Append( |
+ base::FilePath::FromUTF8Unsafe(patch_rel_path)); |
+ return ComponentUnpacker::kNone; |
+} |
+ |
+ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoRun( |
+ ComponentPatcher* patcher, |
+ int* error) { |
+ *error = 0; |
+ return patcher->Patch(kPatchTypeCourgette, |
+ 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. |
+ComponentUnpacker::Error DifferentialUpdatePatch( |
+ const base::FilePath& input_dir, |
+ const base::FilePath& unpack_dir, |
+ ComponentPatcher* patcher, |
+ ComponentInstaller* installer, |
+ int* error) { |
+ *error = 0; |
+ scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); |
+ if (!commands.get()) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ for (base::ValueVector::const_iterator command = commands->begin(), |
+ end = commands->end(); command != end; command++) { |
+ if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) |
+ return ComponentUnpacker::kDeltaBadCommands; |
+ base::DictionaryValue* command_args = |
+ static_cast<base::DictionaryValue*>(*command); |
+ scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); |
+ if (!operation) |
+ return ComponentUnpacker::kDeltaUnsupportedCommand; |
+ ComponentUnpacker::Error result = operation->Run( |
+ command_args, input_dir, unpack_dir, patcher, installer, error); |
+ if (result != ComponentUnpacker::kNone) |
+ return result; |
+ } |
+ return ComponentUnpacker::kNone; |
+} |
+ |