| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/extensions/extension.h" | 5 #include "chrome/browser/extensions/extension.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 path_ = FilePath(path_str); | 156 path_ = FilePath(path_str); |
| 157 #else | 157 #else |
| 158 path_ = path; | 158 path_ = path; |
| 159 #endif | 159 #endif |
| 160 } | 160 } |
| 161 | 161 |
| 162 bool Extension::InitFromValue(const DictionaryValue& source, | 162 bool Extension::InitFromValue(const DictionaryValue& source, |
| 163 std::string* error) { | 163 std::string* error) { |
| 164 // Check format version. | 164 // Check format version. |
| 165 int format_version = 0; | 165 int format_version = 0; |
| 166 if (!source.GetInteger(kFormatVersionKey, &format_version) || | 166 if (!source.GetInteger(WideToUTF16Hack(kFormatVersionKey), &format_version) || |
| 167 static_cast<uint32>(format_version) != kExpectedFormatVersion) { | 167 static_cast<uint32>(format_version) != kExpectedFormatVersion) { |
| 168 *error = kInvalidFormatVersionError; | 168 *error = kInvalidFormatVersionError; |
| 169 return false; | 169 return false; |
| 170 } | 170 } |
| 171 | 171 |
| 172 // Initialize id. | 172 // Initialize id. |
| 173 if (!source.GetString(kIdKey, &id_)) { | 173 if (!source.GetString(WideToUTF16Hack(kIdKey), &id_)) { |
| 174 *error = kInvalidIdError; | 174 *error = kInvalidIdError; |
| 175 return false; | 175 return false; |
| 176 } | 176 } |
| 177 | 177 |
| 178 // Normalize the string to lowercase, so it can be used as an URL component | 178 // Normalize the string to lowercase, so it can be used as an URL component |
| 179 // (where GURL will lowercase it). | 179 // (where GURL will lowercase it). |
| 180 StringToLowerASCII(&id_); | 180 StringToLowerASCII(&id_); |
| 181 | 181 |
| 182 // Verify that the id is legal. The id is a hex string of the SHA-1 hash of | 182 // Verify that the id is legal. The id is a hex string of the SHA-1 hash of |
| 183 // the public key. | 183 // the public key. |
| 184 std::vector<uint8> id_bytes; | 184 std::vector<uint8> id_bytes; |
| 185 if (!HexStringToBytes(id_, &id_bytes) || id_bytes.size() != kIdSize) { | 185 if (!HexStringToBytes(id_, &id_bytes) || id_bytes.size() != kIdSize) { |
| 186 *error = kInvalidIdError; | 186 *error = kInvalidIdError; |
| 187 return false; | 187 return false; |
| 188 } | 188 } |
| 189 | 189 |
| 190 // Initialize URL. | 190 // Initialize URL. |
| 191 extension_url_ = GURL(std::string(chrome::kExtensionScheme) + | 191 extension_url_ = GURL(std::string(chrome::kExtensionScheme) + |
| 192 chrome::kStandardSchemeSeparator + id_ + "/"); | 192 chrome::kStandardSchemeSeparator + id_ + "/"); |
| 193 | 193 |
| 194 // Initialize version. | 194 // Initialize version. |
| 195 std::string version_str; | 195 std::string version_str; |
| 196 if (!source.GetString(kVersionKey, &version_str)) { | 196 if (!source.GetString(WideToUTF16Hack(kVersionKey), &version_str)) { |
| 197 *error = kInvalidVersionError; | 197 *error = kInvalidVersionError; |
| 198 return false; | 198 return false; |
| 199 } | 199 } |
| 200 version_.reset(Version::GetVersionFromString(version_str)); | 200 version_.reset(Version::GetVersionFromString(version_str)); |
| 201 if (!version_.get()) { | 201 if (!version_.get()) { |
| 202 *error = kInvalidVersionError; | 202 *error = kInvalidVersionError; |
| 203 return false; | 203 return false; |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Initialize name. | 206 // Initialize name. |
| 207 if (!source.GetString(kNameKey, &name_)) { | 207 if (!source.GetString(WideToUTF16Hack(kNameKey), &name_)) { |
| 208 *error = kInvalidNameError; | 208 *error = kInvalidNameError; |
| 209 return false; | 209 return false; |
| 210 } | 210 } |
| 211 | 211 |
| 212 // Initialize description (optional). | 212 // Initialize description (optional). |
| 213 if (source.HasKey(kDescriptionKey)) { | 213 if (source.HasKey(WideToUTF16Hack(kDescriptionKey))) { |
| 214 if (!source.GetString(kDescriptionKey, &description_)) { | 214 if (!source.GetString(WideToUTF16Hack(kDescriptionKey), &description_)) { |
| 215 *error = kInvalidDescriptionError; | 215 *error = kInvalidDescriptionError; |
| 216 return false; | 216 return false; |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 // Initialize zip hash (only present in zip) | 220 // Initialize zip hash (only present in zip) |
| 221 // There's no need to verify it at this point. If it's in a bogus format | 221 // There's no need to verify it at this point. If it's in a bogus format |
| 222 // it won't pass the hash verify step. | 222 // it won't pass the hash verify step. |
| 223 if (source.HasKey(kZipHashKey)) { | 223 if (source.HasKey(WideToUTF16Hack(kZipHashKey))) { |
| 224 if (!source.GetString(kZipHashKey, &zip_hash_)) { | 224 if (!source.GetString(WideToUTF16Hack(kZipHashKey), &zip_hash_)) { |
| 225 *error = kInvalidZipHashError; | 225 *error = kInvalidZipHashError; |
| 226 return false; | 226 return false; |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 | 229 |
| 230 // Initialize plugins dir (optional). | 230 // Initialize plugins dir (optional). |
| 231 if (source.HasKey(kPluginsDirKey)) { | 231 if (source.HasKey(WideToUTF16Hack(kPluginsDirKey))) { |
| 232 std::string plugins_dir; | 232 std::string plugins_dir; |
| 233 if (!source.GetString(kPluginsDirKey, &plugins_dir)) { | 233 if (!source.GetString(WideToUTF16Hack(kPluginsDirKey), &plugins_dir)) { |
| 234 *error = kInvalidPluginsDirError; | 234 *error = kInvalidPluginsDirError; |
| 235 return false; | 235 return false; |
| 236 } | 236 } |
| 237 plugins_dir_ = path_.AppendASCII(plugins_dir); | 237 plugins_dir_ = path_.AppendASCII(plugins_dir); |
| 238 } | 238 } |
| 239 | 239 |
| 240 // Initialize content scripts (optional). | 240 // Initialize content scripts (optional). |
| 241 if (source.HasKey(kContentScriptsKey)) { | 241 if (source.HasKey(WideToUTF16Hack(kContentScriptsKey))) { |
| 242 ListValue* list_value; | 242 ListValue* list_value; |
| 243 if (!source.GetList(kContentScriptsKey, &list_value)) { | 243 if (!source.GetList(WideToUTF16Hack(kContentScriptsKey), &list_value)) { |
| 244 *error = kInvalidContentScriptsListError; | 244 *error = kInvalidContentScriptsListError; |
| 245 return false; | 245 return false; |
| 246 } | 246 } |
| 247 | 247 |
| 248 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 248 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
| 249 DictionaryValue* content_script; | 249 DictionaryValue* content_script; |
| 250 if (!list_value->GetDictionary(i, &content_script)) { | 250 if (!list_value->GetDictionary(i, &content_script)) { |
| 251 *error = FormatErrorMessage(kInvalidContentScriptError, IntToString(i)); | 251 *error = FormatErrorMessage(kInvalidContentScriptError, IntToString(i)); |
| 252 return false; | 252 return false; |
| 253 } | 253 } |
| 254 | 254 |
| 255 ListValue* matches; | 255 ListValue* matches; |
| 256 ListValue* js; | 256 ListValue* js; |
| 257 | 257 |
| 258 if (!content_script->GetList(kMatchesKey, &matches)) { | 258 if (!content_script->GetList(WideToUTF16Hack(kMatchesKey), &matches)) { |
| 259 *error = FormatErrorMessage(kInvalidMatchesError, IntToString(i)); | 259 *error = FormatErrorMessage(kInvalidMatchesError, IntToString(i)); |
| 260 return false; | 260 return false; |
| 261 } | 261 } |
| 262 | 262 |
| 263 if (!content_script->GetList(kJsKey, &js)) { | 263 if (!content_script->GetList(WideToUTF16Hack(kJsKey), &js)) { |
| 264 *error = FormatErrorMessage(kInvalidJsListError, IntToString(i)); | 264 *error = FormatErrorMessage(kInvalidJsListError, IntToString(i)); |
| 265 return false; | 265 return false; |
| 266 } | 266 } |
| 267 | 267 |
| 268 if (matches->GetSize() == 0) { | 268 if (matches->GetSize() == 0) { |
| 269 *error = FormatErrorMessage(kInvalidMatchCountError, IntToString(i)); | 269 *error = FormatErrorMessage(kInvalidMatchCountError, IntToString(i)); |
| 270 return false; | 270 return false; |
| 271 } | 271 } |
| 272 | 272 |
| 273 // NOTE: Only one file is supported right now. | 273 // NOTE: Only one file is supported right now. |
| 274 if (js->GetSize() != 1) { | 274 if (js->GetSize() != 1) { |
| 275 *error = FormatErrorMessage(kInvalidJsCountError, IntToString(i)); | 275 *error = FormatErrorMessage(kInvalidJsCountError, IntToString(i)); |
| 276 return false; | 276 return false; |
| 277 } | 277 } |
| 278 | 278 |
| 279 UserScript script; | 279 UserScript script; |
| 280 if (content_script->HasKey(kRunAtKey)) { | 280 if (content_script->HasKey(WideToUTF16Hack(kRunAtKey))) { |
| 281 std::string run_location; | 281 std::string run_location; |
| 282 if (!content_script->GetString(kRunAtKey, &run_location)) { | 282 if (!content_script->GetString(WideToUTF16Hack(kRunAtKey), |
| 283 &run_location)) { |
| 283 *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i)); | 284 *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i)); |
| 284 return false; | 285 return false; |
| 285 } | 286 } |
| 286 | 287 |
| 287 if (run_location == kRunAtDocumentStartValue) { | 288 if (run_location == kRunAtDocumentStartValue) { |
| 288 script.set_run_location(UserScript::DOCUMENT_START); | 289 script.set_run_location(UserScript::DOCUMENT_START); |
| 289 } else if (run_location == kRunAtDocumentEndValue) { | 290 } else if (run_location == kRunAtDocumentEndValue) { |
| 290 script.set_run_location(UserScript::DOCUMENT_END); | 291 script.set_run_location(UserScript::DOCUMENT_END); |
| 291 } else { | 292 } else { |
| 292 *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i)); | 293 *error = FormatErrorMessage(kInvalidRunAtError, IntToString(i)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 321 } | 322 } |
| 322 script.set_path(Extension::GetResourcePath(path(), file)); | 323 script.set_path(Extension::GetResourcePath(path(), file)); |
| 323 script.set_url(Extension::GetResourceURL(url(), file)); | 324 script.set_url(Extension::GetResourceURL(url(), file)); |
| 324 | 325 |
| 325 content_scripts_.push_back(script); | 326 content_scripts_.push_back(script); |
| 326 } | 327 } |
| 327 } | 328 } |
| 328 | 329 |
| 329 return true; | 330 return true; |
| 330 } | 331 } |
| 331 | |
| OLD | NEW |