blob: 5245445a1b0292b0329c9752b625ce105b25b3c2 [file] [log] [blame]
[email protected]3cb0f8d92012-02-29 05:43:341// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]93e50332009-03-02 18:58:262// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]b76ac712011-05-03 22:17:115// This file defines the interface class OmniboxView. Each toolkit will
6// implement the edit view differently, so that code is inherently platform
[email protected]fbdc4232012-06-24 15:28:377// specific. However, the OmniboxEditModel needs to do some communication with
8// the view. Since the model is shared between platforms, we need to define an
9// interface that all view implementations will share.
[email protected]93e50332009-03-02 18:58:2610
blundell7dbd3792015-08-05 15:14:1911#ifndef COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_
12#define COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_
[email protected]93e50332009-03-02 18:58:2613
avif57136c12015-12-25 23:27:4514#include <stddef.h>
15
[email protected]93e50332009-03-02 18:58:2616#include <string>
17
thestiga0e18cd2015-09-25 04:58:3618#include "base/gtest_prod_util.h"
avif57136c12015-12-25 23:27:4519#include "base/macros.h"
[email protected]11521182013-06-11 04:06:3620#include "base/strings/string16.h"
21#include "base/strings/string_util.h"
[email protected]5846d582013-06-08 16:02:1222#include "base/strings/utf_string_conversions.h"
blundell2102f7c2015-07-09 10:00:5323#include "components/omnibox/browser/autocomplete_match.h"
[email protected]f47621b2013-01-22 20:50:3324#include "ui/base/window_open_disposition.h"
[email protected]08397d52011-02-05 01:53:3825#include "ui/gfx/native_widget_types.h"
[email protected]93e50332009-03-02 18:58:2626
[email protected]93e50332009-03-02 18:58:2627class GURL;
blundell7cda1242015-07-15 09:58:2028class OmniboxClient;
[email protected]c51eed72012-08-07 22:01:5529class OmniboxEditController;
[email protected]ac7cc2b62012-12-20 03:33:4330class OmniboxViewMacTest;
[email protected]c51eed72012-08-07 22:01:5531class ToolbarModel;
thomasanderson00687d02016-06-08 16:06:3432class OmniboxEditModel;
[email protected]83a2610a2012-01-05 01:00:2733
estade3c7c6b12015-11-10 01:25:3734namespace gfx {
35enum class VectorIconId;
36}
37
[email protected]b76ac712011-05-03 22:17:1138class OmniboxView {
[email protected]93e50332009-03-02 18:58:2639 public:
thomasanderson00687d02016-06-08 16:06:3440 // Represents the changes between two State objects. This is used by the
41 // model to determine how its internal state should be updated after the view
42 // state changes. See OmniboxEditModel::OnAfterPossibleChange().
43 struct StateChanges {
44 // |old_text| and |new_text| are not owned.
45 const base::string16* old_text;
46 const base::string16* new_text;
47 size_t new_sel_start;
48 size_t new_sel_end;
49 bool selection_differs;
50 bool text_differs;
51 bool keyword_differs;
52 bool just_deleted_text;
53 };
54
[email protected]c51eed72012-08-07 22:01:5555 virtual ~OmniboxView();
56
[email protected]93e50332009-03-02 18:58:2657 // Used by the automation system for getting at the model from the view.
[email protected]c51eed72012-08-07 22:01:5558 OmniboxEditModel* model() { return model_.get(); }
59 const OmniboxEditModel* model() const { return model_.get(); }
60
[email protected]508107c2014-03-14 06:29:2861 // Shared cross-platform focus handling.
62 void OnDidKillFocus();
63
[email protected]7c8147a2013-08-24 03:09:4964 // Called when any relevant state changes other than changing tabs.
65 virtual void Update() = 0;
[email protected]93e50332009-03-02 18:58:2666
[email protected]60b55e92014-02-12 03:14:1867 // Asks the browser to load the specified match, using the supplied
68 // disposition. |alternate_nav_url|, if non-empty, contains the
[email protected]7e41c2b2011-05-06 10:31:1669 // alternate navigation URL for for this match. See comments on
70 // AutocompleteResult::GetAlternateNavURL().
[email protected]93e50332009-03-02 18:58:2671 //
[email protected]60b55e92014-02-12 03:14:1872 // |pasted_text| should only be set if this call is due to a
73 // Paste-And-Go/Search action.
74 //
[email protected]93e50332009-03-02 18:58:2675 // |selected_line| is passed to SendOpenNotification(); see comments there.
76 //
[email protected]93e50332009-03-02 18:58:2677 // This may close the popup.
[email protected]7e41c2b2011-05-06 10:31:1678 virtual void OpenMatch(const AutocompleteMatch& match,
79 WindowOpenDisposition disposition,
80 const GURL& alternate_nav_url,
[email protected]60b55e92014-02-12 03:14:1881 const base::string16& pasted_text,
[email protected]c51eed72012-08-07 22:01:5582 size_t selected_line);
[email protected]93e50332009-03-02 18:58:2683
84 // Returns the current text of the edit control, which could be the
85 // "temporary" text set by the popup, the "permanent" text set by the
86 // browser, or just whatever the user has currently typed.
[email protected]e9273e192013-12-11 17:51:4987 virtual base::string16 GetText() const = 0;
[email protected]93e50332009-03-02 18:58:2688
[email protected]69c579e2010-04-23 20:01:0089 // |true| if the user is in the process of editing the field, or if
90 // the field is empty.
[email protected]c51eed72012-08-07 22:01:5591 bool IsEditingOrEmpty() const;
[email protected]69c579e2010-04-23 20:01:0092
palmerc354e282016-05-24 00:55:5793 // Like GetIcon(), but returns a vector icon identifier.
94 gfx::VectorIconId GetVectorIcon() const;
estade3c7c6b12015-11-10 01:25:3795
[email protected]93e50332009-03-02 18:58:2696 // The user text is the text the user has manually keyed in. When present,
97 // this is shown in preference to the permanent text; hitting escape will
98 // revert to the permanent text.
[email protected]e9273e192013-12-11 17:51:4999 void SetUserText(const base::string16& text);
100 virtual void SetUserText(const base::string16& text,
[email protected]c51eed72012-08-07 22:01:55101 bool update_popup);
[email protected]93e50332009-03-02 18:58:26102
[email protected]01b8d6502013-02-15 17:35:55103 // Sets the window text and the caret position. |notify_text_changed| is true
104 // if the model should be notified of the change.
[email protected]e9273e192013-12-11 17:51:49105 virtual void SetWindowTextAndCaretPos(const base::string16& text,
[email protected]3cb0f8d92012-02-29 05:43:34106 size_t caret_pos,
107 bool update_popup,
108 bool notify_text_changed) = 0;
[email protected]93e50332009-03-02 18:58:26109
thomasanderson00687d02016-06-08 16:06:34110 // Transitions the user into keyword mode with their default search provider,
111 // preserving and selecting the user's text if they already typed in a query.
112 virtual void EnterKeywordModeForDefaultSearchProvider() = 0;
[email protected]731980c2009-06-05 19:41:46113
[email protected]5fdafa32009-08-28 00:31:28114 // Returns true if all text is selected or there is no text at all.
[email protected]fb8e3a32012-05-10 21:03:52115 virtual bool IsSelectAll() const = 0;
[email protected]93e50332009-03-02 18:58:26116
[email protected]b93b79ec2010-11-19 20:21:22117 // Returns true if the user deleted the suggested text.
118 virtual bool DeleteAtEndPressed() = 0;
119
[email protected]3c11b5b02010-09-11 05:20:42120 // Fills |start| and |end| with the indexes of the current selection's bounds.
121 // It is not guaranteed that |*start < *end|, as the selection can be
122 // directed. If there is no selection, |start| and |end| will both be equal
123 // to the current cursor position.
[email protected]4003e722011-10-31 05:04:03124 virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
[email protected]3c11b5b02010-09-11 05:20:42125
[email protected]93e50332009-03-02 18:58:26126 // Selects all the text in the edit. Use this in place of SetSelAll() to
127 // avoid selecting the "phantom newline" at the end of the edit.
128 virtual void SelectAll(bool reversed) = 0;
129
treibf5a29012016-09-02 11:08:13130 // Reverts the edit and popup back to their unedited state (permanent text
131 // showing, popup closed, no user input in progress).
[email protected]c51eed72012-08-07 22:01:55132 virtual void RevertAll();
[email protected]93e50332009-03-02 18:58:26133
[email protected]93e50332009-03-02 18:58:26134 // Updates the autocomplete popup and other state after the text has been
135 // changed by the user.
136 virtual void UpdatePopup() = 0;
137
[email protected]c51eed72012-08-07 22:01:55138 // Closes the autocomplete popup, if it's open. The name |ClosePopup|
139 // conflicts with the OSX class override as that has a base class that also
140 // defines a method with that name.
141 virtual void CloseOmniboxPopup();
[email protected]93e50332009-03-02 18:58:26142
[email protected]2be2c0132009-09-14 20:20:35143 // Sets the focus to the autocomplete view.
144 virtual void SetFocus() = 0;
145
[email protected]c18cb672012-12-05 04:42:12146 // Shows or hides the caret based on whether the model's is_caret_visible() is
147 // true.
148 virtual void ApplyCaretVisibility() = 0;
149
[email protected]93e50332009-03-02 18:58:26150 // Called when the temporary text in the model may have changed.
151 // |display_text| is the new text to show; |save_original_selection| is true
152 // when there wasn't previously a temporary text and thus we need to save off
[email protected]01b8d6502013-02-15 17:35:55153 // the user's existing selection. |notify_text_changed| is true if the model
154 // should be notified of the change.
[email protected]e9273e192013-12-11 17:51:49155 virtual void OnTemporaryTextMaybeChanged(const base::string16& display_text,
[email protected]01b8d6502013-02-15 17:35:55156 bool save_original_selection,
157 bool notify_text_changed) = 0;
[email protected]93e50332009-03-02 18:58:26158
159 // Called when the inline autocomplete text in the model may have changed.
160 // |display_text| is the new text to show; |user_text_length| is the length of
161 // the user input portion of that (so, up to but not including the inline
162 // autocompletion). Returns whether the display text actually changed.
163 virtual bool OnInlineAutocompleteTextMaybeChanged(
[email protected]e9273e192013-12-11 17:51:49164 const base::string16& display_text, size_t user_text_length) = 0;
[email protected]93e50332009-03-02 18:58:26165
[email protected]fa995762013-11-18 11:05:52166 // Called when the inline autocomplete text in the model has been cleared.
167 virtual void OnInlineAutocompleteTextCleared() = 0;
168
[email protected]93e50332009-03-02 18:58:26169 // Called when the temporary text has been reverted by the user. This will
170 // reset the user's original selection.
171 virtual void OnRevertTemporaryText() = 0;
172
[email protected]bbe5b49e2013-04-05 02:27:01173 // Checkpoints the current edit state before an operation that might trigger
174 // a new autocomplete run to open or modify the popup. Call this before
175 // user-initiated edit actions that trigger autocomplete, but *not* for
176 // automatic changes to the textfield that should not affect autocomplete.
[email protected]93e50332009-03-02 18:58:26177 virtual void OnBeforePossibleChange() = 0;
178 // OnAfterPossibleChange() returns true if there was a change that caused it
mpearson49fa6dc2015-12-01 22:20:10179 // to call UpdatePopup(). If |allow_keyword_ui_change| is false, we
180 // prevent alterations to the keyword UI state (enabled vs. disabled).
181 virtual bool OnAfterPossibleChange(bool allow_keyword_ui_change) = 0;
[email protected]fb5153c52009-07-31 19:40:33182
183 // Returns the gfx::NativeView of the edit view.
184 virtual gfx::NativeView GetNativeView() const = 0;
[email protected]135fd3b62009-12-16 01:07:08185
[email protected]b2544aa92012-06-21 04:20:09186 // Gets the relative window for the pop up window of OmniboxPopupView. The pop
187 // up window will be shown under the relative window. When an IME is attached
188 // to the rich edit control, the IME window is the relative window. Otherwise,
189 // the top-most window is the relative window.
[email protected]3510d442011-10-07 22:13:46190 virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
191
[email protected]b1a843a42013-07-11 15:11:41192 // Shows |input| as gray suggested text after what the user has typed.
[email protected]e9273e192013-12-11 17:51:49193 virtual void SetGrayTextAutocompletion(const base::string16& input) = 0;
[email protected]eec44d942011-01-11 08:27:24194
[email protected]b1a843a42013-07-11 15:11:41195 // Returns the current gray suggested text.
[email protected]e9273e192013-12-11 17:51:49196 virtual base::string16 GetGrayTextAutocompletion() const = 0;
[email protected]911696b2011-01-28 02:36:49197
[email protected]eec44d942011-01-11 08:27:24198 // Returns the width in pixels needed to display the current text. The
199 // returned value includes margins.
[email protected]14ac3492013-12-16 20:02:49200 virtual int GetTextWidth() const = 0;
201
202 // Returns the omnibox's width in pixels.
203 virtual int GetWidth() const = 0;
[email protected]eec44d942011-01-11 08:27:24204
[email protected]28ea1c92011-01-13 00:30:18205 // Returns true if the user is composing something in an IME.
206 virtual bool IsImeComposing() const = 0;
207
[email protected]d4606b5d2013-06-20 22:38:59208 // Returns true if we know for sure that an IME is showing a popup window,
209 // which may overlap the omnibox's popup window.
[email protected]13177832013-05-31 07:46:05210 virtual bool IsImeShowingPopup() const;
211
[email protected]183e28d2014-01-20 18:18:02212 // Display a virtual keybaord or alternate input view if enabled.
213 virtual void ShowImeIfNeeded();
214
[email protected]40a01152013-06-20 03:52:00215 // Returns true if the view is displaying UI that indicates that query
216 // refinement will take place when the user selects the current match. For
217 // search matches, this will cause the omnibox to search over the existing
218 // corpus (e.g. Images) rather than start a new Web search. This method will
219 // only ever return true on mobile ports.
220 virtual bool IsIndicatingQueryRefinement() const;
221
pkastingda6dd842016-02-24 15:26:59222 // Called after a match has been opened.
223 virtual void OnMatchOpened(AutocompleteMatch::Type match_type);
[email protected]94b8a51a2014-03-26 20:57:55224
[email protected]6cf51b62013-08-10 13:49:22225 // Returns |text| with any leading javascript schemas stripped.
[email protected]e9273e192013-12-11 17:51:49226 static base::string16 StripJavascriptSchemas(const base::string16& text);
[email protected]067095a2011-05-24 23:43:44227
[email protected]6cf51b62013-08-10 13:49:22228 // First, calls StripJavascriptSchemas(). Then automatically collapses
229 // internal whitespace as follows:
230 // * If the only whitespace in |text| is newlines, users are most likely
231 // pasting in URLs split into multiple lines by terminals, email programs,
232 // etc. So all newlines are removed.
233 // * Otherwise, users may be pasting in search data, e.g. street addresses. In
234 // this case, runs of whitespace are collapsed down to single spaces.
[email protected]e9273e192013-12-11 17:51:49235 static base::string16 SanitizeTextForPaste(const base::string16& text);
[email protected]6cf51b62013-08-10 13:49:22236
[email protected]c51eed72012-08-07 22:01:55237 protected:
thomasanderson00687d02016-06-08 16:06:34238 // Tracks important state that may change between OnBeforePossibleChange() and
239 // OnAfterPossibleChange().
240 struct State {
241 base::string16 text;
242 base::string16 keyword;
243 bool is_keyword_selected;
244 size_t sel_start;
245 size_t sel_end;
246 };
247
blundelld28fa852015-08-04 16:19:54248 OmniboxView(OmniboxEditController* controller,
dcheng259570c2016-04-22 00:45:57249 std::unique_ptr<OmniboxClient> client);
[email protected]c51eed72012-08-07 22:01:55250
thomasanderson00687d02016-06-08 16:06:34251 // Fills |state| with the current text state.
252 void GetState(State* state);
253
254 // Returns the delta between |before| and |after|.
255 StateChanges GetStateChanges(const State& before,
256 const State& after);
257
[email protected]c51eed72012-08-07 22:01:55258 // Internally invoked whenever the text changes in some way.
259 virtual void TextChanged();
260
261 // Return the number of characters in the current buffer. The name
262 // |GetTextLength| can't be used as the Windows override of this class
263 // inherits from a class that defines a method with that name.
264 virtual int GetOmniboxTextLength() const = 0;
265
266 // Try to parse the current text as a URL and colorize the components.
267 virtual void EmphasizeURLComponents() = 0;
268
[email protected]c51eed72012-08-07 22:01:55269 OmniboxEditController* controller() { return controller_; }
[email protected]a40be8b2013-08-22 20:12:14270 const OmniboxEditController* controller() const { return controller_; }
[email protected]c51eed72012-08-07 22:01:55271
272 private:
[email protected]ac7cc2b62012-12-20 03:33:43273 friend class OmniboxViewMacTest;
[email protected]ac7cc2b62012-12-20 03:33:43274
[email protected]c51eed72012-08-07 22:01:55275 // |model_| can be NULL in tests.
dcheng259570c2016-04-22 00:45:57276 std::unique_ptr<OmniboxEditModel> model_;
[email protected]c51eed72012-08-07 22:01:55277 OmniboxEditController* controller_;
[email protected]c51eed72012-08-07 22:01:55278
blundell18b35fd2015-07-28 13:16:06279 DISALLOW_COPY_AND_ASSIGN(OmniboxView);
[email protected]93e50332009-03-02 18:58:26280};
281
blundell7dbd3792015-08-05 15:14:19282#endif // COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_