NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view.h
blobb838682fbaee0eecf09deba311931012b1073cfd
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_
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 "chrome/browser/autocomplete/autocomplete_match.h"
20 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
21 #include "content/public/common/url_constants.h"
22 #include "ui/base/window_open_disposition.h"
23 #include "ui/gfx/native_widget_types.h"
25 class CommandUpdater;
26 class GURL;
27 class OmniboxEditController;
28 class OmniboxViewMacTest;
29 class Profile;
30 class ToolbarModel;
32 namespace content {
33 class WebContents;
36 class OmniboxView {
37 public:
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 // For use when switching tabs, this saves the current state onto the tab so
48 // that it can be restored during a later call to Update().
49 virtual void SaveStateToTab(content::WebContents* tab) = 0;
51 // Called when the window's active tab changes.
52 virtual void OnTabChanged(const content::WebContents* web_contents) = 0;
54 // Called when any relevant state changes other than changing tabs.
55 virtual void Update() = 0;
57 // Asks the browser to load the specified match, using the supplied
58 // disposition. |alternate_nav_url|, if non-empty, contains the
59 // alternate navigation URL for for this match. See comments on
60 // AutocompleteResult::GetAlternateNavURL().
62 // |pasted_text| should only be set if this call is due to a
63 // Paste-And-Go/Search action.
65 // |selected_line| is passed to SendOpenNotification(); see comments there.
67 // This may close the popup.
68 virtual void OpenMatch(const AutocompleteMatch& match,
69 WindowOpenDisposition disposition,
70 const GURL& alternate_nav_url,
71 const base::string16& pasted_text,
72 size_t selected_line);
74 // Returns the current text of the edit control, which could be the
75 // "temporary" text set by the popup, the "permanent" text set by the
76 // browser, or just whatever the user has currently typed.
77 virtual base::string16 GetText() const = 0;
79 // |true| if the user is in the process of editing the field, or if
80 // the field is empty.
81 bool IsEditingOrEmpty() const;
83 // Returns the resource ID of the icon to show for the current text.
84 int GetIcon() const;
86 // Returns the hint text that can be displayed when there is no text in the
87 // omnibox.
88 base::string16 GetHintText() const;
90 // The user text is the text the user has manually keyed in. When present,
91 // this is shown in preference to the permanent text; hitting escape will
92 // revert to the permanent text.
93 void SetUserText(const base::string16& text);
94 virtual void SetUserText(const base::string16& text,
95 const base::string16& display_text,
96 bool update_popup);
98 // Sets the window text and the caret position. |notify_text_changed| is true
99 // if the model should be notified of the change.
100 virtual void SetWindowTextAndCaretPos(const base::string16& text,
101 size_t caret_pos,
102 bool update_popup,
103 bool notify_text_changed) = 0;
105 // Sets the edit to forced query mode. Practically speaking, this means that
106 // if the edit is not in forced query mode, its text is set to "?" with the
107 // cursor at the end, and if the edit is in forced query mode (its first
108 // non-whitespace character is '?'), the text after the '?' is selected.
110 // In the future we should display the search engine UI for the default engine
111 // rather than '?'.
112 virtual void SetForcedQuery() = 0;
114 // Returns true if all text is selected or there is no text at all.
115 virtual bool IsSelectAll() const = 0;
117 // Returns true if the user deleted the suggested text.
118 virtual bool DeleteAtEndPressed() = 0;
120 // 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.
124 virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
126 // 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;
130 // Sets focus, disables search term replacement, reverts the omnibox, and
131 // selects all.
132 void ShowURL();
134 // Re-enables search term replacement on the ToolbarModel, and reverts the
135 // edit and popup back to their unedited state (permanent text showing, popup
136 // closed, no user input in progress).
137 virtual void RevertAll();
139 // Like RevertAll(), but does not touch the search term replacement state.
140 void RevertWithoutResettingSearchTermReplacement();
142 // Updates the autocomplete popup and other state after the text has been
143 // changed by the user.
144 virtual void UpdatePopup() = 0;
146 // Closes the autocomplete popup, if it's open. The name |ClosePopup|
147 // conflicts with the OSX class override as that has a base class that also
148 // defines a method with that name.
149 virtual void CloseOmniboxPopup();
151 // Sets the focus to the autocomplete view.
152 virtual void SetFocus() = 0;
154 // Shows or hides the caret based on whether the model's is_caret_visible() is
155 // true.
156 virtual void ApplyCaretVisibility() = 0;
158 // Called when the temporary text in the model may have changed.
159 // |display_text| is the new text to show; |save_original_selection| is true
160 // when there wasn't previously a temporary text and thus we need to save off
161 // the user's existing selection. |notify_text_changed| is true if the model
162 // should be notified of the change.
163 virtual void OnTemporaryTextMaybeChanged(const base::string16& display_text,
164 bool save_original_selection,
165 bool notify_text_changed) = 0;
167 // Called when the inline autocomplete text in the model may have changed.
168 // |display_text| is the new text to show; |user_text_length| is the length of
169 // the user input portion of that (so, up to but not including the inline
170 // autocompletion). Returns whether the display text actually changed.
171 virtual bool OnInlineAutocompleteTextMaybeChanged(
172 const base::string16& display_text, size_t user_text_length) = 0;
174 // Called when the inline autocomplete text in the model has been cleared.
175 virtual void OnInlineAutocompleteTextCleared() = 0;
177 // Called when the temporary text has been reverted by the user. This will
178 // reset the user's original selection.
179 virtual void OnRevertTemporaryText() = 0;
181 // Checkpoints the current edit state before an operation that might trigger
182 // a new autocomplete run to open or modify the popup. Call this before
183 // user-initiated edit actions that trigger autocomplete, but *not* for
184 // automatic changes to the textfield that should not affect autocomplete.
185 virtual void OnBeforePossibleChange() = 0;
186 // OnAfterPossibleChange() returns true if there was a change that caused it
187 // to call UpdatePopup().
188 virtual bool OnAfterPossibleChange() = 0;
190 // Returns the gfx::NativeView of the edit view.
191 virtual gfx::NativeView GetNativeView() const = 0;
193 // Gets the relative window for the pop up window of OmniboxPopupView. The pop
194 // up window will be shown under the relative window. When an IME is attached
195 // to the rich edit control, the IME window is the relative window. Otherwise,
196 // the top-most window is the relative window.
197 virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
199 // Shows |input| as gray suggested text after what the user has typed.
200 virtual void SetGrayTextAutocompletion(const base::string16& input) = 0;
202 // Returns the current gray suggested text.
203 virtual base::string16 GetGrayTextAutocompletion() const = 0;
205 // Returns the width in pixels needed to display the current text. The
206 // returned value includes margins.
207 virtual int GetTextWidth() const = 0;
209 // Returns the omnibox's width in pixels.
210 virtual int GetWidth() const = 0;
212 // Returns true if the user is composing something in an IME.
213 virtual bool IsImeComposing() const = 0;
215 // Returns true if we know for sure that an IME is showing a popup window,
216 // which may overlap the omnibox's popup window.
217 virtual bool IsImeShowingPopup() const;
219 // Display a virtual keybaord or alternate input view if enabled.
220 virtual void ShowImeIfNeeded();
222 // Returns true if the view is displaying UI that indicates that query
223 // refinement will take place when the user selects the current match. For
224 // search matches, this will cause the omnibox to search over the existing
225 // corpus (e.g. Images) rather than start a new Web search. This method will
226 // only ever return true on mobile ports.
227 virtual bool IsIndicatingQueryRefinement() const;
229 // Returns |text| with any leading javascript schemas stripped.
230 static base::string16 StripJavascriptSchemas(const base::string16& text);
232 // First, calls StripJavascriptSchemas(). Then automatically collapses
233 // internal whitespace as follows:
234 // * If the only whitespace in |text| is newlines, users are most likely
235 // pasting in URLs split into multiple lines by terminals, email programs,
236 // etc. So all newlines are removed.
237 // * Otherwise, users may be pasting in search data, e.g. street addresses. In
238 // this case, runs of whitespace are collapsed down to single spaces.
239 static base::string16 SanitizeTextForPaste(const base::string16& text);
241 // Returns the current clipboard contents as a string that can be pasted in.
242 // In addition to just getting CF_UNICODETEXT out, this can also extract URLs
243 // from bookmarks on the clipboard.
244 static base::string16 GetClipboardText();
246 protected:
247 OmniboxView(Profile* profile,
248 OmniboxEditController* controller,
249 CommandUpdater* command_updater);
251 // Internally invoked whenever the text changes in some way.
252 virtual void TextChanged();
254 // Return the number of characters in the current buffer. The name
255 // |GetTextLength| can't be used as the Windows override of this class
256 // inherits from a class that defines a method with that name.
257 virtual int GetOmniboxTextLength() const = 0;
259 // Try to parse the current text as a URL and colorize the components.
260 virtual void EmphasizeURLComponents() = 0;
262 OmniboxEditController* controller() { return controller_; }
263 const OmniboxEditController* controller() const { return controller_; }
265 private:
266 friend class OmniboxViewMacTest;
267 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ShowURL);
269 // |model_| can be NULL in tests.
270 scoped_ptr<OmniboxEditModel> model_;
271 OmniboxEditController* controller_;
273 // The object that handles additional command functionality exposed on the
274 // edit, such as invoking the keyword editor.
275 CommandUpdater* command_updater_;
278 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_