Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_view.h
blob13729f879fe40d0e16ffbc69efdd3edc5cb052b0
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's |destination_url|, which
58 // is assumed to be one of the popup entries, using the supplied disposition
59 // and transition type. |alternate_nav_url|, if non-empty, contains the
60 // alternate navigation URL for for this match. See comments on
61 // AutocompleteResult::GetAlternateNavURL().
63 // |selected_line| is passed to SendOpenNotification(); see comments there.
65 // This may close the popup.
66 virtual void OpenMatch(const AutocompleteMatch& match,
67 WindowOpenDisposition disposition,
68 const GURL& alternate_nav_url,
69 size_t selected_line);
71 // Returns the current text of the edit control, which could be the
72 // "temporary" text set by the popup, the "permanent" text set by the
73 // browser, or just whatever the user has currently typed.
74 virtual base::string16 GetText() const = 0;
76 // |true| if the user is in the process of editing the field, or if
77 // the field is empty.
78 bool IsEditingOrEmpty() const;
80 // Returns the resource ID of the icon to show for the current text.
81 int GetIcon() const;
83 // The user text is the text the user has manually keyed in. When present,
84 // this is shown in preference to the permanent text; hitting escape will
85 // revert to the permanent text.
86 void SetUserText(const base::string16& text);
87 virtual void SetUserText(const base::string16& text,
88 const base::string16& display_text,
89 bool update_popup);
91 // Sets the window text and the caret position. |notify_text_changed| is true
92 // if the model should be notified of the change.
93 virtual void SetWindowTextAndCaretPos(const base::string16& text,
94 size_t caret_pos,
95 bool update_popup,
96 bool notify_text_changed) = 0;
98 // Sets the edit to forced query mode. Practically speaking, this means that
99 // if the edit is not in forced query mode, its text is set to "?" with the
100 // cursor at the end, and if the edit is in forced query mode (its first
101 // non-whitespace character is '?'), the text after the '?' is selected.
103 // In the future we should display the search engine UI for the default engine
104 // rather than '?'.
105 virtual void SetForcedQuery() = 0;
107 // Returns true if all text is selected or there is no text at all.
108 virtual bool IsSelectAll() const = 0;
110 // Returns true if the user deleted the suggested text.
111 virtual bool DeleteAtEndPressed() = 0;
113 // Fills |start| and |end| with the indexes of the current selection's bounds.
114 // It is not guaranteed that |*start < *end|, as the selection can be
115 // directed. If there is no selection, |start| and |end| will both be equal
116 // to the current cursor position.
117 virtual void GetSelectionBounds(size_t* start, size_t* end) const = 0;
119 // Selects all the text in the edit. Use this in place of SetSelAll() to
120 // avoid selecting the "phantom newline" at the end of the edit.
121 virtual void SelectAll(bool reversed) = 0;
123 // Sets focus, disables search term replacement, reverts the omnibox, and
124 // selects all.
125 void ShowURL();
127 // Re-enables search term replacement on the ToolbarModel, and reverts the
128 // edit and popup back to their unedited state (permanent text showing, popup
129 // closed, no user input in progress).
130 virtual void RevertAll();
132 // Like RevertAll(), but does not touch the search term replacement state.
133 void RevertWithoutResettingSearchTermReplacement();
135 // Updates the autocomplete popup and other state after the text has been
136 // changed by the user.
137 virtual void UpdatePopup() = 0;
139 // Closes the autocomplete popup, if it's open. The name |ClosePopup|
140 // conflicts with the OSX class override as that has a base class that also
141 // defines a method with that name.
142 virtual void CloseOmniboxPopup();
144 // Sets the focus to the autocomplete view.
145 virtual void SetFocus() = 0;
147 // Shows or hides the caret based on whether the model's is_caret_visible() is
148 // true.
149 virtual void ApplyCaretVisibility() = 0;
151 // Called when the temporary text in the model may have changed.
152 // |display_text| is the new text to show; |save_original_selection| is true
153 // when there wasn't previously a temporary text and thus we need to save off
154 // the user's existing selection. |notify_text_changed| is true if the model
155 // should be notified of the change.
156 virtual void OnTemporaryTextMaybeChanged(const base::string16& display_text,
157 bool save_original_selection,
158 bool notify_text_changed) = 0;
160 // Called when the inline autocomplete text in the model may have changed.
161 // |display_text| is the new text to show; |user_text_length| is the length of
162 // the user input portion of that (so, up to but not including the inline
163 // autocompletion). Returns whether the display text actually changed.
164 virtual bool OnInlineAutocompleteTextMaybeChanged(
165 const base::string16& display_text, size_t user_text_length) = 0;
167 // Called when the inline autocomplete text in the model has been cleared.
168 virtual void OnInlineAutocompleteTextCleared() = 0;
170 // Called when the temporary text has been reverted by the user. This will
171 // reset the user's original selection.
172 virtual void OnRevertTemporaryText() = 0;
174 // Checkpoints the current edit state before an operation that might trigger
175 // a new autocomplete run to open or modify the popup. Call this before
176 // user-initiated edit actions that trigger autocomplete, but *not* for
177 // automatic changes to the textfield that should not affect autocomplete.
178 virtual void OnBeforePossibleChange() = 0;
179 // OnAfterPossibleChange() returns true if there was a change that caused it
180 // to call UpdatePopup().
181 virtual bool OnAfterPossibleChange() = 0;
183 // Returns the gfx::NativeView of the edit view.
184 virtual gfx::NativeView GetNativeView() const = 0;
186 // 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.
190 virtual gfx::NativeView GetRelativeWindowForPopup() const = 0;
192 // Shows |input| as gray suggested text after what the user has typed.
193 virtual void SetGrayTextAutocompletion(const base::string16& input) = 0;
195 // Returns the current gray suggested text.
196 virtual base::string16 GetGrayTextAutocompletion() const = 0;
198 // Returns the width in pixels needed to display the current text. The
199 // returned value includes margins.
200 virtual int GetTextWidth() const = 0;
202 // Returns the omnibox's width in pixels.
203 virtual int GetWidth() const = 0;
205 // Returns true if the user is composing something in an IME.
206 virtual bool IsImeComposing() const = 0;
208 // Returns true if we know for sure that an IME is showing a popup window,
209 // which may overlap the omnibox's popup window.
210 virtual bool IsImeShowingPopup() const;
212 // Returns true if the view is displaying UI that indicates that query
213 // refinement will take place when the user selects the current match. For
214 // search matches, this will cause the omnibox to search over the existing
215 // corpus (e.g. Images) rather than start a new Web search. This method will
216 // only ever return true on mobile ports.
217 virtual bool IsIndicatingQueryRefinement() const;
219 // Returns |text| with any leading javascript schemas stripped.
220 static base::string16 StripJavascriptSchemas(const base::string16& text);
222 // First, calls StripJavascriptSchemas(). Then automatically collapses
223 // internal whitespace as follows:
224 // * If the only whitespace in |text| is newlines, users are most likely
225 // pasting in URLs split into multiple lines by terminals, email programs,
226 // etc. So all newlines are removed.
227 // * Otherwise, users may be pasting in search data, e.g. street addresses. In
228 // this case, runs of whitespace are collapsed down to single spaces.
229 static base::string16 SanitizeTextForPaste(const base::string16& text);
231 // Returns the current clipboard contents as a string that can be pasted in.
232 // In addition to just getting CF_UNICODETEXT out, this can also extract URLs
233 // from bookmarks on the clipboard.
234 static base::string16 GetClipboardText();
236 protected:
237 OmniboxView(Profile* profile,
238 OmniboxEditController* controller,
239 CommandUpdater* command_updater);
241 // Internally invoked whenever the text changes in some way.
242 virtual void TextChanged();
244 // Return the number of characters in the current buffer. The name
245 // |GetTextLength| can't be used as the Windows override of this class
246 // inherits from a class that defines a method with that name.
247 virtual int GetOmniboxTextLength() const = 0;
249 // Try to parse the current text as a URL and colorize the components.
250 virtual void EmphasizeURLComponents() = 0;
252 OmniboxEditController* controller() { return controller_; }
253 const OmniboxEditController* controller() const { return controller_; }
255 private:
256 friend class OmniboxViewMacTest;
257 FRIEND_TEST_ALL_PREFIXES(InstantExtendedTest, ShowURL);
259 // |model_| can be NULL in tests.
260 scoped_ptr<OmniboxEditModel> model_;
261 OmniboxEditController* controller_;
263 // The object that handles additional command functionality exposed on the
264 // edit, such as invoking the keyword editor.
265 CommandUpdater* command_updater_;
268 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_VIEW_H_