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" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" |
11 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/memory/weak_ptr.h" |
12 #include "base/values.h" | 15 #include "base/values.h" |
13 #include "chrome/browser/component_updater/component_patcher_operation.h" | 16 #include "chrome/browser/component_updater/component_patcher_operation.h" |
14 #include "chrome/browser/component_updater/component_updater_service.h" | 17 #include "chrome/browser/component_updater/component_updater_service.h" |
| 18 #include "content/public/browser/browser_thread.h" |
15 | 19 |
16 namespace component_updater { | 20 namespace component_updater { |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // Deserialize the commands file (present in delta update packages). The top | 24 // Deserialize the commands file (present in delta update packages). The top |
21 // level must be a list. | 25 // level must be a list. |
22 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { | 26 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { |
23 const base::FilePath commands = | 27 const base::FilePath commands = |
24 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); | 28 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); |
25 if (!base::PathExists(commands)) | 29 if (!base::PathExists(commands)) |
26 return NULL; | 30 return NULL; |
27 | 31 |
28 JSONFileValueSerializer serializer(commands); | 32 JSONFileValueSerializer serializer(commands); |
29 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 33 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
30 | 34 |
31 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? | 35 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? |
32 static_cast<base::ListValue*>(root.release()) : NULL; | 36 static_cast<base::ListValue*>(root.release()) : NULL; |
33 } | 37 } |
34 | 38 |
35 } // namespace | 39 } // namespace |
36 | 40 |
37 // The patching support is not cross-platform at the moment. | 41 ComponentPatcher::ComponentPatcher( |
38 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} | 42 const base::FilePath& input_dir, |
39 | 43 const base::FilePath& unpack_dir, |
40 ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch( | 44 ComponentInstaller* installer, |
41 PatchType patch_type, | 45 bool in_process, |
42 const base::FilePath& input_file, | 46 scoped_refptr<base::SequencedTaskRunner> task_runner) |
43 const base::FilePath& patch_file, | 47 : input_dir_(input_dir), |
44 const base::FilePath& output_file, | 48 unpack_dir_(unpack_dir), |
45 int* error) { | 49 installer_(installer), |
46 return ComponentUnpacker::kDeltaUnsupportedCommand; | 50 in_process_(in_process), |
| 51 task_runner_(task_runner) { |
47 } | 52 } |
48 | 53 |
| 54 ComponentPatcher::~ComponentPatcher() { |
| 55 } |
49 | 56 |
50 // Takes the contents of a differential component update in input_dir | 57 void ComponentPatcher::Start(const ComponentUnpacker::Callback& callback) { |
51 // and produces the contents of a full component update in unpack_dir | 58 callback_ = callback; |
52 // using input_abs_path_ files that the installer knows about. | 59 task_runner_->PostTask(FROM_HERE, |
53 void DifferentialUpdatePatch( | 60 base::Bind(&ComponentPatcher::StartPatching, |
54 const base::FilePath& input_dir, | 61 scoped_refptr<ComponentPatcher>(this))); |
55 const base::FilePath& unpack_dir, | 62 } |
56 ComponentPatcher* patcher, | 63 |
57 ComponentInstaller* installer, | 64 void ComponentPatcher::StartPatching() { |
58 base::Callback<void(ComponentUnpacker::Error, int)> callback) { | 65 commands_.reset(ReadCommands(input_dir_)); |
59 int error = 0; | 66 if (!commands_.get()) { |
60 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); | 67 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); |
61 if (!commands.get()) { | 68 } else { |
62 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 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); |
63 return; | 77 return; |
64 } | 78 } |
| 79 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) { |
| 80 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); |
| 81 return; |
| 82 } |
| 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 } |
65 | 100 |
66 for (base::ValueVector::const_iterator command = commands->begin(), | 101 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error, |
67 end = commands->end(); command != end; command++) { | 102 int extended_error) { |
68 if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) { | 103 if (error == ComponentUnpacker::kNone) { |
69 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 104 DonePatching(error, extended_error); |
70 return; | 105 } else { |
71 } | 106 ++next_command_; |
72 base::DictionaryValue* command_args = | 107 PatchNextFile(); |
73 static_cast<base::DictionaryValue*>(*command); | 108 } |
74 scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); | 109 } |
75 if (!operation) { | |
76 callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error); | |
77 return; | |
78 } | |
79 | 110 |
80 ComponentUnpacker::Error result = operation->Run( | 111 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error, |
81 command_args, input_dir, unpack_dir, patcher, installer, &error); | 112 int extended_error) { |
82 if (result != ComponentUnpacker::kNone) { | 113 callback_.Run(error, extended_error); |
83 callback.Run(result, error); | |
84 return; | |
85 } | |
86 } | |
87 | |
88 callback.Run(ComponentUnpacker::kNone, error); | |
89 } | 114 } |
90 | 115 |
91 } // namespace component_updater | 116 } // namespace component_updater |
OLD | NEW |