diff options
author | sdefresne <sdefresne@chromium.org> | 2015-08-11 03:46:35 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-11 10:47:18 +0000 |
commit | 70948d68e3925e415e29d74230c4c0b39fbf31a4 (patch) | |
tree | f8256193710c9b45488dcd8ef381c430890a8ba0 | |
parent | e5ec88b0b79ff67af77f33f3bda62f296a899fb5 (diff) | |
download | chromium_src-70948d68e3925e415e29d74230c4c0b39fbf31a4.zip chromium_src-70948d68e3925e415e29d74230c4c0b39fbf31a4.tar.gz chromium_src-70948d68e3925e415e29d74230c4c0b39fbf31a4.tar.bz2 |
Move ClipboardURLProvider into //components/omnibox.
Move ClipboardURLProvider with the other AutocompleteProvider into
//components/omnibox to simplify the construction of the provider
and break circular dependency of open_from_clipboard and omnibox
components.
Introduce a new AutocompleteProvider::Type for this new provider
(AutocompleteProvider::TYPE_CLIPBOARD_URL) with the corresponding
value in OmniboxEventProto enumeration.
Pass the AutocompleteProviderClient to the ClipboardURLProvider
and remove another layer of indirection for the construction of
the AutocompleteMatch.
Add AutocompleteProvider::TYPE_CLIPBOARD_URL to the default list
of providers and construct, if ClipboardRecentContent singleton
exists (currently only implemented on iOS), a ClipboardURLProvider.
Add unit tests for ClipboardURLProvider.
BUG=517134,517017,514225
Review URL: https://codereview.chromium.org/1273013002
Cr-Commit-Position: refs/heads/master@{#342806}
34 files changed, 402 insertions, 141 deletions
diff --git a/components/BUILD.gn b/components/BUILD.gn index 8806c7e..b7c12a2 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -68,6 +68,7 @@ group("all_components") { "//components/offline_pages", "//components/omnibox/browser", "//components/onc", + "//components/open_from_clipboard", "//components/os_crypt", "//components/packed_ct_ev_whitelist", "//components/pairing", @@ -309,9 +310,9 @@ test("components_unittests") { "//components/dom_distiller/core:unit_tests", "//components/domain_reliability:unit_tests", "//components/favicon_base:unit_tests", - "//components/gcm_driver:unit_tests", "//components/gcm_driver/crypto:unit_tests", "//components/gcm_driver/instance_id:unit_tests", + "//components/gcm_driver:unit_tests", "//components/google/core/browser:unit_tests", "//components/invalidation/impl:unittests", "//components/login:unit_tests", @@ -319,6 +320,7 @@ test("components_unittests") { "//components/mime_util:unit_tests", "//components/offline_pages:unit_tests", "//components/omnibox/browser:unit_tests", + "//components/open_from_clipboard:unit_tests", "//components/packed_ct_ev_whitelist:unit_tests", "//components/undo:unit_tests", "//components/url_formatter:unit_tests", @@ -326,8 +328,8 @@ test("components_unittests") { "//components/upload_list:unit_tests", "//components/user_prefs/tracked:unit_tests", "//components/variations:unit_tests", - "//components/webcrypto:unit_tests", "//components/web_resource:unit_tests", + "//components/webcrypto:unit_tests", "//components/webdata/common:unit_tests", # These are the deps required by the code in this target. diff --git a/components/OWNERS b/components/OWNERS index 25ee959..f49192a 100644 --- a/components/OWNERS +++ b/components/OWNERS @@ -178,7 +178,6 @@ per-file onc.gypi=pneubeck@chromium.org per-file onc.gypi=stevenjb@chromium.org per-file open_from_clipboard.gypi=jif@chromium.org -per-file open_from_clipboard_strings.grdp=jif@chromium.org per-file ownership.gypi=alemate@chromium.org per-file ownership.gypi=dpolukhin@chromium.org diff --git a/components/components.gyp b/components/components.gyp index e618f9c..246ebca 100644 --- a/components/components.gyp +++ b/components/components.gyp @@ -47,6 +47,7 @@ 'offline_pages.gypi', 'omnibox.gypi', 'onc.gypi', + 'open_from_clipboard.gypi', 'os_crypt.gypi', 'ownership.gypi', 'packed_ct_ev_whitelist.gypi', @@ -110,7 +111,6 @@ }], ['OS == "ios"', { 'includes': [ - 'open_from_clipboard.gypi', 'webp_transcode.gypi', ], }], diff --git a/components/components_strings.grd b/components/components_strings.grd index cba5f09..51a4cce 100644 --- a/components/components_strings.grd +++ b/components/components_strings.grd @@ -174,7 +174,6 @@ <part file="dom_distiller_strings.grdp" /> <part file="error_page_strings.grdp" /> <part file="omnibox_strings.grdp" /> - <part file="open_from_clipboard_strings.grdp" /> <part file="password_manager_strings.grdp" /> <part file="pdf_strings.grdp" /> <part file="policy_strings.grdp" /> diff --git a/components/components_tests.gyp b/components/components_tests.gyp index e04bad9..d5f78fe 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -353,12 +353,16 @@ 'omnibox/browser/autocomplete_match_unittest.cc', 'omnibox/browser/autocomplete_result_unittest.cc', 'omnibox/browser/base_search_provider_unittest.cc', + 'omnibox/browser/clipboard_url_provider_unittest.cc', 'omnibox/browser/in_memory_url_index_types_unittest.cc', 'omnibox/browser/keyword_provider_unittest.cc', 'omnibox/browser/omnibox_field_trial_unittest.cc', 'omnibox/browser/scored_history_match_unittest.cc', 'omnibox/browser/suggestion_answer_unittest.cc', ], + 'open_from_clipboard_unittest_sources': [ + 'open_from_clipboard/clipboard_recent_content_ios_unittest.mm', + ], 'os_crypt_unittest_sources': [ 'os_crypt/ie7_password_win_unittest.cc', 'os_crypt/keychain_password_mac_unittest.mm', @@ -745,8 +749,8 @@ '<@(enhanced_bookmarks_unittest_sources)', '<@(favicon_base_unittest_sources)', '<@(favicon_unittest_sources)', - '<@(gcm_driver_unittest_sources)', '<@(gcm_driver_crypto_unittest_sources)', + '<@(gcm_driver_unittest_sources)', '<@(google_unittest_sources)', '<@(history_unittest_sources)', '<@(instance_id_unittest_sources)', @@ -760,6 +764,7 @@ '<@(network_time_unittest_sources)', '<@(offline_page_unittest_sources)', '<@(omnibox_unittest_sources)', + '<@(open_from_clipboard_unittest_sources)', '<@(os_crypt_unittest_sources)', '<@(packed_ct_ev_whitelist_unittest_sources)', '<@(password_manager_unittest_sources)', @@ -767,9 +772,9 @@ '<@(proxy_config_unittest_sources)', '<@(query_parser_unittest_sources)', '<@(rappor_unittest_sources)', - '<@(search_unittest_sources)', '<@(search_engines_unittest_sources)', '<@(search_provider_logos_unittest_sources)', + '<@(search_unittest_sources)', '<@(sessions_unittest_sources)', '<@(signin_unittest_sources)', '<@(suggestions_unittest_sources)', @@ -868,6 +873,8 @@ 'components.gyp:offline_pages', 'components.gyp:omnibox_browser', 'components.gyp:omnibox_test_support', + 'components.gyp:open_from_clipboard', + 'components.gyp:open_from_clipboard_test_support', 'components.gyp:os_crypt', 'components.gyp:packed_ct_ev_whitelist', 'components.gyp:password_manager_core_browser', @@ -1035,7 +1042,6 @@ ], }, { # 'OS == "ios"' 'sources': [ - 'open_from_clipboard/clipboard_recent_content_ios_unittest.mm', 'webp_transcode/webp_decoder_unittest.mm', 'webp_transcode/webp_network_client_unittest.mm', ], @@ -1055,7 +1061,6 @@ '../ios/web/ios_web.gyp:test_support_ios_web', '../third_party/ocmock/ocmock.gyp:ocmock', 'components.gyp:autofill_ios_browser', - 'components.gyp:open_from_clipboard', 'components.gyp:sessions_ios', 'components.gyp:signin_ios_browser', 'components.gyp:signin_ios_browser_test_support', diff --git a/components/metrics/proto/omnibox_event.proto b/components/metrics/proto/omnibox_event.proto index bf13d4c..229b697 100644 --- a/components/metrics/proto/omnibox_event.proto +++ b/components/metrics/proto/omnibox_event.proto @@ -167,6 +167,7 @@ message OmniboxEventProto { // This enum value is currently only used by Android GSA. It represents // a suggestion powered by a Chrome content provider. ON_DEVICE_CHROME = 13; + CLIPBOARD_URL = 14; // Suggestion coming from clipboard (iOS only). } // The result set displayed on the completion popup diff --git a/components/omnibox.gypi b/components/omnibox.gypi index 9133592..572e9e4 100644 --- a/components/omnibox.gypi +++ b/components/omnibox.gypi @@ -26,6 +26,7 @@ 'keyed_service_core', 'omnibox_common', 'omnibox_in_memory_url_index_cache_proto', + 'open_from_clipboard', 'pref_registry', 'query_parser', 'search', @@ -69,6 +70,8 @@ 'omnibox/browser/bookmark_provider.h', 'omnibox/browser/builtin_provider.cc', 'omnibox/browser/builtin_provider.h', + 'omnibox/browser/clipboard_url_provider.cc', + 'omnibox/browser/clipboard_url_provider.h', 'omnibox/browser/history_provider.cc', 'omnibox/browser/history_provider.h', 'omnibox/browser/history_quick_provider.cc', @@ -90,10 +93,10 @@ 'omnibox/browser/omnibox_edit_controller.h', 'omnibox/browser/omnibox_edit_model.cc', 'omnibox/browser/omnibox_edit_model.h', - 'omnibox/browser/omnibox_field_trial.cc', - 'omnibox/browser/omnibox_field_trial.h', 'omnibox/browser/omnibox_event_global_tracker.cc', 'omnibox/browser/omnibox_event_global_tracker.h', + 'omnibox/browser/omnibox_field_trial.cc', + 'omnibox/browser/omnibox_field_trial.h', 'omnibox/browser/omnibox_log.cc', 'omnibox/browser/omnibox_log.h', 'omnibox/browser/omnibox_navigation_observer.h', @@ -127,6 +130,8 @@ 'omnibox/browser/url_index_private_data.h', 'omnibox/browser/url_prefix.cc', 'omnibox/browser/url_prefix.h', + 'omnibox/browser/verbatim_match.cc', + 'omnibox/browser/verbatim_match.h', 'omnibox/browser/zero_suggest_provider.cc', 'omnibox/browser/zero_suggest_provider.h', ], diff --git a/components/omnibox/OWNERS b/components/omnibox/OWNERS index 827c722..a659a21 100644 --- a/components/omnibox/OWNERS +++ b/components/omnibox/OWNERS @@ -1,2 +1,4 @@ pkasting@chromium.org mpearson@chromium.org + +per-file clipboard_url_provider.*=jif@chromium.org diff --git a/components/omnibox/browser/BUILD.gn b/components/omnibox/browser/BUILD.gn index 709fbdf..5cc7167 100644 --- a/components/omnibox/browser/BUILD.gn +++ b/components/omnibox/browser/BUILD.gn @@ -30,6 +30,8 @@ source_set("browser") { "bookmark_provider.h", "builtin_provider.cc", "builtin_provider.h", + "clipboard_url_provider.cc", + "clipboard_url_provider.h", "history_provider.cc", "history_provider.h", "history_quick_provider.cc", @@ -88,6 +90,8 @@ source_set("browser") { "url_index_private_data.h", "url_prefix.cc", "url_prefix.h", + "verbatim_match.cc", + "verbatim_match.h", "zero_suggest_provider.cc", "zero_suggest_provider.h", ] @@ -97,13 +101,13 @@ source_set("browser") { "//components/metrics/proto", ] deps = [ - ":in_memory_url_index_cache_proto", "//base", "//base:i18n", "//base:prefs", "//components/bookmarks/browser", "//components/keyed_service/core", "//components/omnibox/common", + "//components/open_from_clipboard", "//components/pref_registry", "//components/query_parser", "//components/resources", @@ -122,6 +126,7 @@ source_set("browser") { "//ui/base", "//ui/gfx", "//url", + ":in_memory_url_index_cache_proto", ] } @@ -158,6 +163,7 @@ source_set("unit_tests") { "autocomplete_match_unittest.cc", "autocomplete_result_unittest.cc", "base_search_provider_unittest.cc", + "clipboard_url_provider_unittest.cc", "in_memory_url_index_types_unittest.cc", "keyword_provider_unittest.cc", "omnibox_field_trial_unittest.cc", @@ -166,14 +172,15 @@ source_set("unit_tests") { ] deps = [ - ":browser", - ":test_support", "//base", + "//components/open_from_clipboard:test_support", "//components/search", "//components/search_engines", "//components/variations", "//testing/gmock", "//testing/gtest", "//url", + ":browser", + ":test_support", ] } diff --git a/components/omnibox/browser/DEPS b/components/omnibox/browser/DEPS index 7c81237..480cab6 100644 --- a/components/omnibox/browser/DEPS +++ b/components/omnibox/browser/DEPS @@ -3,6 +3,7 @@ include_rules = [ "+components/history/core/browser", "+components/keyed_service/core", "+components/metrics/proto", + "+components/open_from_clipboard", "+components/pref_registry", "+components/query_parser", "+components/search", diff --git a/components/omnibox/browser/autocomplete_classifier.cc b/components/omnibox/browser/autocomplete_classifier.cc index f1a1366..26e349a6 100644 --- a/components/omnibox/browser/autocomplete_classifier.cc +++ b/components/omnibox/browser/autocomplete_classifier.cc @@ -24,6 +24,9 @@ const int AutocompleteClassifier::kDefaultOmniboxProviders = AutocompleteProvider::TYPE_BUILTIN | AutocompleteProvider::TYPE_SHORTCUTS | AutocompleteProvider::TYPE_ZERO_SUGGEST | +#else + // "URL from clipboard" can only be used on iOS. + AutocompleteProvider::TYPE_CLIPBOARD_URL | #endif AutocompleteProvider::TYPE_BOOKMARK | AutocompleteProvider::TYPE_HISTORY_QUICK | diff --git a/components/omnibox/browser/autocomplete_controller.cc b/components/omnibox/browser/autocomplete_controller.cc index 37665cb..b25d7f5 100644 --- a/components/omnibox/browser/autocomplete_controller.cc +++ b/components/omnibox/browser/autocomplete_controller.cc @@ -13,9 +13,11 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/time/time.h" +#include "build/build_config.h" #include "components/omnibox/browser/autocomplete_controller_delegate.h" #include "components/omnibox/browser/bookmark_provider.h" #include "components/omnibox/browser/builtin_provider.h" +#include "components/omnibox/browser/clipboard_url_provider.h" #include "components/omnibox/browser/history_quick_provider.h" #include "components/omnibox/browser/history_url_provider.h" #include "components/omnibox/browser/keyword_provider.h" @@ -23,6 +25,7 @@ #include "components/omnibox/browser/search_provider.h" #include "components/omnibox/browser/shortcuts_provider.h" #include "components/omnibox/browser/zero_suggest_provider.h" +#include "components/open_from_clipboard/clipboard_recent_content.h" #include "components/search_engines/template_url.h" #include "components/search_engines/template_url_service.h" #include "grit/components_strings.h" @@ -202,6 +205,14 @@ AutocompleteController::AutocompleteController( if (zero_suggest_provider_) providers_.push_back(zero_suggest_provider_); } + if (provider_types & AutocompleteProvider::TYPE_CLIPBOARD_URL) { + ClipboardRecentContent* clipboard_recent_content = + ClipboardRecentContent::GetInstance(); + if (clipboard_recent_content) { + providers_.push_back(new ClipboardURLProvider(provider_client_.get(), + clipboard_recent_content)); + } + } } AutocompleteController::~AutocompleteController() { diff --git a/components/omnibox/browser/autocomplete_provider.cc b/components/omnibox/browser/autocomplete_provider.cc index 5072af2..a592ea3 100644 --- a/components/omnibox/browser/autocomplete_provider.cc +++ b/components/omnibox/browser/autocomplete_provider.cc @@ -39,6 +39,8 @@ const char* AutocompleteProvider::TypeToString(Type type) { return "Shortcuts"; case TYPE_ZERO_SUGGEST: return "ZeroSuggest"; + case TYPE_CLIPBOARD_URL: + return "ClipboardURL"; default: NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type; return "Unknown"; @@ -73,6 +75,8 @@ metrics::OmniboxEventProto_ProviderType AutocompleteProvider:: return metrics::OmniboxEventProto::SHORTCUTS; case TYPE_ZERO_SUGGEST: return metrics::OmniboxEventProto::ZERO_SUGGEST; + case TYPE_CLIPBOARD_URL: + return metrics::OmniboxEventProto::CLIPBOARD_URL; default: NOTREACHED() << "Unhandled AutocompleteProvider::Type " << type_; return metrics::OmniboxEventProto::UNKNOWN_PROVIDER; diff --git a/components/omnibox/browser/autocomplete_provider.h b/components/omnibox/browser/autocomplete_provider.h index 2046a32..05aa666 100644 --- a/components/omnibox/browser/autocomplete_provider.h +++ b/components/omnibox/browser/autocomplete_provider.h @@ -133,6 +133,7 @@ class AutocompleteProvider TYPE_SEARCH = 1 << 5, TYPE_SHORTCUTS = 1 << 6, TYPE_ZERO_SUGGEST = 1 << 7, + TYPE_CLIPBOARD_URL = 1 << 8, }; explicit AutocompleteProvider(Type type); diff --git a/components/omnibox/browser/clipboard_url_provider.cc b/components/omnibox/browser/clipboard_url_provider.cc new file mode 100644 index 0000000..8fd7612 --- /dev/null +++ b/components/omnibox/browser/clipboard_url_provider.cc @@ -0,0 +1,66 @@ +// Copyright 2015 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 "components/omnibox/browser/clipboard_url_provider.h" + +#include "base/logging.h" +#include "base/strings/utf_string_conversions.h" +#include "components/omnibox/browser/autocomplete_input.h" +#include "components/omnibox/browser/autocomplete_provider_client.h" +#include "components/omnibox/browser/verbatim_match.h" +#include "components/open_from_clipboard/clipboard_recent_content.h" +#include "components/url_formatter/url_formatter.h" +#include "grit/components_strings.h" +#include "ui/base/l10n/l10n_util.h" + +ClipboardURLProvider::ClipboardURLProvider( + AutocompleteProviderClient* client, + ClipboardRecentContent* clipboard_content) + : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL), + client_(client), + clipboard_content_(clipboard_content) { + DCHECK(clipboard_content_); +} + +ClipboardURLProvider::~ClipboardURLProvider() {} + +void ClipboardURLProvider::Start(const AutocompleteInput& input, + bool minimal_changes) { + matches_.clear(); + if (!input.from_omnibox_focus()) + return; + + GURL url; + if (!clipboard_content_->GetRecentURLFromClipboard(&url) || + url == input.current_url()) + return; + + DCHECK(url.is_valid()); + // Adds a default match. This match will be opened when the user presses "Go". + AutocompleteMatch verbatim_match = VerbatimMatchForURL( + client_, input.text(), input.current_page_classification(), 0); + if (verbatim_match.destination_url.is_valid()) + matches_.push_back(verbatim_match); + + // Add a clipboard match just below the verbatim match. + AutocompleteMatch match(this, verbatim_match.relevance - 1, false, + AutocompleteMatchType::NAVSUGGEST); + match.destination_url = url; + match.contents.assign(url_formatter::FormatUrl( + url, client_->GetAcceptLanguages(), url_formatter::kFormatUrlOmitAll, + net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); + AutocompleteMatch::ClassifyLocationInString( + base::string16::npos, 0, match.contents.length(), + ACMatchClassification::URL, &match.contents_class); + + match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); + AutocompleteMatch::ClassifyLocationInString( + base::string16::npos, 0, match.description.length(), + ACMatchClassification::NONE, &match.description_class); + + // At least one match must be default, so if verbatim_match was invalid, + // the clipboard match is allowed to be default. + match.allowed_to_be_default_match = matches_.empty(); + matches_.push_back(match); +} diff --git a/components/open_from_clipboard/clipboard_url_provider.h b/components/omnibox/browser/clipboard_url_provider.h index af69ae5..583711c 100644 --- a/components/open_from_clipboard/clipboard_url_provider.h +++ b/components/omnibox/browser/clipboard_url_provider.h @@ -2,23 +2,20 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_URL_PROVIDER_H_ -#define COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_URL_PROVIDER_H_ +#ifndef COMPONENTS_OMNIBOX_BROWSER_CLIPBOARD_URL_PROVIDER_H_ +#define COMPONENTS_OMNIBOX_BROWSER_CLIPBOARD_URL_PROVIDER_H_ -#include "base/callback.h" +#include "base/macros.h" #include "components/omnibox/browser/autocomplete_provider.h" +class AutocompleteProviderClient; class ClipboardRecentContent; // Autocomplete provider offering content based on the clipboard's content. class ClipboardURLProvider : public AutocompleteProvider { public: - typedef base::Callback<AutocompleteMatch(const AutocompleteInput&)> - PlaceholderRequestCallback; - - ClipboardURLProvider( - ClipboardRecentContent* clipboard_recent_content, - const PlaceholderRequestCallback& placeholder_match_getter); + ClipboardURLProvider(AutocompleteProviderClient* client, + ClipboardRecentContent* clipboard_content); // AutocompleteProvider implementation. void Start(const AutocompleteInput& input, bool minimal_changes) override; @@ -26,10 +23,10 @@ class ClipboardURLProvider : public AutocompleteProvider { private: ~ClipboardURLProvider() override; - ClipboardRecentContent* clipboard_recent_content_; - PlaceholderRequestCallback placeholder_match_getter_; + AutocompleteProviderClient* client_; + ClipboardRecentContent* clipboard_content_; DISALLOW_COPY_AND_ASSIGN(ClipboardURLProvider); }; -#endif // COMPONENTS_OPEN_FROM_CLIPBOARD_CLIPBOARD_URL_PROVIDER_H_ +#endif // COMPONENTS_OMNIBOX_BROWSER_CLIPBOARD_URL_PROVIDER_H_ diff --git a/components/omnibox/browser/clipboard_url_provider_unittest.cc b/components/omnibox/browser/clipboard_url_provider_unittest.cc new file mode 100644 index 0000000..eea9882 --- /dev/null +++ b/components/omnibox/browser/clipboard_url_provider_unittest.cc @@ -0,0 +1,80 @@ +// Copyright 2015 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 "components/omnibox/browser/clipboard_url_provider.h" + +#include <string> + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "components/omnibox/browser/autocomplete_input.h" +#include "components/omnibox/browser/mock_autocomplete_provider_client.h" +#include "components/omnibox/browser/test_scheme_classifier.h" +#include "components/open_from_clipboard/fake_clipboard_recent_content.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "url/gurl.h" + +namespace { + +const char kCurrentURL[] = "http://example.com/current"; +const char kClipboardURL[] = "http://example.com/clipboard"; + +class ClipboardURLProviderTest : public testing::Test { + public: + ClipboardURLProviderTest() + : client_(new testing::NiceMock<MockAutocompleteProviderClient>()), + provider_( + new ClipboardURLProvider(client_.get(), &clipboard_content_)) { + SetClipboardUrl(GURL(kClipboardURL)); + } + + ~ClipboardURLProviderTest() override {} + + void ClearClipboard() { clipboard_content_.SuppressClipboardContent(); } + + void SetClipboardUrl(const GURL& url) { + clipboard_content_.SetClipboardContent(url, + base::TimeDelta::FromMinutes(10)); + } + + AutocompleteInput CreateAutocompleteInput(bool from_omnibox_focus) { + return AutocompleteInput( + base::string16(), base::string16::npos, std::string(), + GURL(kCurrentURL), metrics::OmniboxEventProto::INVALID_SPEC, false, + false, false, false, from_omnibox_focus, classifier_); + } + + protected: + TestSchemeClassifier classifier_; + FakeClipboardRecentContent clipboard_content_; + scoped_ptr<testing::NiceMock<MockAutocompleteProviderClient>> client_; + scoped_refptr<ClipboardURLProvider> provider_; +}; + +TEST_F(ClipboardURLProviderTest, NotFromOmniboxFocus) { + provider_->Start(CreateAutocompleteInput(false), false); + EXPECT_TRUE(provider_->matches().empty()); +} + +TEST_F(ClipboardURLProviderTest, EmptyClipboard) { + ClearClipboard(); + provider_->Start(CreateAutocompleteInput(true), false); + EXPECT_TRUE(provider_->matches().empty()); +} + +TEST_F(ClipboardURLProviderTest, ClipboardIsCurrentURL) { + SetClipboardUrl(GURL(kCurrentURL)); + provider_->Start(CreateAutocompleteInput(true), false); + EXPECT_TRUE(provider_->matches().empty()); +} + +TEST_F(ClipboardURLProviderTest, HasMultipleMatches) { + provider_->Start(CreateAutocompleteInput(true), false); + ASSERT_GE(provider_->matches().size(), 1U); + EXPECT_EQ(GURL(kClipboardURL), provider_->matches().back().destination_url); +} + +} // namespace diff --git a/components/omnibox/browser/verbatim_match.cc b/components/omnibox/browser/verbatim_match.cc new file mode 100644 index 0000000..c478d23 --- /dev/null +++ b/components/omnibox/browser/verbatim_match.cc @@ -0,0 +1,24 @@ +// Copyright 2015 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 "components/omnibox/browser/verbatim_match.h" + +#include "components/omnibox/browser/autocomplete_classifier.h" +#include "components/omnibox/browser/autocomplete_provider_client.h" + +AutocompleteMatch VerbatimMatchForURL( + AutocompleteProviderClient* client, + const base::string16& input_text, + metrics::OmniboxEventProto::PageClassification classification, + int verbatim_relevance) { + AutocompleteMatch match; + client->Classify(input_text, false, true, classification, &match, nullptr); + match.allowed_to_be_default_match = true; + // The default relevance to use for relevance match. Should be greater than + // all relevance matches returned by the ZeroSuggest server. + const int kDefaultVerbatimRelevance = 1300; + match.relevance = + verbatim_relevance >= 0 ? verbatim_relevance : kDefaultVerbatimRelevance; + return match; +} diff --git a/components/omnibox/browser/verbatim_match.h b/components/omnibox/browser/verbatim_match.h new file mode 100644 index 0000000..bf630d7 --- /dev/null +++ b/components/omnibox/browser/verbatim_match.h @@ -0,0 +1,23 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_OMNIBOX_BROWSER_VERBATIM_MATCH_H_ +#define COMPONENTS_OMNIBOX_BROWSER_VERBATIM_MATCH_H_ + +#include "base/strings/string16.h" +#include "components/metrics/proto/omnibox_event.pb.h" + +struct AutocompleteMatch; +class AutocompleteProviderClient; + +// Returns a verbatim match for |input_text| with |classification| and a +// relevance of |verbatim_relevance|. If |verbatim_relevance| is negative +// or null, then a default value is used. +AutocompleteMatch VerbatimMatchForURL( + AutocompleteProviderClient* client, + const base::string16& input_text, + metrics::OmniboxEventProto::PageClassification classification, + int verbatim_relevance); + +#endif // COMPONENTS_OMNIBOX_BROWSER_VERBATIM_MATCH_H_ diff --git a/components/omnibox/browser/zero_suggest_provider.cc b/components/omnibox/browser/zero_suggest_provider.cc index 17a1bc9..b93b0ab 100644 --- a/components/omnibox/browser/zero_suggest_provider.cc +++ b/components/omnibox/browser/zero_suggest_provider.cc @@ -25,6 +25,7 @@ #include "components/omnibox/browser/omnibox_field_trial.h" #include "components/omnibox/browser/omnibox_pref_names.h" #include "components/omnibox/browser/search_provider.h" +#include "components/omnibox/browser/verbatim_match.h" #include "components/pref_registry/pref_registry_syncable.h" #include "components/search_engines/template_url_service.h" #include "components/url_formatter/url_formatter.h" @@ -60,9 +61,6 @@ void LogOmniboxZeroSuggestRequest( ZERO_SUGGEST_MAX_REQUEST_HISTOGRAM_VALUE); } -// The maximum relevance of the top match from this provider. -const int kDefaultVerbatimZeroSuggestRelevance = 1300; - // Relevance value to use if it was not set explicitly by the server. const int kDefaultZeroSuggestRelevance = 100; @@ -410,22 +408,12 @@ void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { } AutocompleteMatch ZeroSuggestProvider::MatchForCurrentURL() { - AutocompleteMatch match; - client()->GetAutocompleteClassifier()->Classify( - permanent_text_, false, true, current_page_classification_, &match, NULL); - match.allowed_to_be_default_match = true; - // The placeholder suggestion for the current URL has high relevance so // that it is in the first suggestion slot and inline autocompleted. It // gets dropped as soon as the user types something. - match.relevance = GetVerbatimRelevance(); - - return match; -} - -int ZeroSuggestProvider::GetVerbatimRelevance() const { - return results_.verbatim_relevance >= 0 ? - results_.verbatim_relevance : kDefaultVerbatimZeroSuggestRelevance; + return VerbatimMatchForURL(client(), permanent_text_, + current_page_classification_, + results_.verbatim_relevance); } bool ZeroSuggestProvider::ShouldShowNonContextualZeroSuggest( diff --git a/components/omnibox/browser/zero_suggest_provider.h b/components/omnibox/browser/zero_suggest_provider.h index 107b11f..904aa6d 100644 --- a/components/omnibox/browser/zero_suggest_provider.h +++ b/components/omnibox/browser/zero_suggest_provider.h @@ -113,9 +113,6 @@ class ZeroSuggestProvider : public BaseSearchProvider, // function to return those |urls|. void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls); - // Returns the relevance score for the verbatim result. - int GetVerbatimRelevance() const; - // Whether we can show zero suggest without sending |current_page_url| to // |suggest_url| search provider. Also checks that other conditions for // non-contextual zero suggest are satisfied. diff --git a/components/omnibox_strings.grdp b/components/omnibox_strings.grdp index aaa40eb..93b3eb9 100644 --- a/components/omnibox_strings.grdp +++ b/components/omnibox_strings.grdp @@ -13,5 +13,7 @@ <message name="IDS_EMPTY_KEYWORD_VALUE" desc="Shown in the location bar drop down when the user enters a string that matches a chrome keyword, but they haven't entered any text following the chrome keyword"> <enter query> </message> - + <message name="IDS_LINK_FROM_CLIPBOARD" desc="The label in the omnibox dropdown explaining that the link has been extracted from the user's clipboard. [Length: 21em]"> + Link you copied + </message> </grit-part> diff --git a/components/open_from_clipboard.gypi b/components/open_from_clipboard.gypi index 9103712..d990147 100644 --- a/components/open_from_clipboard.gypi +++ b/components/open_from_clipboard.gypi @@ -5,24 +5,38 @@ { 'targets': [ { + # GN version: //components/open_from_clipboard 'target_name': 'open_from_clipboard', 'type': 'static_library', 'dependencies': [ '../base/base.gyp:base', '../url/url.gyp:url_lib', - 'components_strings.gyp:components_strings', - 'omnibox_browser', ], 'include_dirs': [ '..', ], 'sources': [ + # Note: sources list duplicated in GN build. 'open_from_clipboard/clipboard_recent_content.cc', 'open_from_clipboard/clipboard_recent_content.h', 'open_from_clipboard/clipboard_recent_content_ios.h', 'open_from_clipboard/clipboard_recent_content_ios.mm', - 'open_from_clipboard/clipboard_url_provider.cc', - 'open_from_clipboard/clipboard_url_provider.h', + ], + }, + { + # GN version: //components/open_from_clipboard:test_support + 'target_name': 'open_from_clipboard_test_support', + 'type': 'static_library', + 'dependencies': [ + 'open_from_clipboard', + ], + 'include_dirs': [ + '..', + ], + 'sources': [ + # Note: sources list duplicated in GN build. + 'open_from_clipboard/fake_clipboard_recent_content.cc', + 'open_from_clipboard/fake_clipboard_recent_content.h', ], }, ], diff --git a/components/open_from_clipboard/BUILD.gn b/components/open_from_clipboard/BUILD.gn new file mode 100644 index 0000000..53db21a --- /dev/null +++ b/components/open_from_clipboard/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright 2015 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. + +source_set("open_from_clipboard") { + sources = [ + "clipboard_recent_content.cc", + "clipboard_recent_content.h", + "clipboard_recent_content_ios.h", + "clipboard_recent_content_ios.mm", + ] + + deps = [ + "//base", + "//url", + ] +} + +source_set("test_support") { + testonly = true + sources = [ + "fake_clipboard_recent_content.cc", + "fake_clipboard_recent_content.h", + ] + + deps = [ + ":open_from_clipboard", + ] +} + +source_set("unit_tests") { + testonly = true + sources = [ + "clipboard_recent_content_ios_unittest.mm", + ] + + deps = [ + ":open_from_clipboard", + "//base", + "//testing/gtest", + ] +} diff --git a/components/open_from_clipboard/DEPS b/components/open_from_clipboard/DEPS deleted file mode 100644 index 30a8913..0000000 --- a/components/open_from_clipboard/DEPS +++ /dev/null @@ -1,5 +0,0 @@ -include_rules = [ - "+components/omnibox", - "+grit/components_strings.h", - "+ui/base/l10n", -] diff --git a/components/open_from_clipboard/clipboard_recent_content.cc b/components/open_from_clipboard/clipboard_recent_content.cc index 5af65cd..56312ec 100644 --- a/components/open_from_clipboard/clipboard_recent_content.cc +++ b/components/open_from_clipboard/clipboard_recent_content.cc @@ -4,8 +4,6 @@ #include "components/open_from_clipboard/clipboard_recent_content.h" -#include "base/logging.h" - namespace { ClipboardRecentContent* g_clipboard_recent_content = nullptr; } @@ -16,7 +14,6 @@ ClipboardRecentContent::~ClipboardRecentContent() {} // static ClipboardRecentContent* ClipboardRecentContent::GetInstance() { - DCHECK(g_clipboard_recent_content); return g_clipboard_recent_content; } diff --git a/components/open_from_clipboard/clipboard_recent_content.h b/components/open_from_clipboard/clipboard_recent_content.h index e5bef75..c8f2499 100644 --- a/components/open_from_clipboard/clipboard_recent_content.h +++ b/components/open_from_clipboard/clipboard_recent_content.h @@ -21,8 +21,8 @@ class ClipboardRecentContent { virtual ~ClipboardRecentContent(); // Returns the global instance of the ClipboardRecentContent singleton. This - // method does *not* create the singleton, and an instance must have been set - // via a call to SetInstance() before. + // method does *not* create the singleton and will return null if no instance + // was registered via SetInstance(). static ClipboardRecentContent* GetInstance(); // Sets the global instance of ClipboardRecentContent singleton. diff --git a/components/open_from_clipboard/clipboard_url_provider.cc b/components/open_from_clipboard/clipboard_url_provider.cc deleted file mode 100644 index 3009350..0000000 --- a/components/open_from_clipboard/clipboard_url_provider.cc +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2015 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 "components/open_from_clipboard/clipboard_url_provider.h" - -#include "base/logging.h" -#include "base/strings/utf_string_conversions.h" -#include "components/omnibox/browser/autocomplete_input.h" -#include "components/open_from_clipboard/clipboard_recent_content.h" -#include "grit/components_strings.h" -#include "ui/base/l10n/l10n_util.h" - -namespace { -// Score defining the priority of the suggestion. 1600 guarantees that the -// suggestion will always be first. See table in autocomplete_provider.h. -const int kClipboardURLProviderRelevance = 1600; -} // namespace - -ClipboardURLProvider::ClipboardURLProvider( - ClipboardRecentContent* clipboard_recent_content, - const PlaceholderRequestCallback& placeholder_match_getter) - : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS), - clipboard_recent_content_(clipboard_recent_content), - placeholder_match_getter_(placeholder_match_getter) { - DCHECK(clipboard_recent_content_); -} - -ClipboardURLProvider::~ClipboardURLProvider() { -} - -void ClipboardURLProvider::Start(const AutocompleteInput& input, - bool minimal_changes) { - matches_.clear(); - if (!input.from_omnibox_focus()) - return; - // Attempt to add an AutocompleteMatch only if the user has not entered - // anything in the omnibox. - if (input.cursor_position() == base::string16::npos) { - GURL url; - // Add the suggestion only if the user is not already on the page stored - // in the clipboard. - if (clipboard_recent_content_->GetRecentURLFromClipboard(&url) && - url != input.current_url()) { - DCHECK(url.is_valid()); - AutocompleteMatch match(this, kClipboardURLProviderRelevance, false, - AutocompleteMatchType::NAVSUGGEST_PERSONALIZED); - match.allowed_to_be_default_match = true; - match.destination_url = url; - match.contents.assign(base::UTF8ToUTF16(url.spec())); - AutocompleteMatch::ClassifyLocationInString( - base::string16::npos, 0, match.contents.length(), - ACMatchClassification::URL, &match.contents_class); - - match.description.assign( - l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); - AutocompleteMatch::ClassifyLocationInString( - base::string16::npos, 0, match.description.length(), - ACMatchClassification::URL, &match.description_class); - // Adds a default match. This match will be opened when the user presses - // "Go". - AutocompleteMatch current_url_match(placeholder_match_getter_.Run(input)); - if (current_url_match.destination_url.is_valid()) { - // Makes the placeholder match be above the clipboard match. - current_url_match.relevance = kClipboardURLProviderRelevance + 1; - matches_.push_back(current_url_match); - } - - matches_.push_back(match); - } - } - done_ = true; -} diff --git a/components/open_from_clipboard/fake_clipboard_recent_content.cc b/components/open_from_clipboard/fake_clipboard_recent_content.cc new file mode 100644 index 0000000..a8e6825 --- /dev/null +++ b/components/open_from_clipboard/fake_clipboard_recent_content.cc @@ -0,0 +1,37 @@ +// Copyright 2015 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 "components/open_from_clipboard/fake_clipboard_recent_content.h" + +FakeClipboardRecentContent::FakeClipboardRecentContent() + : content_age_(base::TimeDelta::Max()), suppress_content_(false) {} + +FakeClipboardRecentContent::~FakeClipboardRecentContent() {} + +bool FakeClipboardRecentContent::GetRecentURLFromClipboard(GURL* url) const { + if (suppress_content_) + return false; + + if (!clipboard_content_.is_valid()) + return false; + + *url = clipboard_content_; + return true; +} + +base::TimeDelta FakeClipboardRecentContent::GetClipboardContentAge() const { + return content_age_; +} + +void FakeClipboardRecentContent::SuppressClipboardContent() { + suppress_content_ = true; +} + +void FakeClipboardRecentContent::SetClipboardContent( + const GURL& url, + base::TimeDelta content_age) { + clipboard_content_ = url; + content_age_ = content_age; + suppress_content_ = false; +} diff --git a/components/open_from_clipboard/fake_clipboard_recent_content.h b/components/open_from_clipboard/fake_clipboard_recent_content.h new file mode 100644 index 0000000..e567bc6 --- /dev/null +++ b/components/open_from_clipboard/fake_clipboard_recent_content.h @@ -0,0 +1,36 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_OPEN_FROM_CLIPBOARD_FAKE_CLIPBOARD_RECENT_CONTENT_H_ +#define COMPONENTS_OPEN_FROM_CLIPBOARD_FAKE_CLIPBOARD_RECENT_CONTENT_H_ + +#include "base/macros.h" +#include "base/time/time.h" +#include "components/open_from_clipboard/clipboard_recent_content.h" +#include "url/gurl.h" + +// FakeClipboardRecentContent implements ClipboardRecentContent interface by +// returning configurable values for use by tests. +class FakeClipboardRecentContent : public ClipboardRecentContent { + public: + FakeClipboardRecentContent(); + ~FakeClipboardRecentContent() override; + + // ClipboardRecentContent implementation. + bool GetRecentURLFromClipboard(GURL* url) const override; + base::TimeDelta GetClipboardContentAge() const override; + void SuppressClipboardContent() override; + + // Sets the URL and clipboard content age. + void SetClipboardContent(const GURL& url, base::TimeDelta content_age); + + private: + GURL clipboard_content_; + base::TimeDelta content_age_; + bool suppress_content_; + + DISALLOW_COPY_AND_ASSIGN(FakeClipboardRecentContent); +}; + +#endif // COMPONENTS_OPEN_FROM_CLIPBOARD_FAKE_CLIPBOARD_RECENT_CONTENT_H_ diff --git a/components/open_from_clipboard_strings.grdp b/components/open_from_clipboard_strings.grdp deleted file mode 100644 index c6ec2b5..0000000 --- a/components/open_from_clipboard_strings.grdp +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<grit-part> - <message name="IDS_LINK_FROM_CLIPBOARD" desc="The label in the omnibox dropdown explaining that the link has been extracted from the user's clipboard. [Length: 21em]"> - Link you copied - </message> -</grit-part> diff --git a/ios/chrome/browser/browser_state/browser_state_keyed_service_factories.mm b/ios/chrome/browser/browser_state/browser_state_keyed_service_factories.mm index 32e375b..841db21 100644 --- a/ios/chrome/browser/browser_state/browser_state_keyed_service_factories.mm +++ b/ios/chrome/browser/browser_state/browser_state_keyed_service_factories.mm @@ -4,6 +4,8 @@ #include "ios/chrome/browser/browser_state/browser_state_keyed_service_factories.h" +#import <Foundation/Foundation.h> + #include "ios/chrome/browser/autofill/personal_data_manager_factory.h" #include "ios/chrome/browser/bookmarks/bookmark_client_factory.h" #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" diff --git a/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.h b/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.h index d70b03a..cf0fb71 100644 --- a/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.h +++ b/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.h @@ -14,6 +14,6 @@ class ClipboardRecentContent; // // This helper function allow the construction of ClipboardRecentContentIOS // from a pure C++ (ClipboardRecentContentIOS is an Objective-C++). -scoped_ptr<ClipboardRecentContent> CreateClipboadRecentContentIOS(); +scoped_ptr<ClipboardRecentContent> CreateClipboardRecentContentIOS(); #endif // IOS_CHROME_BROWSER_OPEN_FROM_CLIPBOARD_CREATE_CLIPBOARD_RECENT_CONTENT_H_ diff --git a/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.mm b/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.mm index 483aba8..3b55f8d 100644 --- a/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.mm +++ b/ios/chrome/browser/open_from_clipboard/create_clipboard_recent_content.mm @@ -7,7 +7,7 @@ #import "components/open_from_clipboard/clipboard_recent_content_ios.h" #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" -scoped_ptr<ClipboardRecentContent> CreateClipboadRecentContentIOS() { +scoped_ptr<ClipboardRecentContent> CreateClipboardRecentContentIOS() { return make_scoped_ptr(new ClipboardRecentContentIOS( ios::GetChromeBrowserProvider()->GetChromeUIScheme())); } |