Commit 8190f935 authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by Commit Bot

[ntp] Move omnibox util code into separate file

This util code will be used by the WebUI NTP to implement the fakebox.

Bug: 1061073
Change-Id: I5bbbdeb2c05119d006618baed309234308ad76a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2101854
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarMoe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#750812}
parent 8bad88a4
...@@ -1126,6 +1126,8 @@ jumbo_static_library("ui") { ...@@ -1126,6 +1126,8 @@ jumbo_static_library("ui") {
"search/ntp_user_data_logger.cc", "search/ntp_user_data_logger.cc",
"search/ntp_user_data_logger.h", "search/ntp_user_data_logger.h",
"search/ntp_user_data_types.h", "search/ntp_user_data_types.h",
"search/omnibox_utils.cc",
"search/omnibox_utils.h",
"search/search_ipc_router.cc", "search/search_ipc_router.cc",
"search/search_ipc_router.h", "search/search_ipc_router.h",
"search/search_ipc_router_policy_impl.cc", "search/search_ipc_router_policy_impl.cc",
......
// Copyright 2020 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/ui/search/omnibox_utils.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/clipboard_utils.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_popup_model.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "content/public/browser/web_contents.h"
namespace {
OmniboxView* GetOmniboxView(content::WebContents* web_contents) {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
if (!browser)
return nullptr;
return browser->window()->GetLocationBar()->GetOmniboxView();
}
} // namespace
namespace search {
void FocusOmnibox(bool focus, content::WebContents* web_contents) {
OmniboxView* omnibox_view = GetOmniboxView(web_contents);
if (!omnibox_view)
return;
if (focus) {
// This is an invisible focus to support "realbox" implementations on NTPs
// (including other search providers). We shouldn't consider it as the user
// explicitly focusing the omnibox.
omnibox_view->SetFocus(/*is_user_initiated=*/false);
omnibox_view->model()->SetCaretVisibility(false);
// If the user clicked on the fakebox, any text already in the omnibox
// should get cleared when they start typing. Selecting all the existing
// text is a convenient way to accomplish this. It also gives a slight
// visual cue to users who really understand selection state about what
// will happen if they start typing.
omnibox_view->SelectAll(false);
omnibox_view->ShowVirtualKeyboardIfEnabled();
} else {
// Remove focus only if the popup is closed. This will prevent someone
// from changing the omnibox value and closing the popup without user
// interaction.
if (!omnibox_view->model()->popup_model()->IsOpen())
web_contents->Focus();
}
}
void PasteIntoOmnibox(const base::string16& text,
content::WebContents* web_contents) {
OmniboxView* omnibox_view = GetOmniboxView(web_contents);
if (!omnibox_view)
return;
// The first case is for right click to paste, where the text is retrieved
// from the clipboard already sanitized. The second case is needed to handle
// drag-and-drop value and it has to be sanitazed before setting it into the
// omnibox.
base::string16 text_to_paste = text.empty()
? GetClipboardText()
: omnibox_view->SanitizeTextForPaste(text);
if (text_to_paste.empty())
return;
if (!omnibox_view->model()->has_focus()) {
// Pasting into a "realbox" should not be considered the user explicitly
// focusing the omnibox.
omnibox_view->SetFocus(/*is_user_initiated=*/false);
}
omnibox_view->OnBeforePossibleChange();
omnibox_view->model()->OnPaste();
omnibox_view->SetUserText(text_to_paste);
omnibox_view->OnAfterPossibleChange(true);
}
bool IsOmniboxInputInProgress(content::WebContents* web_contents) {
OmniboxView* omnibox_view = GetOmniboxView(web_contents);
return omnibox_view && omnibox_view->model()->user_input_in_progress() &&
omnibox_view->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE;
}
} // namespace search
// Copyright 2020 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 CHROME_BROWSER_UI_SEARCH_OMNIBOX_UTILS_H_
#define CHROME_BROWSER_UI_SEARCH_OMNIBOX_UTILS_H_
#include "base/strings/string16.h"
namespace content {
class WebContents;
} // namespace content
namespace search {
// Focus or unfocus the omnibox if |focus| is true or false respectively.
void FocusOmnibox(bool focus, content::WebContents* web_contents);
// Pastes |text| (or the clipboard if |text| is empty) into the omnibox.
void PasteIntoOmnibox(const base::string16& text,
content::WebContents* web_contents);
// Returns whether input is in progress, i.e. if the omnibox has focus and the
// active tab is in mode SEARCH_SUGGESTIONS.
bool IsOmniboxInputInProgress(content::WebContents* web_contents);
} // namespace search
#endif // CHROME_BROWSER_UI_SEARCH_OMNIBOX_UTILS_H_
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "chrome/browser/ui/location_bar/location_bar.h" #include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/clipboard_utils.h" #include "chrome/browser/ui/omnibox/clipboard_utils.h"
#include "chrome/browser/ui/search/ntp_user_data_logger.h" #include "chrome/browser/ui/search/ntp_user_data_logger.h"
#include "chrome/browser/ui/search/omnibox_utils.h"
#include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h" #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog.h" #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
...@@ -303,30 +304,7 @@ void SearchTabHelper::MostVisitedInfoChanged( ...@@ -303,30 +304,7 @@ void SearchTabHelper::MostVisitedInfoChanged(
} }
void SearchTabHelper::FocusOmnibox(bool focus) { void SearchTabHelper::FocusOmnibox(bool focus) {
OmniboxView* omnibox_view = GetOmniboxView(); search::FocusOmnibox(focus, web_contents_);
if (!omnibox_view)
return;
if (focus) {
// This is an invisible focus to support "realbox" implementations on NTPs
// (including other search providers). We shouldn't consider it as the user
// explicitly focusing the omnibox.
omnibox_view->SetFocus(/*is_user_initiated=*/false);
omnibox_view->model()->SetCaretVisibility(false);
// If the user clicked on the fakebox, any text already in the omnibox
// should get cleared when they start typing. Selecting all the existing
// text is a convenient way to accomplish this. It also gives a slight
// visual cue to users who really understand selection state about what
// will happen if they start typing.
omnibox_view->SelectAll(false);
omnibox_view->ShowVirtualKeyboardIfEnabled();
} else {
// Remove focus only if the popup is closed. This will prevent someone
// from changing the omnibox value and closing the popup without user
// interaction.
if (!omnibox_view->model()->popup_model()->IsOpen())
web_contents()->Focus();
}
} }
void SearchTabHelper::OnDeleteMostVisitedItem(const GURL& url) { void SearchTabHelper::OnDeleteMostVisitedItem(const GURL& url) {
...@@ -424,30 +402,7 @@ void SearchTabHelper::OnLogMostVisitedNavigation( ...@@ -424,30 +402,7 @@ void SearchTabHelper::OnLogMostVisitedNavigation(
} }
void SearchTabHelper::PasteIntoOmnibox(const base::string16& text) { void SearchTabHelper::PasteIntoOmnibox(const base::string16& text) {
OmniboxView* omnibox_view = GetOmniboxView(); search::PasteIntoOmnibox(text, web_contents_);
if (!omnibox_view)
return;
// The first case is for right click to paste, where the text is retrieved
// from the clipboard already sanitized. The second case is needed to handle
// drag-and-drop value and it has to be sanitazed before setting it into the
// omnibox.
base::string16 text_to_paste = text.empty()
? GetClipboardText()
: omnibox_view->SanitizeTextForPaste(text);
if (text_to_paste.empty())
return;
if (!omnibox_view->model()->has_focus()) {
// Pasting into a "realbox" should not be considered the user explicitly
// focusing the omnibox.
omnibox_view->SetFocus(/*is_user_initiated=*/false);
}
omnibox_view->OnBeforePossibleChange();
omnibox_view->model()->OnPaste();
omnibox_view->SetUserText(text_to_paste);
omnibox_view->OnAfterPossibleChange(true);
} }
void SearchTabHelper::OnSetCustomBackgroundInfo( void SearchTabHelper::OnSetCustomBackgroundInfo(
...@@ -578,14 +533,6 @@ void SearchTabHelper::OnSelectLocalBackgroundImage() { ...@@ -578,14 +533,6 @@ void SearchTabHelper::OnSelectLocalBackgroundImage() {
&file_types, 0, base::FilePath::StringType(), parent_window, nullptr); &file_types, 0, base::FilePath::StringType(), parent_window, nullptr);
} }
const OmniboxView* SearchTabHelper::GetOmniboxView() const {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
if (!browser)
return nullptr;
return browser->window()->GetLocationBar()->GetOmniboxView();
}
void SearchTabHelper::OnBlocklistSearchSuggestion(int task_version, void SearchTabHelper::OnBlocklistSearchSuggestion(int task_version,
long task_id) { long task_id) {
if (search_suggest_service_) if (search_suggest_service_)
...@@ -937,19 +884,12 @@ void SearchTabHelper::OpenAutocompleteMatch( ...@@ -937,19 +884,12 @@ void SearchTabHelper::OpenAutocompleteMatch(
// May delete us. // May delete us.
} }
OmniboxView* SearchTabHelper::GetOmniboxView() {
return const_cast<OmniboxView*>(
const_cast<const SearchTabHelper*>(this)->GetOmniboxView());
}
Profile* SearchTabHelper::profile() const { Profile* SearchTabHelper::profile() const {
return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
} }
bool SearchTabHelper::IsInputInProgress() const { bool SearchTabHelper::IsInputInProgress() const {
const OmniboxView* omnibox_view = GetOmniboxView(); return search::IsOmniboxInputInProgress(web_contents_);
return omnibox_view && omnibox_view->model()->user_input_in_progress() &&
omnibox_view->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE;
} }
WEB_CONTENTS_USER_DATA_KEY_IMPL(SearchTabHelper) WEB_CONTENTS_USER_DATA_KEY_IMPL(SearchTabHelper)
...@@ -42,7 +42,6 @@ struct LoadCommittedDetails; ...@@ -42,7 +42,6 @@ struct LoadCommittedDetails;
class AutocompleteController; class AutocompleteController;
class GURL; class GURL;
class InstantService; class InstantService;
class OmniboxView;
class Profile; class Profile;
class SearchIPCRouterTest; class SearchIPCRouterTest;
class SearchSuggestService; class SearchSuggestService;
...@@ -177,9 +176,6 @@ class SearchTabHelper : public content::WebContentsObserver, ...@@ -177,9 +176,6 @@ class SearchTabHelper : public content::WebContentsObserver,
const std::string& image_url, const std::string& image_url,
const SkBitmap& bitmap); const SkBitmap& bitmap);
OmniboxView* GetOmniboxView();
const OmniboxView* GetOmniboxView() const;
Profile* profile() const; Profile* profile() const;
// Returns whether input is in progress, i.e. if the omnibox has focus and the // Returns whether input is in progress, i.e. if the omnibox has focus and the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment