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 ptr_factory_(this), | |
52 task_runner_(task_runner) { | |
47 } | 53 } |
48 | 54 |
55 ComponentPatcher::~ComponentPatcher() { | |
56 } | |
49 | 57 |
50 // Takes the contents of a differential component update in input_dir | 58 void ComponentPatcher::Start( |
51 // and produces the contents of a full component update in unpack_dir | 59 const base::Callback<void(ComponentUnpacker::Error, int)>& callback) { |
52 // using input_abs_path_ files that the installer knows about. | 60 callback_ = callback; |
53 void DifferentialUpdatePatch( | 61 commands_.reset(ReadCommands(input_dir_)); |
54 const base::FilePath& input_dir, | 62 if (!commands_.get()) { |
55 const base::FilePath& unpack_dir, | 63 task_runner_->PostTask( |
56 ComponentPatcher* patcher, | 64 FROM_HERE, |
57 ComponentInstaller* installer, | 65 base::Bind(callback_, ComponentUnpacker::kDeltaBadCommands, 0)); |
58 base::Callback<void(ComponentUnpacker::Error, int)> callback) { | 66 } else { |
59 int error = 0; | 67 next_command_ = commands_->begin(); |
60 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); | 68 task_runner_->PostTask( |
61 if (!commands.get()) { | 69 FROM_HERE, |
62 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 70 base::Bind(&ComponentPatcher::PatchNextFile, GetWeakPtr())); |
71 } | |
72 } | |
73 | |
74 void ComponentPatcher::PatchNextFile() { | |
75 if (next_command_ == commands_->end()) { | |
76 callback_.Run(ComponentUnpacker::kNone, 0); | |
Sorin Jianu
2014/02/03 20:57:57
Why is this callback called directly and the ones
waffles
2014/02/07 01:00:59
Done.
| |
63 return; | 77 return; |
64 } | 78 } |
79 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) { | |
80 task_runner_->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(callback_, ComponentUnpacker::kDeltaBadCommands, 0)); | |
83 return; | |
84 } | |
85 base::DictionaryValue* command_args = | |
86 static_cast<base::DictionaryValue*>(*next_command_); | |
87 current_operation_.reset(CreateDeltaUpdateOp(command_args)); | |
88 if (!current_operation_) { | |
89 task_runner_->PostTask( | |
90 FROM_HERE, | |
91 base::Bind(callback_, ComponentUnpacker::kDeltaUnsupportedCommand, 0)); | |
92 return; | |
93 } | |
94 current_operation_->Run( | |
95 command_args, | |
96 input_dir_, | |
97 unpack_dir_, | |
98 installer_, | |
99 in_process_, | |
100 base::Bind(&ComponentPatcher::DonePatchingFile, GetWeakPtr()), | |
101 task_runner_); | |
102 } | |
65 | 103 |
66 for (base::ValueVector::const_iterator command = commands->begin(), | 104 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error, |
67 end = commands->end(); command != end; command++) { | 105 int extended_error) { |
68 if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) { | 106 if (error != ComponentUnpacker::kNone) { |
69 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 107 callback_.Run(error, extended_error); |
70 return; | 108 } else if (next_command_ == commands_->end()) { |
Sorin Jianu
2014/02/03 20:57:57
This is the if statement that could be eliminated,
waffles
2014/02/07 01:00:59
Done.
| |
71 } | 109 callback_.Run(ComponentUnpacker::kNone, 0); |
72 base::DictionaryValue* command_args = | 110 } else { |
73 static_cast<base::DictionaryValue*>(*command); | 111 next_command_++; |
Sorin Jianu
2014/02/03 20:57:57
could use preincrement.
waffles
2014/02/07 01:00:59
Done.
| |
74 scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); | 112 task_runner_->PostTask( |
75 if (!operation) { | 113 FROM_HERE, |
76 callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error); | 114 base::Bind(&ComponentPatcher::PatchNextFile, GetWeakPtr())); |
77 return; | 115 } |
78 } | 116 } |
79 | 117 |
80 ComponentUnpacker::Error result = operation->Run( | 118 base::WeakPtr<ComponentPatcher> ComponentPatcher::GetWeakPtr() { |
81 command_args, input_dir, unpack_dir, patcher, installer, &error); | 119 return ptr_factory_.GetWeakPtr(); |
82 if (result != ComponentUnpacker::kNone) { | |
83 callback.Run(result, error); | |
84 return; | |
85 } | |
86 } | |
87 | |
88 callback.Run(ComponentUnpacker::kNone, error); | |
89 } | 120 } |
90 | 121 |
91 } // namespace component_updater | 122 } // namespace component_updater |
OLD | NEW |