Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / omnibox / browser / omnibox_view.h
blob905c7f7c32727690f93bf0816a7e0d06b177eeb7
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file defines the interface class OmniboxView. Each toolkit will
6 // implement the edit view differently, so that code is inherently platform
7 // 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.
11 #ifndef COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_
12 #define COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_
14 #include <string>
16 #include "base/strings/string16.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "components/omnibox/browser/autocomplete_match.h"
20 #include "components/omnibox/browser/omnibox_edit_model.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "ui/gfx/native_widget_types.h"
24 class GURL;
25 class OmniboxClient;
26 class OmniboxEditController;
27 class OmniboxViewMacTest;
28 class ToolbarModel;
30 class OmniboxView {
31 public:
32 virtual ~OmniboxView();
34 // Used by the automation system for getting at the model from the view.
35 OmniboxEditModel* model() { return model_.get(); }
36 const OmniboxEditModel* model() const { return model_.get(); }
38 // Shared cross-platform focus handling.
39 void OnDidKillFocus();
41 // Called when any relevant state changes other than changing tabs.
42 virtual void Update() = 0;
44 // Asks the browser to load the specified match, using the supplied
45 // disposition. |alternate_nav_url|, if non-empty, contains the
46 // alternate navigation URL for for this match. See comments on
47 // AutocompleteResult::GetAlternateNavURL().
49 // |pasted_text| should only be set if this call is due to a
50 // Paste-And-Go/Search action.
52 // |selected_line| is passed to SendOpenNotification(); see comments there.
54 // This may close the popup.
55 virtual void OpenMatch(const AutocompleteMatch& match,
56 WindowOpenDisposition disposition,
57 const GURL& alternate_nav_url,
58 const base::string16& pasted_text,
59 size_t selected_line);
61 // Returns the current text of the edit control, which could be the
62 // "temporary" text set by the popup, the "permanent" text set by the
63 // browser, or just whatever the user has currently typed.
64 virtual base::string16 GetText() const = 0;
66 // |true| if the user is in the process of editing the field, or if
67 // the field is empty.
68 bool IsEditingOrEmpty() const;
70 // Returns the resource ID of the icon to show for the current text.
71 int GetIcon() const;
73 // The user text is the text the user has manually keyed in. When present,
74 // this is shown in preference to the permanent text; hitting escape will
75 // revert to the permanent text.
76 void SetUserText(const base::string16& text);
77 virtual void SetUserText(const base::string16& text,
78 const base::string16& display_text,
79 bool update_popup);
81 // Sets the window text and the caret position. |notify_text_changed| is true
82 // if the model should be notified of the change.
83 virtual void SetWindowTextAndCaretPos(const base::string16& text,
84 size_t caret_pos,
85 bool update_popup,
86 bool notify_text_changed) = 0;
88 // Sets the edit to forced query mode. Practically speaking, this means that
89 // if the edit is not in forced query mode, its text is set to "?" with the
90 // cursor at the end, and if the edit is in forced query mode (its first
91 // non-whitespace character is '?'), the text after the '?' is selected.
93 // In the future we should display the search engine UI for the default engine
94 // rather than '?'.
95 virtual void SetForcedQuery() = 0;
97 // Returns true if all text is selected or there is no text at all.
98 virtual bool IsSelectAll() const = 0;
100 // Returns true if the user deleted the suggested text.
101 virtual bool DeleteAtEndPressed() = 0;
103 // Fills |start| and |end| with the indexes of the current selection's bounds.
104 // It is not guaranteed that |*start < *end|, as the selection can be
105 // directed. If there is no selection, |start| and |end| will both be equal
106 // to the current cursor position.
107 virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
109 // Selects all the text in the edit. Use this in place of SetSelAll() to
110 // avoid selecting the "phantom newline" at the end of the edit.
111 virtual void SelectAll(bool reversed) = 0;
113 // Sets focus, disables search term replacement, reverts the omnibox, and
114 // selects all.
115 void ShowURL();
117 // Enables search term replacement and reverts the omnibox.
118 void HideURL();
120 // Re-enables search term replacement on the ToolbarModel, and reverts the
121 // edit and popup back to their unedited state (permanent text showing, popup
122 // closed, no user input in progress).
123 virtual void RevertAll();
125 // Like RevertAll(), but does not touch the search term replacement state.
126 void RevertWithoutResettingSearchTermReplacement();
128 // Updates the autocomplete popup and other state after the text has been
129 // changed by the user.
130 virtual void UpdatePopup() = 0;
132 // Closes the autocomplete popup, if it's open. The name |ClosePopup|
133 // conflicts with the OSX class override as that has a base class that also
134 // defines a method with that name.
135 virtual void CloseOmniboxPopup();
137 // Sets the focus to the autocomplete view.
138 virtual void SetFocus() = 0;
140 // Shows or hides the caret based on whether the model's is_caret_visible() is
141 // true.
142 virtual void ApplyCaretVisibility() = 0;
144 // Called when the temporary text in the model may have changed.
145 // |display_text| is the new text to show; |save_original_selection| is true
146 // when there wasn't previously a temporary text and thus we need to save off
147 // the user's existing selection. |notify_text_changed| is true if the model
148 // should be notified of the change.
149 virtual void OnTemporaryTextMaybeChanged(const base::string16& display_text,
150 bool save_original_selection,
151 bool notify_text_changed) = 0;
153 // Called when the inline autocomplete text in the model may have changed.
154 // |display_text| is the new text to show; |user_text_length| is the length of
155 // the user input portion of that (so, up to but not including the inline
156 // autocompletion). Returns whether the display text actually changed.
157 virtual bool OnInlineAutocompleteTextMaybeChanged(
158 const base::string16& display_text, size_t user_text_length) = 0;
160 // Called when the inline autocomplete text in the model has been cleared.
161 virtual void OnInlineAutocompleteTextCleared() = 0;
163 // Called when the temporary text has been reverted by the user. This will
164 // reset the user's original selection.
165 virtual void OnRevertTemporaryText() = 0;
167 // Checkpoints the current edit state before an operation that might trigger
168 // a new autocomplete run to open or modify the popup. Call this before
169 // user-initiated edit actions that trigger autocomplete, but *not* for
170 // automatic changes to the textfield that should not affect autocomplete.
171 virtual void OnBeforePossibleChange() = 0;
172 // OnAfterPossibleChange() returns true if there was a change that caused it
173 // to call UpdatePopup().
174 virtual bool OnAfterPossibleChange() = 0;
176 // Returns the gfx::NativeView of the edit view.
177 virtual gfx::NativeView GetNativeView() const = 0;
179 // Gets the relative window for the pop up window of OmniboxPopupView. The pop
180 // up window will be shown under the relative window. When an IME is attached
181 // to the rich edit control, the IME window is the relative window. Otherwise,
182 // the top-most window is the relative window.
183 virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
185 // Shows |input| as gray suggested text after what the user has typed.
186 virtual void SetGrayTextAutocompletion(const base::string16& input) = 0;
188 // Returns the current gray suggested text.
189 virtual base::string16 GetGrayTextAutocompletion() const = 0;
191 // Returns the width in pixels needed to display the current text. The
192 // returned value includes margins.
193 virtual int GetTextWidth() const = 0;
195 // Returns the omnibox's width in pixels.
196 virtual int GetWidth() const = 0;
198 // Returns true if the user is composing something in an IME.
199 virtual bool IsImeComposing() const = 0;
201 // Returns true if we know for sure that an IME is showing a popup window,
202 // which may overlap the omnibox's popup window.
203 virtual bool IsImeShowingPopup() const;
205 // Display a virtual keybaord or alternate input view if enabled.
206 virtual void ShowImeIfNeeded();
208 // Returns true if the view is displaying UI that indicates that query
209 // refinement will take place when the user selects the current match. For
210 // search matches, this will cause the omnibox to search over the existing
211 // corpus (e.g. Images) rather than start a new Web search. This method will
212 // only ever return true on mobile ports.
213 virtual bool IsIndicatingQueryRefinement() const;
215 // Called after a |match| has been opened.
216 virtual void OnMatchOpened(const AutocompleteMatch& match);
218 // Returns |text| with any leading javascript schemas stripped.
219 static base::string16 StripJavascriptSchemas(const base::string16& text);
221 // First, calls StripJavascriptSchemas(). Then automatically collapses
222 // internal whitespace as follows:
223 // * If the only whitespace in |text| is newlines, users are most likely
224 // pasting in URLs split into multiple lines by terminals, email programs,
225 // etc. So all newlines are removed.
226 // * Otherwise, users may be pasting in search data, e.g. street addresses. In
227 // this case, runs of whitespace are collapsed down to single spaces.
228 static base::string16 SanitizeTextForPaste(const base::string16& text);
230 protected:
231 OmniboxView(OmniboxEditController* controller,
232 scoped_ptr<OmniboxClient> client);
234 // Internally invoked whenever the text changes in some way.
235 virtual void TextChanged();
237 // Return the number of characters in the current buffer. The name
238 // |GetTextLength| can't be used as the Windows override of this class
239 // inherits from a class that defines a method with that name.
240 virtual int GetOmniboxTextLength() const = 0;
242 // Try to parse the current text as a URL and colorize the components.
243 virtual void EmphasizeURLComponents() = 0;
245 OmniboxEditController* controller() { return controller_; }
246 const OmniboxEditController* controller() const { return controller_; }
248 private:
249 friend class OmniboxViewMacTest;
250 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ShowURL);
252 // |model_| can be NULL in tests.
253 scoped_ptr<OmniboxEditModel> model_;
254 OmniboxEditController* controller_;
256 DISALLOW_COPY_AND_ASSIGN(OmniboxView);
259 #endif // COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_VIEW_H_