diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-26 22:32:07 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-26 22:32:07 +0000 |
commit | 0f9f337262c383b94dce482aba0fac7e5778c852 (patch) | |
tree | 9359ce10553fadfb4583601da2b7fe58dba7edce /chrome/common | |
parent | cc3d6f596df5fa5fa6258b69e947c8977ade7dc7 (diff) | |
download | chromium_src-0f9f337262c383b94dce482aba0fac7e5778c852.zip chromium_src-0f9f337262c383b94dce482aba0fac7e5778c852.tar.gz chromium_src-0f9f337262c383b94dce482aba0fac7e5778c852.tar.bz2 |
Unrevert r63919: "Part 2 of immutable Extension refactor."
I made Extension a refcounted object, and privitized the existing
con/destructor and InitFromValue. The only way to get an Extension is to call
a factory method.
In the next CL, I plan to make the factory method return a const Extension,
to guarantee that no one can modify the Extension object after creation.
Note: There was a tricky part of this CL because of the difference in
semantics between scoped_ptr and scoped_refptr. I had to be careful not to use
ptr.release(), since that would result in leaks (an un-Released AddRef).
BUG=56558
TEST=no functional change
Original Review URL: http://codereview.chromium.org/3982001
TBR=aa
Review URL: http://codereview.chromium.org/4186002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63962 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/extensions/extension.cc | 28 | ||||
-rw-r--r-- | chrome/common/extensions/extension.h | 53 | ||||
-rw-r--r-- | chrome/common/extensions/extension_file_util.cc | 29 | ||||
-rw-r--r-- | chrome/common/extensions/extension_file_util.h | 8 | ||||
-rw-r--r-- | chrome/common/extensions/extension_file_util_unittest.cc | 10 | ||||
-rw-r--r-- | chrome/common/extensions/extension_l10n_util.cc | 4 | ||||
-rw-r--r-- | chrome/common/extensions/extension_l10n_util.h | 2 | ||||
-rw-r--r-- | chrome/common/extensions/extension_manifests_unittest.cc | 106 | ||||
-rw-r--r-- | chrome/common/extensions/extension_unittest.cc | 110 | ||||
-rw-r--r-- | chrome/common/extensions/extension_unpacker.cc | 13 |
10 files changed, 185 insertions, 178 deletions
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index e0cdc5d..8bf9938 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -283,6 +283,18 @@ Extension::RuntimeData::~RuntimeData() { // // static +scoped_refptr<Extension> Extension::Create(const FilePath& path, + Location location, + const DictionaryValue& value, + bool require_key, + std::string* error) { + scoped_refptr<Extension> extension = new Extension(path, location); + if (!extension->InitFromValue(value, require_key, error)) + return NULL; + return extension; +} + +// static int Extension::GetPermissionMessageId(const std::string& permission) { return ExtensionConfig::GetSingleton()->GetPermissionMessageId(permission); } @@ -401,9 +413,6 @@ bool Extension::IsHostedAppPermission(const std::string& str) { return false; } -Extension::~Extension() { -} - const std::string Extension::VersionString() const { return version()->GetString(); } @@ -998,17 +1007,18 @@ bool Extension::EnsureNotHybridApp(const DictionaryValue* manifest, return true; } -Extension::Extension(const FilePath& path) - : mutable_static_data_(new StaticData), - runtime_data_(new RuntimeData) { +Extension::Extension(const FilePath& path, Location location) + : mutable_static_data_(new StaticData) { DCHECK(path.IsAbsolute()); static_data_ = mutable_static_data_; - mutable_static_data_->location = INVALID; - + mutable_static_data_->location = location; mutable_static_data_->path = MaybeNormalizePath(path); } +Extension::~Extension() { +} + ExtensionResource Extension::GetResource(const std::string& relative_path) { #if defined(OS_POSIX) FilePath relative_file_path(relative_path); @@ -2236,7 +2246,7 @@ bool Extension::CanExecuteScriptEverywhere() const { Extension::RuntimeData* Extension::GetRuntimeData() const { // TODO(mpcomplete): it would be nice if I could verify we were on the UI // thread, but we're in common and don't have access to BrowserThread. - return const_cast<Extension::RuntimeData*>(runtime_data_.get()); + return const_cast<Extension::RuntimeData*>(&runtime_data_); } ExtensionInfo::ExtensionInfo(const DictionaryValue* manifest, diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index c073987..82c007b 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -30,7 +30,7 @@ class SkBitmap; class Version; // Represents a Chrome extension. -class Extension { +class Extension : public base::RefCountedThreadSafe<Extension> { public: typedef std::map<const std::string, GURL> URLOverrideMap; typedef std::vector<std::string> ScriptingWhitelist; @@ -105,18 +105,6 @@ class Extension { struct StaticData : public base::RefCountedThreadSafe<StaticData> { StaticData(); - // TODO(mpcomplete): RefCountedThreadSafe does not allow AddRef/Release on - // const objects. I think that is a mistake. Until we can fix that, here's - // a workaround. - void AddRef() const { - const_cast<StaticData*>(this)-> - base::RefCountedThreadSafe<StaticData>::AddRef(); - } - void Release() const { - const_cast<StaticData*>(this)-> - base::RefCountedThreadSafe<StaticData>::Release(); - } - // A persistent, globally unique ID. An extension's ID is used in things // like directory structures and URLs, and is expected to not change across // versions. It is generated as a SHA-256 hash of the extension's public @@ -286,6 +274,12 @@ class Extension { const int message_id; }; + static scoped_refptr<Extension> Create(const FilePath& path, + Location location, + const DictionaryValue& value, + bool require_key, + std::string* error); + // The install message id for |permission|. Returns 0 if none exists. static int GetPermissionMessageId(const std::string& permission); @@ -359,9 +353,6 @@ class Extension { // The mimetype used for extensions. static const char kMimeType[]; - explicit Extension(const FilePath& path); - virtual ~Extension(); - // Checks to see if the extension has a valid ID. static bool IdIsValid(const std::string& id); @@ -473,22 +464,11 @@ class Extension { // Adds an extension to the scripting whitelist. Used for testing only. static void SetScriptingWhitelist(const ScriptingWhitelist& whitelist); - // Initialize the extension from a parsed manifest. - // Usually, the id of an extension is generated by the "key" property of - // its manifest, but if |require_key| is |false|, a temporary ID will be - // generated based on the path. - bool InitFromValue(const DictionaryValue& value, bool require_key, - std::string* error); - const StaticData* static_data() const { return static_data_; } const FilePath& path() const { return static_data_->path; } const GURL& url() const { return static_data_->extension_url; } Location location() const { return static_data_->location; } - void set_location(Location location) { - mutable_static_data_->location = location; - } - const std::string& id() const { return static_data_->id; } const Version* version() const { return static_data_->version.get(); } // String representation of the version number. @@ -658,10 +638,22 @@ class Extension { bool CanExecuteScriptEverywhere() const; private: + friend class base::RefCountedThreadSafe<Extension>; + // Normalize the path for use by the extension. On Windows, this will make // sure the drive letter is uppercase. static FilePath MaybeNormalizePath(const FilePath& path); + Extension(const FilePath& path, Location location); + ~Extension(); + + // Initialize the extension from a parsed manifest. + // Usually, the id of an extension is generated by the "key" property of + // its manifest, but if |require_key| is |false|, a temporary ID will be + // generated based on the path. + bool InitFromValue(const DictionaryValue& value, bool require_key, + std::string* error); + // Helper function for implementing HasCachedImage/GetCachedImage. A return // value of NULL means there is no matching image cached (we allow caching an // empty SkBitmap). @@ -736,15 +728,18 @@ class Extension { scoped_refptr<const StaticData> static_data_; // Runtime data. - scoped_ptr<const RuntimeData> runtime_data_; + const RuntimeData runtime_data_; FRIEND_TEST_ALL_PREFIXES(ExtensionTest, LoadPageActionHelper); + FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueInvalid); + FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValid); + FRIEND_TEST_ALL_PREFIXES(ExtensionTest, InitFromValueValidNameInRTL); FRIEND_TEST_ALL_PREFIXES(TabStripModelTest, Apps); DISALLOW_COPY_AND_ASSIGN(Extension); }; -typedef std::vector<Extension*> ExtensionList; +typedef std::vector< scoped_refptr<Extension> > ExtensionList; typedef std::set<std::string> ExtensionIdSet; // Handy struct to pass core extension info around. diff --git a/chrome/common/extensions/extension_file_util.cc b/chrome/common/extensions/extension_file_util.cc index d530357..c7be522 100644 --- a/chrome/common/extensions/extension_file_util.cc +++ b/chrome/common/extensions/extension_file_util.cc @@ -81,15 +81,14 @@ void UninstallExtension(const FilePath& extensions_dir, file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. } -Extension* LoadExtension(const FilePath& extension_path, - Extension::Location location, - bool require_key, - std::string* error) { +scoped_refptr<Extension> LoadExtension(const FilePath& extension_path, + Extension::Location location, + bool require_key, + std::string* error) { FilePath manifest_path = extension_path.Append(Extension::kManifestFilename); if (!file_util::PathExists(manifest_path)) { - *error = - l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); + *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); return NULL; } @@ -101,8 +100,7 @@ Extension* LoadExtension(const FilePath& extension_path, // It would be cleaner to have the JSON reader give a specific error // in this case, but other code tests for a file error with // error->empty(). For now, be consistent. - *error = - l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); + *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); } else { *error = StringPrintf("%s %s", errors::kManifestParseError, @@ -112,26 +110,23 @@ Extension* LoadExtension(const FilePath& extension_path, } if (!root->IsType(Value::TYPE_DICTIONARY)) { - *error = - l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); + *error = l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_INVALID); return NULL; } DictionaryValue* manifest = static_cast<DictionaryValue*>(root.get()); - - scoped_ptr<Extension> extension(new Extension(extension_path)); - extension->set_location(location); - - if (!extension_l10n_util::LocalizeExtension(extension.get(), manifest, error)) + if (!extension_l10n_util::LocalizeExtension(extension_path, manifest, error)) return NULL; - if (!extension->InitFromValue(*manifest, require_key, error)) + scoped_refptr<Extension> extension(Extension::Create( + extension_path, location, *manifest, require_key, error)); + if (!extension.get()) return NULL; if (!ValidateExtension(extension.get(), error)) return NULL; - return extension.release(); + return extension; } bool ValidateExtension(Extension* extension, std::string* error) { diff --git a/chrome/common/extensions/extension_file_util.h b/chrome/common/extensions/extension_file_util.h index 1b8fb76..f6a72bc 100644 --- a/chrome/common/extensions/extension_file_util.h +++ b/chrome/common/extensions/extension_file_util.h @@ -37,10 +37,10 @@ void UninstallExtension(const FilePath& extensions_dir, // Loads and validates an extension from the specified directory. Returns NULL // on failure, with a description of the error in |error|. -Extension* LoadExtension(const FilePath& extension_root, - Extension::Location location, - bool require_key, - std::string* error); +scoped_refptr<Extension> LoadExtension(const FilePath& extension_root, + Extension::Location location, + bool require_key, + std::string* error); // Returns true if the given extension object is valid and consistent. // Otherwise, a description of the error is returned in |error|. diff --git a/chrome/common/extensions/extension_file_util_unittest.cc b/chrome/common/extensions/extension_file_util_unittest.cc index 634df05..230712d 100644 --- a/chrome/common/extensions/extension_file_util_unittest.cc +++ b/chrome/common/extensions/extension_file_util_unittest.cc @@ -78,7 +78,7 @@ TEST(ExtensionFileUtil, LoadExtensionWithValidLocales) { .AppendASCII("1.0.0.0"); std::string error; - scoped_ptr<Extension> extension(extension_file_util::LoadExtension( + scoped_refptr<Extension> extension(extension_file_util::LoadExtension( install_dir, Extension::LOAD, false, &error)); ASSERT_TRUE(extension != NULL); EXPECT_EQ("The first extension that I made.", extension->description()); @@ -94,7 +94,7 @@ TEST(ExtensionFileUtil, LoadExtensionWithoutLocalesFolder) { .AppendASCII("1.0"); std::string error; - scoped_ptr<Extension> extension(extension_file_util::LoadExtension( + scoped_refptr<Extension> extension(extension_file_util::LoadExtension( install_dir, Extension::LOAD, false, &error)); ASSERT_FALSE(extension == NULL); EXPECT_TRUE(error.empty()); @@ -152,7 +152,7 @@ TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnMissingManifest) { .AppendASCII("1.0"); std::string error; - scoped_ptr<Extension> extension(extension_file_util::LoadExtension( + scoped_refptr<Extension> extension(extension_file_util::LoadExtension( install_dir, Extension::LOAD, false, &error)); ASSERT_TRUE(extension == NULL); ASSERT_FALSE(error.empty()); @@ -169,7 +169,7 @@ TEST(ExtensionFileUtil, LoadExtensionGivesHelpfullErrorOnBadManifest) { .AppendASCII("1.0"); std::string error; - scoped_ptr<Extension> extension(extension_file_util::LoadExtension( + scoped_refptr<Extension> extension(extension_file_util::LoadExtension( install_dir, Extension::LOAD, false, &error)); ASSERT_TRUE(extension == NULL); ASSERT_FALSE(error.empty()); @@ -185,7 +185,7 @@ TEST(ExtensionFileUtil, FailLoadingNonUTF8Scripts) { .AppendASCII("bad_encoding"); std::string error; - scoped_ptr<Extension> extension(extension_file_util::LoadExtension( + scoped_refptr<Extension> extension(extension_file_util::LoadExtension( install_dir, Extension::LOAD, false, &error)); ASSERT_TRUE(extension == NULL); ASSERT_STREQ("Could not load file 'bad_encoding.js' for content script. " diff --git a/chrome/common/extensions/extension_l10n_util.cc b/chrome/common/extensions/extension_l10n_util.cc index 4187689..53950cb 100644 --- a/chrome/common/extensions/extension_l10n_util.cc +++ b/chrome/common/extensions/extension_l10n_util.cc @@ -113,7 +113,7 @@ bool LocalizeManifest(const ExtensionMessageBundle& messages, return true; } -bool LocalizeExtension(Extension* extension, +bool LocalizeExtension(const FilePath& extension_path, DictionaryValue* manifest, std::string* error) { DCHECK(manifest); @@ -122,7 +122,7 @@ bool LocalizeExtension(Extension* extension, scoped_ptr<ExtensionMessageBundle> message_bundle( extension_file_util::LoadExtensionMessageBundle( - extension->path(), default_locale, error)); + extension_path, default_locale, error)); if (!message_bundle.get() && !error->empty()) return false; diff --git a/chrome/common/extensions/extension_l10n_util.h b/chrome/common/extensions/extension_l10n_util.h index 48dc901..e1fdfa0 100644 --- a/chrome/common/extensions/extension_l10n_util.h +++ b/chrome/common/extensions/extension_l10n_util.h @@ -44,7 +44,7 @@ bool LocalizeManifest(const ExtensionMessageBundle& messages, // Load message catalogs, localize manifest and attach message bundle to the // extension. -bool LocalizeExtension(Extension* extension, +bool LocalizeExtension(const FilePath& extension_path, DictionaryValue* manifest, std::string* error); diff --git a/chrome/common/extensions/extension_manifests_unittest.cc b/chrome/common/extensions/extension_manifests_unittest.cc index e800cd9..c27e2dd 100644 --- a/chrome/common/extensions/extension_manifests_unittest.cc +++ b/chrome/common/extensions/extension_manifests_unittest.cc @@ -37,53 +37,48 @@ class ExtensionManifestTest : public testing::Test { return static_cast<DictionaryValue*>(serializer.Deserialize(NULL, error)); } - Extension* LoadExtensionWithLocation(DictionaryValue* value, - Extension::Location location, - std::string* error) { + scoped_refptr<Extension> LoadExtensionWithLocation( + DictionaryValue* value, + Extension::Location location, + std::string* error) { FilePath path; PathService::Get(chrome::DIR_TEST_DATA, &path); path = path.AppendASCII("extensions").AppendASCII("manifest_tests"); - - scoped_ptr<Extension> extension(new Extension(path.DirName())); - extension->set_location(location); - - if (!extension->InitFromValue(*value, false, error)) - return NULL; - - return extension.release(); + return Extension::Create(path.DirName(), location, *value, false, error); } - Extension* LoadExtension(const std::string& name, - std::string* error) { + scoped_refptr<Extension> LoadExtension(const std::string& name, + std::string* error) { return LoadExtensionWithLocation(name, Extension::INTERNAL, error); } - Extension* LoadExtension(DictionaryValue* value, - std::string* error) { + scoped_refptr<Extension> LoadExtension(DictionaryValue* value, + std::string* error) { return LoadExtensionWithLocation(value, Extension::INTERNAL, error); } - Extension* LoadExtensionWithLocation(const std::string& name, - Extension::Location location, - std::string* error) { + scoped_refptr<Extension> LoadExtensionWithLocation( + const std::string& name, + Extension::Location location, + std::string* error) { scoped_ptr<DictionaryValue> value(LoadManifestFile(name, error)); if (!value.get()) return NULL; return LoadExtensionWithLocation(value.get(), location, error); } - Extension* LoadAndExpectSuccess(const std::string& name) { + scoped_refptr<Extension> LoadAndExpectSuccess(const std::string& name) { std::string error; - Extension* extension = LoadExtension(name, &error); + scoped_refptr<Extension> extension = LoadExtension(name, &error); EXPECT_TRUE(extension) << name; EXPECT_EQ("", error) << name; return extension; } - Extension* LoadAndExpectSuccess(DictionaryValue* manifest, - const std::string& name) { + scoped_refptr<Extension> LoadAndExpectSuccess(DictionaryValue* manifest, + const std::string& name) { std::string error; - Extension* extension = LoadExtension(manifest, &error); + scoped_refptr<Extension> extension = LoadExtension(manifest, &error); EXPECT_TRUE(extension) << "Unexpected success for " << name; EXPECT_EQ("", error) << "Unexpected no error for " << name; return extension; @@ -103,7 +98,7 @@ class ExtensionManifestTest : public testing::Test { void LoadAndExpectError(const std::string& name, const std::string& expected_error) { std::string error; - scoped_ptr<Extension> extension(LoadExtension(name, &error)); + scoped_refptr<Extension> extension(LoadExtension(name, &error)); VerifyExpectedError(extension.get(), name, error, expected_error); } @@ -111,7 +106,7 @@ class ExtensionManifestTest : public testing::Test { const std::string& name, const std::string& expected_error) { std::string error; - scoped_ptr<Extension> extension(LoadExtension(manifest, &error)); + scoped_refptr<Extension> extension(LoadExtension(manifest, &error)); VerifyExpectedError(extension.get(), name, error, expected_error); } @@ -119,7 +114,7 @@ class ExtensionManifestTest : public testing::Test { }; TEST_F(ExtensionManifestTest, ValidApp) { - scoped_ptr<Extension> extension(LoadAndExpectSuccess("valid_app.json")); + scoped_refptr<Extension> extension(LoadAndExpectSuccess("valid_app.json")); ASSERT_EQ(2u, extension->web_extent().patterns().size()); EXPECT_EQ("http://www.google.com/mail/*", extension->web_extent().patterns()[0].GetAsString()); @@ -145,7 +140,7 @@ TEST_F(ExtensionManifestTest, AppWebUrls) { ExtensionErrorUtils::FormatErrorMessage( errors::kInvalidWebURL, "0")); - scoped_ptr<Extension> extension( + scoped_refptr<Extension> extension( LoadAndExpectSuccess("web_urls_default.json")); ASSERT_EQ(1u, extension->web_extent().patterns().size()); EXPECT_EQ("*://www.google.com/*", @@ -153,21 +148,21 @@ TEST_F(ExtensionManifestTest, AppWebUrls) { } TEST_F(ExtensionManifestTest, AppLaunchContainer) { - scoped_ptr<Extension> extension; + scoped_refptr<Extension> extension; - extension.reset(LoadAndExpectSuccess("launch_tab.json")); + extension = LoadAndExpectSuccess("launch_tab.json"); EXPECT_EQ(extension_misc::LAUNCH_TAB, extension->launch_container()); - extension.reset(LoadAndExpectSuccess("launch_panel.json")); + extension = LoadAndExpectSuccess("launch_panel.json"); EXPECT_EQ(extension_misc::LAUNCH_PANEL, extension->launch_container()); - extension.reset(LoadAndExpectSuccess("launch_default.json")); + extension = LoadAndExpectSuccess("launch_default.json"); EXPECT_EQ(extension_misc::LAUNCH_TAB, extension->launch_container()); - extension.reset(LoadAndExpectSuccess("launch_width.json")); + extension = LoadAndExpectSuccess("launch_width.json"); EXPECT_EQ(640, extension->launch_width()); - extension.reset(LoadAndExpectSuccess("launch_height.json")); + extension = LoadAndExpectSuccess("launch_height.json"); EXPECT_EQ(480, extension->launch_height()); LoadAndExpectError("launch_window.json", @@ -198,15 +193,15 @@ TEST_F(ExtensionManifestTest, AppLaunchURL) { LoadAndExpectError("launch_url_invalid_type.json", errors::kInvalidLaunchWebURL); - scoped_ptr<Extension> extension; - extension.reset(LoadAndExpectSuccess("launch_local_path.json")); + scoped_refptr<Extension> extension; + extension = LoadAndExpectSuccess("launch_local_path.json"); EXPECT_EQ(extension->url().spec() + "launch.html", extension->GetFullLaunchURL().spec()); LoadAndExpectError("launch_web_url_relative.json", errors::kInvalidLaunchWebURL); - extension.reset(LoadAndExpectSuccess("launch_web_url_absolute.json")); + extension = LoadAndExpectSuccess("launch_web_url_absolute.json"); EXPECT_EQ(GURL("http://www.google.com/launch.html"), extension->GetFullLaunchURL()); } @@ -217,13 +212,13 @@ TEST_F(ExtensionManifestTest, Override) { LoadAndExpectError("override_invalid_page.json", errors::kInvalidChromeURLOverrides); - scoped_ptr<Extension> extension; + scoped_refptr<Extension> extension; - extension.reset(LoadAndExpectSuccess("override_new_tab.json")); + extension = LoadAndExpectSuccess("override_new_tab.json"); EXPECT_EQ(extension->url().spec() + "newtab.html", extension->GetChromeURLOverrides().find("newtab")->second.spec()); - extension.reset(LoadAndExpectSuccess("override_history.json")); + extension = LoadAndExpectSuccess("override_history.json"); EXPECT_EQ(extension->url().spec() + "history.html", extension->GetChromeURLOverrides().find("history")->second.spec()); } @@ -237,11 +232,11 @@ TEST_F(ExtensionManifestTest, ChromeResourcesPermissionValidOnlyForComponents) { LoadAndExpectError("permission_chrome_resources_url.json", errors::kInvalidPermissionScheme); std::string error; - scoped_ptr<Extension> extension; - extension.reset(LoadExtensionWithLocation( + scoped_refptr<Extension> extension; + extension = LoadExtensionWithLocation( "permission_chrome_resources_url.json", Extension::COMPONENT, - &error)); + &error); EXPECT_EQ("", error); } @@ -260,8 +255,8 @@ TEST_F(ExtensionManifestTest, DevToolsExtensions) { CommandLine::ForCurrentProcess()->AppendSwitch( switches::kEnableExperimentalExtensionApis); - scoped_ptr<Extension> extension; - extension.reset(LoadAndExpectSuccess("devtools_extension.json")); + scoped_refptr<Extension> extension; + extension = LoadAndExpectSuccess("devtools_extension.json"); EXPECT_EQ(extension->url().spec() + "devtools.html", extension->devtools_url().spec()); *CommandLine::ForCurrentProcess() = old_command_line; @@ -275,11 +270,10 @@ TEST_F(ExtensionManifestTest, DisallowHybridApps) { } TEST_F(ExtensionManifestTest, OptionsPageInApps) { - scoped_ptr<Extension> extension; + scoped_refptr<Extension> extension; // Allow options page with absolute URL in hosed apps. - extension.reset( - LoadAndExpectSuccess("hosted_app_absolute_options.json")); + extension = LoadAndExpectSuccess("hosted_app_absolute_options.json"); EXPECT_STREQ("http", extension->options_url().scheme().c_str()); EXPECT_STREQ("example.com", @@ -315,8 +309,8 @@ TEST_F(ExtensionManifestTest, DisallowExtensionPermissions) { permissions->Append(p); std::string message_name = StringPrintf("permission-%s", name); if (Extension::IsHostedAppPermission(name)) { - scoped_ptr<Extension> extension; - extension.reset(LoadAndExpectSuccess(manifest.get(), message_name)); + scoped_refptr<Extension> extension; + extension = LoadAndExpectSuccess(manifest.get(), message_name); } else { LoadAndExpectError(manifest.get(), message_name, errors::kInvalidPermission); @@ -325,7 +319,7 @@ TEST_F(ExtensionManifestTest, DisallowExtensionPermissions) { } TEST_F(ExtensionManifestTest, NormalizeIconPaths) { - scoped_ptr<Extension> extension( + scoped_refptr<Extension> extension( LoadAndExpectSuccess("normalize_icon_paths.json")); EXPECT_EQ("16.png", extension->icons().Get(16, ExtensionIconSet::MATCH_EXACTLY)); @@ -340,7 +334,8 @@ TEST_F(ExtensionManifestTest, DisallowMultipleUISurfaces) { } TEST_F(ExtensionManifestTest, ParseHomepageURLs) { - scoped_ptr<Extension> extension(LoadAndExpectSuccess("homepage_valid.json")); + scoped_refptr<Extension> extension( + LoadAndExpectSuccess("homepage_valid.json")); LoadAndExpectError("homepage_empty.json", extension_manifest_errors::kInvalidHomepageURL); LoadAndExpectError("homepage_invalid.json", @@ -348,22 +343,23 @@ TEST_F(ExtensionManifestTest, ParseHomepageURLs) { } TEST_F(ExtensionManifestTest, GetHomepageURL) { - scoped_ptr<Extension> extension(LoadAndExpectSuccess("homepage_valid.json")); + scoped_refptr<Extension> extension( + LoadAndExpectSuccess("homepage_valid.json")); EXPECT_EQ(GURL("http://foo.com#bar"), extension->GetHomepageURL()); // The Google Gallery URL ends with the id, which depends on the path, which // can be different in testing, so we just check the part before id. - extension.reset(LoadAndExpectSuccess("homepage_google_hosted.json")); + extension = LoadAndExpectSuccess("homepage_google_hosted.json"); EXPECT_TRUE(StartsWithASCII(extension->GetHomepageURL().spec(), "https://chrome.google.com/extensions/detail/", false)); - extension.reset(LoadAndExpectSuccess("homepage_externally_hosted.json")); + extension = LoadAndExpectSuccess("homepage_externally_hosted.json"); EXPECT_EQ(GURL(), extension->GetHomepageURL()); } TEST_F(ExtensionManifestTest, DefaultPathForExtent) { - scoped_ptr<Extension> extension( + scoped_refptr<Extension> extension( LoadAndExpectSuccess("default_path_for_extent.json")); ASSERT_EQ(1u, extension->web_extent().patterns().size()); diff --git a/chrome/common/extensions/extension_unittest.cc b/chrome/common/extensions/extension_unittest.cc index d2a7049..3bad1dc 100644 --- a/chrome/common/extensions/extension_unittest.cc +++ b/chrome/common/extensions/extension_unittest.cc @@ -77,7 +77,9 @@ TEST(ExtensionTest, InitFromValueInvalid) { #elif defined(OS_POSIX) FilePath path(FILE_PATH_LITERAL("/foo")); #endif - Extension extension(path); + scoped_refptr<Extension> extension_ptr(new Extension(path, + Extension::INVALID)); + Extension& extension = *extension_ptr; int error_code = 0; std::string error; @@ -303,7 +305,9 @@ TEST(ExtensionTest, InitFromValueValid) { #elif defined(OS_POSIX) FilePath path(FILE_PATH_LITERAL("/foo")); #endif - Extension extension(path); + scoped_refptr<Extension> extension_ptr(new Extension(path, + Extension::INVALID)); + Extension& extension = *extension_ptr; std::string error; DictionaryValue input_value; @@ -366,7 +370,9 @@ TEST(ExtensionTest, InitFromValueValidNameInRTL) { #elif defined(OS_POSIX) FilePath path(FILE_PATH_LITERAL("/foo")); #endif - Extension extension(path); + scoped_refptr<Extension> extension_ptr(new Extension(path, + Extension::INVALID)); + Extension& extension = *extension_ptr; std::string error; DictionaryValue input_value; @@ -403,18 +409,20 @@ TEST(ExtensionTest, GetResourceURLAndPath) { #elif defined(OS_POSIX) FilePath path(FILE_PATH_LITERAL("/foo")); #endif - Extension extension(path); DictionaryValue input_value; input_value.SetString(keys::kVersion, "1.0.0.0"); input_value.SetString(keys::kName, "my extension"); - EXPECT_TRUE(extension.InitFromValue(input_value, false, NULL)); - - EXPECT_EQ(extension.url().spec() + "bar/baz.js", - Extension::GetResourceURL(extension.url(), "bar/baz.js").spec()); - EXPECT_EQ(extension.url().spec() + "baz.js", - Extension::GetResourceURL(extension.url(), "bar/../baz.js").spec()); - EXPECT_EQ(extension.url().spec() + "baz.js", - Extension::GetResourceURL(extension.url(), "../baz.js").spec()); + scoped_refptr<Extension> extension(Extension::Create( + path, Extension::INVALID, input_value, false, NULL)); + EXPECT_TRUE(extension.get()); + + EXPECT_EQ(extension->url().spec() + "bar/baz.js", + Extension::GetResourceURL(extension->url(), "bar/baz.js").spec()); + EXPECT_EQ(extension->url().spec() + "baz.js", + Extension::GetResourceURL(extension->url(), + "bar/../baz.js").spec()); + EXPECT_EQ(extension->url().spec() + "baz.js", + Extension::GetResourceURL(extension->url(), "../baz.js").spec()); } TEST(ExtensionTest, LoadPageActionHelper) { @@ -423,7 +431,9 @@ TEST(ExtensionTest, LoadPageActionHelper) { #else FilePath path(StringPrintf("/extension")); #endif - Extension extension(path); + scoped_refptr<Extension> extension_ptr(new Extension(path, + Extension::INVALID)); + Extension& extension = *extension_ptr; std::string error_msg; scoped_ptr<ExtensionAction> action; DictionaryValue input; @@ -655,14 +665,15 @@ TEST(ExtensionTest, UpdateUrls) { #else FilePath path(StringPrintf("/extension%" PRIuS, i)); #endif - Extension extension(path); std::string error; input_value.SetString(keys::kVersion, "1.0"); input_value.SetString(keys::kName, "Test"); input_value.SetString(keys::kUpdateURL, url.spec()); - EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); + scoped_refptr<Extension> extension(Extension::Create( + path, Extension::INVALID, input_value, false, &error)); + EXPECT_TRUE(extension.get()) << error; } // Test some invalid update urls @@ -679,13 +690,14 @@ TEST(ExtensionTest, UpdateUrls) { #else FilePath path(StringPrintf("/extension%" PRIuS, i)); #endif - Extension extension(path); std::string error; input_value.SetString(keys::kVersion, "1.0"); input_value.SetString(keys::kName, "Test"); input_value.SetString(keys::kUpdateURL, invalid[i]); - EXPECT_FALSE(extension.InitFromValue(input_value, false, &error)); + scoped_refptr<Extension> extension(Extension::Create( + path, Extension::INVALID, input_value, false, &error)); + EXPECT_FALSE(extension.get()); EXPECT_TRUE(MatchPattern(error, errors::kInvalidUpdateURL)); } } @@ -714,8 +726,8 @@ TEST(ExtensionTest, MimeTypeSniffing) { EXPECT_EQ("application/octet-stream", result); } -static Extension* LoadManifest(const std::string& dir, - const std::string& test_file) { +static scoped_refptr<Extension> LoadManifest(const std::string& dir, + const std::string& test_file) { FilePath path; PathService::Get(chrome::DIR_TEST_DATA, &path); path = path.AppendASCII("extensions") @@ -730,74 +742,70 @@ static Extension* LoadManifest(const std::string& dir, return NULL; } - scoped_ptr<Extension> extension(new Extension(path.DirName())); - EXPECT_TRUE(extension->InitFromValue( - *static_cast<DictionaryValue*>(result.get()), false, &error)) << error; - - return extension.release(); + scoped_refptr<Extension> extension = Extension::Create( + path.DirName(), Extension::INVALID, + *static_cast<DictionaryValue*>(result.get()), false, &error); + EXPECT_TRUE(extension) << error; + return extension; } TEST(ExtensionTest, EffectiveHostPermissions) { - scoped_ptr<Extension> extension; + scoped_refptr<Extension> extension; ExtensionExtent hosts; - extension.reset(LoadManifest("effective_host_permissions", "empty.json")); + extension = LoadManifest("effective_host_permissions", "empty.json"); EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); EXPECT_FALSE(hosts.ContainsURL(GURL("http://www.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", "one_host.json")); + extension = LoadManifest("effective_host_permissions", "one_host.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); EXPECT_FALSE(hosts.ContainsURL(GURL("https://www.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "one_host_wildcard.json")); + extension = LoadManifest("effective_host_permissions", + "one_host_wildcard.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://foo.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "two_hosts.json")); + extension = LoadManifest("effective_host_permissions", "two_hosts.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "https_not_considered.json")); + extension = LoadManifest("effective_host_permissions", + "https_not_considered.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); EXPECT_TRUE(hosts.ContainsURL(GURL("https://google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "two_content_scripts.json")); + extension = LoadManifest("effective_host_permissions", + "two_content_scripts.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://news.ycombinator.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "all_hosts.json")); + extension = LoadManifest("effective_host_permissions", "all_hosts.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); EXPECT_FALSE(hosts.ContainsURL(GURL("https://test/"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "all_hosts2.json")); + extension = LoadManifest("effective_host_permissions", "all_hosts2.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); - extension.reset(LoadManifest("effective_host_permissions", - "all_hosts3.json")); + extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); hosts = extension->GetEffectiveHostPermissions(); EXPECT_FALSE(hosts.ContainsURL(GURL("http://test/"))); EXPECT_TRUE(hosts.ContainsURL(GURL("https://test/"))); @@ -834,10 +842,10 @@ TEST(ExtensionTest, IsPrivilegeIncrease) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { - scoped_ptr<Extension> old_extension( + scoped_refptr<Extension> old_extension( LoadManifest("allow_silent_upgrade", std::string(kTests[i].base_name) + "_old.json")); - scoped_ptr<Extension> new_extension( + scoped_refptr<Extension> new_extension( LoadManifest("allow_silent_upgrade", std::string(kTests[i].base_name) + "_new.json")); @@ -912,11 +920,12 @@ TEST(ExtensionTest, ImageCaching) { // Initialize the Extension. std::string errors; - scoped_ptr<Extension> extension(new Extension(path)); DictionaryValue values; values.SetString(keys::kName, "test"); values.SetString(keys::kVersion, "0.1"); - ASSERT_TRUE(extension->InitFromValue(values, false, &errors)); + scoped_refptr<Extension> extension(Extension::Create( + path, Extension::INVALID, values, false, &errors)); + ASSERT_TRUE(extension.get()); // Create an ExtensionResource pointing at an icon. FilePath icon_relative_path(FILE_PATH_LITERAL("icon3.png")); @@ -997,10 +1006,11 @@ TEST(ExtensionTest, OldUnlimitedStoragePermission) { // Initialize the extension and make sure the permission for unlimited storage // is present. - Extension extension(extension_path); std::string errors; - EXPECT_TRUE(extension.InitFromValue(dictionary, false, &errors)); - EXPECT_TRUE(extension.HasApiPermission( + scoped_refptr<Extension> extension(Extension::Create( + extension_path, Extension::INVALID, dictionary, false, &errors)); + EXPECT_TRUE(extension.get()); + EXPECT_TRUE(extension->HasApiPermission( Extension::kUnlimitedStoragePermission)); } @@ -1036,8 +1046,8 @@ TEST(ExtensionTest, ApiPermissions) { { "tabs.getSelected", false}, }; - scoped_ptr<Extension> extension; - extension.reset(LoadManifest("empty_manifest", "empty.json")); + scoped_refptr<Extension> extension; + extension = LoadManifest("empty_manifest", "empty.json"); for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { EXPECT_EQ(kTests[i].expect_success, diff --git a/chrome/common/extensions/extension_unpacker.cc b/chrome/common/extensions/extension_unpacker.cc index d431bcb..3f718c9 100644 --- a/chrome/common/extensions/extension_unpacker.cc +++ b/chrome/common/extensions/extension_unpacker.cc @@ -175,20 +175,21 @@ bool ExtensionUnpacker::Run() { // InitFromValue is allowed to generate a temporary id for the extension. // ANY CODE THAT FOLLOWS SHOULD NOT DEPEND ON THE CORRECT ID OF THIS // EXTENSION. - Extension extension(temp_install_dir_); std::string error; - if (!extension.InitFromValue(*parsed_manifest_, false, &error)) { + scoped_refptr<Extension> extension(Extension::Create( + temp_install_dir_, Extension::INVALID, *parsed_manifest_, false, &error)); + if (!extension.get()) { SetError(error); return false; } - if (!extension_file_util::ValidateExtension(&extension, &error)) { + if (!extension_file_util::ValidateExtension(extension.get(), &error)) { SetError(error); return false; } // Decode any images that the browser needs to display. - std::set<FilePath> image_paths = extension.GetBrowserImages(); + std::set<FilePath> image_paths = extension->GetBrowserImages(); for (std::set<FilePath>::iterator it = image_paths.begin(); it != image_paths.end(); ++it) { if (!AddDecodedImage(*it)) @@ -197,8 +198,8 @@ bool ExtensionUnpacker::Run() { // Parse all message catalogs (if any). parsed_catalogs_.reset(new DictionaryValue); - if (!extension.default_locale().empty()) { - if (!ReadAllMessageCatalogs(extension.default_locale())) + if (!extension->default_locale().empty()) { + if (!ReadAllMessageCatalogs(extension->default_locale())) return false; // Error was already reported. } |