| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/omnibox/browser/omnibox_edit_model.h" | 5 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 // to make any change to the edit. While auto-commits modify the underlying | 286 // to make any change to the edit. While auto-commits modify the underlying |
| 287 // permanent URL, they're intended to have no effect on the user's editing | 287 // permanent URL, they're intended to have no effect on the user's editing |
| 288 // process -- before and after the auto-commit, the omnibox should show the | 288 // process -- before and after the auto-commit, the omnibox should show the |
| 289 // same user text and the same instant suggestion, even if the auto-commit | 289 // same user text and the same instant suggestion, even if the auto-commit |
| 290 // happens while the edit doesn't have focus. | 290 // happens while the edit doesn't have focus. |
| 291 base::string16 new_permanent_text = controller_->GetToolbarModel()->GetText(); | 291 base::string16 new_permanent_text = controller_->GetToolbarModel()->GetText(); |
| 292 base::string16 gray_text = view_->GetGrayTextAutocompletion(); | 292 base::string16 gray_text = view_->GetGrayTextAutocompletion(); |
| 293 const bool visibly_changed_permanent_text = | 293 const bool visibly_changed_permanent_text = |
| 294 (permanent_text_ != new_permanent_text) && | 294 (permanent_text_ != new_permanent_text) && |
| 295 (!has_focus() || | 295 (!has_focus() || |
| 296 (!user_input_in_progress_ && | 296 (!user_input_in_progress_ && !PopupIsOpen() && |
| 297 !(popup_model() && popup_model()->IsOpen()) && | |
| 298 controller_->GetToolbarModel()->url_replacement_enabled())) && | 297 controller_->GetToolbarModel()->url_replacement_enabled())) && |
| 299 (gray_text.empty() || | 298 (gray_text.empty() || |
| 300 new_permanent_text != user_text_ + gray_text); | 299 new_permanent_text != user_text_ + gray_text); |
| 301 | 300 |
| 302 permanent_text_ = new_permanent_text; | 301 permanent_text_ = new_permanent_text; |
| 303 return visibly_changed_permanent_text; | 302 return visibly_changed_permanent_text; |
| 304 } | 303 } |
| 305 | 304 |
| 306 GURL OmniboxEditModel::PermanentURL() { | 305 GURL OmniboxEditModel::PermanentURL() { |
| 307 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_), | 306 return url_formatter::FixupURL(base::UTF16ToUTF8(permanent_text_), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 332 } | 331 } |
| 333 | 332 |
| 334 void OmniboxEditModel::OnChanged() { | 333 void OmniboxEditModel::OnChanged() { |
| 335 // Hide any suggestions we might be showing. | 334 // Hide any suggestions we might be showing. |
| 336 view_->SetGrayTextAutocompletion(base::string16()); | 335 view_->SetGrayTextAutocompletion(base::string16()); |
| 337 | 336 |
| 338 // Don't call CurrentMatch() when there's no editing, as in this case we'll | 337 // Don't call CurrentMatch() when there's no editing, as in this case we'll |
| 339 // never actually use it. This avoids running the autocomplete providers (and | 338 // never actually use it. This avoids running the autocomplete providers (and |
| 340 // any systems they then spin up) during startup. | 339 // any systems they then spin up) during startup. |
| 341 const AutocompleteMatch& current_match = user_input_in_progress_ ? | 340 const AutocompleteMatch& current_match = user_input_in_progress_ ? |
| 342 CurrentMatch(NULL) : AutocompleteMatch(); | 341 CurrentMatch(nullptr) : AutocompleteMatch(); |
| 343 | 342 |
| 344 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_, | 343 client_->OnTextChanged(current_match, user_input_in_progress_, user_text_, |
| 345 result(), popup_model() && popup_model()->IsOpen(), | 344 result(), PopupIsOpen(), has_focus()); |
| 346 has_focus()); | |
| 347 controller_->OnChanged(); | 345 controller_->OnChanged(); |
| 348 } | 346 } |
| 349 | 347 |
| 350 void OmniboxEditModel::GetDataForURLExport(GURL* url, | 348 void OmniboxEditModel::GetDataForURLExport(GURL* url, |
| 351 base::string16* title, | 349 base::string16* title, |
| 352 gfx::Image* favicon) { | 350 gfx::Image* favicon) { |
| 353 *url = CurrentMatch(NULL).destination_url; | 351 *url = CurrentMatch(nullptr).destination_url; |
| 354 if (*url == client_->GetURL()) { | 352 if (*url == client_->GetURL()) { |
| 355 *title = client_->GetTitle(); | 353 *title = client_->GetTitle(); |
| 356 *favicon = client_->GetFavicon(); | 354 *favicon = client_->GetFavicon(); |
| 357 } | 355 } |
| 358 } | 356 } |
| 359 | 357 |
| 360 bool OmniboxEditModel::CurrentTextIsURL() const { | 358 bool OmniboxEditModel::CurrentTextIsURL() const { |
| 361 if (controller_->GetToolbarModel()->WouldReplaceURL()) | 359 if (controller_->GetToolbarModel()->WouldReplaceURL()) |
| 362 return false; | 360 return false; |
| 363 | 361 |
| 364 // If current text is not composed of replaced search terms and | 362 // If current text is not composed of replaced search terms and |
| 365 // !user_input_in_progress_, then permanent text is showing and should be a | 363 // !user_input_in_progress_, then permanent text is showing and should be a |
| 366 // URL, so no further checking is needed. By avoiding checking in this case, | 364 // URL, so no further checking is needed. By avoiding checking in this case, |
| 367 // we avoid calling into the autocomplete providers, and thus initializing the | 365 // we avoid calling into the autocomplete providers, and thus initializing the |
| 368 // history system, as long as possible, which speeds startup. | 366 // history system, as long as possible, which speeds startup. |
| 369 if (!user_input_in_progress_) | 367 if (!user_input_in_progress_) |
| 370 return true; | 368 return true; |
| 371 | 369 |
| 372 return !AutocompleteMatch::IsSearchType(CurrentMatch(NULL).type); | 370 return !AutocompleteMatch::IsSearchType(CurrentMatch(nullptr).type); |
| 373 } | 371 } |
| 374 | 372 |
| 375 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const { | 373 AutocompleteMatch::Type OmniboxEditModel::CurrentTextType() const { |
| 376 return CurrentMatch(NULL).type; | 374 return CurrentMatch(nullptr).type; |
| 377 } | 375 } |
| 378 | 376 |
| 379 void OmniboxEditModel::AdjustTextForCopy(int sel_min, | 377 void OmniboxEditModel::AdjustTextForCopy(int sel_min, |
| 380 bool is_all_selected, | 378 bool is_all_selected, |
| 381 base::string16* text, | 379 base::string16* text, |
| 382 GURL* url, | 380 GURL* url, |
| 383 bool* write_url) { | 381 bool* write_url) { |
| 384 *write_url = false; | 382 *write_url = false; |
| 385 | 383 |
| 386 // Do not adjust if selection did not start at the beginning of the field, or | 384 // Do not adjust if selection did not start at the beginning of the field, or |
| 387 // if the URL was omitted. | 385 // if the URL was omitted. |
| 388 if ((sel_min != 0) || controller_->GetToolbarModel()->WouldReplaceURL()) | 386 if ((sel_min != 0) || controller_->GetToolbarModel()->WouldReplaceURL()) |
| 389 return; | 387 return; |
| 390 | 388 |
| 391 // Check whether the user is trying to copy the current page's URL by | 389 // Check whether the user is trying to copy the current page's URL by |
| 392 // selecting the whole thing without editing it. | 390 // selecting the whole thing without editing it. |
| 393 // | 391 // |
| 394 // This is complicated by ZeroSuggest. When ZeroSuggest is active, the user | 392 // This is complicated by ZeroSuggest. When ZeroSuggest is active, the user |
| 395 // may be selecting different items and thus changing the address bar text, | 393 // may be selecting different items and thus changing the address bar text, |
| 396 // even though !user_input_in_progress_; and the permanent URL may change | 394 // even though !user_input_in_progress_; and the permanent URL may change |
| 397 // without updating the visible text, just like when user input is in | 395 // without updating the visible text, just like when user input is in |
| 398 // progress. In these cases, we don't want to copy the underlying URL, we | 396 // progress. In these cases, we don't want to copy the underlying URL, we |
| 399 // want to copy what the user actually sees. However, if we simply never do | 397 // want to copy what the user actually sees. However, if we simply never do |
| 400 // this block when !popup_model()->IsOpen(), then just clicking into the | 398 // this block when !PopupIsOpen(), then just clicking into the address bar and |
| 401 // address bar and trying to copy will always bypass this block on pages that | 399 // trying to copy will always bypass this block on pages that trigger |
| 402 // trigger ZeroSuggest, which is too conservative. Instead, in the | 400 // ZeroSuggest, which is too conservative. Instead, in the ZeroSuggest case, |
| 403 // ZeroSuggest case, we check that (a) the user hasn't selected one of the | 401 // we check that (a) the user hasn't selected one of the other suggestions, |
| 404 // other suggestions, and (b) the selected text is still the same as the | 402 // and (b) the selected text is still the same as the permanent text. ((b) |
| 405 // permanent text. ((b) probably implies (a), but it doesn't hurt to be | 403 // probably implies (a), but it doesn't hurt to be sure.) If these hold, then |
| 406 // sure.) If these hold, then it's safe to copy the underlying URL. | 404 // it's safe to copy the underlying URL. |
| 407 if (!user_input_in_progress_ && is_all_selected && | 405 if (!user_input_in_progress_ && is_all_selected && |
| 408 (!popup_model() || !popup_model()->IsOpen() || | 406 (!PopupIsOpen() || |
| 409 ((popup_model()->selected_line() == 0) && (*text == permanent_text_)))) { | 407 ((popup_model()->selected_line() == 0) && (*text == permanent_text_)))) { |
| 410 // It's safe to copy the underlying URL. These lines ensure that if the | 408 // It's safe to copy the underlying URL. These lines ensure that if the |
| 411 // scheme was stripped it's added back, and the URL is unescaped (we escape | 409 // scheme was stripped it's added back, and the URL is unescaped (we escape |
| 412 // parts of it for display). | 410 // parts of it for display). |
| 413 *url = PermanentURL(); | 411 *url = PermanentURL(); |
| 414 *text = base::UTF8ToUTF16(url->spec()); | 412 *text = base::UTF8ToUTF16(url->spec()); |
| 415 *write_url = true; | 413 *write_url = true; |
| 416 return; | 414 return; |
| 417 } | 415 } |
| 418 | 416 |
| 419 // We can't use CurrentTextIsURL() or GetDataForURLExport() because right now | 417 // We can't use CurrentTextIsURL() or GetDataForURLExport() because right now |
| 420 // the user is probably holding down control to cause the copy, which will | 418 // the user is probably holding down control to cause the copy, which will |
| 421 // screw up our calculation of the desired_tld. | 419 // screw up our calculation of the desired_tld. |
| 422 AutocompleteMatch match; | 420 AutocompleteMatch match; |
| 423 client_->GetAutocompleteClassifier()->Classify( | 421 client_->GetAutocompleteClassifier()->Classify( |
| 424 *text, is_keyword_selected(), true, ClassifyPage(), &match, NULL); | 422 *text, is_keyword_selected(), true, ClassifyPage(), &match, nullptr); |
| 425 if (AutocompleteMatch::IsSearchType(match.type)) | 423 if (AutocompleteMatch::IsSearchType(match.type)) |
| 426 return; | 424 return; |
| 427 *url = match.destination_url; | 425 *url = match.destination_url; |
| 428 | 426 |
| 429 // Prefix the text with 'http://' if the text doesn't start with 'http://', | 427 // Prefix the text with 'http://' if the text doesn't start with 'http://', |
| 430 // the text parses as a url with a scheme of http, the user selected the | 428 // the text parses as a url with a scheme of http, the user selected the |
| 431 // entire host, and the user hasn't edited the host or manually removed the | 429 // entire host, and the user hasn't edited the host or manually removed the |
| 432 // scheme. | 430 // scheme. |
| 433 GURL perm_url(PermanentURL()); | 431 GURL perm_url(PermanentURL()); |
| 434 if (perm_url.SchemeIs(url::kHttpScheme) && | 432 if (perm_url.SchemeIs(url::kHttpScheme) && |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 | 528 |
| 531 void OmniboxEditModel::StopAutocomplete() { | 529 void OmniboxEditModel::StopAutocomplete() { |
| 532 autocomplete_controller()->Stop(true); | 530 autocomplete_controller()->Stop(true); |
| 533 } | 531 } |
| 534 | 532 |
| 535 bool OmniboxEditModel::CanPasteAndGo(const base::string16& text) const { | 533 bool OmniboxEditModel::CanPasteAndGo(const base::string16& text) const { |
| 536 if (!client_->IsPasteAndGoEnabled()) | 534 if (!client_->IsPasteAndGoEnabled()) |
| 537 return false; | 535 return false; |
| 538 | 536 |
| 539 AutocompleteMatch match; | 537 AutocompleteMatch match; |
| 540 ClassifyStringForPasteAndGo(text, &match, NULL); | 538 ClassifyStringForPasteAndGo(text, &match, nullptr); |
| 541 return match.destination_url.is_valid(); | 539 return match.destination_url.is_valid(); |
| 542 } | 540 } |
| 543 | 541 |
| 544 void OmniboxEditModel::PasteAndGo(const base::string16& text) { | 542 void OmniboxEditModel::PasteAndGo(const base::string16& text) { |
| 545 DCHECK(CanPasteAndGo(text)); | 543 DCHECK(CanPasteAndGo(text)); |
| 546 UMA_HISTOGRAM_COUNTS("Omnibox.PasteAndGo", 1); | 544 UMA_HISTOGRAM_COUNTS("Omnibox.PasteAndGo", 1); |
| 547 | 545 |
| 548 view_->RevertAll(); | 546 view_->RevertAll(); |
| 549 AutocompleteMatch match; | 547 AutocompleteMatch match; |
| 550 GURL alternate_nav_url; | 548 GURL alternate_nav_url; |
| 551 ClassifyStringForPasteAndGo(text, &match, &alternate_nav_url); | 549 ClassifyStringForPasteAndGo(text, &match, &alternate_nav_url); |
| 552 view_->OpenMatch(match, CURRENT_TAB, alternate_nav_url, text, | 550 view_->OpenMatch(match, CURRENT_TAB, alternate_nav_url, text, |
| 553 OmniboxPopupModel::kNoMatch); | 551 OmniboxPopupModel::kNoMatch); |
| 554 } | 552 } |
| 555 | 553 |
| 556 bool OmniboxEditModel::IsPasteAndSearch(const base::string16& text) const { | 554 bool OmniboxEditModel::IsPasteAndSearch(const base::string16& text) const { |
| 557 AutocompleteMatch match; | 555 AutocompleteMatch match; |
| 558 ClassifyStringForPasteAndGo(text, &match, NULL); | 556 ClassifyStringForPasteAndGo(text, &match, nullptr); |
| 559 return AutocompleteMatch::IsSearchType(match.type); | 557 return AutocompleteMatch::IsSearchType(match.type); |
| 560 } | 558 } |
| 561 | 559 |
| 562 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition, | 560 void OmniboxEditModel::AcceptInput(WindowOpenDisposition disposition, |
| 563 bool for_drop) { | 561 bool for_drop) { |
| 564 // Get the URL and transition type for the selected entry. | 562 // Get the URL and transition type for the selected entry. |
| 565 GURL alternate_nav_url; | 563 GURL alternate_nav_url; |
| 566 AutocompleteMatch match = CurrentMatch(&alternate_nav_url); | 564 AutocompleteMatch match = CurrentMatch(&alternate_nav_url); |
| 567 | 565 |
| 568 // If CTRL is down it means the user wants to append ".com" to the text he | 566 // If CTRL is down it means the user wants to append ".com" to the text he |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 676 | 674 |
| 677 base::TimeDelta elapsed_time_since_last_change_to_default_match( | 675 base::TimeDelta elapsed_time_since_last_change_to_default_match( |
| 678 now - autocomplete_controller()->last_time_default_match_changed()); | 676 now - autocomplete_controller()->last_time_default_match_changed()); |
| 679 DCHECK(match.provider); | 677 DCHECK(match.provider); |
| 680 // These elapsed times don't really make sense for ZeroSuggest matches | 678 // These elapsed times don't really make sense for ZeroSuggest matches |
| 681 // (because the user does not modify the omnibox for ZeroSuggest), so for | 679 // (because the user does not modify the omnibox for ZeroSuggest), so for |
| 682 // those we set the elapsed times to something that will be ignored by | 680 // those we set the elapsed times to something that will be ignored by |
| 683 // metrics_log.cc. They also don't necessarily make sense if the omnibox | 681 // metrics_log.cc. They also don't necessarily make sense if the omnibox |
| 684 // dropdown is closed or the user used a paste-and-go action. (In most | 682 // dropdown is closed or the user used a paste-and-go action. (In most |
| 685 // cases when this happens, the user never modified the omnibox.) | 683 // cases when this happens, the user never modified the omnibox.) |
| 684 const bool popup_open = PopupIsOpen(); |
| 686 if ((match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) || | 685 if ((match.provider->type() == AutocompleteProvider::TYPE_ZERO_SUGGEST) || |
| 687 !popup_model()->IsOpen() || !pasted_text.empty()) { | 686 !popup_open || !pasted_text.empty()) { |
| 688 const base::TimeDelta default_time_delta = | 687 const base::TimeDelta default_time_delta = |
| 689 base::TimeDelta::FromMilliseconds(-1); | 688 base::TimeDelta::FromMilliseconds(-1); |
| 690 elapsed_time_since_user_first_modified_omnibox = default_time_delta; | 689 elapsed_time_since_user_first_modified_omnibox = default_time_delta; |
| 691 elapsed_time_since_last_change_to_default_match = default_time_delta; | 690 elapsed_time_since_last_change_to_default_match = default_time_delta; |
| 692 } | 691 } |
| 693 // If the popup is closed or this is a paste-and-go action (meaning the | 692 // If the popup is closed or this is a paste-and-go action (meaning the |
| 694 // contents of the dropdown are ignored regardless), we record for logging | 693 // contents of the dropdown are ignored regardless), we record for logging |
| 695 // purposes a selected_index of 0 and a suggestion list as having a single | 694 // purposes a selected_index of 0 and a suggestion list as having a single |
| 696 // entry of the match used. | 695 // entry of the match used. |
| 696 const bool dropdown_ignored = !popup_open || !pasted_text.empty(); |
| 697 ACMatches fake_single_entry_matches; | 697 ACMatches fake_single_entry_matches; |
| 698 fake_single_entry_matches.push_back(match); | 698 fake_single_entry_matches.push_back(match); |
| 699 AutocompleteResult fake_single_entry_result; | 699 AutocompleteResult fake_single_entry_result; |
| 700 fake_single_entry_result.AppendMatches(input_, fake_single_entry_matches); | 700 fake_single_entry_result.AppendMatches(input_, fake_single_entry_matches); |
| 701 OmniboxLog log( | 701 OmniboxLog log( |
| 702 input_.from_omnibox_focus() ? base::string16() : input_text, | 702 input_.from_omnibox_focus() ? base::string16() : input_text, |
| 703 just_deleted_text_, | 703 just_deleted_text_, |
| 704 input_.type(), | 704 input_.type(), |
| 705 popup_model()->IsOpen(), | 705 popup_open, |
| 706 (!popup_model()->IsOpen() || !pasted_text.empty()) ? 0 : index, | 706 dropdown_ignored ? 0 : index, |
| 707 !pasted_text.empty(), | 707 !pasted_text.empty(), |
| 708 -1, // don't yet know tab ID; set later if appropriate | 708 -1, // don't yet know tab ID; set later if appropriate |
| 709 ClassifyPage(), | 709 ClassifyPage(), |
| 710 elapsed_time_since_user_first_modified_omnibox, | 710 elapsed_time_since_user_first_modified_omnibox, |
| 711 match.allowed_to_be_default_match ? match.inline_autocompletion.length() : | 711 match.allowed_to_be_default_match ? match.inline_autocompletion.length() : |
| 712 base::string16::npos, | 712 base::string16::npos, |
| 713 elapsed_time_since_last_change_to_default_match, | 713 elapsed_time_since_last_change_to_default_match, |
| 714 (!popup_model()->IsOpen() || !pasted_text.empty()) ? | 714 dropdown_ignored ? fake_single_entry_result : result()); |
| 715 fake_single_entry_result : result()); | 715 DCHECK(dropdown_ignored || |
| 716 DCHECK(!popup_model()->IsOpen() || !pasted_text.empty() || | |
| 717 (log.elapsed_time_since_user_first_modified_omnibox >= | 716 (log.elapsed_time_since_user_first_modified_omnibox >= |
| 718 log.elapsed_time_since_last_change_to_default_match)) | 717 log.elapsed_time_since_last_change_to_default_match)) |
| 719 << "We should've got the notification that the user modified the " | 718 << "We should've got the notification that the user modified the " |
| 720 << "omnibox text at same time or before the most recent time the " | 719 << "omnibox text at same time or before the most recent time the " |
| 721 << "default match changed."; | 720 << "default match changed."; |
| 722 | 721 |
| 723 if ((disposition == CURRENT_TAB) && client_->CurrentPageExists()) { | 722 if ((disposition == CURRENT_TAB) && client_->CurrentPageExists()) { |
| 724 // If we know the destination is being opened in the current tab, | 723 // If we know the destination is being opened in the current tab, |
| 725 // we can easily get the tab ID. (If it's being opened in a new | 724 // we can easily get the tab ID. (If it's being opened in a new |
| 726 // tab, we don't know the tab ID yet.) | 725 // tab, we don't know the tab ID yet.) |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 DCHECK(is_keyword_hint_ && !keyword_.empty()); | 807 DCHECK(is_keyword_hint_ && !keyword_.empty()); |
| 809 | 808 |
| 810 autocomplete_controller()->Stop(false); | 809 autocomplete_controller()->Stop(false); |
| 811 | 810 |
| 812 is_keyword_hint_ = false; | 811 is_keyword_hint_ = false; |
| 813 keyword_mode_entry_method_ = entry_method; | 812 keyword_mode_entry_method_ = entry_method; |
| 814 user_text_ = MaybeStripKeyword(user_text_); | 813 user_text_ = MaybeStripKeyword(user_text_); |
| 815 | 814 |
| 816 user_text_ = MaybeStripKeyword(user_text_); | 815 user_text_ = MaybeStripKeyword(user_text_); |
| 817 | 816 |
| 818 if (popup_model() && popup_model()->IsOpen()) | 817 if (PopupIsOpen()) |
| 819 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); | 818 popup_model()->SetSelectedLineState(OmniboxPopupModel::KEYWORD); |
| 820 else | 819 else |
| 821 StartAutocomplete(false, true, true); | 820 StartAutocomplete(false, true, true); |
| 822 | 821 |
| 823 // When entering keyword mode via tab, the new text to show is whatever the | 822 // When entering keyword mode via tab, the new text to show is whatever the |
| 824 // newly-selected match in the dropdown is. When entering via space, however, | 823 // newly-selected match in the dropdown is. When entering via space, however, |
| 825 // we should make sure to use the actual |user_text_| as the basis for the new | 824 // we should make sure to use the actual |user_text_| as the basis for the new |
| 826 // text. This ensures that if the user types "<keyword><space>" and the | 825 // text. This ensures that if the user types "<keyword><space>" and the |
| 827 // default match would have inline autocompleted a further string (e.g. | 826 // default match would have inline autocompleted a further string (e.g. |
| 828 // because there's a past multi-word search beginning with this keyword), the | 827 // because there's a past multi-word search beginning with this keyword), the |
| 829 // inline autocompletion doesn't get filled in as the keyword search query | 828 // inline autocompletion doesn't get filled in as the keyword search query |
| 830 // text. | 829 // text. |
| 831 // | 830 // |
| 832 // We also treat tabbing into keyword mode like tabbing through the popup in | 831 // We also treat tabbing into keyword mode like tabbing through the popup in |
| 833 // that we set |has_temporary_text_|, whereas pressing space is treated like | 832 // that we set |has_temporary_text_|, whereas pressing space is treated like |
| 834 // a new keystroke that changes the current match instead of overlaying it | 833 // a new keystroke that changes the current match instead of overlaying it |
| 835 // with a temporary one. This is important because rerunning autocomplete | 834 // with a temporary one. This is important because rerunning autocomplete |
| 836 // after the user pressed space, which will have happened just before reaching | 835 // after the user pressed space, which will have happened just before reaching |
| 837 // here, may have generated a new match, which the user won't actually see and | 836 // here, may have generated a new match, which the user won't actually see and |
| 838 // which we don't want to switch back to when existing keyword mode; see | 837 // which we don't want to switch back to when existing keyword mode; see |
| 839 // comments in ClearKeyword(). | 838 // comments in ClearKeyword(). |
| 840 if (entry_method == KeywordModeEntryMethod::TAB) { | 839 if (entry_method == KeywordModeEntryMethod::TAB) { |
| 841 // Ensure the current selection is saved before showing keyword mode | 840 // Ensure the current selection is saved before showing keyword mode |
| 842 // so that moving to another line and then reverting the text will restore | 841 // so that moving to another line and then reverting the text will restore |
| 843 // the current state properly. | 842 // the current state properly. |
| 844 bool save_original_selection = !has_temporary_text_; | 843 bool save_original_selection = !has_temporary_text_; |
| 845 has_temporary_text_ = true; | 844 has_temporary_text_ = true; |
| 846 const AutocompleteMatch& match = CurrentMatch(NULL); | 845 const AutocompleteMatch& match = CurrentMatch(nullptr); |
| 847 original_url_ = match.destination_url; | 846 original_url_ = match.destination_url; |
| 848 view_->OnTemporaryTextMaybeChanged( | 847 view_->OnTemporaryTextMaybeChanged( |
| 849 MaybeStripKeyword(match.fill_into_edit), save_original_selection, | 848 MaybeStripKeyword(match.fill_into_edit), save_original_selection, |
| 850 true); | 849 true); |
| 851 } else { | 850 } else { |
| 852 view_->OnTemporaryTextMaybeChanged(user_text_, false, true); | 851 view_->OnTemporaryTextMaybeChanged(user_text_, false, true); |
| 853 } | 852 } |
| 854 | 853 |
| 855 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint")); | 854 base::RecordAction(base::UserMetricsAction("AcceptedKeywordHint")); |
| 856 UMA_HISTOGRAM_ENUMERATION( | 855 UMA_HISTOGRAM_ENUMERATION( |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 void OmniboxEditModel::OnKillFocus() { | 1009 void OmniboxEditModel::OnKillFocus() { |
| 1011 SetFocusState(OMNIBOX_FOCUS_NONE, OMNIBOX_FOCUS_CHANGE_EXPLICIT); | 1010 SetFocusState(OMNIBOX_FOCUS_NONE, OMNIBOX_FOCUS_CHANGE_EXPLICIT); |
| 1012 focus_source_ = INVALID; | 1011 focus_source_ = INVALID; |
| 1013 control_key_state_ = UP; | 1012 control_key_state_ = UP; |
| 1014 paste_state_ = NONE; | 1013 paste_state_ = NONE; |
| 1015 } | 1014 } |
| 1016 | 1015 |
| 1017 bool OmniboxEditModel::WillHandleEscapeKey() const { | 1016 bool OmniboxEditModel::WillHandleEscapeKey() const { |
| 1018 return user_input_in_progress_ || | 1017 return user_input_in_progress_ || |
| 1019 (has_temporary_text_ && | 1018 (has_temporary_text_ && |
| 1020 (CurrentMatch(NULL).destination_url != original_url_)); | 1019 (CurrentMatch(nullptr).destination_url != original_url_)); |
| 1021 } | 1020 } |
| 1022 | 1021 |
| 1023 bool OmniboxEditModel::OnEscapeKeyPressed() { | 1022 bool OmniboxEditModel::OnEscapeKeyPressed() { |
| 1024 if (has_temporary_text_ && | 1023 if (has_temporary_text_ && |
| 1025 (CurrentMatch(NULL).destination_url != original_url_)) { | 1024 (CurrentMatch(nullptr).destination_url != original_url_)) { |
| 1026 RevertTemporaryText(true); | 1025 RevertTemporaryText(true); |
| 1027 return true; | 1026 return true; |
| 1028 } | 1027 } |
| 1029 | 1028 |
| 1030 // We do not clear the pending entry from the omnibox when a load is first | 1029 // We do not clear the pending entry from the omnibox when a load is first |
| 1031 // stopped. If the user presses Escape while stopped, whether editing or not, | 1030 // stopped. If the user presses Escape while stopped, whether editing or not, |
| 1032 // we clear it. | 1031 // we clear it. |
| 1033 if (client_->CurrentPageExists() && !client_->IsLoading()) { | 1032 if (client_->CurrentPageExists() && !client_->IsLoading()) { |
| 1034 client_->DiscardNonCommittedNavigations(); | 1033 client_->DiscardNonCommittedNavigations(); |
| 1035 view_->Update(); | 1034 view_->Update(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1063 control_key_state_ = pressed ? DOWN_WITHOUT_CHANGE : UP; | 1062 control_key_state_ = pressed ? DOWN_WITHOUT_CHANGE : UP; |
| 1064 } | 1063 } |
| 1065 | 1064 |
| 1066 void OmniboxEditModel::OnPaste() { | 1065 void OmniboxEditModel::OnPaste() { |
| 1067 UMA_HISTOGRAM_COUNTS("Omnibox.Paste", 1); | 1066 UMA_HISTOGRAM_COUNTS("Omnibox.Paste", 1); |
| 1068 paste_state_ = PASTING; | 1067 paste_state_ = PASTING; |
| 1069 } | 1068 } |
| 1070 | 1069 |
| 1071 void OmniboxEditModel::OnUpOrDownKeyPressed(int count) { | 1070 void OmniboxEditModel::OnUpOrDownKeyPressed(int count) { |
| 1072 // NOTE: This purposefully doesn't trigger any code that resets paste_state_. | 1071 // NOTE: This purposefully doesn't trigger any code that resets paste_state_. |
| 1073 if (popup_model() && popup_model()->IsOpen()) { | 1072 if (PopupIsOpen()) { |
| 1074 // The popup is open, so the user should be able to interact with it | 1073 // The popup is open, so the user should be able to interact with it |
| 1075 // normally. | 1074 // normally. |
| 1076 popup_model()->Move(count); | 1075 popup_model()->Move(count); |
| 1077 return; | 1076 return; |
| 1078 } | 1077 } |
| 1079 | 1078 |
| 1080 if (!query_in_progress()) { | 1079 if (!query_in_progress()) { |
| 1081 // The popup is neither open nor working on a query already. So, start an | 1080 // The popup is neither open nor working on a query already. So, start an |
| 1082 // autocomplete query for the current text. This also sets | 1081 // autocomplete query for the current text. This also sets |
| 1083 // user_input_in_progress_ to true, which we want: if the user has started | 1082 // user_input_in_progress_ to true, which we want: if the user has started |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1114 if (!keyword_was_selected && is_keyword_selected()) { | 1113 if (!keyword_was_selected && is_keyword_selected()) { |
| 1115 // We just entered keyword mode, so remove the keyword from the input. | 1114 // We just entered keyword mode, so remove the keyword from the input. |
| 1116 user_text_ = MaybeStripKeyword(user_text_); | 1115 user_text_ = MaybeStripKeyword(user_text_); |
| 1117 } | 1116 } |
| 1118 | 1117 |
| 1119 // |is_keyword_hint_| should always be false if |keyword_| is empty. | 1118 // |is_keyword_hint_| should always be false if |keyword_| is empty. |
| 1120 DCHECK(!keyword_.empty() || !is_keyword_hint_); | 1119 DCHECK(!keyword_.empty() || !is_keyword_hint_); |
| 1121 } | 1120 } |
| 1122 | 1121 |
| 1123 // Handle changes to temporary text. | 1122 // Handle changes to temporary text. |
| 1124 if (destination_for_temporary_text_change != NULL) { | 1123 if (destination_for_temporary_text_change) { |
| 1125 const bool save_original_selection = !has_temporary_text_; | 1124 const bool save_original_selection = !has_temporary_text_; |
| 1126 if (save_original_selection) { | 1125 if (save_original_selection) { |
| 1127 // Save the original selection and URL so it can be reverted later. | 1126 // Save the original selection and URL so it can be reverted later. |
| 1128 has_temporary_text_ = true; | 1127 has_temporary_text_ = true; |
| 1129 original_url_ = *destination_for_temporary_text_change; | 1128 original_url_ = *destination_for_temporary_text_change; |
| 1130 inline_autocomplete_text_.clear(); | 1129 inline_autocomplete_text_.clear(); |
| 1131 view_->OnInlineAutocompleteTextCleared(); | 1130 view_->OnInlineAutocompleteTextCleared(); |
| 1132 } | 1131 } |
| 1133 if (control_key_state_ == DOWN_WITHOUT_CHANGE) { | 1132 if (control_key_state_ == DOWN_WITHOUT_CHANGE) { |
| 1134 // Arrowing around the popup cancels control-enter. | 1133 // Arrowing around the popup cancels control-enter. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 // that case the RevertTemporaryText() call below will reset the caret or | 1168 // that case the RevertTemporaryText() call below will reset the caret or |
| 1170 // selection correctly so the caret positioning we do here won't matter. | 1169 // selection correctly so the caret positioning we do here won't matter. |
| 1171 view_->SetWindowTextAndCaretPos(user_text, 0, false, false); | 1170 view_->SetWindowTextAndCaretPos(user_text, 0, false, false); |
| 1172 } else if (view_->OnInlineAutocompleteTextMaybeChanged( | 1171 } else if (view_->OnInlineAutocompleteTextMaybeChanged( |
| 1173 user_text + inline_autocomplete_text_, user_text.length())) { | 1172 user_text + inline_autocomplete_text_, user_text.length())) { |
| 1174 call_controller_onchanged = false; | 1173 call_controller_onchanged = false; |
| 1175 } | 1174 } |
| 1176 | 1175 |
| 1177 // If |has_temporary_text_| is true, then we previously had a manual selection | 1176 // If |has_temporary_text_| is true, then we previously had a manual selection |
| 1178 // but now don't (or |destination_for_temporary_text_change| would have been | 1177 // but now don't (or |destination_for_temporary_text_change| would have been |
| 1179 // non-NULL). This can happen when deleting the selected item in the popup. | 1178 // non-null). This can happen when deleting the selected item in the popup. |
| 1180 // In this case, we've already reverted the popup to the default match, so we | 1179 // In this case, we've already reverted the popup to the default match, so we |
| 1181 // need to revert ourselves as well. | 1180 // need to revert ourselves as well. |
| 1182 if (has_temporary_text_) { | 1181 if (has_temporary_text_) { |
| 1183 RevertTemporaryText(false); | 1182 RevertTemporaryText(false); |
| 1184 call_controller_onchanged = false; | 1183 call_controller_onchanged = false; |
| 1185 } | 1184 } |
| 1186 | 1185 |
| 1187 // We need to invoke OnChanged in case the destination url changed (as could | 1186 // We need to invoke OnChanged in case the destination url changed (as could |
| 1188 // happen when control is toggled). | 1187 // happen when control is toggled). |
| 1189 if (call_controller_onchanged) | 1188 if (call_controller_onchanged) |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1331 base::string16 keyword; | 1330 base::string16 keyword; |
| 1332 bool is_keyword_hint; | 1331 bool is_keyword_hint; |
| 1333 TemplateURLService* service = client_->GetTemplateURLService(); | 1332 TemplateURLService* service = client_->GetTemplateURLService(); |
| 1334 match.GetKeywordUIState(service, &keyword, &is_keyword_hint); | 1333 match.GetKeywordUIState(service, &keyword, &is_keyword_hint); |
| 1335 if (popup_model()) | 1334 if (popup_model()) |
| 1336 popup_model()->OnResultChanged(); | 1335 popup_model()->OnResultChanged(); |
| 1337 // OnPopupDataChanged() resets OmniboxController's |current_match_| early | 1336 // OnPopupDataChanged() resets OmniboxController's |current_match_| early |
| 1338 // on. Therefore, copy match.inline_autocompletion to a temp to preserve | 1337 // on. Therefore, copy match.inline_autocompletion to a temp to preserve |
| 1339 // its value across the entire call. | 1338 // its value across the entire call. |
| 1340 const base::string16 inline_autocompletion(match.inline_autocompletion); | 1339 const base::string16 inline_autocompletion(match.inline_autocompletion); |
| 1341 OnPopupDataChanged(inline_autocompletion, NULL, keyword, is_keyword_hint); | 1340 OnPopupDataChanged(inline_autocompletion, nullptr, keyword, is_keyword_hint); |
| 1342 } | 1341 } |
| 1343 | 1342 |
| 1344 // static | 1343 // static |
| 1345 const char OmniboxEditModel::kCutOrCopyAllTextHistogram[] = | 1344 const char OmniboxEditModel::kCutOrCopyAllTextHistogram[] = |
| 1346 "Omnibox.CutOrCopyAllText"; | 1345 "Omnibox.CutOrCopyAllText"; |
| 1347 | 1346 |
| 1348 bool OmniboxEditModel::query_in_progress() const { | 1347 bool OmniboxEditModel::PopupIsOpen() const { |
| 1349 return !autocomplete_controller()->done(); | 1348 return popup_model() && popup_model()->IsOpen(); |
| 1350 } | 1349 } |
| 1351 | 1350 |
| 1352 void OmniboxEditModel::InternalSetUserText(const base::string16& text) { | 1351 void OmniboxEditModel::InternalSetUserText(const base::string16& text) { |
| 1353 user_text_ = text; | 1352 user_text_ = text; |
| 1354 just_deleted_text_ = false; | 1353 just_deleted_text_ = false; |
| 1355 inline_autocomplete_text_.clear(); | 1354 inline_autocomplete_text_.clear(); |
| 1356 view_->OnInlineAutocompleteTextCleared(); | 1355 view_->OnInlineAutocompleteTextCleared(); |
| 1357 } | 1356 } |
| 1358 | 1357 |
| 1359 void OmniboxEditModel::ClearPopupKeywordMode() const { | 1358 void OmniboxEditModel::ClearPopupKeywordMode() const { |
| 1360 omnibox_controller_->ClearPopupKeywordMode(); | 1359 omnibox_controller_->ClearPopupKeywordMode(); |
| 1361 } | 1360 } |
| 1362 | 1361 |
| 1363 base::string16 OmniboxEditModel::MaybeStripKeyword( | 1362 base::string16 OmniboxEditModel::MaybeStripKeyword( |
| 1364 const base::string16& text) const { | 1363 const base::string16& text) const { |
| 1365 return is_keyword_selected() ? | 1364 return is_keyword_selected() ? |
| 1366 KeywordProvider::SplitReplacementStringFromInput(text, false) : text; | 1365 KeywordProvider::SplitReplacementStringFromInput(text, false) : text; |
| 1367 } | 1366 } |
| 1368 | 1367 |
| 1369 base::string16 OmniboxEditModel::MaybePrependKeyword( | 1368 base::string16 OmniboxEditModel::MaybePrependKeyword( |
| 1370 const base::string16& text) const { | 1369 const base::string16& text) const { |
| 1371 return is_keyword_selected() ? (keyword_ + base::char16(' ') + text) : text; | 1370 return is_keyword_selected() ? (keyword_ + base::char16(' ') + text) : text; |
| 1372 } | 1371 } |
| 1373 | 1372 |
| 1374 void OmniboxEditModel::GetInfoForCurrentText(AutocompleteMatch* match, | 1373 void OmniboxEditModel::GetInfoForCurrentText(AutocompleteMatch* match, |
| 1375 GURL* alternate_nav_url) const { | 1374 GURL* alternate_nav_url) const { |
| 1376 DCHECK(match != NULL); | 1375 DCHECK(match); |
| 1377 | 1376 |
| 1378 if (controller_->GetToolbarModel()->WouldPerformSearchTermReplacement( | 1377 if (controller_->GetToolbarModel()->WouldPerformSearchTermReplacement( |
| 1379 false)) { | 1378 false)) { |
| 1380 // Any time the user hits enter on the unchanged omnibox, we should reload. | 1379 // Any time the user hits enter on the unchanged omnibox, we should reload. |
| 1381 // When we're not extracting search terms, AcceptInput() will take care of | 1380 // When we're not extracting search terms, AcceptInput() will take care of |
| 1382 // this (see code referring to PAGE_TRANSITION_RELOAD there), but when we're | 1381 // this (see code referring to PAGE_TRANSITION_RELOAD there), but when we're |
| 1383 // extracting search terms, the conditionals there won't fire, so we | 1382 // extracting search terms, the conditionals there won't fire, so we |
| 1384 // explicitly set up a match that will reload here. | 1383 // explicitly set up a match that will reload here. |
| 1385 | 1384 |
| 1386 // It's important that we fetch the current visible URL to reload instead of | 1385 // It's important that we fetch the current visible URL to reload instead of |
| 1387 // just getting a "search what you typed" URL from | 1386 // just getting a "search what you typed" URL from |
| 1388 // SearchProvider::CreateSearchSuggestion(), since the user may be in a | 1387 // SearchProvider::CreateSearchSuggestion(), since the user may be in a |
| 1389 // non-default search mode such as image search. | 1388 // non-default search mode such as image search. |
| 1390 match->type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; | 1389 match->type = AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED; |
| 1391 match->provider = autocomplete_controller()->search_provider(); | 1390 match->provider = autocomplete_controller()->search_provider(); |
| 1392 match->destination_url = client_->GetURL(); | 1391 match->destination_url = client_->GetURL(); |
| 1393 match->transition = ui::PAGE_TRANSITION_RELOAD; | 1392 match->transition = ui::PAGE_TRANSITION_RELOAD; |
| 1394 } else if (query_in_progress() || | 1393 } else if (query_in_progress() || PopupIsOpen()) { |
| 1395 (popup_model() && popup_model()->IsOpen())) { | |
| 1396 if (query_in_progress()) { | 1394 if (query_in_progress()) { |
| 1397 // It's technically possible for |result| to be empty if no provider | 1395 // It's technically possible for |result| to be empty if no provider |
| 1398 // returns a synchronous result but the query has not completed | 1396 // returns a synchronous result but the query has not completed |
| 1399 // synchronously; pratically, however, that should never actually happen. | 1397 // synchronously; pratically, however, that should never actually happen. |
| 1400 if (result().empty()) | 1398 if (result().empty()) |
| 1401 return; | 1399 return; |
| 1402 // The user cannot have manually selected a match, or the query would have | 1400 // The user cannot have manually selected a match, or the query would have |
| 1403 // stopped. So the default match must be the desired selection. | 1401 // stopped. So the default match must be the desired selection. |
| 1404 *match = *result().default_match(); | 1402 *match = *result().default_match(); |
| 1405 } else { | 1403 } else { |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1528 // Update state and notify view if the omnibox has focus and the caret | 1526 // Update state and notify view if the omnibox has focus and the caret |
| 1529 // visibility changed. | 1527 // visibility changed. |
| 1530 const bool was_caret_visible = is_caret_visible(); | 1528 const bool was_caret_visible = is_caret_visible(); |
| 1531 focus_state_ = state; | 1529 focus_state_ = state; |
| 1532 if (focus_state_ != OMNIBOX_FOCUS_NONE && | 1530 if (focus_state_ != OMNIBOX_FOCUS_NONE && |
| 1533 is_caret_visible() != was_caret_visible) | 1531 is_caret_visible() != was_caret_visible) |
| 1534 view_->ApplyCaretVisibility(); | 1532 view_->ApplyCaretVisibility(); |
| 1535 | 1533 |
| 1536 client_->OnFocusChanged(focus_state_, reason); | 1534 client_->OnFocusChanged(focus_state_, reason); |
| 1537 } | 1535 } |
| OLD | NEW |