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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 id_(rhs.id_), | 96 id_(rhs.id_), |
97 version_(new Version(*rhs.version_)), | 97 version_(new Version(*rhs.version_)), |
98 name_(rhs.name_), | 98 name_(rhs.name_), |
99 description_(rhs.description_), | 99 description_(rhs.description_), |
100 content_scripts_(rhs.content_scripts_), | 100 content_scripts_(rhs.content_scripts_), |
101 plugins_dir_(rhs.plugins_dir_), | 101 plugins_dir_(rhs.plugins_dir_), |
102 zip_hash_(rhs.zip_hash_), | 102 zip_hash_(rhs.zip_hash_), |
103 theme_paths_(rhs.theme_paths_) { | 103 theme_paths_(rhs.theme_paths_) { |
104 } | 104 } |
105 | 105 |
106 const GURL& Extension::url() { | |
107 if (!extension_url_.is_valid()) | |
108 extension_url_ = GURL(std::string(chrome::kExtensionScheme) + | |
109 chrome::kStandardSchemeSeparator + id_ + "/"); | |
110 | |
111 return extension_url_; | |
112 } | |
113 | |
114 void Extension::set_id(const std::string& id) { | |
115 id_ = id; | |
116 | |
117 // Reset url_ so that it gets reinitialized next time. | |
118 extension_url_ = GURL(); | |
119 } | |
120 | |
121 const std::string Extension::VersionString() const { | 106 const std::string Extension::VersionString() const { |
122 return version_->GetString(); | 107 return version_->GetString(); |
123 } | 108 } |
124 | 109 |
125 // static | 110 // static |
126 GURL Extension::GetResourceURL(const GURL& extension_url, | 111 GURL Extension::GetResourceURL(const GURL& extension_url, |
127 const std::string& relative_path) { | 112 const std::string& relative_path) { |
128 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme)); | 113 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme)); |
129 DCHECK(extension_url.path() == "/"); | 114 DCHECK(extension_url.path() == "/"); |
130 | 115 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // TODO(georged): Make GetResourceURL accept wstring too | 313 // TODO(georged): Make GetResourceURL accept wstring too |
329 GURL url = GetResourceURL(WideToUTF8(relative)); | 314 GURL url = GetResourceURL(WideToUTF8(relative)); |
330 FilePath path = GetResourcePath(WideToUTF8(relative)); | 315 FilePath path = GetResourcePath(WideToUTF8(relative)); |
331 result->css_scripts().push_back(UserScript::File(path, url)); | 316 result->css_scripts().push_back(UserScript::File(path, url)); |
332 } | 317 } |
333 } | 318 } |
334 | 319 |
335 return true; | 320 return true; |
336 } | 321 } |
337 | 322 |
338 bool Extension::InitFromValue(const DictionaryValue& source, | 323 bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, |
339 std::string* error) { | 324 std::string* error) { |
340 // Initialize id. The ID is not required here because we don't require IDs for | 325 // Initialize id. |
341 // extensions used with --load-extension. | |
342 if (source.HasKey(kIdKey)) { | 326 if (source.HasKey(kIdKey)) { |
343 if (!source.GetString(kIdKey, &id_)) { | 327 if (!source.GetString(kIdKey, &id_)) { |
344 *error = kInvalidIdError; | 328 *error = kInvalidIdError; |
345 return false; | 329 return false; |
346 } | 330 } |
347 | 331 |
348 // Normalize the string to lowercase, so it can be used as an URL component | 332 // Normalize the string to lowercase, so it can be used as an URL component |
349 // (where GURL will lowercase it). | 333 // (where GURL will lowercase it). |
350 StringToLowerASCII(&id_); | 334 StringToLowerASCII(&id_); |
351 | 335 |
352 // Verify that the id is legal. The id is a hex string of the SHA-1 hash of | 336 // Verify that the id is legal. The id is a hex string of the SHA-1 hash of |
353 // the public key. | 337 // the public key. |
354 std::vector<uint8> id_bytes; | 338 std::vector<uint8> id_bytes; |
355 if (!HexStringToBytes(id_, &id_bytes) || id_bytes.size() != kIdSize) { | 339 if (!HexStringToBytes(id_, &id_bytes) || id_bytes.size() != kIdSize) { |
356 *error = kInvalidIdError; | 340 *error = kInvalidIdError; |
357 return false; | 341 return false; |
358 } | 342 } |
| 343 } else if (require_id) { |
| 344 *error = kInvalidIdError; |
| 345 return false; |
| 346 } else { |
| 347 // Generate a random ID |
| 348 static int counter = 0; |
| 349 id_ = StringPrintf("%x", counter); |
| 350 ++counter; |
| 351 |
| 352 // pad the string out to 40 chars with zeroes. |
| 353 id_.insert(0, 40 - id_.length(), '0'); |
359 } | 354 } |
360 | 355 |
| 356 // Initialize the URL. |
| 357 extension_url_ = GURL(std::string(chrome::kExtensionScheme) + |
| 358 chrome::kStandardSchemeSeparator + id_ + "/"); |
| 359 |
361 // Initialize version. | 360 // Initialize version. |
362 std::string version_str; | 361 std::string version_str; |
363 if (!source.GetString(kVersionKey, &version_str)) { | 362 if (!source.GetString(kVersionKey, &version_str)) { |
364 *error = kInvalidVersionError; | 363 *error = kInvalidVersionError; |
365 return false; | 364 return false; |
366 } | 365 } |
367 version_.reset(Version::GetVersionFromString(version_str)); | 366 version_.reset(Version::GetVersionFromString(version_str)); |
368 if (!version_.get()) { | 367 if (!version_.get()) { |
369 *error = kInvalidVersionError; | 368 *error = kInvalidVersionError; |
370 return false; | 369 return false; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 DictionaryValue* content_script; | 449 DictionaryValue* content_script; |
451 if (!list_value->GetDictionary(i, &content_script)) { | 450 if (!list_value->GetDictionary(i, &content_script)) { |
452 *error = FormatErrorMessage(kInvalidContentScriptError, | 451 *error = FormatErrorMessage(kInvalidContentScriptError, |
453 IntToString(i)); | 452 IntToString(i)); |
454 return false; | 453 return false; |
455 } | 454 } |
456 | 455 |
457 UserScript script; | 456 UserScript script; |
458 if (!LoadUserScriptHelper(content_script, i, error, &script)) | 457 if (!LoadUserScriptHelper(content_script, i, error, &script)) |
459 return false; // Failed to parse script context definition | 458 return false; // Failed to parse script context definition |
| 459 script.set_extension_id(id()); |
460 content_scripts_.push_back(script); | 460 content_scripts_.push_back(script); |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 // Initialize the permissions (optional). | 464 // Initialize the permissions (optional). |
465 if (source.HasKey(kPermissionsKey)) { | 465 if (source.HasKey(kPermissionsKey)) { |
466 ListValue* hosts = NULL; | 466 ListValue* hosts = NULL; |
467 if (!source.GetList(kPermissionsKey, &hosts)) { | 467 if (!source.GetList(kPermissionsKey, &hosts)) { |
468 *error = FormatErrorMessage(kInvalidPermissionsError, ""); | 468 *error = FormatErrorMessage(kInvalidPermissionsError, ""); |
469 return false; | 469 return false; |
(...skipping 26 matching lines...) Expand all Loading... |
496 IntToString(i)); | 496 IntToString(i)); |
497 return false; | 497 return false; |
498 } | 498 } |
499 | 499 |
500 permissions_.push_back(pattern); | 500 permissions_.push_back(pattern); |
501 } | 501 } |
502 } | 502 } |
503 | 503 |
504 return true; | 504 return true; |
505 } | 505 } |
OLD | NEW |