diff options
author | melandory <melandory@chromium.org> | 2015-01-07 04:41:18 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-07 12:42:21 +0000 |
commit | 62cf737bd2efe4e27d9a5604c849bb4a61c5e723 (patch) | |
tree | 3b05e919e5a1e336d2602dd89ff5cf70a5bbbd11 /components | |
parent | 448922ef986eb3d99b724ad490b91e98c906b13d (diff) | |
download | chromium_src-62cf737bd2efe4e27d9a5604c849bb4a61c5e723.zip chromium_src-62cf737bd2efe4e27d9a5604c849bb4a61c5e723.tar.gz chromium_src-62cf737bd2efe4e27d9a5604c849bb4a61c5e723.tar.bz2 |
Scheme and android package name extraction routine for FacetURI.
First step in implementation of human readable names of Android
credentials in password manager.
BUG=437865
R=mkwst@chromium.org,engedy@chromium.org
Review URL: https://codereview.chromium.org/842623002
Cr-Commit-Position: refs/heads/master@{#310270}
Diffstat (limited to 'components')
3 files changed, 53 insertions, 18 deletions
diff --git a/components/password_manager/core/browser/affiliation_utils.cc b/components/password_manager/core/browser/affiliation_utils.cc index e03cec9..d34abd8 100644 --- a/components/password_manager/core/browser/affiliation_utils.cc +++ b/components/password_manager/core/browser/affiliation_utils.cc @@ -30,15 +30,6 @@ base::StringPiece ComponentString(const std::string& uri, return base::StringPiece(uri.c_str() + component.begin, component.len); } -// Extracts the scheme of an unparsed |uri| as a StringPiece, or returns the -// empty string on failure. -base::StringPiece ExtractScheme(const std::string& uri) { - url::Component scheme_component; - if (url::ExtractScheme(uri.c_str(), uri.size(), &scheme_component)) - return ComponentString(uri, scheme_component); - return base::StringPiece(); -} - // Returns true if the passed ASCII |input| string contains nothing else than // alphanumeric characters and those in |other_characters|. bool ContainsOnlyAlphanumericAnd(const base::StringPiece& input, @@ -236,15 +227,31 @@ bool FacetURI::operator>(const FacetURI& other) const { } bool FacetURI::IsValidWebFacetURI() const { - return is_valid_ && ExtractScheme(canonical_spec_) == url::kHttpsScheme; + return scheme() == url::kHttpsScheme; } bool FacetURI::IsValidAndroidFacetURI() const { - return is_valid_ && ExtractScheme(canonical_spec_) == kAndroidAppScheme; + return scheme() == kAndroidAppScheme; +} + +std::string FacetURI::scheme() const { + return is_valid() + ? ComponentString(canonical_spec_, parsed_.scheme).as_string() + : ""; +} + +std::string FacetURI::android_package_name() const { + if (!IsValidAndroidFacetURI()) + return ""; + return ComponentString(canonical_spec_, parsed_.host).as_string(); } FacetURI::FacetURI(const std::string& canonical_spec, bool is_valid) : is_valid_(is_valid), canonical_spec_(canonical_spec) { + // TODO(engedy): Refactor code in order to avoid to avoid parsing the URL + // twice. + url::ParseStandardURL(canonical_spec_.c_str(), canonical_spec_.size(), + &parsed_); } bool AreEquivalenceClassesEqual(const AffiliatedFacets& a, diff --git a/components/password_manager/core/browser/affiliation_utils.h b/components/password_manager/core/browser/affiliation_utils.h index 38ab24e..af153b9 100644 --- a/components/password_manager/core/browser/affiliation_utils.h +++ b/components/password_manager/core/browser/affiliation_utils.h @@ -48,6 +48,7 @@ #include <vector> #include "base/logging.h" +#include "url/url_parse.h" namespace password_manager { @@ -103,6 +104,15 @@ class FacetURI { return is_valid_; } + // Returns the canonical scheme of the encapsulated facet URI, provided it is + // valid, or the empty string otherwise. + std::string scheme() const; + + // Returns the canonical package name that the encapsulated facet URI + // references, provided it is a valid Android facet URI, or the empty string + // otherwise. + std::string android_package_name() const; + // Returns the text of the encapsulated canonical URI, which must be valid. const std::string& canonical_spec() const { DCHECK(is_valid_); @@ -119,6 +129,9 @@ class FacetURI { // The text of the encapsulated canonical URI, valid if and only if // |is_valid_| is true. std::string canonical_spec_; + + // Identified components of the canonical spec. + url::Parsed parsed_; }; // A collection of facets affiliated with each other, i.e. an equivalence class. diff --git a/components/password_manager/core/browser/affiliation_utils_unittest.cc b/components/password_manager/core/browser/affiliation_utils_unittest.cc index 3af672f..0f8c880 100644 --- a/components/password_manager/core/browser/affiliation_utils_unittest.cc +++ b/components/password_manager/core/browser/affiliation_utils_unittest.cc @@ -5,6 +5,7 @@ #include "components/password_manager/core/browser/affiliation_utils.h" #include "testing/gtest/include/gtest/gtest.h" +#include "url/url_constants.h" namespace password_manager { @@ -38,6 +39,8 @@ TEST(AffiliationUtilsTest, ValidWebFacetURIs) { ASSERT_TRUE(facet_uri.IsValidWebFacetURI()); EXPECT_EQ(std::string(test_case.expected_canonical_facet_uri), facet_uri.canonical_spec()); + EXPECT_EQ(url::kHttpsScheme, facet_uri.scheme()); + EXPECT_EQ("", facet_uri.android_package_name()); } } @@ -70,27 +73,35 @@ TEST(AffiliationUtilsTest, ValidAndroidFacetURIs) { struct { const char* valid_facet_uri; const char* expected_canonical_facet_uri; + const char* expected_package_name; } kTestCases[] = { {"android://" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" "@com.example.android", "android://" "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" - "@com.example.android"}, + "@com.example.android", + "com.example.android"}, {"ANDROID://" "hash@abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ", "android://" - "hash@abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, + "hash@abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ", + "abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, {"android://needpadding@com.example.android", - "android://needpadding=@com.example.android"}, + "android://needpadding=@com.example.android", + "com.example.android"}, {"android://needtounescape%3D%3D@com.%65xample.android", - "android://needtounescape==@com.example.android"}, + "android://needtounescape==@com.example.android", + "com.example.android"}, {"ANDROID://hash@com.example.android", - "android://hash@com.example.android"}, + "android://hash@com.example.android", + "com.example.android"}, {"android://hash@com.example.android/", - "android://hash@com.example.android"}, + "android://hash@com.example.android", + "com.example.android"}, {"android://hash:@com.example.android", - "android://hash@com.example.android"}}; + "android://hash@com.example.android", + "com.example.android"}}; for (const auto& test_case : kTestCases) { SCOPED_TRACE(testing::Message("URI = ") << test_case.valid_facet_uri); FacetURI facet_uri = @@ -98,6 +109,9 @@ TEST(AffiliationUtilsTest, ValidAndroidFacetURIs) { ASSERT_TRUE(facet_uri.IsValidAndroidFacetURI()); EXPECT_EQ(test_case.expected_canonical_facet_uri, facet_uri.canonical_spec()); + EXPECT_EQ("android", facet_uri.scheme()); + EXPECT_EQ(test_case.expected_package_name, + facet_uri.android_package_name()); } } @@ -135,6 +149,7 @@ TEST(AffiliationUtilsTest, InvalidAndroidFacetURIs) { SCOPED_TRACE(testing::Message("URI = ") << uri); FacetURI facet_uri = FacetURI::FromPotentiallyInvalidSpec(uri); EXPECT_FALSE(facet_uri.IsValidAndroidFacetURI()); + EXPECT_EQ("", facet_uri.android_package_name()); } } |