From 4259f9cc708d8f67d7f935d267da9a7ffa852b65 Mon Sep 17 00:00:00 2001 From: "aa@chromium.org" Date: Tue, 7 Apr 2009 08:01:50 +0000 Subject: Implement chromium.self in content scripts, so that developers don't have to know and copy/paste their extension ID. This required moving the code that defaults the extension ID earlier in the load process. Also fixed some bugs: * fixed a bug that was causing all user scripts to get executed in the same context. * made the greasemonkey api only available in 'standalone' user scripts. * re-added the anonymous function wrapper that is supposed to wrap content scripts. Also added unit tests for the fixed bugs. Review URL: http://codereview.chromium.org/60112 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13238 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/extensions/extension.cc | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'chrome/browser/extensions/extension.cc') diff --git a/chrome/browser/extensions/extension.cc b/chrome/browser/extensions/extension.cc index d8ab403..1aeaeaa 100644 --- a/chrome/browser/extensions/extension.cc +++ b/chrome/browser/extensions/extension.cc @@ -103,21 +103,6 @@ Extension::Extension(const Extension& rhs) theme_paths_(rhs.theme_paths_) { } -const GURL& Extension::url() { - if (!extension_url_.is_valid()) - extension_url_ = GURL(std::string(chrome::kExtensionScheme) + - chrome::kStandardSchemeSeparator + id_ + "/"); - - return extension_url_; -} - -void Extension::set_id(const std::string& id) { - id_ = id; - - // Reset url_ so that it gets reinitialized next time. - extension_url_ = GURL(); -} - const std::string Extension::VersionString() const { return version_->GetString(); } @@ -335,10 +320,9 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, return true; } -bool Extension::InitFromValue(const DictionaryValue& source, +bool Extension::InitFromValue(const DictionaryValue& source, bool require_id, std::string* error) { - // Initialize id. The ID is not required here because we don't require IDs for - // extensions used with --load-extension. + // Initialize id. if (source.HasKey(kIdKey)) { if (!source.GetString(kIdKey, &id_)) { *error = kInvalidIdError; @@ -356,8 +340,23 @@ bool Extension::InitFromValue(const DictionaryValue& source, *error = kInvalidIdError; return false; } + } else if (require_id) { + *error = kInvalidIdError; + return false; + } else { + // Generate a random ID + static int counter = 0; + id_ = StringPrintf("%x", counter); + ++counter; + + // pad the string out to 40 chars with zeroes. + id_.insert(0, 40 - id_.length(), '0'); } + // Initialize the URL. + extension_url_ = GURL(std::string(chrome::kExtensionScheme) + + chrome::kStandardSchemeSeparator + id_ + "/"); + // Initialize version. std::string version_str; if (!source.GetString(kVersionKey, &version_str)) { @@ -457,6 +456,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, UserScript script; if (!LoadUserScriptHelper(content_script, i, error, &script)) return false; // Failed to parse script context definition + script.set_extension_id(id()); content_scripts_.push_back(script); } } -- cgit v1.1