autocomplete_edit.h revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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/string16.h" 10#include "chrome/browser/autocomplete/autocomplete_match.h" 11#include "chrome/common/notification_observer.h" 12#include "chrome/common/notification_registrar.h" 13#include "chrome/common/page_transition_types.h" 14#include "gfx/native_widget_types.h" 15#include "googleurl/src/gurl.h" 16#include "webkit/glue/window_open_disposition.h" 17 18class AutocompletePopupModel; 19class Profile; 20class SkBitmap; 21 22class AutocompleteEditController; 23class AutocompleteEditModel; 24class AutocompleteEditView; 25class AutocompleteResult; 26 27namespace gfx { 28class Rect; 29} 30 31// TODO(pkasting): The names and contents of the classes in 32// this file are temporary. I am in hack-and-slash mode right now. 33// http://code.google.com/p/chromium/issues/detail?id=6772 34 35// Embedders of an AutocompleteEdit widget must implement this class. 36class AutocompleteEditController { 37 public: 38 // Sent when the autocomplete popup is about to close. 39 virtual void OnAutocompleteWillClosePopup() = 0; 40 41 // Sent when the edit is losing focus. |view_gaining_focus| is the view 42 // gaining focus and may be null. 43 virtual void OnAutocompleteLosingFocus( 44 gfx::NativeView view_gaining_focus) = 0; 45 46 // Sent prior to OnAutoCompleteAccept and before the model has been reverted. 47 virtual void OnAutocompleteWillAccept() = 0; 48 49 // Commits the suggested text. |typed_text| is the current text showing in the 50 // autocomplete. Returns true if the text was committed. 51 virtual bool OnCommitSuggestedText(const std::wstring& typed_text) = 0; 52 53 // Sets the suggested search text to |suggested_text|. 54 virtual void OnSetSuggestedSearchText(const string16& suggested_text) = 0; 55 56 // Invoked when the popup is going to change its bounds to |bounds|. 57 virtual void OnPopupBoundsChanged(const gfx::Rect& bounds) = 0; 58 59 // When the user presses enter or selects a line with the mouse, this 60 // function will get called synchronously with the url to open and 61 // disposition and transition to use when opening it. 62 // 63 // |alternate_nav_url|, if non-empty, contains the alternate navigation URL 64 // for |url|, which the controller can check for existence. See comments on 65 // AutocompleteResult::GetAlternateNavURL(). 66 virtual void OnAutocompleteAccept(const GURL& url, 67 WindowOpenDisposition disposition, 68 PageTransition::Type transition, 69 const GURL& alternate_nav_url) = 0; 70 71 // Called when anything has changed that might affect the layout or contents 72 // of the views around the edit, including the text of the edit and the 73 // status of any keyword- or hint-related state. 74 virtual void OnChanged() = 0; 75 76 // Called when the selection of the AutocompleteEditView changes. 77 virtual void OnSelectionBoundsChanged() = 0; 78 79 // Called whenever the user starts or stops an input session (typing, 80 // interacting with the edit, etc.). When user input is not in progress, 81 // the edit is guaranteed to be showing the permanent text. 82 virtual void OnInputInProgress(bool in_progress) = 0; 83 84 // Called whenever the autocomplete edit is losing focus. 85 virtual void OnKillFocus() = 0; 86 87 // Called whenever the autocomplete edit gets focused. 88 virtual void OnSetFocus() = 0; 89 90 // Returns the favicon of the current page. 91 virtual SkBitmap GetFavIcon() const = 0; 92 93 // Returns the title of the current page. 94 virtual std::wstring GetTitle() const = 0; 95 96 protected: 97 virtual ~AutocompleteEditController(); 98}; 99 100class AutocompleteEditModel : public NotificationObserver { 101 public: 102 enum KeywordUIState { 103 // The user is typing normally. 104 NORMAL, 105 // The user is editing in the middle of the input string. Even if the 106 // input looks like a keyword, don't display the keyword UI, as to not 107 // interfere with the user's editing. 108 NO_KEYWORD, 109 // The user has triggered the keyword UI. Until it disappears, bias 110 // autocomplete results so that input strings of the keyword alone default 111 // to the keyword provider, not a normal navigation or search. 112 KEYWORD, 113 }; 114 115 struct State { 116 State(bool user_input_in_progress, 117 const std::wstring& user_text, 118 const std::wstring& keyword, 119 bool is_keyword_hint, 120 KeywordUIState keyword_ui_state); 121 ~State(); 122 123 bool user_input_in_progress; 124 const std::wstring user_text; 125 const std::wstring keyword; 126 const bool is_keyword_hint; 127 const KeywordUIState keyword_ui_state; 128 }; 129 130 AutocompleteEditModel(AutocompleteEditView* view, 131 AutocompleteEditController* controller, 132 Profile* profile); 133 ~AutocompleteEditModel(); 134 135 void SetPopupModel(AutocompletePopupModel* popup_model); 136 137 // It should only be used by testing code. 138 AutocompletePopupModel* popup_model() const { return popup_; } 139 140 // Invoked when the profile has changed. 141 void SetProfile(Profile* profile); 142 143 Profile* profile() const { return profile_; } 144 145 // Returns the current state. This assumes we are switching tabs, and changes 146 // the internal state appropriately. 147 const State GetStateForTabSwitch(); 148 149 // Restores local state from the saved |state|. 150 void RestoreState(const State& state); 151 152 // Returns the match for the current text. If the user has not edited the text 153 // this is the match corresponding to the permanent text. 154 AutocompleteMatch CurrentMatch(); 155 156 // Called when the user wants to export the entire current text as a URL. 157 // Sets the url, and if known, the title and favicon. 158 void GetDataForURLExport(GURL* url, std::wstring* title, SkBitmap* favicon); 159 160 // Returns true if inline autocomplete was prevented the last time 161 // autocomplete was run. 162 bool PreventInlineAutocomplete(); 163 164 // If the user presses ctrl-enter, it means "add .com to the the end". The 165 // desired TLD is the TLD the user desires to add to the end of the current 166 // input, if any, based on their control key state and any other actions 167 // they've taken. 168 std::wstring GetDesiredTLD() const; 169 170 // Returns true if the current edit contents will be treated as a 171 // URL/navigation, as opposed to a search. 172 bool CurrentTextIsURL() const; 173 174 // Returns the match type for the current edit contents. 175 AutocompleteMatch::Type CurrentTextType() const; 176 177 // Returns true if |text| (which is display text in the current context) 178 // parses as a URL, and in that case sets |url| to the calculated URL. 179 // Subtle note: This ignores the desired_tld_ (unlike GetDataForURLExport() 180 // and CurrentTextIsURL()). The view needs this because it calls this 181 // function during copy handling, when the control key is down to trigger the 182 // copy. 183 bool GetURLForText(const std::wstring& text, GURL* url) const; 184 185 // Invoked to adjust the text before writting to the clipboard for a copy 186 // (e.g. by adding 'http' to the front). |sel_min| gives the minimum position 187 // of the selection e.g. min(selection_start, selection_end). |text| is the 188 // currently selected text. If |is_all_selected| is true all the text in the 189 // edit is selected. If the url should be copied to the clipboard |write_url| 190 // is set to true and |url| set to the url to write. 191 void AdjustTextForCopy(int sel_min, 192 bool is_all_selected, 193 std::wstring* text, 194 GURL* url, 195 bool* write_url); 196 197 bool user_input_in_progress() const { return user_input_in_progress_; } 198 199 // Sets the state of user_input_in_progress_, and notifies the observer if 200 // that state has changed. 201 void SetInputInProgress(bool in_progress); 202 203 // Updates permanent_text_ to |new_permanent_text|. Returns true if this 204 // change should be immediately user-visible, because either the user is not 205 // editing or the edit does not have focus. 206 bool UpdatePermanentText(const std::wstring& new_permanent_text); 207 208 // Sets the user_text_ to |text|. Only the View should call this. 209 void SetUserText(const std::wstring& text); 210 211 // Reverts the edit model back to its unedited state (permanent text showing, 212 // no user input in progress). 213 void Revert(); 214 215 // Directs the popup to start autocomplete. 216 void StartAutocomplete(bool has_selected_text, 217 bool prevent_inline_autocomplete) const; 218 219 // Determines whether the user can "paste and go", given the specified text. 220 // This also updates the internal paste-and-go-related state variables as 221 // appropriate so that the controller doesn't need to be repeatedly queried 222 // for the same text in every clipboard-related function. 223 bool CanPasteAndGo(const std::wstring& text) const; 224 225 // Navigates to the destination last supplied to CanPasteAndGo. 226 void PasteAndGo(); 227 228 // Returns the url set by way of CanPasteAndGo. 229 const GURL& paste_and_go_url() const { return paste_and_go_url_; } 230 231 // Returns true if this is a paste-and-search rather than paste-and-go (or 232 // nothing). 233 bool is_paste_and_search() const { 234 return (paste_and_go_transition_ != PageTransition::TYPED); 235 } 236 237 // Asks the browser to load the popup's currently selected item, using the 238 // supplied disposition. This may close the popup. If |for_drop| is true, 239 // it indicates the input is being accepted as part of a drop operation and 240 // the transition should be treated as LINK (so that it won't trigger the 241 // URL to be autocompleted). 242 void AcceptInput(WindowOpenDisposition disposition, 243 bool for_drop); 244 245 // Asks the browser to load the item at |index|, with the given properties. 246 void OpenURL(const GURL& url, 247 WindowOpenDisposition disposition, 248 PageTransition::Type transition, 249 const GURL& alternate_nav_url, 250 size_t index, 251 const std::wstring& keyword); 252 253 bool has_focus() const { return has_focus_; } 254 255 // Accessors for keyword-related state (see comments on keyword_ and 256 // is_keyword_hint_). 257 std::wstring keyword() const { 258 return (is_keyword_hint_ || (keyword_ui_state_ != NO_KEYWORD)) ? 259 keyword_ : std::wstring(); 260 } 261 bool is_keyword_hint() const { return is_keyword_hint_; } 262 263 // Accepts the current keyword hint as a keyword. 264 void AcceptKeyword(); 265 266 // Clears the current keyword. |visible_text| is the (non-keyword) text 267 // currently visible in the edit. 268 void ClearKeyword(const std::wstring& visible_text); 269 270 // Returns true if a query to an autocomplete provider is currently 271 // in progress. This logic should in the future live in 272 // AutocompleteController but resides here for now. This method is used by 273 // AutomationProvider::AutocompleteEditIsQueryInProgress. 274 bool query_in_progress() const; 275 276 // Returns the current autocomplete result. This logic should in the future 277 // live in AutocompleteController but resides here for now. This method is 278 // used by AutomationProvider::AutocompleteEditGetMatches. 279 const AutocompleteResult& result() const; 280 281 // Called when the view is gaining focus. |control_down| is whether the 282 // control key is down (at the time we're gaining focus). 283 void OnSetFocus(bool control_down); 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 that replaces the entire edit contents. 297 void on_paste_replacing_all() { paste_state_ = REPLACING_ALL; } 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 std::wstring& text, 317 GURL* destination_for_temporary_text_change, 318 const std::wstring& 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 bool OnAfterPossibleChange(const std::wstring& new_text, 325 bool selection_differs, 326 bool text_differs, 327 bool just_deleted_text, 328 bool at_end_of_edit); 329 330 // Invoked when the popup is going to change its bounds to |bounds|. 331 void PopupBoundsChangedTo(const gfx::Rect& bounds); 332 333 // Invoked when the autocomplete results may have changed in some way. 334 void ResultsUpdated(); 335 336 private: 337 enum PasteState { 338 NONE, // Most recent edit was not a paste that replaced all text. 339 REPLACED_ALL, // Most recent edit was a paste that replaced all text. 340 REPLACING_ALL, // In the middle of doing a paste that replaces all 341 // text. We need this intermediate state because OnPaste() 342 // does the actual detection of such pastes, but 343 // OnAfterPossibleChange() has to update the paste state 344 // for every edit. If OnPaste() set the state directly to 345 // REPLACED_ALL, OnAfterPossibleChange() wouldn't know 346 // whether that represented the current edit or a past one. 347 }; 348 349 enum ControlKeyState { 350 UP, // The control key is not depressed. 351 DOWN_WITHOUT_CHANGE, // The control key is depressed, and the edit's 352 // contents/selection have not changed since it was 353 // depressed. This is the only state in which we 354 // do the "ctrl-enter" behavior when the user hits 355 // enter. 356 DOWN_WITH_CHANGE, // The control key is depressed, and the edit's 357 // contents/selection have changed since it was 358 // depressed. If the user now hits enter, we assume 359 // he simply hasn't released the key, rather than that 360 // he intended to hit "ctrl-enter". 361 }; 362 363 // NotificationObserver 364 virtual void Observe(NotificationType type, 365 const NotificationSource& source, 366 const NotificationDetails& details); 367 368 // Called whenever user_text_ should change. 369 void InternalSetUserText(const std::wstring& text); 370 371 // Returns true if a keyword is selected. 372 bool KeywordIsSelected() const; 373 374 // Conversion between user text and display text. User text is the text the 375 // user has input. Display text is the text being shown in the edit. The 376 // two are different if a keyword is selected. 377 std::wstring DisplayTextFromUserText(const std::wstring& text) const; 378 std::wstring UserTextFromDisplayText(const std::wstring& text) const; 379 380 // Returns the default match for the current text, as well as the alternate 381 // nav URL, if |alternate_nav_url| is non-NULL and there is such a URL. 382 void GetInfoForCurrentText(AutocompleteMatch* match, 383 GURL* alternate_nav_url) const; 384 385 // Determines the suggested search text and invokes OnSetSuggestedSearchText 386 // on the controller. 387 void UpdateSuggestedSearchText(); 388 389 AutocompleteEditView* view_; 390 391 AutocompletePopupModel* popup_; 392 393 AutocompleteEditController* controller_; 394 395 NotificationRegistrar registrar_; 396 397 // Whether the edit has focus. 398 bool has_focus_; 399 400 // The URL of the currently displayed page. 401 std::wstring permanent_text_; 402 403 // This flag is true when the user has modified the contents of the edit, but 404 // not yet accepted them. We use this to determine when we need to save 405 // state (on switching tabs) and whether changes to the page URL should be 406 // immediately displayed. 407 // This flag will be true in a superset of the cases where the popup is open. 408 bool user_input_in_progress_; 409 410 // The text that the user has entered. This does not include inline 411 // autocomplete text that has not yet been accepted. 412 std::wstring user_text_; 413 414 // When the user closes the popup, we need to remember the URL for their 415 // desired choice, so that if they hit enter without reopening the popup we 416 // know where to go. We could simply rerun autocomplete in this case, but 417 // we'd need to either wait for all results to come in (unacceptably slow) or 418 // do the wrong thing when the user had chosen some provider whose results 419 // were not returned instantaneously. 420 // 421 // This variable is only valid when user_input_in_progress_ is true, since 422 // when it is false the user has either never input anything (so there won't 423 // be a value here anyway) or has canceled their input, which should be 424 // treated the same way. Also, since this is for preserving a desired URL 425 // after the popup has been closed, we ignore this if the popup is open, and 426 // simply ask the popup for the desired URL directly. As a result, the 427 // contents of this variable only need to be updated when the popup is closed 428 // but user_input_in_progress_ is not being cleared. 429 std::wstring url_for_remembered_user_selection_; 430 431 // Inline autocomplete is allowed if the user has not just deleted text, and 432 // no temporary text is showing. In this case, inline_autocomplete_text_ is 433 // appended to the user_text_ and displayed selected (at least initially). 434 // 435 // NOTE: When the popup is closed there should never be inline autocomplete 436 // text (actions that close the popup should either accept the text, convert 437 // it to a normal selection, or change the edit entirely). 438 bool just_deleted_text_; 439 std::wstring inline_autocomplete_text_; 440 441 // Used by OnPopupDataChanged to keep track of whether there is currently a 442 // temporary text. 443 // 444 // Example of use: If the user types "goog", then arrows down in the 445 // autocomplete popup until, say, "google.com" appears in the edit box, then 446 // the user_text_ is still "goog", and "google.com" is "temporary text". 447 // When the user hits <esc>, the edit box reverts to "goog". Hit <esc> again 448 // and the popup is closed and "goog" is replaced by the permanent_text_, 449 // which is the URL of the current page. 450 // 451 // original_url_ is only valid when there is temporary text, and is used as 452 // the unique identifier of the originally selected item. Thus, if the user 453 // arrows to a different item with the same text, we can still distinguish 454 // them and not revert all the way to the permanent_text_. 455 bool has_temporary_text_; 456 GURL original_url_; 457 KeywordUIState original_keyword_ui_state_; 458 459 // When the user's last action was to paste and replace all the text, we 460 // disallow inline autocomplete (on the theory that the user is trying to 461 // paste in a new URL or part of one, and in either case inline autocomplete 462 // would get in the way). 463 PasteState paste_state_; 464 465 // Whether the control key is depressed. We track this to avoid calling 466 // UpdatePopup() repeatedly if the user holds down the key, and to know 467 // whether to trigger "ctrl-enter" behavior. 468 ControlKeyState control_key_state_; 469 470 // The keyword associated with the current match. The user may have an actual 471 // selected keyword, or just some input text that looks like a keyword (so we 472 // can show a hint to press <tab>). This is the keyword in either case; 473 // is_keyword_hint_ (below) distinguishes the two cases. 474 std::wstring keyword_; 475 476 // True if the keyword associated with this match is merely a hint, i.e. the 477 // user hasn't actually selected a keyword yet. When this is true, we can use 478 // keyword_ to show a "Press <tab> to search" sort of hint. 479 bool is_keyword_hint_; 480 481 // See KeywordUIState enum. 482 KeywordUIState keyword_ui_state_; 483 484 // Paste And Go-related state. See CanPasteAndGo(). 485 mutable GURL paste_and_go_url_; 486 mutable PageTransition::Type paste_and_go_transition_; 487 mutable GURL paste_and_go_alternate_nav_url_; 488 489 Profile* profile_; 490 491 DISALLOW_COPY_AND_ASSIGN(AutocompleteEditModel); 492}; 493 494#endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_EDIT_H_ 495