OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_unpacker.h" | 5 #include "chrome/browser/component_updater/component_unpacker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 bool is_delta_; | 91 bool is_delta_; |
92 std::vector<uint8> public_key_; | 92 std::vector<uint8> public_key_; |
93 }; | 93 }; |
94 | 94 |
95 } // namespace | 95 } // namespace |
96 | 96 |
97 ComponentUnpacker::ComponentUnpacker( | 97 ComponentUnpacker::ComponentUnpacker( |
98 const std::vector<uint8>& pk_hash, | 98 const std::vector<uint8>& pk_hash, |
99 const base::FilePath& path, | 99 const base::FilePath& path, |
100 const std::string& fingerprint, | 100 const std::string& fingerprint, |
101 ComponentPatcher* patcher, | |
102 ComponentInstaller* installer, | 101 ComponentInstaller* installer, |
| 102 bool in_process, |
103 scoped_refptr<base::SequencedTaskRunner> task_runner) | 103 scoped_refptr<base::SequencedTaskRunner> task_runner) |
104 : pk_hash_(pk_hash), | 104 : pk_hash_(pk_hash), |
105 path_(path), | 105 path_(path), |
106 is_delta_(false), | 106 is_delta_(false), |
107 fingerprint_(fingerprint), | 107 fingerprint_(fingerprint), |
108 patcher_(patcher), | |
109 installer_(installer), | 108 installer_(installer), |
| 109 in_process_(in_process), |
110 error_(kNone), | 110 error_(kNone), |
111 extended_error_(0), | 111 extended_error_(0), |
112 ptr_factory_(this), | |
113 task_runner_(task_runner) { | 112 task_runner_(task_runner) { |
114 } | 113 } |
115 | 114 |
116 // TODO(cpu): add a specific attribute check to a component json that the | 115 // TODO(cpu): add a specific attribute check to a component json that the |
117 // extension unpacker will reject, so that a component cannot be installed | 116 // extension unpacker will reject, so that a component cannot be installed |
118 // as an extension. | 117 // as an extension. |
119 scoped_ptr<base::DictionaryValue> ReadManifest( | 118 scoped_ptr<base::DictionaryValue> ReadManifest( |
120 const base::FilePath& unpack_path) { | 119 const base::FilePath& unpack_path) { |
121 base::FilePath manifest = | 120 base::FilePath manifest = |
122 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); | 121 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); |
123 if (!base::PathExists(manifest)) | 122 if (!base::PathExists(manifest)) |
124 return scoped_ptr<base::DictionaryValue>(); | 123 return scoped_ptr<base::DictionaryValue>(); |
125 JSONFileValueSerializer serializer(manifest); | 124 JSONFileValueSerializer serializer(manifest); |
126 std::string error; | 125 std::string error; |
127 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); | 126 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); |
128 if (!root.get()) | 127 if (!root.get()) |
129 return scoped_ptr<base::DictionaryValue>(); | 128 return scoped_ptr<base::DictionaryValue>(); |
130 if (!root->IsType(base::Value::TYPE_DICTIONARY)) | 129 if (!root->IsType(base::Value::TYPE_DICTIONARY)) |
131 return scoped_ptr<base::DictionaryValue>(); | 130 return scoped_ptr<base::DictionaryValue>(); |
132 return scoped_ptr<base::DictionaryValue>( | 131 return scoped_ptr<base::DictionaryValue>( |
133 static_cast<base::DictionaryValue*>(root.release())).Pass(); | 132 static_cast<base::DictionaryValue*>(root.release())).Pass(); |
134 } | 133 } |
135 | 134 |
136 bool ComponentUnpacker::UnpackInternal() { | 135 bool ComponentUnpacker::UnpackInternal() { |
137 return Verify() && Unzip() && BeginPatching(); | 136 return Verify() && Unzip() && BeginPatching(); |
138 } | 137 } |
139 | 138 |
140 void ComponentUnpacker::Unpack( | 139 void ComponentUnpacker::Unpack(const Callback& callback) { |
141 const base::Callback<void(Error, int)>& callback) { | |
142 callback_ = callback; | 140 callback_ = callback; |
143 if (!UnpackInternal()) | 141 if (!UnpackInternal()) |
144 Finish(); | 142 Finish(); |
145 } | 143 } |
146 | 144 |
147 bool ComponentUnpacker::Verify() { | 145 bool ComponentUnpacker::Verify() { |
148 if (pk_hash_.empty() || path_.empty()) { | 146 if (pk_hash_.empty() || path_.empty()) { |
149 error_ = kInvalidParams; | 147 error_ = kInvalidParams; |
150 return false; | 148 return false; |
151 } | 149 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 193 |
196 | 194 |
197 bool ComponentUnpacker::BeginPatching() { | 195 bool ComponentUnpacker::BeginPatching() { |
198 if (is_delta_) { // Package is a diff package. | 196 if (is_delta_) { // Package is a diff package. |
199 // Use a different temp directory for the patch output files. | 197 // Use a different temp directory for the patch output files. |
200 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), | 198 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), |
201 &unpack_path_)) { | 199 &unpack_path_)) { |
202 error_ = kUnzipPathError; | 200 error_ = kUnzipPathError; |
203 return false; | 201 return false; |
204 } | 202 } |
| 203 patcher_ = new ComponentPatcher(unpack_diff_path_, |
| 204 unpack_path_, |
| 205 installer_, |
| 206 in_process_, |
| 207 task_runner_); |
205 task_runner_->PostTask( | 208 task_runner_->PostTask( |
206 FROM_HERE, base::Bind(&DifferentialUpdatePatch, | 209 FROM_HERE, |
207 unpack_diff_path_, | 210 base::Bind(&ComponentPatcher::Start, |
208 unpack_path_, | 211 patcher_, |
209 patcher_, | 212 base::Bind(&ComponentUnpacker::EndPatching, |
210 installer_, | 213 scoped_refptr<ComponentUnpacker>(this)))); |
211 base::Bind(&ComponentUnpacker::EndPatching, | |
212 GetWeakPtr()))); | |
213 } else { | 214 } else { |
214 task_runner_->PostTask( | 215 task_runner_->PostTask(FROM_HERE, |
215 FROM_HERE, base::Bind(&ComponentUnpacker::EndPatching, | 216 base::Bind(&ComponentUnpacker::EndPatching, |
216 GetWeakPtr(), | 217 scoped_refptr<ComponentUnpacker>(this), |
217 kNone, | 218 kNone, |
218 0)); | 219 0)); |
219 } | 220 } |
220 return true; | 221 return true; |
221 } | 222 } |
222 | 223 |
223 void ComponentUnpacker::EndPatching(Error error, int extended_error) { | 224 void ComponentUnpacker::EndPatching(Error error, int extended_error) { |
224 error_ = error; | 225 error_ = error; |
225 extended_error_ = extended_error; | 226 extended_error_ = extended_error; |
226 if (error_ != kNone) { | 227 if (error_ != kNone) { |
227 Finish(); | 228 Finish(); |
228 return; | 229 return; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 261 } |
261 | 262 |
262 void ComponentUnpacker::Finish() { | 263 void ComponentUnpacker::Finish() { |
263 if (!unpack_diff_path_.empty()) | 264 if (!unpack_diff_path_.empty()) |
264 base::DeleteFile(unpack_diff_path_, true); | 265 base::DeleteFile(unpack_diff_path_, true); |
265 if (!unpack_path_.empty()) | 266 if (!unpack_path_.empty()) |
266 base::DeleteFile(unpack_path_, true); | 267 base::DeleteFile(unpack_path_, true); |
267 callback_.Run(error_, extended_error_); | 268 callback_.Run(error_, extended_error_); |
268 } | 269 } |
269 | 270 |
270 base::WeakPtr<ComponentUnpacker> ComponentUnpacker::GetWeakPtr() { | |
271 return ptr_factory_.GetWeakPtr(); | |
272 } | |
273 | |
274 ComponentUnpacker::~ComponentUnpacker() { | 271 ComponentUnpacker::~ComponentUnpacker() { |
275 } | 272 } |
276 | 273 |
277 } // namespace component_updater | 274 } // namespace component_updater |
OLD | NEW |