diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-25 07:50:11 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-25 07:50:11 +0000 |
commit | 75709691d97122dbefd7d73ce0cfade557e498b5 (patch) | |
tree | f79b37918e4c345a21036a11d5d7c6520e118ab6 /chrome/common | |
parent | 5ed550d5336a5c159adf5dd319a62d13d01c0eed (diff) | |
download | chromium_src-75709691d97122dbefd7d73ce0cfade557e498b5.zip chromium_src-75709691d97122dbefd7d73ce0cfade557e498b5.tar.gz chromium_src-75709691d97122dbefd7d73ce0cfade557e498b5.tar.bz2 |
Allow multiple domains in app.
Subsequent changes will:
* Allow URLPatterns to specify * for scheme, so that http/https doesn't need to be repeated for each host.
* Fix the overlap detection.
Suggested review order:
- test files
- url_pattern*
- extension_extent*
- extension*
- everything else
BUG=46633
Review URL: http://codereview.chromium.org/2876007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50834 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/extensions/docs/examples/extensions/maps_app/manifest.json | 13 | ||||
-rw-r--r-- | chrome/common/extensions/extension.cc | 113 | ||||
-rw-r--r-- | chrome/common/extensions/extension.h | 3 | ||||
-rw-r--r-- | chrome/common/extensions/extension_constants.cc | 22 | ||||
-rw-r--r-- | chrome/common/extensions/extension_constants.h | 12 | ||||
-rw-r--r-- | chrome/common/extensions/extension_extent.cc | 19 | ||||
-rw-r--r-- | chrome/common/extensions/extension_extent.h | 20 | ||||
-rw-r--r-- | chrome/common/extensions/extension_extent_unittest.cc | 42 | ||||
-rw-r--r-- | chrome/common/extensions/extension_manifests_unittest.cc | 50 | ||||
-rw-r--r-- | chrome/common/extensions/url_pattern.cc | 19 | ||||
-rw-r--r-- | chrome/common/extensions/url_pattern.h | 16 | ||||
-rw-r--r-- | chrome/common/render_messages.h | 16 |
12 files changed, 141 insertions, 204 deletions
diff --git a/chrome/common/extensions/docs/examples/extensions/maps_app/manifest.json b/chrome/common/extensions/docs/examples/extensions/maps_app/manifest.json index 7ac6a16..d402a74 100644 --- a/chrome/common/extensions/docs/examples/extensions/maps_app/manifest.json +++ b/chrome/common/extensions/docs/examples/extensions/maps_app/manifest.json @@ -2,12 +2,13 @@ "name": "Google Maps", "version": "3", "icons": { "24": "24.png", "128": "128.png" }, - "launch": { - "web_url": "http://maps.google.com/" + "app": { + "urls": [ + "http://maps.google.com/" + ], + "launch": { + "web_url": "http://maps.google.com/" + } }, "permissions": ["geolocation"], - "web_content": { - "enabled": true, - "origin": "http://maps.google.com" - } } diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 3631af0..97ee0201 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -532,77 +532,43 @@ bool Extension::LoadIsApp(const DictionaryValue* manifest, return true; } -bool Extension::LoadWebOrigin(const DictionaryValue* manifest, - std::string* error) { +bool Extension::LoadWebURLs(const DictionaryValue* manifest, + std::string* error) { Value* temp = NULL; - if (!manifest->Get(keys::kWebOrigin, &temp)) + if (!manifest->Get(keys::kWebURLs, &temp)) return true; - // Check datatype. - std::string origin_string; - if (!temp->GetAsString(&origin_string)) { - *error = errors::kInvalidWebOrigin; - return false; - } - - // Origin must be a valid URL. - GURL origin_gurl(origin_string); - if (!origin_gurl.is_valid() || origin_gurl.is_empty()) { - *error = errors::kInvalidWebOrigin; + if (temp->GetType() != Value::TYPE_LIST) { + *error = errors::kInvalidWebURLs; return false; } - // Origins can only be http or https. - if (!origin_gurl.SchemeIs(chrome::kHttpScheme) && - !origin_gurl.SchemeIs(chrome::kHttpsScheme)) { - *error = errors::kInvalidWebOrigin; - return false; - } - - // Check that the origin doesn't include any extraneous information. - if (origin_gurl.GetOrigin() != origin_gurl) { - *error = errors::kInvalidWebOrigin; - return false; - } - - web_extent_.set_origin(origin_gurl); - return true; -} - -bool Extension::LoadWebPaths(const DictionaryValue* manifest, - std::string* error) { - Value* temp = NULL; - if (!manifest->Get(keys::kWebPaths, &temp)) - return true; - - // Check datatype. - if (!temp->IsType(Value::TYPE_LIST)) { - *error = errors::kInvalidWebPaths; - return false; - } + ListValue* pattern_list = static_cast<ListValue*>(temp); + for (size_t i = 0; i < pattern_list->GetSize(); ++i) { + std::string pattern_string; + if (!pattern_list->GetString(i, &pattern_string)) { + *error = ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidWebURL, UintToString(i)); + return false; + } - ListValue* web_paths = static_cast<ListValue*>(temp); - for (size_t i = 0; i < web_paths->GetSize(); ++i) { - // Get item and check datatype. - std::string item; - if (!web_paths->GetString(i, &item) || item.empty()) { + URLPattern pattern; + if (!pattern.Parse(pattern_string)) { *error = ExtensionErrorUtils::FormatErrorMessage( - errors::kInvalidWebPath, IntToString(i)); + errors::kInvalidWebURL, UintToString(i)); return false; } - // Ensure the path is a valid relative URL by resolving it against the - // extension root. - // TODO(aa): This is hacky. Is there another way to know whether a string - // is a valid relative URL? - GURL resolved = extension_url_.Resolve(item); - if (!resolved.is_valid() || resolved.GetOrigin() != extension_url_) { + // We do not allow authors to put wildcards in their paths. Instead, we + // imply one at the end. + if (pattern.path().find('*') != std::string::npos) { *error = ExtensionErrorUtils::FormatErrorMessage( - errors::kInvalidWebPath, IntToString(i)); + errors::kInvalidWebURL, UintToString(i)); return false; } + pattern.set_path(pattern.path() + '*'); - web_extent_.add_path(item); + web_extent_.AddPattern(pattern); } return true; @@ -612,8 +578,8 @@ bool Extension::LoadLaunchURL(const DictionaryValue* manifest, std::string* error) { Value* temp = NULL; - // launch URL can be either local (to chrome-extension:// root) or web (either - // relative to the origin, or an absolute URL). + // launch URL can be either local (to chrome-extension:// root) or an absolute + // web URL. if (manifest->Get(keys::kLaunchLocalPath, &temp)) { if (manifest->Get(keys::kLaunchWebURL, NULL)) { *error = errors::kLaunchPathAndURLAreExclusive; @@ -641,8 +607,8 @@ bool Extension::LoadLaunchURL(const DictionaryValue* manifest, return false; } - // Ensure the launch URL is a valid relative or absolute URL. - if (!extension_url_.Resolve(launch_url).is_valid()) { + // Ensure the launch URL is a valid absolute URL. + if (!GURL(launch_url).is_valid()) { *error = errors::kInvalidLaunchWebURL; return false; } @@ -653,6 +619,16 @@ bool Extension::LoadLaunchURL(const DictionaryValue* manifest, return false; } + // If there is no extent, we default the extent based on the launch URL. + if (web_extent_.is_empty() && !launch_web_url_.empty()) { + GURL launch_url(launch_web_url_); + URLPattern pattern; + pattern.set_scheme(launch_url.scheme()); + pattern.set_host(launch_url.host()); + pattern.set_path("/*"); + web_extent_.AddPattern(pattern); + } + return true; } @@ -1479,8 +1455,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_key, } if (!LoadIsApp(manifest_value_.get(), error) || - !LoadWebOrigin(manifest_value_.get(), error) || - !LoadWebPaths(manifest_value_.get(), error) || + !LoadWebURLs(manifest_value_.get(), error) || !LoadLaunchURL(manifest_value_.get(), error) || !LoadLaunchContainer(manifest_value_.get(), error) || !LoadLaunchFullscreen(manifest_value_.get(), error)) { @@ -1557,18 +1532,10 @@ std::set<FilePath> Extension::GetBrowserImages() { } GURL Extension::GetFullLaunchURL() const { - if (!launch_local_path_.empty()) { + if (!launch_local_path_.empty()) return extension_url_.Resolve(launch_local_path_); - } else if (!launch_web_url_.empty()) { - // If there is a web origin, we interpret the launch URL relatively to that. - // Otherwise, hopefully it was an absolute URL. - if (web_extent_.origin().is_valid()) - return web_extent_.origin().Resolve(launch_web_url_); - else - return GURL(launch_web_url_); - } else { - return GURL(); - } + else + return GURL(launch_web_url_); } bool Extension::GetBackgroundPageReady() { diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 0d4c10a..8748deb 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -397,8 +397,7 @@ class Extension { // Helpers to load various chunks of the manifest. bool LoadIsApp(const DictionaryValue* manifest, std::string* error); - bool LoadWebOrigin(const DictionaryValue* manifest, std::string* error); - bool LoadWebPaths(const DictionaryValue* manifest, std::string* error); + bool LoadWebURLs(const DictionaryValue* manifest, std::string* error); bool LoadLaunchContainer(const DictionaryValue* manifest, std::string* error); bool LoadLaunchFullscreen(const DictionaryValue* manifest, std::string* error); diff --git a/chrome/common/extensions/extension_constants.cc b/chrome/common/extensions/extension_constants.cc index 423d491..e4f368a 100644 --- a/chrome/common/extensions/extension_constants.cc +++ b/chrome/common/extensions/extension_constants.cc @@ -17,7 +17,9 @@ const wchar_t* kCss = L"css"; const wchar_t* kCurrentLocale = L"current_locale"; const wchar_t* kDefaultLocale = L"default_locale"; const wchar_t* kDescription = L"description"; +const wchar_t* kExcludeGlobs = L"exclude_globs"; const wchar_t* kIcons = L"icons"; +const wchar_t* kIncludeGlobs = L"include_globs"; const wchar_t* kJs = L"js"; const wchar_t* kLaunch = L"app.launch"; const wchar_t* kLaunchContainer = L"app.launch.container"; @@ -28,9 +30,8 @@ const wchar_t* kLaunchWebURL = L"app.launch.web_url"; const wchar_t* kLaunchWidth = L"app.launch.width"; const wchar_t* kMatches = L"matches"; const wchar_t* kMinimumChromeVersion = L"minimum_chrome_version"; -const wchar_t* kIncludeGlobs = L"include_globs"; -const wchar_t* kExcludeGlobs = L"exclude_globs"; const wchar_t* kName = L"name"; +const wchar_t* kOmniboxKeyword = L"omnibox_keyword"; const wchar_t* kPageActionId = L"id"; const wchar_t* kPageAction = L"page_action"; const wchar_t* kPageActions = L"page_actions"; @@ -61,10 +62,7 @@ const wchar_t* kType = L"type"; const wchar_t* kVersion = L"version"; const wchar_t* kUpdateURL = L"update_url"; const wchar_t* kOptionsPage = L"options_page"; -const wchar_t* kWebContent = L"app.web_content"; -const wchar_t* kWebOrigin = L"app.web_content.origin"; -const wchar_t* kWebPaths = L"app.web_content.paths"; -const wchar_t* kOmniboxKeyword = L"omnibox_keyword"; +const wchar_t* kWebURLs = L"app.urls"; } // namespace extension_manifest_keys namespace extension_manifest_values { @@ -218,14 +216,10 @@ const char* kInvalidThemeTints = "Invalid value for theme images - tints must be decimal numbers."; const char* kInvalidUpdateURL = "Invalid value for update url: '[*]'."; -const char* kInvalidWebContentEnabled = - "Invalid value for 'app.web_content.enabled'."; -const char* kInvalidWebOrigin = - "Invalid value for 'app.web_content.origin'."; -const char* kInvalidWebPaths = - "Invalid value for 'app.web_content.paths'."; -const char* kInvalidWebPath = - "Invalid value for 'app.web_contents.paths[*]'."; +const char* kInvalidWebURLs = + "Invalid value for 'app.urls'."; +const char* kInvalidWebURL = + "Invalid value for 'app.urls[*]'."; const char* kInvalidDefaultLocale = "Invalid value for default locale - locale name must be a string."; const char* kOneUISurfaceOnly = diff --git a/chrome/common/extensions/extension_constants.h b/chrome/common/extensions/extension_constants.h index b7f9ec8..a2a972b 100644 --- a/chrome/common/extensions/extension_constants.h +++ b/chrome/common/extensions/extension_constants.h @@ -32,6 +32,7 @@ namespace extension_manifest_keys { extern const wchar_t* kJs; extern const wchar_t* kMatches; extern const wchar_t* kName; + extern const wchar_t* kOmniboxKeyword; extern const wchar_t* kPageActionId; extern const wchar_t* kPageAction; extern const wchar_t* kPageActions; @@ -62,11 +63,8 @@ namespace extension_manifest_keys { extern const wchar_t* kVersion; extern const wchar_t* kUpdateURL; extern const wchar_t* kOptionsPage; - extern const wchar_t* kWebContent; extern const wchar_t* kWebLaunchUrl; - extern const wchar_t* kWebOrigin; - extern const wchar_t* kWebPaths; - extern const wchar_t* kOmniboxKeyword; + extern const wchar_t* kWebURLs; } // namespace extension_manifest_keys // Some values expected in manifests. @@ -144,10 +142,8 @@ namespace extension_manifest_errors { extern const char* kInvalidThemeImages; extern const char* kInvalidThemeColors; extern const char* kInvalidThemeTints; - extern const char* kInvalidWebContentEnabled; - extern const char* kInvalidWebOrigin; - extern const char* kInvalidWebPaths; - extern const char* kInvalidWebPath; + extern const char* kInvalidWebURLs; + extern const char* kInvalidWebURL; extern const char* kOneUISurfaceOnly; extern const char* kThemesCannotContainExtensions; extern const char* kManifestParseError; diff --git a/chrome/common/extensions/extension_extent.cc b/chrome/common/extensions/extension_extent.cc index f716291..b479c5b 100644 --- a/chrome/common/extensions/extension_extent.cc +++ b/chrome/common/extensions/extension_extent.cc @@ -4,24 +4,11 @@ #include "chrome/common/extensions/extension_extent.h" -#include "base/string_util.h" - bool ExtensionExtent::ContainsURL(const GURL& url) const { - if (origin_.is_empty() || !url.is_valid()) - return false; - - if (url.GetOrigin() != origin_) - return false; - - if (paths_.empty()) - return true; - - for (size_t i = 0; i < paths_.size(); ++i) { - if (StartsWithASCII(url.path(), - std::string("/") + paths_[i], - false)) { // not case sensitive + for (PatternList::const_iterator pattern = patterns_.begin(); + pattern != patterns_.end(); ++pattern) { + if (pattern->MatchesUrl(url)) return true; - } } return false; diff --git a/chrome/common/extensions/extension_extent.h b/chrome/common/extensions/extension_extent.h index 187303d..df8d02d 100644 --- a/chrome/common/extensions/extension_extent.h +++ b/chrome/common/extensions/extension_extent.h @@ -8,27 +8,25 @@ #include <string> #include <vector> +#include "chrome/common/extensions/url_pattern.h" #include "googleurl/src/gurl.h" // Represents the set of URLs an extension uses for web content. class ExtensionExtent { public: - const std::vector<std::string>& paths() const { return paths_; } - void add_path(const std::string& val) { paths_.push_back(val); } - void clear_paths() { paths_.clear(); } + typedef std::vector<URLPattern> PatternList; - const GURL& origin() const { return origin_; } - void set_origin(const GURL& val) { origin_ = val; } + bool is_empty() const { return patterns_.empty(); } + + const PatternList& patterns() const { return patterns_; } + void AddPattern(const URLPattern& pattern) { patterns_.push_back(pattern); } + void ClearPaths() { patterns_.clear(); } bool ContainsURL(const GURL& url) const; private: - // The security origin (scheme+host+port) of the extent. - GURL origin_; - - // A set of path prefixes that further restrict the set of valid URLs below - // origin_. This may be empty. - std::vector<std::string> paths_; + // The list of URL patterns that comprise the extent. + PatternList patterns_; }; #endif // CHROME_COMMON_EXTENSIONS_EXTENSION_EXTENT_H_ diff --git a/chrome/common/extensions/extension_extent_unittest.cc b/chrome/common/extensions/extension_extent_unittest.cc index 4e308fa..41f2d15 100644 --- a/chrome/common/extensions/extension_extent_unittest.cc +++ b/chrome/common/extensions/extension_extent_unittest.cc @@ -14,44 +14,22 @@ TEST(ExtensionExtentTest, Empty) { EXPECT_FALSE(extent.ContainsURL(GURL("invalid"))); } -TEST(ExtensionExtentTest, OriginOnly) { +TEST(ExtensionExtentTest, One) { ExtensionExtent extent; - extent.set_origin(GURL("http://www.google.com/")); + extent.AddPattern(*URLPattern::CreateFromString("http://www.google.com/*")); EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foo"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foobar"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foo/bar"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/?stuff#here"))); - + EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey"))); EXPECT_FALSE(extent.ContainsURL(GURL("https://www.google.com/"))); - EXPECT_FALSE(extent.ContainsURL(GURL("http://www.google.com:8080/"))); + EXPECT_FALSE(extent.ContainsURL(GURL("https://www.microsoft.com/"))); } -TEST(ExtensionExtentTest, OriginAndOnePath) { +TEST(ExtensionExtentTest, Two) { ExtensionExtent extent; - extent.set_origin(GURL("http://www.google.com/")); - extent.add_path("foo"); - - EXPECT_FALSE(extent.ContainsURL(GURL("http://www.google.com/"))); - EXPECT_FALSE(extent.ContainsURL(GURL("http://www.google.com/fo"))); - - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foo"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/FOO"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foobar"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foo/bar"))); -} - -TEST(ExtensionExtentTest, OriginAndTwoPaths) { - ExtensionExtent extent; - extent.set_origin(GURL("http://www.google.com/")); - extent.add_path("foo"); - extent.add_path("hot"); - - EXPECT_FALSE(extent.ContainsURL(GURL("http://www.google.com/monkey"))); + extent.AddPattern(*URLPattern::CreateFromString("http://www.google.com/*")); + extent.AddPattern(*URLPattern::CreateFromString("http://www.yahoo.com/*")); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foo"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/hot"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/foobar"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/hotdog"))); + EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey"))); + EXPECT_TRUE(extent.ContainsURL(GURL("http://www.yahoo.com/monkey"))); + EXPECT_FALSE(extent.ContainsURL(GURL("https://www.apple.com/monkey"))); } diff --git a/chrome/common/extensions/extension_manifests_unittest.cc b/chrome/common/extensions/extension_manifests_unittest.cc index 21f38537..f91fdef 100644 --- a/chrome/common/extensions/extension_manifests_unittest.cc +++ b/chrome/common/extensions/extension_manifests_unittest.cc @@ -87,35 +87,34 @@ TEST_F(ManifestTest, AppsDisabledByDefault) { TEST_F(ManifestTest, ValidApp) { scoped_ptr<Extension> extension(LoadAndExpectSuccess("valid_app.json")); - EXPECT_EQ(GURL("http://www.google.com/"), extension->web_extent().origin()); - EXPECT_EQ(2u, extension->web_extent().paths().size()); - EXPECT_EQ("mail/", extension->web_extent().paths()[0]); - EXPECT_EQ("foobar/", extension->web_extent().paths()[1]); + ASSERT_EQ(2u, extension->web_extent().patterns().size()); + EXPECT_EQ("http://www.google.com/mail/*", + extension->web_extent().patterns()[0].GetAsString()); + EXPECT_EQ("http://www.google.com/foobar/*", + extension->web_extent().patterns()[1].GetAsString()); EXPECT_EQ(Extension::LAUNCH_WINDOW, extension->launch_container()); EXPECT_EQ(false, extension->launch_fullscreen()); - EXPECT_EQ("mail/", extension->launch_web_url()); + EXPECT_EQ("http://www.google.com/mail/", extension->launch_web_url()); } -TEST_F(ManifestTest, AppWebOrigin) { - LoadAndExpectError("web_origin_wrong_type.json", - errors::kInvalidWebOrigin); - LoadAndExpectError("web_origin_invalid_1.json", - errors::kInvalidWebOrigin); - LoadAndExpectError("web_origin_invalid_2.json", - errors::kInvalidWebOrigin); - LoadAndExpectError("web_origin_invalid_3.json", - errors::kInvalidWebOrigin); -} - -TEST_F(ManifestTest, AppWebPaths) { - LoadAndExpectError("web_paths_wrong_type.json", - errors::kInvalidWebPaths); - LoadAndExpectError("web_paths_invalid_path_1.json", +TEST_F(ManifestTest, AppWebUrls) { + LoadAndExpectError("web_urls_wrong_type.json", + errors::kInvalidWebURLs); + LoadAndExpectError("web_urls_invalid_1.json", ExtensionErrorUtils::FormatErrorMessage( - errors::kInvalidWebPath, "0")); - LoadAndExpectError("web_paths_invalid_path_2.json", + errors::kInvalidWebURL, "0")); + LoadAndExpectError("web_urls_invalid_2.json", ExtensionErrorUtils::FormatErrorMessage( - errors::kInvalidWebPath, "0")); + errors::kInvalidWebURL, "0")); + LoadAndExpectError("web_urls_invalid_3.json", + ExtensionErrorUtils::FormatErrorMessage( + errors::kInvalidWebURL, "0")); + + scoped_ptr<Extension> extension( + LoadAndExpectSuccess("web_urls_default.json")); + ASSERT_EQ(1u, extension->web_extent().patterns().size()); + EXPECT_EQ("http://www.google.com/*", + extension->web_extent().patterns()[0].GetAsString()); } TEST_F(ManifestTest, AppLaunchContainer) { @@ -175,9 +174,8 @@ TEST_F(ManifestTest, AppLaunchURL) { EXPECT_EQ(extension->url().spec() + "launch.html", extension->GetFullLaunchURL().spec()); - extension.reset(LoadAndExpectSuccess("launch_web_url_relative.json")); - EXPECT_EQ(GURL("http://www.google.com/launch.html"), - extension->GetFullLaunchURL()); + LoadAndExpectError("launch_web_url_relative.json", + errors::kInvalidLaunchWebURL); extension.reset(LoadAndExpectSuccess("launch_web_url_absolute.json")); EXPECT_EQ(GURL("http://www.google.com/launch.html"), diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc index e129903..1aae528 100644 --- a/chrome/common/extensions/url_pattern.cc +++ b/chrome/common/extensions/url_pattern.cc @@ -4,6 +4,7 @@ #include "chrome/common/extensions/url_pattern.h" +#include "base/scoped_ptr.h" #include "base/string_piece.h" #include "base/string_util.h" #include "chrome/common/url_constants.h" @@ -21,6 +22,15 @@ static const char* kValidSchemes[] = { static const char kPathSeparator[] = "/"; // static +URLPattern* URLPattern::CreateFromString(const std::string& pattern_string) { + scoped_ptr<URLPattern> pattern(new URLPattern); + if (pattern->Parse(pattern_string)) + return pattern.release(); + else + return NULL; +} + +// static bool URLPattern::IsValidScheme(const std::string& scheme) { for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { if (scheme == kValidSchemes[i]) @@ -78,6 +88,7 @@ bool URLPattern::Parse(const std::string& pattern) { } path_ = pattern.substr(path_start_pos); + return true; } @@ -94,6 +105,14 @@ bool URLPattern::MatchesUrl(const GURL &test) const { return true; } +bool URLPattern::MatchesHost(const std::string& host) const { + std::string test(chrome::kHttpScheme); + test += chrome::kStandardSchemeSeparator; + test += host; + test += "/"; + return MatchesHost(GURL(test)); +} + bool URLPattern::MatchesHost(const GURL& test) const { // If the hosts are exactly equal, we have a match. if (test.host() == host_) diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h index dba393a..e25faaf 100644 --- a/chrome/common/extensions/url_pattern.h +++ b/chrome/common/extensions/url_pattern.h @@ -73,6 +73,9 @@ class URLPattern { // otherwise. static bool IsValidScheme(const std::string& scheme); + // Convenience to create a pattern from a string. + static URLPattern* CreateFromString(const std::string& pattern); + URLPattern() : match_subdomains_(false) {} // Initializes this instance by parsing the provided string. On failure, the @@ -82,6 +85,13 @@ class URLPattern { // Returns true if this instance matches the specified URL. bool MatchesUrl(const GURL& url) const; + // Returns true if |test| matches our host. + bool MatchesHost(const std::string& host) const; + bool MatchesHost(const GURL& test) const; + + // Returns true if |test| matches our path. + bool MatchesPath(const GURL& test) const; + std::string GetAsString() const; // Get the scheme the pattern matches. This will always return a valid scheme @@ -107,12 +117,6 @@ class URLPattern { } private: - // Returns true if |test| matches our host. - bool MatchesHost(const GURL& test) const; - - // Returns true if |test| matches our path. - bool MatchesPath(const GURL& test) const; - // The scheme for the pattern. std::string scheme_; diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 2b757e9..061a236 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -2683,25 +2683,21 @@ template <> struct ParamTraits<ExtensionExtent> { typedef ExtensionExtent param_type; static void Write(Message* m, const param_type& p) { - WriteParam(m, p.origin()); - WriteParam(m, p.paths()); + WriteParam(m, p.patterns()); } static bool Read(const Message* m, void** iter, param_type* p) { - GURL origin; - std::vector<std::string> paths; + std::vector<URLPattern> patterns; bool success = - ReadParam(m, iter, &origin) && - ReadParam(m, iter, &paths); + ReadParam(m, iter, &patterns); if (!success) return false; - p->set_origin(origin); - for (size_t i = 0; i < paths.size(); ++i) - p->add_path(paths[i]); + for (size_t i = 0; i < patterns.size(); ++i) + p->AddPattern(patterns[i]); return true; } static void Log(const param_type& p, std::wstring* l) { - LogParam(p.origin(), l); + LogParam(p.patterns(), l); } }; |