1 // Copyright (c) 2011 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_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ 7 #pragma once 8 9 #include "base/memory/scoped_ptr.h" 10 #include "base/string16.h" 11 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" 12 #include "chrome/browser/autocomplete/autocomplete_match.h" 13 #include "chrome/common/instant_types.h" 14 #include "content/common/page_transition_types.h" 15 #include "googleurl/src/gurl.h" 16 #include "ui/gfx/native_widget_types.h" 17 #include "webkit/glue/window_open_disposition.h" 18 19 class AutocompleteController; 20 class AutocompleteEditController; 21 class AutocompleteEditModel; 22 class AutocompleteEditView; 23 class AutocompletePopupModel; 24 class AutocompleteResult; 25 class InstantController; 26 class Profile; 27 class SkBitmap; 28 class TabContentsWrapper; 29 30 namespace gfx { 31 class Rect; 32 } 33 34 // TODO(pkasting): The names and contents of the classes in 35 // this file are temporary. I am in hack-and-slash mode right now. 36 // http://code.google.com/p/chromium/issues/detail?id=6772 37 38 // Embedders of an AutocompleteEdit widget must implement this class. 39 class AutocompleteEditController { 40 public: 41 // When the user presses enter or selects a line with the mouse, this 42 // function will get called synchronously with the url to open and 43 // disposition and transition to use when opening it. 44 // 45 // |alternate_nav_url|, if non-empty, contains the alternate navigation URL 46 // for |url|, which the controller can check for existence. See comments on 47 // AutocompleteResult::GetAlternateNavURL(). 48 virtual void OnAutocompleteAccept(const GURL& url, 49 WindowOpenDisposition disposition, 50 PageTransition::Type transition, 51 const GURL& alternate_nav_url) = 0; 52 53 // Called when anything has changed that might affect the layout or contents 54 // of the views around the edit, including the text of the edit and the 55 // status of any keyword- or hint-related state. 56 virtual void OnChanged() = 0; 57 58 // Called when the selection of the AutocompleteEditView changes. 59 virtual void OnSelectionBoundsChanged() = 0; 60 61 // Called whenever the user starts or stops an input session (typing, 62 // interacting with the edit, etc.). When user input is not in progress, 63 // the edit is guaranteed to be showing the permanent text. 64 virtual void OnInputInProgress(bool in_progress) = 0; 65 66 // Called whenever the autocomplete edit is losing focus. 67 virtual void OnKillFocus() = 0; 68 69 // Called whenever the autocomplete edit gets focused. 70 virtual void OnSetFocus() = 0; 71 72 // Returns the favicon of the current page. 73 virtual SkBitmap GetFavicon() const = 0; 74 75 // Returns the title of the current page. 76 virtual string16 GetTitle() const = 0; 77 78 // Returns the InstantController, or NULL if instant is not enabled. 79 virtual InstantController* GetInstant() = 0; 80 81 // Returns the TabContentsWrapper of the currently selected tab. 82 virtual TabContentsWrapper* GetTabContentsWrapper() const = 0; 83 84 protected: 85 virtual ~AutocompleteEditController(); 86 }; 87 88 class AutocompleteEditModel : public AutocompleteControllerDelegate { 89 public: 90 struct State { 91 State(bool user_input_in_progress, 92 const string16& user_text, 93 const string16& keyword, 94 bool is_keyword_hint); 95 ~State(); 96 97 bool user_input_in_progress; 98 const string16 user_text; 99 const string16 keyword; 100 const bool is_keyword_hint; 101 }; 102 103 AutocompleteEditModel(AutocompleteEditView* view, 104 AutocompleteEditController* controller, 105 Profile* profile); 106 ~AutocompleteEditModel(); 107 autocomplete_controller()108 AutocompleteController* autocomplete_controller() const { 109 return autocomplete_controller_.get(); 110 } 111 set_popup_model(AutocompletePopupModel * popup_model)112 void set_popup_model(AutocompletePopupModel* popup_model) { 113 popup_ = popup_model; 114 } 115 116 // TODO: The edit and popup should be siblings owned by the LocationBarView, 117 // making this accessor unnecessary. popup_model()118 AutocompletePopupModel* popup_model() const { return popup_; } 119 120 // Invoked when the profile has changed. 121 void SetProfile(Profile* profile); 122 profile()123 Profile* profile() const { return profile_; } 124 125 // Returns the current state. This assumes we are switching tabs, and changes 126 // the internal state appropriately. 127 const State GetStateForTabSwitch(); 128 129 // Restores local state from the saved |state|. 130 void RestoreState(const State& state); 131 132 // Returns the match for the current text. If the user has not edited the text 133 // this is the match corresponding to the permanent text. 134 AutocompleteMatch CurrentMatch(); 135 136 // Called when the user wants to export the entire current text as a URL. 137 // Sets the url, and if known, the title and favicon. 138 void GetDataForURLExport(GURL* url, string16* title, SkBitmap* favicon); 139 140 // Returns true if a verbatim query should be used for instant. A verbatim 141 // query is forced in certain situations, such as pressing delete at the end 142 // of the edit. 143 bool UseVerbatimInstant(); 144 145 // If the user presses ctrl-enter, it means "add .com to the the end". The 146 // desired TLD is the TLD the user desires to add to the end of the current 147 // input, if any, based on their control key state and any other actions 148 // they've taken. 149 string16 GetDesiredTLD() const; 150 151 // Returns true if the current edit contents will be treated as a 152 // URL/navigation, as opposed to a search. 153 bool CurrentTextIsURL() const; 154 155 // Returns the match type for the current edit contents. 156 AutocompleteMatch::Type CurrentTextType() const; 157 158 // Invoked to adjust the text before writting to the clipboard for a copy 159 // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position 160 // of the selection e.g. min(selection_start, selection_end). |text| is the 161 // currently selected text. If |is_all_selected| is true all the text in the 162 // edit is selected. If the url should be copied to the clipboard |write_url| 163 // is set to true and |url| set to the url to write. 164 void AdjustTextForCopy(int sel_min, 165 bool is_all_selected, 166 string16* text, 167 GURL* url, 168 bool* write_url); 169 user_input_in_progress()170 bool user_input_in_progress() const { return user_input_in_progress_; } 171 172 // Sets the state of user_input_in_progress_, and notifies the observer if 173 // that state has changed. 174 void SetInputInProgress(bool in_progress); 175 176 // Updates permanent_text_ to |new_permanent_text|. Returns true if this 177 // change should be immediately user-visible, because either the user is not 178 // editing or the edit does not have focus. 179 bool UpdatePermanentText(const string16& new_permanent_text); 180 181 // Returns the URL corresponding to the permanent text. 182 GURL PermanentURL(); 183 184 // Sets the user_text_ to |text|. Only the View should call this. 185 void SetUserText(const string16& text); 186 187 // Calls through to SearchProvider::FinalizeInstantQuery. 188 // If |skip_inline_autocomplete| is true then the |suggest_text| will be 189 // turned into final text instead of inline autocomplete suggest. 190 void FinalizeInstantQuery(const string16& input_text, 191 const string16& suggest_text, 192 bool skip_inline_autocomplete); 193 194 // Sets the suggestion text. 195 void SetSuggestedText(const string16& text, 196 InstantCompleteBehavior behavior); 197 198 // Commits the suggested text. If |skip_inline_autocomplete| is true then the 199 // suggested text will be committed as final text as if it's inputted by the 200 // user, rather than as inline autocomplete suggest. 201 // Returns true if the text was committed. 202 // TODO: can the return type be void? 203 bool CommitSuggestedText(bool skip_inline_autocomplete); 204 205 // Accepts the currently showing instant preview, if any, and returns true. 206 // Returns false if there is no instant preview showing. 207 bool AcceptCurrentInstantPreview(); 208 209 // Invoked any time the text may have changed in the edit. Updates instant and 210 // notifies the controller. 211 void OnChanged(); 212 213 // Reverts the edit model back to its unedited state (permanent text showing, 214 // no user input in progress). 215 void Revert(); 216 217 // Directs the popup to start autocomplete. 218 void StartAutocomplete(bool has_selected_text, 219 bool prevent_inline_autocomplete) const; 220 221 // Closes the popup and cancels any pending asynchronous queries. 222 void StopAutocomplete(); 223 224 // Determines whether the user can "paste and go", given the specified text. 225 // This also updates the internal paste-and-go-related state variables as 226 // appropriate so that the controller doesn't need to be repeatedly queried 227 // for the same text in every clipboard-related function. 228 bool CanPasteAndGo(const string16& text) const; 229 230 // Navigates to the destination last supplied to CanPasteAndGo. 231 void PasteAndGo(); 232 233 // Returns the url set by way of CanPasteAndGo. paste_and_go_url()234 const GURL& paste_and_go_url() const { return paste_and_go_url_; } 235 236 // Returns true if this is a paste-and-search rather than paste-and-go (or 237 // nothing). is_paste_and_search()238 bool is_paste_and_search() const { 239 return (paste_and_go_transition_ != PageTransition::TYPED); 240 } 241 242 // Asks the browser to load the popup's currently selected item, using the 243 // supplied disposition. This may close the popup. If |for_drop| is true, 244 // it indicates the input is being accepted as part of a drop operation and 245 // the transition should be treated as LINK (so that it won't trigger the 246 // URL to be autocompleted). 247 void AcceptInput(WindowOpenDisposition disposition, 248 bool for_drop); 249 250 // Asks the browser to load the item at |index|, with the given properties. 251 void OpenURL(const GURL& url, 252 WindowOpenDisposition disposition, 253 PageTransition::Type transition, 254 const GURL& alternate_nav_url, 255 size_t index, 256 const string16& keyword); 257 has_focus()258 bool has_focus() const { return has_focus_; } 259 260 // Accessors for keyword-related state (see comments on keyword_ and 261 // is_keyword_hint_). keyword()262 const string16& keyword() const { return keyword_; } is_keyword_hint()263 bool is_keyword_hint() const { return is_keyword_hint_; } 264 265 // Accepts the current keyword hint as a keyword. It always returns true for 266 // caller convenience. 267 bool AcceptKeyword(); 268 269 // Clears the current keyword. |visible_text| is the (non-keyword) text 270 // currently visible in the edit. 271 void ClearKeyword(const string16& visible_text); 272 273 // Returns the current autocomplete result. This logic should in the future 274 // live in AutocompleteController but resides here for now. This method is 275 // used by AutomationProvider::AutocompleteEditGetMatches. 276 const AutocompleteResult& result() const; 277 278 // Called when the view is gaining focus. |control_down| is whether the 279 // control key is down (at the time we're gaining focus). 280 void OnSetFocus(bool control_down); 281 282 // Sent before |OnKillFocus| and before the popup is closed. 283 void OnWillKillFocus(gfx::NativeView view_gaining_focus); 284 285 // Called when the view is losing focus. Resets some state. 286 void OnKillFocus(); 287 288 // Called when the user presses the escape key. Decides what, if anything, to 289 // revert about any current edits. Returns whether the key was handled. 290 bool OnEscapeKeyPressed(); 291 292 // Called when the user presses or releases the control key. Changes state as 293 // necessary. 294 void OnControlKeyChanged(bool pressed); 295 296 // Called when the user pastes in text. on_paste()297 void on_paste() { paste_state_ = PASTING; } 298 299 // Called when the user presses up or down. |count| is a repeat count, 300 // negative for moving up, positive for moving down. 301 void OnUpOrDownKeyPressed(int count); 302 303 // Called when any relevant data changes. This rolls together several 304 // separate pieces of data into one call so we can update all the UI 305 // efficiently: 306 // |text| is either the new temporary text from the user manually selecting 307 // a different match, or the inline autocomplete text. We distinguish by 308 // checking if |destination_for_temporary_text_change| is NULL. 309 // |destination_for_temporary_text_change| is NULL (if temporary text should 310 // not change) or the pre-change destination URL (if temporary text should 311 // change) so we can save it off to restore later. 312 // |keyword| is the keyword to show a hint for if |is_keyword_hint| is true, 313 // or the currently selected keyword if |is_keyword_hint| is false (see 314 // comments on keyword_ and is_keyword_hint_). 315 void OnPopupDataChanged( 316 const string16& text, 317 GURL* destination_for_temporary_text_change, 318 const string16& keyword, 319 bool is_keyword_hint); 320 321 // Called by the AutocompleteEditView after something changes, with details 322 // about what state changes occured. Updates internal state, updates the 323 // popup if necessary, and returns true if any significant changes occurred. 324 // If |allow_keyword_ui_change| is false then the change should not affect 325 // keyword ui state, even if the text matches a keyword exactly. This value 326 // may be false when the user is composing a text with an IME. 327 bool OnAfterPossibleChange(const 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 // Invoked when the popup is going to change its bounds to |bounds|. 336 void PopupBoundsChangedTo(const gfx::Rect& bounds); 337 338 #if defined(UNIT_TEST) instant_complete_behavior()339 InstantCompleteBehavior instant_complete_behavior() const { 340 return instant_complete_behavior_; 341 } 342 #endif 343 344 private: 345 enum PasteState { 346 NONE, // Most recent edit was not a paste. 347 PASTING, // In the middle of doing a paste. We need this intermediate 348 // state because OnPaste() does the actual detection of 349 // paste, but OnAfterPossibleChange() has to update the 350 // paste state for every edit. If OnPaste() set the state 351 // directly to PASTED, OnAfterPossibleChange() wouldn't know 352 // whether that represented the current edit or a past one. 353 PASTED, // Most recent edit was a paste. 354 }; 355 356 enum ControlKeyState { 357 UP, // The control key is not depressed. 358 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's 359 // contents/selection have not changed since it was 360 // depressed. This is the only state in which we 361 // do the "ctrl-enter" behavior when the user hits 362 // enter. 363 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's 364 // contents/selection have changed since it was 365 // depressed. If the user now hits enter, we assume 366 // he simply hasn't released the key, rather than that 367 // he intended to hit "ctrl-enter". 368 }; 369 370 // AutocompleteControllerDelegate: 371 virtual void OnResultChanged(bool default_match_changed); 372 373 // Returns true if a query to an autocomplete provider is currently 374 // in progress. This logic should in the future live in 375 // AutocompleteController but resides here for now. This method is used by 376 // AutomationProvider::AutocompleteEditIsQueryInProgress. 377 bool query_in_progress() const; 378 379 // Called whenever user_text_ should change. 380 void InternalSetUserText(const string16& text); 381 382 // Returns true if a keyword is selected. 383 bool KeywordIsSelected() const; 384 385 // Conversion between user text and display text. User text is the text the 386 // user has input. Display text is the text being shown in the edit. The 387 // two are different if a keyword is selected. 388 string16 DisplayTextFromUserText(const string16& text) const; 389 string16 UserTextFromDisplayText(const string16& text) const; 390 391 // Copies the selected match into |match|. If an update is in progress, 392 // "selected" means "default in the latest matches". If there are no matches, 393 // does not update |match|. 394 // 395 // If |alternate_nav_url| is non-NULL, it will be set to the alternate 396 // navigation URL for |url| if one exists, or left unchanged otherwise. See 397 // comments on AutocompleteResult::GetAlternateNavURL(). 398 // 399 // TODO(pkasting): When manually_selected_match_ moves to the controller, this 400 // can move too. 401 void InfoForCurrentSelection(AutocompleteMatch* match, 402 GURL* alternate_nav_url) const; 403 404 // Returns the default match for the current text, as well as the alternate 405 // nav URL, if |alternate_nav_url| is non-NULL and there is such a URL. 406 void GetInfoForCurrentText(AutocompleteMatch* match, 407 GURL* alternate_nav_url) const; 408 409 // Returns true if |text| (which is display text in the current context) 410 // parses as a URL, and in that case sets |url| to the calculated URL. 411 // Subtle note: This ignores the desired_tld_ (unlike GetDataForURLExport() 412 // and CurrentTextIsURL()). The view needs this because it calls this 413 // function during copy handling, when the control key is down to trigger the 414 // copy. 415 bool GetURLForText(const string16& text, GURL* url) const; 416 417 // Reverts the edit box from a temporary text back to the original user text. 418 // If |revert_popup| is true then the popup will be reverted as well. 419 void RevertTemporaryText(bool revert_popup); 420 421 // Accepts current keyword if the user only typed a space at the end of 422 // |new_user_text| comparing to the |old_user_text|. 423 // Returns true if the current keyword is accepted. 424 bool MaybeAcceptKeywordBySpace(const string16& old_user_text, 425 const string16& new_user_text); 426 427 // Checks if |allow_exact_keyword_match_| should be set to true according to 428 // the old and new user text and the current caret position. It does not take 429 // other factors into account, e.g. if the view is ready to change the keyword 430 // ui or not. This is only for the case of inserting a space character in the 431 // middle of the text. See the comment of |allow_exact_keyword_match_| below. 432 bool ShouldAllowExactKeywordMatch(const string16& old_user_text, 433 const string16& new_user_text, 434 size_t caret_position); 435 436 // Checks if a given character is a valid space character for accepting 437 // keyword. 438 static bool IsSpaceCharForAcceptingKeyword(wchar_t c); 439 440 scoped_ptr<AutocompleteController> autocomplete_controller_; 441 442 AutocompleteEditView* view_; 443 444 AutocompletePopupModel* popup_; 445 446 AutocompleteEditController* controller_; 447 448 // Whether the edit has focus. 449 bool has_focus_; 450 451 // The URL of the currently displayed page. 452 string16 permanent_text_; 453 454 // This flag is true when the user has modified the contents of the edit, but 455 // not yet accepted them. We use this to determine when we need to save 456 // state (on switching tabs) and whether changes to the page URL should be 457 // immediately displayed. 458 // This flag will be true in a superset of the cases where the popup is open. 459 bool user_input_in_progress_; 460 461 // The text that the user has entered. This does not include inline 462 // autocomplete text that has not yet been accepted. 463 string16 user_text_; 464 465 // When the user closes the popup, we need to remember the URL for their 466 // desired choice, so that if they hit enter without reopening the popup we 467 // know where to go. We could simply rerun autocomplete in this case, but 468 // we'd need to either wait for all results to come in (unacceptably slow) or 469 // do the wrong thing when the user had chosen some provider whose results 470 // were not returned instantaneously. 471 // 472 // This variable is only valid when user_input_in_progress_ is true, since 473 // when it is false the user has either never input anything (so there won't 474 // be a value here anyway) or has canceled their input, which should be 475 // treated the same way. Also, since this is for preserving a desired URL 476 // after the popup has been closed, we ignore this if the popup is open, and 477 // simply ask the popup for the desired URL directly. As a result, the 478 // contents of this variable only need to be updated when the popup is closed 479 // but user_input_in_progress_ is not being cleared. 480 string16 url_for_remembered_user_selection_; 481 482 // Inline autocomplete is allowed if the user has not just deleted text, and 483 // no temporary text is showing. In this case, inline_autocomplete_text_ is 484 // appended to the user_text_ and displayed selected (at least initially). 485 // 486 // NOTE: When the popup is closed there should never be inline autocomplete 487 // text (actions that close the popup should either accept the text, convert 488 // it to a normal selection, or change the edit entirely). 489 bool just_deleted_text_; 490 string16 inline_autocomplete_text_; 491 492 // Used by OnPopupDataChanged to keep track of whether there is currently a 493 // temporary text. 494 // 495 // Example of use: If the user types "goog", then arrows down in the 496 // autocomplete popup until, say, "google.com" appears in the edit box, then 497 // the user_text_ is still "goog", and "google.com" is "temporary text". 498 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again 499 // and the popup is closed and "goog" is replaced by the permanent_text_, 500 // which is the URL of the current page. 501 // 502 // original_url_ is only valid when there is temporary text, and is used as 503 // the unique identifier of the originally selected item. Thus, if the user 504 // arrows to a different item with the same text, we can still distinguish 505 // them and not revert all the way to the permanent_text_. 506 bool has_temporary_text_; 507 GURL original_url_; 508 509 // When the user's last action was to paste, we disallow inline autocomplete 510 // (on the theory that the user is trying to paste in a new URL or part of 511 // one, and in either case inline autocomplete would get in the way). 512 PasteState paste_state_; 513 514 // Whether the control key is depressed. We track this to avoid calling 515 // UpdatePopup() repeatedly if the user holds down the key, and to know 516 // whether to trigger "ctrl-enter" behavior. 517 ControlKeyState control_key_state_; 518 519 // The keyword associated with the current match. The user may have an actual 520 // selected keyword, or just some input text that looks like a keyword (so we 521 // can show a hint to press <tab>). This is the keyword in either case; 522 // is_keyword_hint_ (below) distinguishes the two cases. 523 string16 keyword_; 524 525 // True if the keyword associated with this match is merely a hint, i.e. the 526 // user hasn't actually selected a keyword yet. When this is true, we can use 527 // keyword_ to show a "Press <tab> to search" sort of hint. 528 bool is_keyword_hint_; 529 530 // Paste And Go-related state. See CanPasteAndGo(). 531 mutable GURL paste_and_go_url_; 532 mutable PageTransition::Type paste_and_go_transition_; 533 mutable GURL paste_and_go_alternate_nav_url_; 534 535 Profile* profile_; 536 537 // Should instant be updated? This is needed as prior to accepting the current 538 // text the model is reverted, which triggers resetting instant. We don't want 539 // to update instant in this case, so we use the flag to determine if this is 540 // happening. 541 bool update_instant_; 542 543 // Indicates if the upcoming autocomplete search is allowed to be treated as 544 // an exact keyword match. If it's true then keyword mode will be triggered 545 // automatically if the input is "<keyword> <search string>". We only allow 546 // such trigger when: 547 // 1.A single space character is added at the end of a keyword, such as: 548 // (assume "foo" is a keyword, | is the input caret) 549 // foo| -> foo | 550 // foo[bar] -> foo | ([bar] indicates a selected text "bar") 551 // 2.A single space character is inserted after a keyword when the caret is 552 // not at the end of the line, such as: 553 // foo|bar -> foo |bar 554 // 555 // It has no effect if a keyword is already selected. 556 bool allow_exact_keyword_match_; 557 558 // Last value of InstantCompleteBehavior supplied to |SetSuggestedText|. 559 InstantCompleteBehavior instant_complete_behavior_; 560 561 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditModel); 562 }; 563 564 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ 565