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
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/component_patcher.h"
10 #include "base/file_util.h"
11 #include "base/json/json_file_value_serializer.h"
12 #include "base/values.h"
13 #include "chrome/browser/component_updater/component_patcher_operation.h"
14 #include "chrome/browser/component_updater/component_updater_service.h"
16 namespace component_updater {
20 // Deserialize the commands file (present in delta update packages). The top
21 // level must be a list.
22 base::ListValue* ReadCommands(const base::FilePath& unpack_path) {
23 const base::FilePath commands =
24 unpack_path.Append(FILE_PATH_LITERAL("commands.json"));
25 if (!base::PathExists(commands))
28 JSONFileValueSerializer serializer(commands);
29 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
31 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ?
32 static_cast<base::ListValue*>(root.release()) : NULL;
37 // The patching support is not cross-platform at the moment.
38 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {}
40 ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch(
42 const base::FilePath& input_file,
43 const base::FilePath& patch_file,
44 const base::FilePath& output_file,
46 return ComponentUnpacker::kDeltaUnsupportedCommand;
50 // Takes the contents of a differential component update in input_dir
51 // and produces the contents of a full component update in unpack_dir
52 // using input_abs_path_ files that the installer knows about.
53 void DifferentialUpdatePatch(
54 const base::FilePath& input_dir,
55 const base::FilePath& unpack_dir,
56 ComponentPatcher* patcher,
57 ComponentInstaller* installer,
58 base::Callback<void(ComponentUnpacker::Error, int)> callback) {
60 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir));
61 if (!commands.get()) {
62 callback.Run(ComponentUnpacker::kDeltaBadCommands, error);
66 for (base::ValueVector::const_iterator command = commands->begin(),
67 end = commands->end(); command != end; command++) {
68 if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) {
69 callback.Run(ComponentUnpacker::kDeltaBadCommands, error);
72 base::DictionaryValue* command_args =
73 static_cast<base::DictionaryValue*>(*command);
74 scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args));
76 callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error);
80 ComponentUnpacker::Error result = operation->Run(
81 command_args, input_dir, unpack_dir, patcher, installer, &error);
82 if (result != ComponentUnpacker::kNone) {
83 callback.Run(result, error);
88 callback.Run(ComponentUnpacker::kNone, error);
91 } // namespace component_updater