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 CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
12 #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_
16 #include "base/strings/string16.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
20 #include "components/omnibox/browser/autocomplete_match.h"
21 #include "ui/base/window_open_disposition.h"
22 #include "ui/gfx/native_widget_types.h"
27 class OmniboxEditController
;
28 class OmniboxViewMacTest
;
38 virtual ~OmniboxView();
40 // Used by the automation system for getting at the model from the view.
41 OmniboxEditModel
* model() { return model_
.get(); }
42 const OmniboxEditModel
* model() const { return model_
.get(); }
44 CommandUpdater
* command_updater() { return command_updater_
; }
45 const CommandUpdater
* command_updater() const { return command_updater_
; }
47 // Shared cross-platform focus handling.
48 void OnDidKillFocus();
50 // For use when switching tabs, this saves the current state onto the tab so
51 // that it can be restored during a later call to Update().
52 virtual void SaveStateToTab(content::WebContents
* tab
) = 0;
54 // Called when the window's active tab changes.
55 virtual void OnTabChanged(const content::WebContents
* web_contents
) = 0;
57 // Called to clear the saved state for |web_contents|.
58 virtual void ResetTabState(content::WebContents
* web_contents
) = 0;
60 // Called when any relevant state changes other than changing tabs.
61 virtual void Update() = 0;
63 // Asks the browser to load the specified match, using the supplied
64 // disposition. |alternate_nav_url|, if non-empty, contains the
65 // alternate navigation URL for for this match. See comments on
66 // AutocompleteResult::GetAlternateNavURL().
68 // |pasted_text| should only be set if this call is due to a
69 // Paste-And-Go/Search action.
71 // |selected_line| is passed to SendOpenNotification(); see comments there.
73 // This may close the popup.
74 virtual void OpenMatch(const AutocompleteMatch
& match
,
75 WindowOpenDisposition disposition
,
76 const GURL
& alternate_nav_url
,
77 const base::string16
& pasted_text
,
78 size_t selected_line
);
80 // Returns the current text of the edit control, which could be the
81 // "temporary" text set by the popup, the "permanent" text set by the
82 // browser, or just whatever the user has currently typed.
83 virtual base::string16
GetText() const = 0;
85 // |true| if the user is in the process of editing the field, or if
86 // the field is empty.
87 bool IsEditingOrEmpty() const;
89 // Returns the resource ID of the icon to show for the current text.
92 // The user text is the text the user has manually keyed in. When present,
93 // this is shown in preference to the permanent text; hitting escape will
94 // revert to the permanent text.
95 void SetUserText(const base::string16
& text
);
96 virtual void SetUserText(const base::string16
& text
,
97 const base::string16
& display_text
,
100 // Sets the window text and the caret position. |notify_text_changed| is true
101 // if the model should be notified of the change.
102 virtual void SetWindowTextAndCaretPos(const base::string16
& text
,
105 bool notify_text_changed
) = 0;
107 // Sets the edit to forced query mode. Practically speaking, this means that
108 // if the edit is not in forced query mode, its text is set to "?" with the
109 // cursor at the end, and if the edit is in forced query mode (its first
110 // non-whitespace character is '?'), the text after the '?' is selected.
112 // In the future we should display the search engine UI for the default engine
114 virtual void SetForcedQuery() = 0;
116 // Returns true if all text is selected or there is no text at all.
117 virtual bool IsSelectAll() const = 0;
119 // Returns true if the user deleted the suggested text.
120 virtual bool DeleteAtEndPressed() = 0;
122 // Fills |start| and |end| with the indexes of the current selection's bounds.
123 // It is not guaranteed that |*start < *end|, as the selection can be
124 // directed. If there is no selection, |start| and |end| will both be equal
125 // to the current cursor position.
126 virtual void GetSelectionBounds(size_t* start
, size_t* end
) const = 0;
128 // Selects all the text in the edit. Use this in place of SetSelAll() to
129 // avoid selecting the "phantom newline" at the end of the edit.
130 virtual void SelectAll(bool reversed
) = 0;
132 // Sets focus, disables search term replacement, reverts the omnibox, and
136 // Enables search term replacement and reverts the omnibox.
139 // Re-enables search term replacement on the ToolbarModel, and reverts the
140 // edit and popup back to their unedited state (permanent text showing, popup
141 // closed, no user input in progress).
142 virtual void RevertAll();
144 // Like RevertAll(), but does not touch the search term replacement state.
145 void RevertWithoutResettingSearchTermReplacement();
147 // Updates the autocomplete popup and other state after the text has been
148 // changed by the user.
149 virtual void UpdatePopup() = 0;
151 // Closes the autocomplete popup, if it's open. The name |ClosePopup|
152 // conflicts with the OSX class override as that has a base class that also
153 // defines a method with that name.
154 virtual void CloseOmniboxPopup();
156 // Sets the focus to the autocomplete view.
157 virtual void SetFocus() = 0;
159 // Shows or hides the caret based on whether the model's is_caret_visible() is
161 virtual void ApplyCaretVisibility() = 0;
163 // Called when the temporary text in the model may have changed.
164 // |display_text| is the new text to show; |save_original_selection| is true
165 // when there wasn't previously a temporary text and thus we need to save off
166 // the user's existing selection. |notify_text_changed| is true if the model
167 // should be notified of the change.
168 virtual void OnTemporaryTextMaybeChanged(const base::string16
& display_text
,
169 bool save_original_selection
,
170 bool notify_text_changed
) = 0;
172 // Called when the inline autocomplete text in the model may have changed.
173 // |display_text| is the new text to show; |user_text_length| is the length of
174 // the user input portion of that (so, up to but not including the inline
175 // autocompletion). Returns whether the display text actually changed.
176 virtual bool OnInlineAutocompleteTextMaybeChanged(
177 const base::string16
& display_text
, size_t user_text_length
) = 0;
179 // Called when the inline autocomplete text in the model has been cleared.
180 virtual void OnInlineAutocompleteTextCleared() = 0;
182 // Called when the temporary text has been reverted by the user. This will
183 // reset the user's original selection.
184 virtual void OnRevertTemporaryText() = 0;
186 // Checkpoints the current edit state before an operation that might trigger
187 // a new autocomplete run to open or modify the popup. Call this before
188 // user-initiated edit actions that trigger autocomplete, but *not* for
189 // automatic changes to the textfield that should not affect autocomplete.
190 virtual void OnBeforePossibleChange() = 0;
191 // OnAfterPossibleChange() returns true if there was a change that caused it
192 // to call UpdatePopup().
193 virtual bool OnAfterPossibleChange() = 0;
195 // Returns the gfx::NativeView of the edit view.
196 virtual gfx::NativeView
GetNativeView() const = 0;
198 // Gets the relative window for the pop up window of OmniboxPopupView. The pop
199 // up window will be shown under the relative window. When an IME is attached
200 // to the rich edit control, the IME window is the relative window. Otherwise,
201 // the top-most window is the relative window.
202 virtual gfx::NativeView
GetRelativeWindowForPopup() const = 0;
204 // Shows |input| as gray suggested text after what the user has typed.
205 virtual void SetGrayTextAutocompletion(const base::string16
& input
) = 0;
207 // Returns the current gray suggested text.
208 virtual base::string16
GetGrayTextAutocompletion() const = 0;
210 // Returns the width in pixels needed to display the current text. The
211 // returned value includes margins.
212 virtual int GetTextWidth() const = 0;
214 // Returns the omnibox's width in pixels.
215 virtual int GetWidth() const = 0;
217 // Returns true if the user is composing something in an IME.
218 virtual bool IsImeComposing() const = 0;
220 // Returns true if we know for sure that an IME is showing a popup window,
221 // which may overlap the omnibox's popup window.
222 virtual bool IsImeShowingPopup() const;
224 // Display a virtual keybaord or alternate input view if enabled.
225 virtual void ShowImeIfNeeded();
227 // Returns true if the view is displaying UI that indicates that query
228 // refinement will take place when the user selects the current match. For
229 // search matches, this will cause the omnibox to search over the existing
230 // corpus (e.g. Images) rather than start a new Web search. This method will
231 // only ever return true on mobile ports.
232 virtual bool IsIndicatingQueryRefinement() const;
234 // Called after a |match| has been opened for the given |web_contents|.
235 virtual void OnMatchOpened(const AutocompleteMatch
& match
,
236 content::WebContents
* web_contents
);
238 // Returns |text| with any leading javascript schemas stripped.
239 static base::string16
StripJavascriptSchemas(const base::string16
& text
);
241 // First, calls StripJavascriptSchemas(). Then automatically collapses
242 // internal whitespace as follows:
243 // * If the only whitespace in |text| is newlines, users are most likely
244 // pasting in URLs split into multiple lines by terminals, email programs,
245 // etc. So all newlines are removed.
246 // * Otherwise, users may be pasting in search data, e.g. street addresses. In
247 // this case, runs of whitespace are collapsed down to single spaces.
248 static base::string16
SanitizeTextForPaste(const base::string16
& text
);
250 // Returns the current clipboard contents as a string that can be pasted in.
251 // In addition to just getting CF_UNICODETEXT out, this can also extract URLs
252 // from bookmarks on the clipboard.
253 static base::string16
GetClipboardText();
256 OmniboxView(Profile
* profile
,
257 OmniboxEditController
* controller
,
258 scoped_ptr
<OmniboxClient
> client
,
259 CommandUpdater
* command_updater
);
261 // Internally invoked whenever the text changes in some way.
262 virtual void TextChanged();
264 // Return the number of characters in the current buffer. The name
265 // |GetTextLength| can't be used as the Windows override of this class
266 // inherits from a class that defines a method with that name.
267 virtual int GetOmniboxTextLength() const = 0;
269 // Try to parse the current text as a URL and colorize the components.
270 virtual void EmphasizeURLComponents() = 0;
272 Profile
* profile() { return model_
->profile(); }
273 OmniboxEditController
* controller() { return controller_
; }
274 const OmniboxEditController
* controller() const { return controller_
; }
277 friend class OmniboxViewMacTest
;
278 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest
, ShowURL
);
280 // |model_| can be NULL in tests.
281 scoped_ptr
<OmniboxEditModel
> model_
;
282 OmniboxEditController
* controller_
;
284 // The object that handles additional command functionality exposed on the
285 // edit, such as invoking the keyword editor.
286 CommandUpdater
* command_updater_
;
289 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_