Chromium Code Reviews| 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 "chrome/browser/instant/instant_controller.h" | 5 #include "chrome/browser/instant/instant_controller.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 bool IsFullHeight(const InstantModel& model) { | 158 bool IsFullHeight(const InstantModel& model) { |
| 159 return model.height() == 100 && model.height_units() == INSTANT_SIZE_PERCENT; | 159 return model.height() == 100 && model.height_units() == INSTANT_SIZE_PERCENT; |
| 160 } | 160 } |
| 161 | 161 |
| 162 } // namespace | 162 } // namespace |
| 163 | 163 |
| 164 // static | 164 // static |
| 165 const char* InstantController::kLocalOmniboxPopupURL = | 165 const char* InstantController::kLocalOmniboxPopupURL = |
| 166 "chrome://local-omnibox-popup/local-omnibox-popup.html"; | 166 "chrome://local-omnibox-popup/local-omnibox-popup.html"; |
| 167 | 167 |
| 168 // static | |
| 169 const std::string InstantController::kSearchTermsKey = "search_terms"; | |
| 170 | |
| 168 InstantController::InstantController(chrome::BrowserInstantController* browser, | 171 InstantController::InstantController(chrome::BrowserInstantController* browser, |
| 169 bool extended_enabled, | 172 bool extended_enabled, |
| 170 bool use_local_preview_only) | 173 bool use_local_preview_only) |
| 171 : browser_(browser), | 174 : browser_(browser), |
| 172 extended_enabled_(extended_enabled), | 175 extended_enabled_(extended_enabled), |
| 173 instant_enabled_(false), | 176 instant_enabled_(false), |
| 174 use_local_preview_only_(use_local_preview_only), | 177 use_local_preview_only_(use_local_preview_only), |
| 175 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 178 model_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 176 last_omnibox_text_has_inline_autocompletion_(false), | 179 last_omnibox_text_has_inline_autocompletion_(false), |
| 177 last_verbatim_(false), | 180 last_verbatim_(false), |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 content::WebContents* InstantController::GetPreviewContents() const { | 474 content::WebContents* InstantController::GetPreviewContents() const { |
| 472 return loader_ ? loader_->contents() : NULL; | 475 return loader_ ? loader_->contents() : NULL; |
| 473 } | 476 } |
| 474 | 477 |
| 475 bool InstantController::IsPreviewingSearchResults() const { | 478 bool InstantController::IsPreviewingSearchResults() const { |
| 476 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && | 479 return model_.mode().is_search_suggestions() && IsFullHeight(model_) && |
| 477 (last_match_was_search_ || | 480 (last_match_was_search_ || |
| 478 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); | 481 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER); |
| 479 } | 482 } |
| 480 | 483 |
| 484 bool InstantController::IsInstantExtendedSearch() const { | |
| 485 return extended_enabled_ && instant_enabled_ && last_match_was_search_; | |
| 486 } | |
| 487 | |
| 481 bool InstantController::CommitIfPossible(InstantCommitType type) { | 488 bool InstantController::CommitIfPossible(InstantCommitType type) { |
| 482 if (!extended_enabled_ && !instant_enabled_) | 489 if (!extended_enabled_ && !instant_enabled_) |
| 483 return false; | 490 return false; |
| 484 | 491 |
| 485 DVLOG(1) << "CommitIfPossible: type=" << type << " last_omnibox_text_='" | 492 DVLOG(1) << "CommitIfPossible: type=" << type << " last_omnibox_text_='" |
| 486 << last_omnibox_text_ << "' last_match_was_search_=" | 493 << last_omnibox_text_ << "' last_match_was_search_=" |
| 487 << last_match_was_search_ << " instant_tab_=" << instant_tab_; | 494 << last_match_was_search_ << " instant_tab_=" << instant_tab_; |
| 488 | 495 |
| 489 // If we are on an already committed search results page, send a submit event | 496 // If we are on an already committed search results page, send a submit event |
| 490 // to the page, but otherwise, nothing else to do. | 497 // to the page, but otherwise, nothing else to do. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 514 | 521 |
| 515 if (type == INSTANT_COMMIT_FOCUS_LOST) | 522 if (type == INSTANT_COMMIT_FOCUS_LOST) |
| 516 loader_->Cancel(last_omnibox_text_); | 523 loader_->Cancel(last_omnibox_text_); |
| 517 else if (type != INSTANT_COMMIT_NAVIGATED && | 524 else if (type != INSTANT_COMMIT_NAVIGATED && |
| 518 type != INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION) | 525 type != INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION) |
| 519 loader_->Submit(last_omnibox_text_); | 526 loader_->Submit(last_omnibox_text_); |
| 520 | 527 |
| 521 content::WebContents* preview = loader_->ReleaseContents(); | 528 content::WebContents* preview = loader_->ReleaseContents(); |
| 522 | 529 |
| 523 if (extended_enabled_) { | 530 if (extended_enabled_) { |
| 524 // Consider what's happening: | |
| 525 // 1. The user has typed a query in the omnibox and committed it (either | |
| 526 // by pressing Enter or clicking on the preview). | |
| 527 // 2. We commit the preview to the tab strip, and tell the page. | |
| 528 // 3. The page will update the URL hash fragment with the query terms. | |
| 529 // After steps 1 and 3, the omnibox will show the query terms. However, if | |
| 530 // the URL we are committing at step 2 doesn't already have query terms, it | |
| 531 // will flash for a brief moment as a plain URL. So, avoid that flicker by | |
| 532 // pretending that the plain URL is actually the typed query terms. | |
| 533 // TODO(samarth,beaudoin): Instead of this hack, we should add a new field | |
| 534 // to NavigationEntry to keep track of what the correct query, if any, is. | |
| 535 content::NavigationEntry* entry = | 531 content::NavigationEntry* entry = |
| 536 preview->GetController().GetVisibleEntry(); | 532 preview->GetController().GetVisibleEntry(); |
| 537 std::string url = entry->GetVirtualURL().spec(); | 533 // Adjust the search terms shown in the omnibox for this query. Hitting |
| 538 if (!google_util::IsInstantExtendedAPIGoogleSearchUrl(url) && | 534 // ENTER searches for what the user typed, so use last_omnibox_text_. |
| 539 google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN, | 535 // Clicking on the overlay commits what is currently showing, so add in the |
| 540 google_util::ALLOW_NON_STANDARD_PORTS)) { | 536 // gray text in that case. |
| 541 // Hitting ENTER searches for what the user typed, so use | 537 std::string query(UTF16ToUTF8(last_omnibox_text_)); |
| 542 // last_omnibox_text_. Clicking on the overlay commits what is currently | 538 if (type != INSTANT_COMMIT_PRESSED_ENTER) |
| 543 // showing, so add in the gray text in that case. | 539 query += UTF16ToUTF8(last_suggestion_.text); |
| 544 std::string query(UTF16ToUTF8(last_omnibox_text_)); | 540 |
| 545 if (type != INSTANT_COMMIT_PRESSED_ENTER) | 541 if (type == INSTANT_COMMIT_CLICKED_QUERY_SUGGESTION || |
| 546 query += UTF16ToUTF8(last_suggestion_.text); | 542 type == INSTANT_COMMIT_FOCUS_LOST) { |
| 543 // We update the virtual URL's hash fragment to the current omnibox text | |
| 544 // to avoid the flash of a URL that may not contain any search terms. The | |
| 545 // virtual URL will get reparsed once it is updated by the page to contain | |
| 546 // the valid search terms. | |
| 547 entry->SetVirtualURL(GURL( | 547 entry->SetVirtualURL(GURL( |
| 548 url + "#q=" + | 548 entry->GetVirtualURL().spec() + "#q=" + |
| 549 net::EscapeQueryParamValue(query, true))); | 549 net::EscapeQueryParamValue(query, true))); |
| 550 chrome::search::SearchTabHelper::FromWebContents(preview)-> | 550 } else { |
| 551 NavigationEntryUpdated(); | 551 // The search terms contained in |query| are valid and should be |
| 552 // persisted, with no need to be reparsed. | |
| 553 entry->SetExtraData(kSearchTermsKey, UTF8ToUTF16(query)); | |
| 552 } | 554 } |
| 555 | |
| 556 chrome::search::SearchTabHelper::FromWebContents(preview)-> | |
| 557 NavigationEntryUpdated(); | |
| 553 } | 558 } |
| 554 | 559 |
| 555 // If the preview page has navigated since the last Update(), we need to add | 560 // If the preview page has navigated since the last Update(), we need to add |
| 556 // the navigation to history ourselves. Else, the page will navigate after | 561 // the navigation to history ourselves. Else, the page will navigate after |
| 557 // commit, and it will be added to history in the usual manner. | 562 // commit, and it will be added to history in the usual manner. |
| 558 const history::HistoryAddPageArgs& last_navigation = | 563 const history::HistoryAddPageArgs& last_navigation = |
| 559 loader_->last_navigation(); | 564 loader_->last_navigation(); |
| 560 if (!last_navigation.url.is_empty()) { | 565 if (!last_navigation.url.is_empty()) { |
| 561 content::NavigationEntry* entry = preview->GetController().GetActiveEntry(); | 566 content::NavigationEntry* entry = preview->GetController().GetActiveEntry(); |
| 562 | 567 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 733 !allow_preview_to_show_search_suggestions_) { | 738 !allow_preview_to_show_search_suggestions_) { |
| 734 return; | 739 return; |
| 735 } | 740 } |
| 736 | 741 |
| 737 InstantSuggestion suggestion; | 742 InstantSuggestion suggestion; |
| 738 if (!suggestions.empty()) | 743 if (!suggestions.empty()) |
| 739 suggestion = suggestions[0]; | 744 suggestion = suggestions[0]; |
| 740 | 745 |
| 741 if (instant_tab_ && search_mode_.is_search_results() && | 746 if (instant_tab_ && search_mode_.is_search_results() && |
| 742 suggestion.behavior == INSTANT_COMPLETE_REPLACE) { | 747 suggestion.behavior == INSTANT_COMPLETE_REPLACE) { |
| 743 // This means a committed page in state search called setValue(). We should | 748 // This means a committed page in state search called setValue(). We should |
|
samarth
2013/01/17 17:40:45
Instead of setting temporary text, calling setValu
| |
| 744 // update the omnibox to reflect what the search page says. | 749 // update the omnibox to reflect what the search page says. |
| 745 browser_->SetInstantSuggestion(suggestion); | 750 browser_->SetInstantSuggestion(suggestion); |
| 746 // Don't update last_omnibox_text_ or last_suggestion_ since the user is not | 751 // Don't update last_omnibox_text_ or last_suggestion_ since the user is not |
| 747 // currently editing text in the omnibox. | 752 // currently editing text in the omnibox. |
| 748 return; | 753 return; |
| 749 } | 754 } |
| 750 | 755 |
| 751 // Ignore if we are not currently accepting search suggestions. | 756 // Ignore if we are not currently accepting search suggestions. |
| 752 if (!search_mode_.is_search_suggestions() || last_omnibox_text_.empty()) | 757 if (!search_mode_.is_search_suggestions() || last_omnibox_text_.empty()) |
| 753 return; | 758 return; |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1225 std::map<std::string, int>::const_iterator iter = | 1230 std::map<std::string, int>::const_iterator iter = |
| 1226 blacklisted_urls_.find(*instant_url); | 1231 blacklisted_urls_.find(*instant_url); |
| 1227 if (iter != blacklisted_urls_.end() && | 1232 if (iter != blacklisted_urls_.end() && |
| 1228 iter->second > kMaxInstantSupportFailures) { | 1233 iter->second > kMaxInstantSupportFailures) { |
| 1229 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | 1234 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); |
| 1230 return false; | 1235 return false; |
| 1231 } | 1236 } |
| 1232 | 1237 |
| 1233 return true; | 1238 return true; |
| 1234 } | 1239 } |
| OLD | NEW |