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. 4 5 #ifndef CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_ 6 #define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_ 7 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/ui/omnibox/omnibox_controller.h" 15 #include "chrome/common/instant_types.h" 16 #include "chrome/common/omnibox_focus_state.h" 17 #include "components/metrics/proto/omnibox_event.pb.h" 18 #include "components/omnibox/autocomplete_input.h" 19 #include "components/omnibox/autocomplete_match.h" 20 #include "ui/base/window_open_disposition.h" 21 #include "ui/gfx/native_widget_types.h" 22 #include "url/gurl.h" 23 24 class AutocompleteController; 25 class AutocompleteResult; 26 class OmniboxCurrentPageDelegate; 27 class OmniboxEditController; 28 class OmniboxPopupModel; 29 class OmniboxView; 30 class Profile; 31 32 namespace gfx { 33 class Image; 34 class Rect; 35 } 36 37 // Reasons why the Omnibox could change into keyword mode. 38 // These numeric values are used in UMA logs; do not change them. 39 enum EnteredKeywordModeMethod { 40 ENTERED_KEYWORD_MODE_VIA_TAB = 0, 41 ENTERED_KEYWORD_MODE_VIA_SPACE_AT_END = 1, 42 ENTERED_KEYWORD_MODE_VIA_SPACE_IN_MIDDLE = 2, 43 ENTERED_KEYWORD_MODE_NUM_ITEMS 44 }; 45 46 class OmniboxEditModel { 47 public: 48 // Did the Omnibox focus originate via the user clicking on the Omnibox or on 49 // the Fakebox? 50 enum FocusSource { 51 INVALID = 0, 52 OMNIBOX = 1, 53 FAKEBOX = 2 54 }; 55 56 struct State { 57 State(bool user_input_in_progress, 58 const base::string16& user_text, 59 const base::string16& gray_text, 60 const base::string16& keyword, 61 bool is_keyword_hint, 62 bool url_replacement_enabled, 63 OmniboxFocusState focus_state, 64 FocusSource focus_source, 65 const AutocompleteInput& autocomplete_input); 66 ~State(); 67 68 bool user_input_in_progress; 69 const base::string16 user_text; 70 const base::string16 gray_text; 71 const base::string16 keyword; 72 const bool is_keyword_hint; 73 bool url_replacement_enabled; 74 OmniboxFocusState focus_state; 75 FocusSource focus_source; 76 const AutocompleteInput autocomplete_input; 77 }; 78 79 OmniboxEditModel(OmniboxView* view, 80 OmniboxEditController* controller, 81 Profile* profile); 82 virtual ~OmniboxEditModel(); 83 84 // TODO(beaudoin): Remove this accessor when the AutocompleteController has 85 // completely moved to OmniboxController. 86 AutocompleteController* autocomplete_controller() const { 87 return omnibox_controller_->autocomplete_controller(); 88 } 89 90 void set_popup_model(OmniboxPopupModel* popup_model) { 91 omnibox_controller_->set_popup_model(popup_model); 92 } 93 94 // TODO: The edit and popup should be siblings owned by the LocationBarView, 95 // making this accessor unnecessary. 96 // NOTE: popup_model() can be NULL for testing. 97 OmniboxPopupModel* popup_model() const { 98 return omnibox_controller_->popup_model(); 99 } 100 101 OmniboxEditController* controller() const { return controller_; } 102 103 Profile* profile() const { return profile_; } 104 105 // Returns the current state. This assumes we are switching tabs, and changes 106 // the internal state appropriately. 107 const State GetStateForTabSwitch(); 108 109 // Resets the tab state, then restores local state from the saved |state|. 110 // |state| may be NULL if there is no saved state. 111 void RestoreState(const State* state); 112 113 // Returns the match for the current text. If the user has not edited the text 114 // this is the match corresponding to the permanent text. Returns the 115 // alternate nav URL, if |alternate_nav_url| is non-NULL and there is such a 116 // URL. 117 AutocompleteMatch CurrentMatch(GURL* alternate_nav_url) const; 118 119 // Called when the user wants to export the entire current text as a URL. 120 // Sets the url, and if known, the title and favicon. 121 void GetDataForURLExport(GURL* url, 122 base::string16* title, 123 gfx::Image* favicon); 124 125 // Returns true if the current edit contents will be treated as a 126 // URL/navigation, as opposed to a search. 127 bool CurrentTextIsURL() const; 128 129 // Returns the match type for the current edit contents. 130 AutocompleteMatch::Type CurrentTextType() const; 131 132 // Invoked to adjust the text before writting to the clipboard for a copy 133 // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position 134 // of the selection e.g. min(selection_start, selection_end). |text| is the 135 // currently selected text. If |is_all_selected| is true all the text in the 136 // edit is selected. If the url should be copied to the clipboard |write_url| 137 // is set to true and |url| set to the url to write. 138 void AdjustTextForCopy(int sel_min, 139 bool is_all_selected, 140 base::string16* text, 141 GURL* url, 142 bool* write_url); 143 144 bool user_input_in_progress() const { return user_input_in_progress_; } 145 146 // Sets the state of user_input_in_progress_, and notifies the observer if 147 // that state has changed. 148 void SetInputInProgress(bool in_progress); 149 150 // Updates permanent_text_ to the current permanent text from the toolbar 151 // model. Returns true if the permanent text changed and the change should be 152 // immediately user-visible, because either the user is not editing or the 153 // edit does not have focus. 154 bool UpdatePermanentText(); 155 156 // Returns the URL corresponding to the permanent text. 157 GURL PermanentURL(); 158 159 // Sets the user_text_ to |text|. Only the View should call this. 160 void SetUserText(const base::string16& text); 161 162 // Commits the gray suggested text as if it's been input by the user. 163 // Returns true if the text was committed. 164 // TODO: can the return type be void? 165 bool CommitSuggestedText(); 166 167 // Invoked any time the text may have changed in the edit. Notifies the 168 // controller. 169 void OnChanged(); 170 171 // Reverts the edit model back to its unedited state (permanent text showing, 172 // no user input in progress). 173 void Revert(); 174 175 // Directs the popup to start autocomplete. 176 void StartAutocomplete(bool has_selected_text, 177 bool prevent_inline_autocomplete); 178 179 // Closes the popup and cancels any pending asynchronous queries. 180 void StopAutocomplete(); 181 182 // Determines whether the user can "paste and go", given the specified text. 183 bool CanPasteAndGo(const base::string16& text) const; 184 185 // Navigates to the destination last supplied to CanPasteAndGo. 186 void PasteAndGo(const base::string16& text); 187 188 // Returns true if this is a paste-and-search rather than paste-and-go (or 189 // nothing). 190 bool IsPasteAndSearch(const base::string16& text) const; 191 192 // Asks the browser to load the popup's currently selected item, using the 193 // supplied disposition. This may close the popup. If |for_drop| is true, 194 // it indicates the input is being accepted as part of a drop operation and 195 // the transition should be treated as LINK (so that it won't trigger the 196 // URL to be autocompleted). 197 void AcceptInput(WindowOpenDisposition disposition, 198 bool for_drop); 199 200 // Asks the browser to load the item at |index|, with the given properties. 201 // OpenMatch() needs to know the original text that drove this action. If 202 // |pasted_text| is non-empty, this is a Paste-And-Go/Search action, and 203 // that's the relevant input text. Otherwise, the relevant input text is 204 // either the user text or the permanent text, depending on if user input is 205 // in progress. 206 // 207 // |match| is passed by value for two reasons: 208 // (1) This function needs to modify |match|, so a const ref isn't 209 // appropriate. Callers don't actually care about the modifications, so a 210 // pointer isn't required. 211 // (2) The passed-in match is, on the caller side, typically coming from data 212 // associated with the popup. Since this call can close the popup, that 213 // could clear that data, leaving us with a pointer-to-garbage. So at 214 // some point someone needs to make a copy of the match anyway, to 215 // preserve it past the popup closure. 216 void OpenMatch(AutocompleteMatch match, 217 WindowOpenDisposition disposition, 218 const GURL& alternate_nav_url, 219 const base::string16& pasted_text, 220 size_t index); 221 222 OmniboxFocusState focus_state() const { return focus_state_; } 223 bool has_focus() const { return focus_state_ != OMNIBOX_FOCUS_NONE; } 224 bool is_caret_visible() const { 225 return focus_state_ == OMNIBOX_FOCUS_VISIBLE; 226 } 227 228 // Accessors for keyword-related state (see comments on keyword_ and 229 // is_keyword_hint_). 230 const base::string16& keyword() const { return keyword_; } 231 bool is_keyword_hint() const { return is_keyword_hint_; } 232 bool is_keyword_selected() const { 233 return !is_keyword_hint_ && !keyword_.empty(); 234 } 235 236 // Accepts the current keyword hint as a keyword. It always returns true for 237 // caller convenience. |entered_method| indicates how the use entered 238 // keyword mode. This parameter is only used for metrics/logging; it's not 239 // used to change user-visible behavior. 240 bool AcceptKeyword(EnteredKeywordModeMethod entered_method); 241 242 // Accepts the current temporary text as the user text. 243 void AcceptTemporaryTextAsUserText(); 244 245 // Clears the current keyword. |visible_text| is the (non-keyword) text 246 // currently visible in the edit. 247 void ClearKeyword(const base::string16& visible_text); 248 249 // Returns the current autocomplete result. This logic should in the future 250 // live in AutocompleteController but resides here for now. This method is 251 // used by AutomationProvider::AutocompleteEditGetMatches. 252 const AutocompleteResult& result() const { 253 return omnibox_controller_->result(); 254 } 255 256 // Called when the view is gaining focus. |control_down| is whether the 257 // control key is down (at the time we're gaining focus). 258 void OnSetFocus(bool control_down); 259 260 // Sets the visibility of the caret in the omnibox, if it has focus. The 261 // visibility of the caret is reset to visible if either 262 // - The user starts typing, or 263 // - We explicitly focus the omnibox again. 264 // The latter case must be handled in three separate places--OnSetFocus(), 265 // OmniboxView::SetFocus(), and the mouse handlers in OmniboxView. See 266 // accompanying comments for why each of these is necessary. 267 // 268 // Caret visibility is tracked per-tab and updates automatically upon 269 // switching tabs. 270 void SetCaretVisibility(bool visible); 271 272 // Sent before |OnKillFocus| and before the popup is closed. 273 void OnWillKillFocus(gfx::NativeView view_gaining_focus); 274 275 // Called when the view is losing focus. Resets some state. 276 void OnKillFocus(); 277 278 // Called when the user presses the escape key. Decides what, if anything, to 279 // revert about any current edits. Returns whether the key was handled. 280 bool OnEscapeKeyPressed(); 281 282 // Called when the user presses or releases the control key. Changes state as 283 // necessary. 284 void OnControlKeyChanged(bool pressed); 285 286 // Called when the user pastes in text. 287 void OnPaste(); 288 289 // Returns true if pasting is in progress. 290 bool is_pasting() const { return paste_state_ == PASTING; } 291 292 // TODO(beaudoin): Try not to expose this. 293 bool in_revert() const { return in_revert_; } 294 295 // Called when the user presses up or down. |count| is a repeat count, 296 // negative for moving up, positive for moving down. 297 virtual void OnUpOrDownKeyPressed(int count); 298 299 // Called when any relevant data changes. This rolls together several 300 // separate pieces of data into one call so we can update all the UI 301 // efficiently: 302 // |text| is either the new temporary text from the user manually selecting 303 // a different match, or the inline autocomplete text. We distinguish by 304 // checking if |destination_for_temporary_text_change| is NULL. 305 // |destination_for_temporary_text_change| is NULL (if temporary text should 306 // not change) or the pre-change destination URL (if temporary text should 307 // change) so we can save it off to restore later. 308 // |keyword| is the keyword to show a hint for if |is_keyword_hint| is true, 309 // or the currently selected keyword if |is_keyword_hint| is false (see 310 // comments on keyword_ and is_keyword_hint_). 311 void OnPopupDataChanged( 312 const base::string16& text, 313 GURL* destination_for_temporary_text_change, 314 const base::string16& keyword, 315 bool is_keyword_hint); 316 317 // Called by the OmniboxView after something changes, with details about what 318 // state changes occured. Updates internal state, updates the popup if 319 // necessary, and returns true if any significant changes occurred. Note that 320 // |text_differs| may be set even if |old_text| == |new_text|, e.g. if we've 321 // just committed an IME composition. 322 // 323 // If |allow_keyword_ui_change| is false then the change should not affect 324 // keyword ui state, even if the text matches a keyword exactly. This value 325 // may be false when the user is composing a text with an IME. 326 bool OnAfterPossibleChange(const base::string16& old_text, 327 const base::string16& new_text, 328 size_t selection_start, 329 size_t selection_end, 330 bool selection_differs, 331 bool text_differs, 332 bool just_deleted_text, 333 bool allow_keyword_ui_change); 334 335 // Called when the current match has changed in the OmniboxController. 336 void OnCurrentMatchChanged(); 337 338 // Sends the current SearchProvider suggestion to the Instant page if any. 339 void SetSuggestionToPrefetch(const InstantSuggestion& suggestion); 340 341 // Name of the histogram tracking cut or copy omnibox commands. 342 static const char kCutOrCopyAllTextHistogram[]; 343 344 private: 345 friend class OmniboxControllerTest; 346 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. 356 }; 357 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". 370 }; 371 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; 377 378 // Called whenever user_text_ should change. 379 void InternalSetUserText(const base::string16& text); 380 381 // Turns off keyword mode for the current match. 382 void ClearPopupKeywordMode() const; 383 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; 389 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; 395 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); 399 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); 407 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; 414 415 // Checks if a given character is a valid space character for accepting 416 // keyword. 417 static bool IsSpaceCharForAcceptingKeyword(wchar_t c); 418 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; 424 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; 430 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); 436 437 scoped_ptr<OmniboxController> omnibox_controller_; 438 439 OmniboxView* view_; 440 441 OmniboxEditController* controller_; 442 443 scoped_ptr<OmniboxCurrentPageDelegate> delegate_; 444 445 OmniboxFocusState focus_state_; 446 447 // Used to keep track whether the input currently in progress originated by 448 // focusing in the Omnibox or in the Fakebox. This will be INVALID if no input 449 // is in progress or the Omnibox is not focused. 450 FocusSource focus_source_; 451 452 // The URL of the currently displayed page. 453 base::string16 permanent_text_; 454 455 // This flag is true when the user has modified the contents of the edit, but 456 // not yet accepted them. We use this to determine when we need to save 457 // state (on switching tabs) and whether changes to the page URL should be 458 // immediately displayed. 459 // This flag will be true in a superset of the cases where the popup is open. 460 bool user_input_in_progress_; 461 462 // The text that the user has entered. This does not include inline 463 // autocomplete text that has not yet been accepted. 464 base::string16 user_text_; 465 466 // We keep track of when the user last focused on the omnibox. 467 base::TimeTicks last_omnibox_focus_; 468 469 // Whether any user input has occurred since focusing on the omnibox. This is 470 // used along with |last_omnibox_focus_| to calculate the time between a user 471 // focusing on the omnibox and editing. It is initialized to true since 472 // there was no focus event. 473 bool user_input_since_focus_; 474 475 // We keep track of when the user began modifying the omnibox text. 476 // This should be valid whenever user_input_in_progress_ is true. 477 base::TimeTicks time_user_first_modified_omnibox_; 478 479 // When the user closes the popup, we need to remember the URL for their 480 // desired choice, so that if they hit enter without reopening the popup we 481 // know where to go. We could simply rerun autocomplete in this case, but 482 // we'd need to either wait for all results to come in (unacceptably slow) or 483 // do the wrong thing when the user had chosen some provider whose results 484 // were not returned instantaneously. 485 // 486 // This variable is only valid when user_input_in_progress_ is true, since 487 // when it is false the user has either never input anything (so there won't 488 // be a value here anyway) or has canceled their input, which should be 489 // treated the same way. Also, since this is for preserving a desired URL 490 // after the popup has been closed, we ignore this if the popup is open, and 491 // simply ask the popup for the desired URL directly. As a result, the 492 // contents of this variable only need to be updated when the popup is closed 493 // but user_input_in_progress_ is not being cleared. 494 base::string16 url_for_remembered_user_selection_; 495 496 // Inline autocomplete is allowed if the user has not just deleted text, and 497 // no temporary text is showing. In this case, inline_autocomplete_text_ is 498 // appended to the user_text_ and displayed selected (at least initially). 499 // 500 // NOTE: When the popup is closed there should never be inline autocomplete 501 // text (actions that close the popup should either accept the text, convert 502 // it to a normal selection, or change the edit entirely). 503 bool just_deleted_text_; 504 base::string16 inline_autocomplete_text_; 505 506 // Used by OnPopupDataChanged to keep track of whether there is currently a 507 // temporary text. 508 // 509 // Example of use: If the user types "goog", then arrows down in the 510 // autocomplete popup until, say, "google.com" appears in the edit box, then 511 // the user_text_ is still "goog", and "google.com" is "temporary text". 512 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again 513 // and the popup is closed and "goog" is replaced by the permanent_text_, 514 // which is the URL of the current page. 515 // 516 // original_url_ is only valid when there is temporary text, and is used as 517 // the unique identifier of the originally selected item. Thus, if the user 518 // arrows to a different item with the same text, we can still distinguish 519 // them and not revert all the way to the permanent_text_. 520 bool has_temporary_text_; 521 GURL original_url_; 522 523 // When the user's last action was to paste, we disallow inline autocomplete 524 // (on the theory that the user is trying to paste in a new URL or part of 525 // one, and in either case inline autocomplete would get in the way). 526 PasteState paste_state_; 527 528 // Whether the control key is depressed. We track this to avoid calling 529 // UpdatePopup() repeatedly if the user holds down the key, and to know 530 // whether to trigger "ctrl-enter" behavior. 531 ControlKeyState control_key_state_; 532 533 // The keyword associated with the current match. The user may have an actual 534 // selected keyword, or just some input text that looks like a keyword (so we 535 // can show a hint to press <tab>). This is the keyword in either case; 536 // is_keyword_hint_ (below) distinguishes the two cases. 537 base::string16 keyword_; 538 539 // True if the keyword associated with this match is merely a hint, i.e. the 540 // user hasn't actually selected a keyword yet. When this is true, we can use 541 // keyword_ to show a "Press <tab> to search" sort of hint. 542 bool is_keyword_hint_; 543 544 Profile* profile_; 545 546 // This is needed to properly update the SearchModel state when the user 547 // presses escape. 548 bool in_revert_; 549 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_; 556 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_; 561 562 DISALLOW_COPY_AND_ASSIGN(OmniboxEditModel); 563 }; 564 565 #endif // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_EDIT_MODEL_H_ 566