Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / component_patcher.cc
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.
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/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"
15
16 namespace component_updater {
17
18 namespace {
19
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))
26     return NULL;
27
28   JSONFileValueSerializer serializer(commands);
29   scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
30
31   return (root.get() && root->IsType(base::Value::TYPE_LIST)) ?
32       static_cast<base::ListValue*>(root.release()) : NULL;
33 }
34
35 }  // namespace
36
37 // The patching support is not cross-platform at the moment.
38 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {}
39
40 ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch(
41     PatchType patch_type,
42     const base::FilePath& input_file,
43     const base::FilePath& patch_file,
44     const base::FilePath& output_file,
45     int* error) {
46   return ComponentUnpacker::kDeltaUnsupportedCommand;
47 }
48
49
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) {
59   int error = 0;
60   scoped_ptr<base::ListValue> commands(ReadCommands(input_dir));
61   if (!commands.get()) {
62     callback.Run(ComponentUnpacker::kDeltaBadCommands, error);
63     return;
64   }
65
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);
70       return;
71     }
72     base::DictionaryValue* command_args =
73         static_cast<base::DictionaryValue*>(*command);
74     scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args));
75     if (!operation) {
76       callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error);
77       return;
78     }
79
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);
84       return;
85     }
86   }
87
88   callback.Run(ComponentUnpacker::kNone, error);
89 }
90
91 }  // namespace component_updater