Omnibox UMA Logging: Add Instant New Tab Page as a Classification
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_edit_model.h
blob57e0d3c4d7302ec9012dc5a8dcf69e3573a20828
1 // Copyright 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 #ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_
6 #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h"
14 #include "chrome/browser/autocomplete/autocomplete_match.h"
15 #include "chrome/browser/ui/omnibox/omnibox_controller.h"
16 #include "chrome/common/metrics/proto/omnibox_event.pb.h"
17 #include "chrome/common/omnibox_focus_state.h"
18 #include "content/public/common/page_transition_types.h"
19 #include "ui/base/window_open_disposition.h"
20 #include "ui/gfx/native_widget_types.h"
21 #include "url/gurl.h"
23 class AutocompleteController;
24 class AutocompleteResult;
25 class OmniboxCurrentPageDelegate;
26 class OmniboxEditController;
27 class OmniboxPopupModel;
28 class OmniboxView;
29 class Profile;
31 namespace gfx {
32 class Image;
33 class Rect;
36 // Reasons why the Omnibox could change into keyword mode.
37 // These numeric values are used in UMA logs; do not change them.
38 enum EnteredKeywordModeMethod {
39 ENTERED_KEYWORD_MODE_VIA_TAB = 0,
40 ENTERED_KEYWORD_MODE_VIA_SPACE_AT_END = 1,
41 ENTERED_KEYWORD_MODE_VIA_SPACE_IN_MIDDLE = 2,
42 ENTERED_KEYWORD_MODE_NUM_ITEMS
45 class OmniboxEditModel {
46 public:
47 struct State {
48 State(bool user_input_in_progress,
49 const string16& user_text,
50 const string16& gray_text,
51 const string16& keyword,
52 bool is_keyword_hint,
53 OmniboxFocusState focus_state);
54 ~State();
56 bool user_input_in_progress;
57 const string16 user_text;
58 const string16 gray_text;
59 const string16 keyword;
60 const bool is_keyword_hint;
61 OmniboxFocusState focus_state;
64 OmniboxEditModel(OmniboxView* view,
65 OmniboxEditController* controller,
66 Profile* profile);
67 virtual ~OmniboxEditModel();
69 // TODO(beaudoin): Remove this accessor when the AutocompleteController has
70 // completely moved to OmniboxController.
71 AutocompleteController* autocomplete_controller() const {
72 return omnibox_controller_->autocomplete_controller();
75 void set_popup_model(OmniboxPopupModel* popup_model) {
76 omnibox_controller_->set_popup_model(popup_model);
79 // TODO: The edit and popup should be siblings owned by the LocationBarView,
80 // making this accessor unnecessary.
81 OmniboxPopupModel* popup_model() const {
82 return omnibox_controller_->popup_model();
85 OmniboxEditController* controller() const { return controller_; }
87 Profile* profile() const { return profile_; }
89 // Returns the current state. This assumes we are switching tabs, and changes
90 // the internal state appropriately.
91 const State GetStateForTabSwitch();
93 // Restores local state from the saved |state|.
94 void RestoreState(const State& state);
96 // Returns the match for the current text. If the user has not edited the text
97 // this is the match corresponding to the permanent text. Returns the
98 // alternate nav URL, if |alternate_nav_url| is non-NULL and there is such a
99 // URL.
100 AutocompleteMatch CurrentMatch(GURL* alternate_nav_url) const;
102 // Called when the user wants to export the entire current text as a URL.
103 // Sets the url, and if known, the title and favicon.
104 void GetDataForURLExport(GURL* url, string16* title, gfx::Image* favicon);
106 // Returns true if the current edit contents will be treated as a
107 // URL/navigation, as opposed to a search.
108 bool CurrentTextIsURL() const;
110 // Returns the match type for the current edit contents.
111 AutocompleteMatch::Type CurrentTextType() const;
113 // Invoked to adjust the text before writting to the clipboard for a copy
114 // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position
115 // of the selection e.g. min(selection_start, selection_end). |text| is the
116 // currently selected text. If |is_all_selected| is true all the text in the
117 // edit is selected. If the url should be copied to the clipboard |write_url|
118 // is set to true and |url| set to the url to write.
119 void AdjustTextForCopy(int sel_min,
120 bool is_all_selected,
121 string16* text,
122 GURL* url,
123 bool* write_url);
125 bool user_input_in_progress() const { return user_input_in_progress_; }
127 // Sets the state of user_input_in_progress_, and notifies the observer if
128 // that state has changed.
129 void SetInputInProgress(bool in_progress);
131 // Updates permanent_text_ to |new_permanent_text|. Returns true if this
132 // change should be immediately user-visible, because either the user is not
133 // editing or the edit does not have focus.
134 bool UpdatePermanentText(const string16& new_permanent_text);
136 // Returns the URL corresponding to the permanent text.
137 GURL PermanentURL();
139 // Sets the user_text_ to |text|. Only the View should call this.
140 void SetUserText(const string16& text);
142 // Commits the gray suggested text as if it's been input by the user.
143 // Returns true if the text was committed.
144 // TODO: can the return type be void?
145 bool CommitSuggestedText();
147 // Invoked any time the text may have changed in the edit. Notifies the
148 // controller.
149 void OnChanged();
151 // Reverts the edit model back to its unedited state (permanent text showing,
152 // no user input in progress).
153 void Revert();
155 // Directs the popup to start autocomplete.
156 void StartAutocomplete(bool has_selected_text,
157 bool prevent_inline_autocomplete) const;
159 // Closes the popup and cancels any pending asynchronous queries.
160 void StopAutocomplete();
162 // Determines whether the user can "paste and go", given the specified text.
163 bool CanPasteAndGo(const string16& text) const;
165 // Navigates to the destination last supplied to CanPasteAndGo.
166 void PasteAndGo(const string16& text);
168 // Returns true if this is a paste-and-search rather than paste-and-go (or
169 // nothing).
170 bool IsPasteAndSearch(const string16& text) const;
172 // Asks the browser to load the popup's currently selected item, using the
173 // supplied disposition. This may close the popup. If |for_drop| is true,
174 // it indicates the input is being accepted as part of a drop operation and
175 // the transition should be treated as LINK (so that it won't trigger the
176 // URL to be autocompleted).
177 void AcceptInput(WindowOpenDisposition disposition,
178 bool for_drop);
180 // Asks the browser to load the item at |index|, with the given properties.
181 void OpenMatch(const AutocompleteMatch& match,
182 WindowOpenDisposition disposition,
183 const GURL& alternate_nav_url,
184 size_t index);
186 OmniboxFocusState focus_state() const { return focus_state_; }
187 bool has_focus() const { return focus_state_ != OMNIBOX_FOCUS_NONE; }
188 bool is_caret_visible() const {
189 return focus_state_ == OMNIBOX_FOCUS_VISIBLE;
192 // Accessors for keyword-related state (see comments on keyword_ and
193 // is_keyword_hint_).
194 const string16& keyword() const { return keyword_; }
195 bool is_keyword_hint() const { return is_keyword_hint_; }
197 // Accepts the current keyword hint as a keyword. It always returns true for
198 // caller convenience. |entered_method| indicates how the use entered
199 // keyword mode. This parameter is only used for metrics/logging; it's not
200 // used to change user-visible behavior.
201 bool AcceptKeyword(EnteredKeywordModeMethod entered_method);
203 // Accepts the current temporary text as the user text.
204 void AcceptTemporaryTextAsUserText();
206 // Clears the current keyword. |visible_text| is the (non-keyword) text
207 // currently visible in the edit.
208 void ClearKeyword(const string16& visible_text);
210 // Returns the current autocomplete result. This logic should in the future
211 // live in AutocompleteController but resides here for now. This method is
212 // used by AutomationProvider::AutocompleteEditGetMatches.
213 const AutocompleteResult& result() const {
214 return omnibox_controller_->result();
217 // Called when the view is gaining focus. |control_down| is whether the
218 // control key is down (at the time we're gaining focus).
219 void OnSetFocus(bool control_down);
221 // Sets the visibility of the caret in the omnibox, if it has focus. The
222 // visibility of the caret is reset to visible if either
223 // - The user starts typing, or
224 // - We explicitly focus the omnibox again.
225 // The latter case must be handled in three separate places--OnSetFocus(),
226 // OmniboxView::SetFocus(), and the mouse handlers in OmniboxView. See
227 // accompanying comments for why each of these is necessary.
229 // Caret visibility is tracked per-tab and updates automatically upon
230 // switching tabs.
231 void SetCaretVisibility(bool visible);
233 // Sent before |OnKillFocus| and before the popup is closed.
234 void OnWillKillFocus(gfx::NativeView view_gaining_focus);
236 // Called when the view is losing focus. Resets some state.
237 void OnKillFocus();
239 // Called when the user presses the escape key. Decides what, if anything, to
240 // revert about any current edits. Returns whether the key was handled.
241 bool OnEscapeKeyPressed();
243 // Called when the user presses or releases the control key. Changes state as
244 // necessary.
245 void OnControlKeyChanged(bool pressed);
247 // Called when the user pastes in text.
248 void on_paste() { paste_state_ = PASTING; }
250 // Returns true if pasting is in progress.
251 bool is_pasting() const { return paste_state_ == PASTING; }
253 // TODO(beaudoin): Try not to expose this.
254 bool in_revert() const { return in_revert_; }
256 // Called when the user presses up or down. |count| is a repeat count,
257 // negative for moving up, positive for moving down.
258 virtual void OnUpOrDownKeyPressed(int count);
260 // Called when any relevant data changes. This rolls together several
261 // separate pieces of data into one call so we can update all the UI
262 // efficiently:
263 // |text| is either the new temporary text from the user manually selecting
264 // a different match, or the inline autocomplete text. We distinguish by
265 // checking if |destination_for_temporary_text_change| is NULL.
266 // |destination_for_temporary_text_change| is NULL (if temporary text should
267 // not change) or the pre-change destination URL (if temporary text should
268 // change) so we can save it off to restore later.
269 // |keyword| is the keyword to show a hint for if |is_keyword_hint| is true,
270 // or the currently selected keyword if |is_keyword_hint| is false (see
271 // comments on keyword_ and is_keyword_hint_).
272 void OnPopupDataChanged(
273 const string16& text,
274 GURL* destination_for_temporary_text_change,
275 const string16& keyword,
276 bool is_keyword_hint);
278 // Called by the OmniboxView after something changes, with details about what
279 // state changes occured. Updates internal state, updates the popup if
280 // necessary, and returns true if any significant changes occurred. Note that
281 // |text_differs| may be set even if |old_text| == |new_text|, e.g. if we've
282 // just committed an IME composition.
284 // If |allow_keyword_ui_change| is false then the change should not affect
285 // keyword ui state, even if the text matches a keyword exactly. This value
286 // may be false when the user is composing a text with an IME.
287 bool OnAfterPossibleChange(const string16& old_text,
288 const string16& new_text,
289 size_t selection_start,
290 size_t selection_end,
291 bool selection_differs,
292 bool text_differs,
293 bool just_deleted_text,
294 bool allow_keyword_ui_change);
296 // Called when the current match has changed in the OmniboxController.
297 void OnCurrentMatchChanged();
299 // Access the current view text.
300 string16 GetViewText() const;
302 string16 user_text() const { return user_text_; }
304 // TODO(beaudoin): We need this to allow OmniboxController access the
305 // InstantController via OmniboxEditController, because the only valid pointer
306 // to InstantController is kept in Browser. We should try to get rid of this,
307 // maybe by ensuring InstantController lives as long as Browser.
308 InstantController* GetInstantController() const;
310 private:
311 friend class OmniboxControllerTest;
313 enum PasteState {
314 NONE, // Most recent edit was not a paste.
315 PASTING, // In the middle of doing a paste. We need this intermediate
316 // state because OnPaste() does the actual detection of
317 // paste, but OnAfterPossibleChange() has to update the
318 // paste state for every edit. If OnPaste() set the state
319 // directly to PASTED, OnAfterPossibleChange() wouldn't know
320 // whether that represented the current edit or a past one.
321 PASTED, // Most recent edit was a paste.
324 enum ControlKeyState {
325 UP, // The control key is not depressed.
326 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's
327 // contents/selection have not changed since it was
328 // depressed. This is the only state in which we
329 // do the "ctrl-enter" behavior when the user hits
330 // enter.
331 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's
332 // contents/selection have changed since it was
333 // depressed. If the user now hits enter, we assume
334 // he simply hasn't released the key, rather than that
335 // he intended to hit "ctrl-enter".
338 // Returns true if a query to an autocomplete provider is currently
339 // in progress. This logic should in the future live in
340 // AutocompleteController but resides here for now. This method is used by
341 // AutomationProvider::AutocompleteEditIsQueryInProgress.
342 bool query_in_progress() const;
344 // Called whenever user_text_ should change.
345 void InternalSetUserText(const string16& text);
347 // Returns true if a keyword is selected.
348 bool KeywordIsSelected() const;
350 // Turns off keyword mode for the current match.
351 void ClearPopupKeywordMode() const;
353 // Conversion between user text and display text. User text is the text the
354 // user has input. Display text is the text being shown in the edit. The
355 // two are different if a keyword is selected.
356 string16 DisplayTextFromUserText(const string16& text) const;
357 string16 UserTextFromDisplayText(const string16& text) const;
359 // If there's a selected match, copies it into |match|. Else, returns the
360 // default match for the current text, as well as the alternate nav URL, if
361 // |alternate_nav_url| is non-NULL and there is such a URL.
362 void GetInfoForCurrentText(AutocompleteMatch* match,
363 GURL* alternate_nav_url) const;
365 // Reverts the edit box from a temporary text back to the original user text.
366 // If |revert_popup| is true then the popup will be reverted as well.
367 void RevertTemporaryText(bool revert_popup);
369 // Accepts current keyword if the user just typed a space at the end of
370 // |new_text|. This handles both of the following cases:
371 // (assume "foo" is a keyword, | is the input caret, [] is selected text)
372 // foo| -> foo | (a space was appended to a keyword)
373 // foo[bar] -> foo | (a space replaced other text after a keyword)
374 // Returns true if the current keyword is accepted.
375 bool MaybeAcceptKeywordBySpace(const string16& new_text);
377 // Checks whether the user inserted a space into |old_text| and by doing so
378 // created a |new_text| that looks like "<keyword> <search phrase>".
379 bool CreatedKeywordSearchByInsertingSpaceInMiddle(
380 const string16& old_text,
381 const string16& new_text,
382 size_t caret_position) const;
384 // Checks if a given character is a valid space character for accepting
385 // keyword.
386 static bool IsSpaceCharForAcceptingKeyword(wchar_t c);
388 // Classify the current page being viewed as, for example, the new tab
389 // page or a normal web page. Used for logging omnibox events for
390 // UMA opted-in users. Examines the user's profile to determine if the
391 // current page is the user's home page.
392 metrics::OmniboxEventProto::PageClassification ClassifyPage() const;
394 // Sets |match| and |alternate_nav_url| based on classifying |text|.
395 // |alternate_nav_url| may be NULL.
396 void ClassifyStringForPasteAndGo(const string16& text,
397 AutocompleteMatch* match,
398 GURL* alternate_nav_url) const;
400 // If focus_state_ does not match |state|, we update it and notify the
401 // InstantController about the change (passing along the |reason| for the
402 // change). If the caret visibility changes, we call ApplyCaretVisibility() on
403 // the view.
404 void SetFocusState(OmniboxFocusState state, OmniboxFocusChangeReason reason);
406 scoped_ptr<OmniboxController> omnibox_controller_;
408 OmniboxView* view_;
410 OmniboxEditController* controller_;
412 scoped_ptr<OmniboxCurrentPageDelegate> delegate_;
414 OmniboxFocusState focus_state_;
416 // The URL of the currently displayed page.
417 string16 permanent_text_;
419 // This flag is true when the user has modified the contents of the edit, but
420 // not yet accepted them. We use this to determine when we need to save
421 // state (on switching tabs) and whether changes to the page URL should be
422 // immediately displayed.
423 // This flag will be true in a superset of the cases where the popup is open.
424 bool user_input_in_progress_;
426 // The text that the user has entered. This does not include inline
427 // autocomplete text that has not yet been accepted.
428 string16 user_text_;
430 // We keep track of when the user last focused on the omnibox, but reset it
431 // to null when user input occurs.
432 base::TimeTicks last_omnibox_focus_without_user_input_;
434 // We keep track of when the user began modifying the omnibox text.
435 // This should be valid whenever user_input_in_progress_ is true.
436 base::TimeTicks time_user_first_modified_omnibox_;
438 // When the user closes the popup, we need to remember the URL for their
439 // desired choice, so that if they hit enter without reopening the popup we
440 // know where to go. We could simply rerun autocomplete in this case, but
441 // we'd need to either wait for all results to come in (unacceptably slow) or
442 // do the wrong thing when the user had chosen some provider whose results
443 // were not returned instantaneously.
445 // This variable is only valid when user_input_in_progress_ is true, since
446 // when it is false the user has either never input anything (so there won't
447 // be a value here anyway) or has canceled their input, which should be
448 // treated the same way. Also, since this is for preserving a desired URL
449 // after the popup has been closed, we ignore this if the popup is open, and
450 // simply ask the popup for the desired URL directly. As a result, the
451 // contents of this variable only need to be updated when the popup is closed
452 // but user_input_in_progress_ is not being cleared.
453 string16 url_for_remembered_user_selection_;
455 // Inline autocomplete is allowed if the user has not just deleted text, and
456 // no temporary text is showing. In this case, inline_autocomplete_text_ is
457 // appended to the user_text_ and displayed selected (at least initially).
459 // NOTE: When the popup is closed there should never be inline autocomplete
460 // text (actions that close the popup should either accept the text, convert
461 // it to a normal selection, or change the edit entirely).
462 bool just_deleted_text_;
463 string16 inline_autocomplete_text_;
465 // Used by OnPopupDataChanged to keep track of whether there is currently a
466 // temporary text.
468 // Example of use: If the user types "goog", then arrows down in the
469 // autocomplete popup until, say, "google.com" appears in the edit box, then
470 // the user_text_ is still "goog", and "google.com" is "temporary text".
471 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again
472 // and the popup is closed and "goog" is replaced by the permanent_text_,
473 // which is the URL of the current page.
475 // original_url_ is only valid when there is temporary text, and is used as
476 // the unique identifier of the originally selected item. Thus, if the user
477 // arrows to a different item with the same text, we can still distinguish
478 // them and not revert all the way to the permanent_text_.
479 bool has_temporary_text_;
480 GURL original_url_;
482 // When the user's last action was to paste, we disallow inline autocomplete
483 // (on the theory that the user is trying to paste in a new URL or part of
484 // one, and in either case inline autocomplete would get in the way).
485 PasteState paste_state_;
487 // Whether the control key is depressed. We track this to avoid calling
488 // UpdatePopup() repeatedly if the user holds down the key, and to know
489 // whether to trigger "ctrl-enter" behavior.
490 ControlKeyState control_key_state_;
492 // The keyword associated with the current match. The user may have an actual
493 // selected keyword, or just some input text that looks like a keyword (so we
494 // can show a hint to press <tab>). This is the keyword in either case;
495 // is_keyword_hint_ (below) distinguishes the two cases.
496 string16 keyword_;
498 // True if the keyword associated with this match is merely a hint, i.e. the
499 // user hasn't actually selected a keyword yet. When this is true, we can use
500 // keyword_ to show a "Press <tab> to search" sort of hint.
501 bool is_keyword_hint_;
503 Profile* profile_;
505 // This is needed to properly update the SearchModel state when the user
506 // presses escape.
507 bool in_revert_;
509 // Indicates if the upcoming autocomplete search is allowed to be treated as
510 // an exact keyword match. If this is true then keyword mode will be
511 // triggered automatically if the input is "<keyword> <search string>". We
512 // allow this when CreatedKeywordSearchByInsertingSpaceInMiddle() is true.
513 // This has no effect if we're already in keyword mode.
514 bool allow_exact_keyword_match_;
516 DISALLOW_COPY_AND_ASSIGN(OmniboxEditModel);
519 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_