diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-20 01:05:35 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-20 01:05:35 +0000 |
commit | 665e57e0aa1502af457c40a7165d85ce2a6cc0a1 (patch) | |
tree | 6520d48453bc5994760f627c425a80664b62fd1e | |
parent | ae61568f48cfae61a33bdcfdc5c3b9402cdbd2be (diff) | |
download | chromium_src-665e57e0aa1502af457c40a7165d85ce2a6cc0a1.zip chromium_src-665e57e0aa1502af457c40a7165d85ce2a6cc0a1.tar.gz chromium_src-665e57e0aa1502af457c40a7165d85ce2a6cc0a1.tar.bz2 |
Make the extension install UI de-dupe hosts after disregarding scheme and path.
BUG=52723
Review URL: http://codereview.chromium.org/3126023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@56795 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/extension_install_ui.cc | 37 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_install_ui.h | 8 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_install_ui_unittest.cc | 72 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
4 files changed, 108 insertions, 10 deletions
diff --git a/chrome/browser/extensions/extension_install_ui.cc b/chrome/browser/extensions/extension_install_ui.cc index accb80e..741e58a 100644 --- a/chrome/browser/extensions/extension_install_ui.cc +++ b/chrome/browser/extensions/extension_install_ui.cc @@ -78,29 +78,30 @@ static void GetV2Warnings(Extension* extension, warnings->push_back( l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT2_WARNING_ALL_HOSTS)); } else { - std::vector<URLPattern> hosts = - extension->GetEffectiveHostPermissions().patterns(); + std::vector<std::string> hosts = + ExtensionInstallUI::GetDistinctHostsForDisplay( + extension->GetEffectiveHostPermissions().patterns()); if (hosts.size() == 1) { warnings->push_back( l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_1_HOST, - UTF8ToUTF16(hosts.begin()->host()))); + UTF8ToUTF16(hosts[0]))); } else if (hosts.size() == 2) { warnings->push_back( l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_2_HOSTS, - UTF8ToUTF16(hosts.begin()->host()), - UTF8ToUTF16((++hosts.begin())->host()))); + UTF8ToUTF16(hosts[0]), + UTF8ToUTF16(hosts[1]))); } else if (hosts.size() == 3) { warnings->push_back( l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT2_WARNING_3_HOSTS, - UTF8ToUTF16(hosts.begin()->host()), - UTF8ToUTF16((++hosts.begin())->host()), - UTF8ToUTF16((++++hosts.begin())->host()))); + UTF8ToUTF16(hosts[0]), + UTF8ToUTF16(hosts[1]), + UTF8ToUTF16(hosts[2]))); } else if (hosts.size() >= 4) { warnings->push_back( l10n_util::GetStringFUTF16( IDS_EXTENSION_PROMPT2_WARNING_4_OR_MORE_HOSTS, - UTF8ToUTF16(hosts.begin()->host()), - UTF8ToUTF16((++hosts.begin())->host()), + UTF8ToUTF16(hosts[0]), + UTF8ToUTF16(hosts[1]), base::IntToString16(hosts.size() - 2))); } } @@ -124,6 +125,22 @@ static void GetV2Warnings(Extension* extension, } // namespace +std::vector<std::string> ExtensionInstallUI::GetDistinctHostsForDisplay( + const std::vector<URLPattern>& host_patterns) { + // Vector because we later want to access these by index. + std::vector<std::string> distinct_hosts; + + for (size_t i = 0; i < host_patterns.size(); ++i) { + std::string candidate = host_patterns[i].host(); + if (std::find(distinct_hosts.begin(), distinct_hosts.end(), candidate) == + distinct_hosts.end()) { + distinct_hosts.push_back(candidate); + } + } + + return distinct_hosts; +} + ExtensionInstallUI::ExtensionInstallUI(Profile* profile) : profile_(profile), ui_loop_(MessageLoop::current()), diff --git a/chrome/browser/extensions/extension_install_ui.h b/chrome/browser/extensions/extension_install_ui.h index 8aafa96..c97878a 100644 --- a/chrome/browser/extensions/extension_install_ui.h +++ b/chrome/browser/extensions/extension_install_ui.h @@ -20,6 +20,7 @@ class MessageLoop; class Profile; class InfoBarDelegate; class TabContents; +class URLPattern; // Displays all the UI around extension installation and uninstallation. class ExtensionInstallUI : public ImageLoadingTracker::Observer { @@ -49,6 +50,13 @@ class ExtensionInstallUI : public ImageLoadingTracker::Observer { virtual ~Delegate() {} }; + // Returns the distinct hosts that should be displayed in the install UI. This + // discards some of the detail that is present in the manifest to make it as + // easy as possible to process by users. In particular we disregard the scheme + // and path components of URLPatterns and de-dupe the result. + static std::vector<std::string> GetDistinctHostsForDisplay( + const std::vector<URLPattern>& host_patterns); + explicit ExtensionInstallUI(Profile* profile); virtual ~ExtensionInstallUI(); diff --git a/chrome/browser/extensions/extension_install_ui_unittest.cc b/chrome/browser/extensions/extension_install_ui_unittest.cc new file mode 100644 index 0000000..4854b2a --- /dev/null +++ b/chrome/browser/extensions/extension_install_ui_unittest.cc @@ -0,0 +1,72 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/extension_install_ui.h" + +#include "chrome/common/extensions/url_pattern.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +void CompareLists(const std::vector<std::string>& expected, + const std::vector<std::string>& actual) { + ASSERT_EQ(expected.size(), actual.size()); + + for (size_t i = 0; i < expected.size(); ++i) { + EXPECT_EQ(expected[i], actual[i]); + } +} + +} + +TEST(ExtensionInstallUITest, GetDistinctHostsForDisplay) { + std::vector<std::string> expected; + expected.push_back("www.foo.com"); + expected.push_back("www.bar.com"); + expected.push_back("www.baz.com"); + + // Simple list with no dupes. + std::vector<URLPattern> actual; + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path")); + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); + CompareLists(expected, + ExtensionInstallUI::GetDistinctHostsForDisplay(actual)); + + // Add some dupes. + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path")); + CompareLists(expected, + ExtensionInstallUI::GetDistinctHostsForDisplay(actual)); + + + // Add a pattern that differs only by scheme. This should be filtered out. + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path")); + CompareLists(expected, + ExtensionInstallUI::GetDistinctHostsForDisplay(actual)); + + // Add some dupes by path. + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath")); + CompareLists(expected, + ExtensionInstallUI::GetDistinctHostsForDisplay(actual)); + + // We don't do anything special for subdomains. + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path")); + actual.push_back( + URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path")); + + expected.push_back("monkey.www.bar.com"); + expected.push_back("bar.com"); + + CompareLists(expected, + ExtensionInstallUI::GetDistinctHostsForDisplay(actual)); +} diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e68b000..67335f7 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1047,6 +1047,7 @@ 'browser/download/save_package_unittest.cc', 'browser/encoding_menu_controller_unittest.cc', 'browser/extensions/convert_user_script_unittest.cc', + 'browser/extensions/extension_install_ui_unittest.cc', 'browser/extensions/extension_menu_manager_unittest.cc', 'browser/extensions/extension_messages_unittest.cc', 'browser/extensions/extension_prefs_unittest.cc', |