OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/component_updater/component_patcher.h" | 5 #include "chrome/browser/component_updater/component_patcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | |
11 #include "base/file_util.h" | 10 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | |
13 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
14 #include "base/memory/weak_ptr.h" | |
15 #include "base/values.h" | 12 #include "base/values.h" |
16 #include "chrome/browser/component_updater/component_patcher_operation.h" | 13 #include "chrome/browser/component_updater/component_patcher_operation.h" |
17 #include "chrome/browser/component_updater/component_updater_service.h" | 14 #include "chrome/browser/component_updater/component_updater_service.h" |
18 #include "content/public/browser/browser_thread.h" | |
19 | 15 |
20 namespace component_updater { | 16 namespace component_updater { |
21 | 17 |
22 namespace { | 18 namespace { |
23 | 19 |
24 // Deserialize the commands file (present in delta update packages). The top | 20 // Deserialize the commands file (present in delta update packages). The top |
25 // level must be a list. | 21 // level must be a list. |
26 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { | 22 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { |
27 const base::FilePath commands = | 23 const base::FilePath commands = |
28 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); | 24 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); |
29 if (!base::PathExists(commands)) | 25 if (!base::PathExists(commands)) |
30 return NULL; | 26 return NULL; |
31 | 27 |
32 JSONFileValueSerializer serializer(commands); | 28 JSONFileValueSerializer serializer(commands); |
33 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 29 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
34 | 30 |
35 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? | 31 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? |
36 static_cast<base::ListValue*>(root.release()) : NULL; | 32 static_cast<base::ListValue*>(root.release()) : NULL; |
37 } | 33 } |
38 | 34 |
39 } // namespace | 35 } // namespace |
40 | 36 |
41 ComponentPatcher::ComponentPatcher( | 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( |
42 const base::FilePath& input_dir, | 54 const base::FilePath& input_dir, |
43 const base::FilePath& unpack_dir, | 55 const base::FilePath& unpack_dir, |
| 56 ComponentPatcher* patcher, |
44 ComponentInstaller* installer, | 57 ComponentInstaller* installer, |
45 bool in_process, | 58 base::Callback<void(ComponentUnpacker::Error, int)> callback) { |
46 scoped_refptr<base::SequencedTaskRunner> task_runner) | 59 int error = 0; |
47 : input_dir_(input_dir), | 60 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); |
48 unpack_dir_(unpack_dir), | 61 if (!commands.get()) { |
49 installer_(installer), | 62 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); |
50 in_process_(in_process), | |
51 task_runner_(task_runner) { | |
52 } | |
53 | |
54 ComponentPatcher::~ComponentPatcher() { | |
55 } | |
56 | |
57 void ComponentPatcher::Start(const ComponentUnpacker::Callback& callback) { | |
58 callback_ = callback; | |
59 task_runner_->PostTask(FROM_HERE, | |
60 base::Bind(&ComponentPatcher::StartPatching, | |
61 scoped_refptr<ComponentPatcher>(this))); | |
62 } | |
63 | |
64 void ComponentPatcher::StartPatching() { | |
65 commands_.reset(ReadCommands(input_dir_)); | |
66 if (!commands_.get()) { | |
67 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); | |
68 } else { | |
69 next_command_ = commands_->begin(); | |
70 PatchNextFile(); | |
71 } | |
72 } | |
73 | |
74 void ComponentPatcher::PatchNextFile() { | |
75 if (next_command_ == commands_->end()) { | |
76 DonePatching(ComponentUnpacker::kNone, 0); | |
77 return; | 63 return; |
78 } | 64 } |
79 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) { | 65 |
80 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); | 66 for (base::ValueVector::const_iterator command = commands->begin(), |
81 return; | 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 } |
82 } | 86 } |
83 const base::DictionaryValue* command_args = | |
84 static_cast<base::DictionaryValue*>(*next_command_); | |
85 current_operation_ = CreateDeltaUpdateOp(*command_args); | |
86 if (!current_operation_) { | |
87 DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand, 0); | |
88 return; | |
89 } | |
90 current_operation_->Run( | |
91 command_args, | |
92 input_dir_, | |
93 unpack_dir_, | |
94 installer_, | |
95 in_process_, | |
96 base::Bind(&ComponentPatcher::DonePatchingFile, | |
97 scoped_refptr<ComponentPatcher>(this)), | |
98 task_runner_); | |
99 } | |
100 | 87 |
101 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error, | 88 callback.Run(ComponentUnpacker::kNone, error); |
102 int extended_error) { | |
103 if (error != ComponentUnpacker::kNone) { | |
104 DonePatching(error, extended_error); | |
105 } else { | |
106 ++next_command_; | |
107 PatchNextFile(); | |
108 } | |
109 } | |
110 | |
111 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error, | |
112 int extended_error) { | |
113 current_operation_ = NULL; | |
114 task_runner_->PostTask(FROM_HERE, base::Bind(callback_, | |
115 error, | |
116 extended_error)); | |
117 callback_.Reset(); | |
118 } | 89 } |
119 | 90 |
120 } // namespace component_updater | 91 } // namespace component_updater |
OLD | NEW |