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/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/json/json_file_value_serializer.h" | |
12 #include "base/memory/scoped_handle.h" | 11 #include "base/memory/scoped_handle.h" |
13 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/values.h" | |
15 #include "chrome/browser/component_updater/component_patcher.h" | 15 #include "chrome/browser/component_updater/component_patcher.h" |
16 #include "chrome/browser/component_updater/component_updater_service.h" | 16 #include "chrome/browser/component_updater/component_updater_service.h" |
17 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
18 #include "crypto/secure_hash.h" | 18 #include "crypto/secure_hash.h" |
19 #include "crypto/signature_verifier.h" | 19 #include "crypto/signature_verifier.h" |
20 #include "extensions/common/crx_file.h" | 20 #include "extensions/common/crx_file.h" |
21 #include "third_party/zlib/google/zip.h" | 21 #include "third_party/zlib/google/zip.h" |
22 | 22 |
23 using crypto::SecureHash; | 23 using crypto::SecureHash; |
24 | 24 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 bool delta() const { return delta_; } | 78 bool delta() const { return delta_; } |
79 | 79 |
80 const std::vector<uint8>& public_key() const { return public_key_; } | 80 const std::vector<uint8>& public_key() const { return public_key_; } |
81 | 81 |
82 private: | 82 private: |
83 bool valid_; | 83 bool valid_; |
84 bool delta_; | 84 bool delta_; |
85 std::vector<uint8> public_key_; | 85 std::vector<uint8> public_key_; |
86 }; | 86 }; |
87 | 87 |
88 // Deserialize the CRX manifest. The top level must be a dictionary. | |
89 // TODO(cpu): add a specific attribute check to a component json that the | |
90 // extension unpacker will reject, so that a component cannot be installed | |
91 // as an extension. | |
92 base::DictionaryValue* ReadManifest(const base::FilePath& unpack_path) { | |
93 base::FilePath manifest = | |
94 unpack_path.Append(FILE_PATH_LITERAL("manifest.json")); | |
95 if (!base::PathExists(manifest)) | |
96 return NULL; | |
97 JSONFileValueSerializer serializer(manifest); | |
98 std::string error; | |
99 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, &error)); | |
100 if (!root.get()) | |
101 return NULL; | |
102 if (!root->IsType(base::Value::TYPE_DICTIONARY)) | |
103 return NULL; | |
104 return static_cast<base::DictionaryValue*>(root.release()); | |
105 } | |
106 | |
107 } // namespace. | 88 } // namespace. |
108 | 89 |
109 ComponentUnpacker::ComponentUnpacker(const std::vector<uint8>& pk_hash, | 90 ComponentUnpacker::ComponentUnpacker(const std::vector<uint8>& pk_hash, |
110 const base::FilePath& path, | 91 const base::FilePath& path, |
111 const std::string& fingerprint, | 92 const std::string& fingerprint, |
112 ComponentPatcher* patcher, | 93 ComponentPatcher* patcher, |
113 ComponentInstaller* installer) | 94 ComponentInstaller* installer) |
114 : error_(kNone), | 95 : error_(kNone), |
115 extended_error_(0) { | 96 extended_error_(0) { |
116 if (pk_hash.empty() || path.empty()) { | 97 if (pk_hash.empty() || path.empty()) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 if (error_ != kNone) { | 153 if (error_ != kNone) { |
173 return; | 154 return; |
174 } | 155 } |
175 } else { | 156 } else { |
176 // Package is a normal update/install; unzip it into unpack_path_ directly. | 157 // Package is a normal update/install; unzip it into unpack_path_ directly. |
177 if (!zip::Unzip(path, unpack_path_)) { | 158 if (!zip::Unzip(path, unpack_path_)) { |
178 error_ = kUnzipFailed; | 159 error_ = kUnzipFailed; |
179 return; | 160 return; |
180 } | 161 } |
181 } | 162 } |
182 scoped_ptr<base::DictionaryValue> manifest(ReadManifest(unpack_path_)); | 163 scoped_ptr<base::DictionaryValue> manifest( |
164 ReadComponentManifest(unpack_path_)); | |
183 if (!manifest.get()) { | 165 if (!manifest.get()) { |
184 error_ = kBadManifest; | 166 error_ = kBadManifest; |
185 return; | 167 return; |
186 } | 168 } |
187 // Write the fingerprint to disk. | 169 // Write the fingerprint to disk. |
188 if (static_cast<int>(fingerprint.size()) != | 170 if (static_cast<int>(fingerprint.size()) != |
189 file_util::WriteFile( | 171 file_util::WriteFile( |
190 unpack_path_.Append(FILE_PATH_LITERAL("manifest.fingerprint")), | 172 unpack_path_.Append(FILE_PATH_LITERAL("manifest.fingerprint")), |
191 fingerprint.c_str(), | 173 fingerprint.c_str(), |
192 fingerprint.size())) { | 174 fingerprint.size())) { |
193 error_ = kFingerprintWriteFailed; | 175 error_ = kFingerprintWriteFailed; |
194 return; | 176 return; |
195 } | 177 } |
196 if (!installer->Install(*manifest, unpack_path_)) { | 178 if (!installer->Install(*manifest, unpack_path_)) { |
xhwang
2013/10/25 17:33:22
Can we change the Install() to take scoped_ptr<bas
ddorwin
2013/10/25 19:03:40
Added TODO in other file. I don't want to add more
| |
197 error_ = kInstallerError; | 179 error_ = kInstallerError; |
198 return; | 180 return; |
199 } | 181 } |
200 // Installation successful. The directory is not our concern now. | 182 // Installation successful. The directory is not our concern now. |
201 unpack_path_.clear(); | 183 unpack_path_.clear(); |
202 } | 184 } |
203 | 185 |
204 ComponentUnpacker::~ComponentUnpacker() { | 186 ComponentUnpacker::~ComponentUnpacker() { |
205 if (!unpack_path_.empty()) | 187 if (!unpack_path_.empty()) |
206 base::DeleteFile(unpack_path_, true); | 188 base::DeleteFile(unpack_path_, true); |
207 } | 189 } |
OLD | NEW |