Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / omnibox_edit_model.h
blob1e8dcf8e1eadb1e0a0bc661a96146bb1fbdb0c93
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 COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_EDIT_MODEL_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_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 "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/omnibox/browser/autocomplete_controller_delegate.h"
15 #include "components/omnibox/browser/autocomplete_input.h"
16 #include "components/omnibox/browser/autocomplete_match.h"
17 #include "components/omnibox/browser/omnibox_controller.h"
18 #include "components/omnibox/common/omnibox_focus_state.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 OmniboxClient;
26 class OmniboxEditController;
27 class OmniboxPopupModel;
28 class OmniboxView;
30 namespace gfx {
31 class Image;
32 class Rect;
35 // Reasons why the Omnibox could change into keyword mode.
36 // These numeric values are used in UMA logs; do not change them.
37 enum EnteredKeywordModeMethod {
38 ENTERED_KEYWORD_MODE_VIA_TAB = 0,
39 ENTERED_KEYWORD_MODE_VIA_SPACE_AT_END = 1,
40 ENTERED_KEYWORD_MODE_VIA_SPACE_IN_MIDDLE = 2,
41 ENTERED_KEYWORD_MODE_NUM_ITEMS
44 class OmniboxEditModel {
45 public:
46 // Did the Omnibox focus originate via the user clicking on the Omnibox or on
47 // the Fakebox?
48 enum FocusSource {
49 INVALID = 0,
50 OMNIBOX = 1,
51 FAKEBOX = 2
54 struct State {
55 State(bool user_input_in_progress,
56 const base::string16& user_text,
57 const base::string16& gray_text,
58 const base::string16& keyword,
59 bool is_keyword_hint,
60 bool url_replacement_enabled,
61 OmniboxFocusState focus_state,
62 FocusSource focus_source,
63 const AutocompleteInput& autocomplete_input);
64 ~State();
66 bool user_input_in_progress;
67 const base::string16 user_text;
68 const base::string16 gray_text;
69 const base::string16 keyword;
70 const bool is_keyword_hint;
71 bool url_replacement_enabled;
72 OmniboxFocusState focus_state;
73 FocusSource focus_source;
74 const AutocompleteInput autocomplete_input;
77 OmniboxEditModel(OmniboxView* view,
78 OmniboxEditController* controller,
79 scoped_ptr<OmniboxClient> client);
80 virtual ~OmniboxEditModel();
82 // TODO(beaudoin): Remove this accessor when the AutocompleteController has
83 // completely moved to OmniboxController.
84 AutocompleteController* autocomplete_controller() const {
85 return omnibox_controller_->autocomplete_controller();
88 void set_popup_model(OmniboxPopupModel* popup_model) {
89 omnibox_controller_->set_popup_model(popup_model);
92 // TODO: The edit and popup should be siblings owned by the LocationBarView,
93 // making this accessor unnecessary.
94 // NOTE: popup_model() can be NULL for testing.
95 OmniboxPopupModel* popup_model() const {
96 return omnibox_controller_->popup_model();
99 OmniboxEditController* controller() const { return controller_; }
101 OmniboxClient* client() { return client_.get(); }
103 // Returns the current state. This assumes we are switching tabs, and changes
104 // the internal state appropriately.
105 const State GetStateForTabSwitch();
107 // Resets the tab state, then restores local state from the saved |state|.
108 // |state| may be NULL if there is no saved state.
109 void RestoreState(const State* state);
111 // Returns the match for the current text. If the user has not edited the text
112 // this is the match corresponding to the permanent text. Returns the
113 // alternate nav URL, if |alternate_nav_url| is non-NULL and there is such a
114 // URL.
115 AutocompleteMatch CurrentMatch(GURL* alternate_nav_url) const;
117 // Called when the user wants to export the entire current text as a URL.
118 // Sets the url, and if known, the title and favicon.
119 void GetDataForURLExport(GURL* url,
120 base::string16* title,
121 gfx::Image* favicon);
123 // Returns true if the current edit contents will be treated as a
124 // URL/navigation, as opposed to a search.
125 bool CurrentTextIsURL() const;
127 // Returns the match type for the current edit contents.
128 AutocompleteMatch::Type CurrentTextType() const;
130 // Invoked to adjust the text before writting to the clipboard for a copy
131 // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position
132 // of the selection e.g. min(selection_start, selection_end). |text| is the
133 // currently selected text. If |is_all_selected| is true all the text in the
134 // edit is selected. If the url should be copied to the clipboard |write_url|
135 // is set to true and |url| set to the url to write.
136 void AdjustTextForCopy(int sel_min,
137 bool is_all_selected,
138 base::string16* text,
139 GURL* url,
140 bool* write_url);
142 bool user_input_in_progress() const { return user_input_in_progress_; }
144 // Sets the state of user_input_in_progress_, and notifies the observer if
145 // that state has changed.
146 void SetInputInProgress(bool in_progress);
148 // Updates permanent_text_ to the current permanent text from the toolbar
149 // model. Returns true if the permanent text changed and the change should be
150 // immediately user-visible, because either the user is not editing or the
151 // edit does not have focus.
152 bool UpdatePermanentText();
154 // Returns the URL corresponding to the permanent text.
155 GURL PermanentURL();
157 // Sets the user_text_ to |text|. Only the View should call this.
158 void SetUserText(const base::string16& text);
160 // Commits the gray suggested text as if it's been input by the user.
161 // Returns true if the text was committed.
162 // TODO: can the return type be void?
163 bool CommitSuggestedText();
165 // Invoked any time the text may have changed in the edit. Notifies the
166 // controller.
167 void OnChanged();
169 // Reverts the edit model back to its unedited state (permanent text showing,
170 // no user input in progress).
171 void Revert();
173 // Directs the popup to start autocomplete.
174 void StartAutocomplete(bool has_selected_text,
175 bool prevent_inline_autocomplete,
176 bool entering_keyword_mode);
178 // Closes the popup and cancels any pending asynchronous queries.
179 void StopAutocomplete();
181 // Determines whether the user can "paste and go", given the specified text.
182 bool CanPasteAndGo(const base::string16& text) const;
184 // Navigates to the destination last supplied to CanPasteAndGo.
185 void PasteAndGo(const base::string16& text);
187 // Returns true if this is a paste-and-search rather than paste-and-go (or
188 // nothing).
189 bool IsPasteAndSearch(const base::string16& text) const;
191 // Asks the browser to load the popup's currently selected item, using the
192 // supplied disposition. This may close the popup. If |for_drop| is true,
193 // it indicates the input is being accepted as part of a drop operation and
194 // the transition should be treated as LINK (so that it won't trigger the
195 // URL to be autocompleted).
196 void AcceptInput(WindowOpenDisposition disposition,
197 bool for_drop);
199 // Asks the browser to load the item at |index|, with the given properties.
200 // OpenMatch() needs to know the original text that drove this action. If
201 // |pasted_text| is non-empty, this is a Paste-And-Go/Search action, and
202 // that's the relevant input text. Otherwise, the relevant input text is
203 // either the user text or the permanent text, depending on if user input is
204 // in progress.
206 // |match| is passed by value for two reasons:
207 // (1) This function needs to modify |match|, so a const ref isn't
208 // appropriate. Callers don't actually care about the modifications, so a
209 // pointer isn't required.
210 // (2) The passed-in match is, on the caller side, typically coming from data
211 // associated with the popup. Since this call can close the popup, that
212 // could clear that data, leaving us with a pointer-to-garbage. So at
213 // some point someone needs to make a copy of the match anyway, to
214 // preserve it past the popup closure.
215 void OpenMatch(AutocompleteMatch match,
216 WindowOpenDisposition disposition,
217 const GURL& alternate_nav_url,
218 const base::string16& pasted_text,
219 size_t index);
221 OmniboxFocusState focus_state() const { return focus_state_; }
222 bool has_focus() const { return focus_state_ != OMNIBOX_FOCUS_NONE; }
223 bool is_caret_visible() const {
224 return focus_state_ == OMNIBOX_FOCUS_VISIBLE;
227 // Accessors for keyword-related state (see comments on keyword_ and
228 // is_keyword_hint_).
229 const base::string16& keyword() const { return keyword_; }
230 bool is_keyword_hint() const { return is_keyword_hint_; }
231 bool is_keyword_selected() const {
232 return !is_keyword_hint_ && !keyword_.empty();
235 // Accepts the current keyword hint as a keyword. It always returns true for
236 // caller convenience. |entered_method| indicates how the use entered
237 // keyword mode. This parameter is only used for metrics/logging; it's not
238 // used to change user-visible behavior.
239 bool AcceptKeyword(EnteredKeywordModeMethod entered_method);
241 // Accepts the current temporary text as the user text.
242 void AcceptTemporaryTextAsUserText();
244 // Clears the current keyword.
245 void ClearKeyword();
247 // Returns the current autocomplete result. This logic should in the future
248 // live in AutocompleteController but resides here for now. This method is
249 // used by AutomationProvider::AutocompleteEditGetMatches.
250 const AutocompleteResult& result() const {
251 return omnibox_controller_->result();
254 // Called when the view is gaining focus. |control_down| is whether the
255 // control key is down (at the time we're gaining focus).
256 void OnSetFocus(bool control_down);
258 // Sets the visibility of the caret in the omnibox, if it has focus. The
259 // visibility of the caret is reset to visible if either
260 // - The user starts typing, or
261 // - We explicitly focus the omnibox again.
262 // The latter case must be handled in three separate places--OnSetFocus(),
263 // OmniboxView::SetFocus(), and the mouse handlers in OmniboxView. See
264 // accompanying comments for why each of these is necessary.
266 // Caret visibility is tracked per-tab and updates automatically upon
267 // switching tabs.
268 void SetCaretVisibility(bool visible);
270 // Sent before |OnKillFocus| and before the popup is closed.
271 void OnWillKillFocus();
273 // Called when the view is losing focus. Resets some state.
274 void OnKillFocus();
276 // Returns whether the omnibox will handle a press of the escape key. The
277 // caller can use this to decide whether the browser should process escape as
278 // "stop current page load".
279 bool WillHandleEscapeKey() const;
281 // Called when the user presses the escape key. Decides what, if anything, to
282 // revert about any current edits. Returns whether the key was handled.
283 bool OnEscapeKeyPressed();
285 // Called when the user presses or releases the control key. Changes state as
286 // necessary.
287 void OnControlKeyChanged(bool pressed);
289 // Called when the user pastes in text.
290 void OnPaste();
292 // Returns true if pasting is in progress.
293 bool is_pasting() const { return paste_state_ == PASTING; }
295 // TODO(beaudoin): Try not to expose this.
296 bool in_revert() const { return in_revert_; }
298 // Called when the user presses up or down. |count| is a repeat count,
299 // negative for moving up, positive for moving down.
300 virtual void OnUpOrDownKeyPressed(int count);
302 // Called when any relevant data changes. This rolls together several
303 // separate pieces of data into one call so we can update all the UI
304 // efficiently:
305 // |text| is either the new temporary text from the user manually selecting
306 // a different match, or the inline autocomplete text. We distinguish by
307 // checking if |destination_for_temporary_text_change| is NULL.
308 // |destination_for_temporary_text_change| is NULL (if temporary text should
309 // not change) or the pre-change destination URL (if temporary text should
310 // change) so we can save it off to restore later.
311 // |keyword| is the keyword to show a hint for if |is_keyword_hint| is true,
312 // or the currently selected keyword if |is_keyword_hint| is false (see
313 // comments on keyword_ and is_keyword_hint_).
314 void OnPopupDataChanged(
315 const base::string16& text,
316 GURL* destination_for_temporary_text_change,
317 const base::string16& keyword,
318 bool is_keyword_hint);
320 // Called by the OmniboxView after something changes, with details about what
321 // state changes occured. Updates internal state, updates the popup if
322 // necessary, and returns true if any significant changes occurred. Note that
323 // |text_differs| may be set even if |old_text| == |new_text|, e.g. if we've
324 // just committed an IME composition.
326 // If |allow_keyword_ui_change| is false then the change should not affect
327 // keyword ui state, even if the text matches a keyword exactly. This value
328 // may be false when the user is composing a text with an IME.
329 bool OnAfterPossibleChange(const base::string16& old_text,
330 const base::string16& new_text,
331 size_t selection_start,
332 size_t selection_end,
333 bool selection_differs,
334 bool text_differs,
335 bool just_deleted_text,
336 bool allow_keyword_ui_change);
338 // Called when the current match has changed in the OmniboxController.
339 void OnCurrentMatchChanged();
341 // Name of the histogram tracking cut or copy omnibox commands.
342 static const char kCutOrCopyAllTextHistogram[];
344 private:
345 friend class OmniboxControllerTest;
347 enum PasteState {
348 NONE, // Most recent edit was not a paste.
349 PASTING, // In the middle of doing a paste. We need this intermediate
350 // state because OnPaste() does the actual detection of
351 // paste, but OnAfterPossibleChange() has to update the
352 // paste state for every edit. If OnPaste() set the state
353 // directly to PASTED, OnAfterPossibleChange() wouldn't know
354 // whether that represented the current edit or a past one.
355 PASTED, // Most recent edit was a paste.
358 enum ControlKeyState {
359 UP, // The control key is not depressed.
360 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's
361 // contents/selection have not changed since it was
362 // depressed. This is the only state in which we
363 // do the "ctrl-enter" behavior when the user hits
364 // enter.
365 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's
366 // contents/selection have changed since it was
367 // depressed. If the user now hits enter, we assume
368 // he simply hasn't released the key, rather than that
369 // he intended to hit "ctrl-enter".
372 // Returns true if a query to an autocomplete provider is currently
373 // in progress. This logic should in the future live in
374 // AutocompleteController but resides here for now. This method is used by
375 // AutomationProvider::AutocompleteEditIsQueryInProgress.
376 bool query_in_progress() const;
378 // Called whenever user_text_ should change.
379 void InternalSetUserText(const base::string16& text);
381 // Turns off keyword mode for the current match.
382 void ClearPopupKeywordMode() const;
384 // Conversion between user text and display text. User text is the text the
385 // user has input. Display text is the text being shown in the edit. The
386 // two are different if a keyword is selected.
387 base::string16 DisplayTextFromUserText(const base::string16& text) const;
388 base::string16 UserTextFromDisplayText(const base::string16& text) const;
390 // If there's a selected match, copies it into |match|. Else, returns the
391 // default match for the current text, as well as the alternate nav URL, if
392 // |alternate_nav_url| is non-NULL and there is such a URL.
393 void GetInfoForCurrentText(AutocompleteMatch* match,
394 GURL* alternate_nav_url) const;
396 // Reverts the edit box from a temporary text back to the original user text.
397 // If |revert_popup| is true then the popup will be reverted as well.
398 void RevertTemporaryText(bool revert_popup);
400 // Accepts current keyword if the user just typed a space at the end of
401 // |new_text|. This handles both of the following cases:
402 // (assume "foo" is a keyword, | is the input caret, [] is selected text)
403 // foo| -> foo | (a space was appended to a keyword)
404 // foo[bar] -> foo | (a space replaced other text after a keyword)
405 // Returns true if the current keyword is accepted.
406 bool MaybeAcceptKeywordBySpace(const base::string16& new_text);
408 // Checks whether the user inserted a space into |old_text| and by doing so
409 // created a |new_text| that looks like "<keyword> <search phrase>".
410 bool CreatedKeywordSearchByInsertingSpaceInMiddle(
411 const base::string16& old_text,
412 const base::string16& new_text,
413 size_t caret_position) const;
415 // Checks if a given character is a valid space character for accepting
416 // keyword.
417 static bool IsSpaceCharForAcceptingKeyword(wchar_t c);
419 // Classify the current page being viewed as, for example, the new tab
420 // page or a normal web page. Used for logging omnibox events for
421 // UMA opted-in users. Examines the user's profile to determine if the
422 // current page is the user's home page.
423 metrics::OmniboxEventProto::PageClassification ClassifyPage() const;
425 // Sets |match| and |alternate_nav_url| based on classifying |text|.
426 // |alternate_nav_url| may be NULL.
427 void ClassifyStringForPasteAndGo(const base::string16& text,
428 AutocompleteMatch* match,
429 GURL* alternate_nav_url) const;
431 // If focus_state_ does not match |state|, we update it and notify the
432 // InstantController about the change (passing along the |reason| for the
433 // change). If the caret visibility changes, we call ApplyCaretVisibility() on
434 // the view.
435 void SetFocusState(OmniboxFocusState state, OmniboxFocusChangeReason reason);
437 // NOTE: |client_| must outlive |omnibox_controller_|, as the latter has a
438 // reference to the former.
439 scoped_ptr<OmniboxClient> client_;
441 scoped_ptr<OmniboxController> omnibox_controller_;
443 OmniboxView* view_;
445 OmniboxEditController* controller_;
447 OmniboxFocusState focus_state_;
449 // Used to keep track whether the input currently in progress originated by
450 // focusing in the Omnibox or in the Fakebox. This will be INVALID if no input
451 // is in progress or the Omnibox is not focused.
452 FocusSource focus_source_;
454 // The URL of the currently displayed page.
455 base::string16 permanent_text_;
457 // This flag is true when the user has modified the contents of the edit, but
458 // not yet accepted them. We use this to determine when we need to save
459 // state (on switching tabs) and whether changes to the page URL should be
460 // immediately displayed.
461 // This flag will be true in a superset of the cases where the popup is open.
462 bool user_input_in_progress_;
464 // The text that the user has entered. This does not include inline
465 // autocomplete text that has not yet been accepted.
466 base::string16 user_text_;
468 // We keep track of when the user last focused on the omnibox.
469 base::TimeTicks last_omnibox_focus_;
471 // Whether any user input has occurred since focusing on the omnibox. This is
472 // used along with |last_omnibox_focus_| to calculate the time between a user
473 // focusing on the omnibox and editing. It is initialized to true since
474 // there was no focus event.
475 bool user_input_since_focus_;
477 // We keep track of when the user began modifying the omnibox text.
478 // This should be valid whenever user_input_in_progress_ is true.
479 base::TimeTicks time_user_first_modified_omnibox_;
481 // When the user closes the popup, we need to remember the URL for their
482 // desired choice, so that if they hit enter without reopening the popup we
483 // know where to go. We could simply rerun autocomplete in this case, but
484 // we'd need to either wait for all results to come in (unacceptably slow) or
485 // do the wrong thing when the user had chosen some provider whose results
486 // were not returned instantaneously.
488 // This variable is only valid when user_input_in_progress_ is true, since
489 // when it is false the user has either never input anything (so there won't
490 // be a value here anyway) or has canceled their input, which should be
491 // treated the same way. Also, since this is for preserving a desired URL
492 // after the popup has been closed, we ignore this if the popup is open, and
493 // simply ask the popup for the desired URL directly. As a result, the
494 // contents of this variable only need to be updated when the popup is closed
495 // but user_input_in_progress_ is not being cleared.
496 base::string16 url_for_remembered_user_selection_;
498 // Inline autocomplete is allowed if the user has not just deleted text, and
499 // no temporary text is showing. In this case, inline_autocomplete_text_ is
500 // appended to the user_text_ and displayed selected (at least initially).
502 // NOTE: When the popup is closed there should never be inline autocomplete
503 // text (actions that close the popup should either accept the text, convert
504 // it to a normal selection, or change the edit entirely).
505 bool just_deleted_text_;
506 base::string16 inline_autocomplete_text_;
508 // Used by OnPopupDataChanged to keep track of whether there is currently a
509 // temporary text.
511 // Example of use: If the user types "goog", then arrows down in the
512 // autocomplete popup until, say, "google.com" appears in the edit box, then
513 // the user_text_ is still "goog", and "google.com" is "temporary text".
514 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again
515 // and the popup is closed and "goog" is replaced by the permanent_text_,
516 // which is the URL of the current page.
518 // original_url_ is only valid when there is temporary text, and is used as
519 // the unique identifier of the originally selected item. Thus, if the user
520 // arrows to a different item with the same text, we can still distinguish
521 // them and not revert all the way to the permanent_text_.
522 bool has_temporary_text_;
523 GURL original_url_;
525 // When the user's last action was to paste, we disallow inline autocomplete
526 // (on the theory that the user is trying to paste in a new URL or part of
527 // one, and in either case inline autocomplete would get in the way).
528 PasteState paste_state_;
530 // Whether the control key is depressed. We track this to avoid calling
531 // UpdatePopup() repeatedly if the user holds down the key, and to know
532 // whether to trigger "ctrl-enter" behavior.
533 ControlKeyState control_key_state_;
535 // The keyword associated with the current match. The user may have an actual
536 // selected keyword, or just some input text that looks like a keyword (so we
537 // can show a hint to press <tab>). This is the keyword in either case;
538 // is_keyword_hint_ (below) distinguishes the two cases.
539 base::string16 keyword_;
541 // True if the keyword associated with this match is merely a hint, i.e. the
542 // user hasn't actually selected a keyword yet. When this is true, we can use
543 // keyword_ to show a "Press <tab> to search" sort of hint.
544 bool is_keyword_hint_;
546 // This is needed to properly update the SearchModel state when the user
547 // presses escape.
548 bool in_revert_;
550 // Indicates if the upcoming autocomplete search is allowed to be treated as
551 // an exact keyword match. If this is true then keyword mode will be
552 // triggered automatically if the input is "<keyword> <search string>". We
553 // allow this when CreatedKeywordSearchByInsertingSpaceInMiddle() is true.
554 // This has no effect if we're already in keyword mode.
555 bool allow_exact_keyword_match_;
557 // The input that was sent to the AutocompleteController. Since no
558 // autocomplete query is started after a tab switch, it is possible for this
559 // |input_| to differ from the one currently stored in AutocompleteController.
560 AutocompleteInput input_;
562 DISALLOW_COPY_AND_ASSIGN(OmniboxEditModel);
565 #endif // COMPONENTS_OMNIBOX_BROWSER_OMNIBOX_EDIT_MODEL_H_