- add sources.
[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 {
17
18 // Deserialize the commands file (present in delta update packages). The top
19 // level must be a list.
20 base::ListValue* ReadCommands(const base::FilePath& unpack_path) {
21   const base::FilePath commands =
22       unpack_path.Append(FILE_PATH_LITERAL("commands.json"));
23   if (!base::PathExists(commands))
24     return NULL;
25
26   JSONFileValueSerializer serializer(commands);
27   scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL));
28
29   return (root.get() && root->IsType(base::Value::TYPE_LIST)) ?
30       static_cast<base::ListValue*>(root.release()) : NULL;
31 }
32
33 }  // namespace
34
35
36 // The patching support is not cross-platform at the moment.
37 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {}
38
39 ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch(
40     PatchType patch_type,
41     const base::FilePath& input_file,
42     const base::FilePath& patch_file,
43     const base::FilePath& output_file,
44     int* error) {
45   return ComponentUnpacker::kDeltaUnsupportedCommand;
46 }
47
48
49 // Takes the contents of a differential component update in input_dir
50 // and produces the contents of a full component update in unpack_dir
51 // using input_abs_path_ files that the installer knows about.
52 ComponentUnpacker::Error DifferentialUpdatePatch(
53     const base::FilePath& input_dir,
54     const base::FilePath& unpack_dir,
55     ComponentPatcher* patcher,
56     ComponentInstaller* installer,
57     int* error) {
58   *error = 0;
59   scoped_ptr<base::ListValue> commands(ReadCommands(input_dir));
60   if (!commands.get())
61     return ComponentUnpacker::kDeltaBadCommands;
62
63   for (base::ValueVector::const_iterator command = commands->begin(),
64       end = commands->end(); command != end; command++) {
65     if (!(*command)->IsType(base::Value::TYPE_DICTIONARY))
66       return ComponentUnpacker::kDeltaBadCommands;
67     base::DictionaryValue* command_args =
68         static_cast<base::DictionaryValue*>(*command);
69     scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args));
70     if (!operation)
71       return ComponentUnpacker::kDeltaUnsupportedCommand;
72
73     ComponentUnpacker::Error result = operation->Run(
74         command_args, input_dir, unpack_dir, patcher, installer, error);
75     if (result != ComponentUnpacker::kNone)
76       return result;
77   }
78
79   return ComponentUnpacker::kNone;
80 }
81