summaryrefslogtreecommitdiffstats
path: root/components/omnibox
diff options
context:
space:
mode:
authorsdefresne <sdefresne@chromium.org>2015-08-11 03:46:35 -0700
committerCommit bot <commit-bot@chromium.org>2015-08-11 10:47:18 +0000
commit70948d68e3925e415e29d74230c4c0b39fbf31a4 (patch)
treef8256193710c9b45488dcd8ef381c430890a8ba0 /components/omnibox
parente5ec88b0b79ff67af77f33f3bda62f296a899fb5 (diff)
downloadchromium_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}
Diffstat (limited to 'components/omnibox')
-rw-r--r--components/omnibox/OWNERS2
-rw-r--r--components/omnibox/browser/BUILD.gn13
-rw-r--r--components/omnibox/browser/DEPS1
-rw-r--r--components/omnibox/browser/autocomplete_classifier.cc3
-rw-r--r--components/omnibox/browser/autocomplete_controller.cc11
-rw-r--r--components/omnibox/browser/autocomplete_provider.cc4
-rw-r--r--components/omnibox/browser/autocomplete_provider.h1
-rw-r--r--components/omnibox/browser/clipboard_url_provider.cc66
-rw-r--r--components/omnibox/browser/clipboard_url_provider.h32
-rw-r--r--components/omnibox/browser/clipboard_url_provider_unittest.cc80
-rw-r--r--components/omnibox/browser/verbatim_match.cc24
-rw-r--r--components/omnibox/browser/verbatim_match.h23
-rw-r--r--components/omnibox/browser/zero_suggest_provider.cc20
-rw-r--r--components/omnibox/browser/zero_suggest_provider.h3
14 files changed, 261 insertions, 22 deletions
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/omnibox/browser/clipboard_url_provider.h b/components/omnibox/browser/clipboard_url_provider.h
new file mode 100644
index 0000000..583711c
--- /dev/null
+++ b/components/omnibox/browser/clipboard_url_provider.h
@@ -0,0 +1,32 @@
+// 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_CLIPBOARD_URL_PROVIDER_H_
+#define COMPONENTS_OMNIBOX_BROWSER_CLIPBOARD_URL_PROVIDER_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:
+ ClipboardURLProvider(AutocompleteProviderClient* client,
+ ClipboardRecentContent* clipboard_content);
+
+ // AutocompleteProvider implementation.
+ void Start(const AutocompleteInput& input, bool minimal_changes) override;
+
+ private:
+ ~ClipboardURLProvider() override;
+
+ AutocompleteProviderClient* client_;
+ ClipboardRecentContent* clipboard_content_;
+
+ DISALLOW_COPY_AND_ASSIGN(ClipboardURLProvider);
+};
+
+#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.