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 char* InstantController::kInstantExtendedSearchTermsKey = "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); |
sreeram
2013/01/22 04:34:47
This isn't quite right. See https://chromiumcodere
Mathieu
2013/01/22 23:28:15
Done.
| |
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))); |
sreeram
2013/01/22 04:34:47
Huh. Why are we not using SetExtraData here?
Mathieu
2013/01/22 23:28:15
There is a case where grey text is showing and las
| |
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. This is to ensure that the | |
553 // correct terms are displayed in the omnibox (ToolbarModelImpl::GetText). | |
554 entry->SetExtraData(std::string(kInstantExtendedSearchTermsKey), | |
555 UTF8ToUTF16(query)); | |
552 } | 556 } |
557 | |
558 chrome::search::SearchTabHelper::FromWebContents(preview)-> | |
559 NavigationEntryUpdated(); | |
sreeram
2013/01/22 04:34:47
We don't need this anymore, I think. This was orig
Mathieu
2013/01/22 23:28:15
I've added a check for the search terms in Navigat
| |
553 } | 560 } |
554 | 561 |
555 // If the preview page has navigated since the last Update(), we need to add | 562 // 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 | 563 // the navigation to history ourselves. Else, the page will navigate after |
557 // commit, and it will be added to history in the usual manner. | 564 // commit, and it will be added to history in the usual manner. |
558 const history::HistoryAddPageArgs& last_navigation = | 565 const history::HistoryAddPageArgs& last_navigation = |
559 loader_->last_navigation(); | 566 loader_->last_navigation(); |
560 if (!last_navigation.url.is_empty()) { | 567 if (!last_navigation.url.is_empty()) { |
561 content::NavigationEntry* entry = preview->GetController().GetActiveEntry(); | 568 content::NavigationEntry* entry = preview->GetController().GetActiveEntry(); |
562 | 569 |
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1236 std::map<std::string, int>::const_iterator iter = | 1243 std::map<std::string, int>::const_iterator iter = |
1237 blacklisted_urls_.find(*instant_url); | 1244 blacklisted_urls_.find(*instant_url); |
1238 if (iter != blacklisted_urls_.end() && | 1245 if (iter != blacklisted_urls_.end() && |
1239 iter->second > kMaxInstantSupportFailures) { | 1246 iter->second > kMaxInstantSupportFailures) { |
1240 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); | 1247 RecordEventHistogram(INSTANT_CONTROLLER_EVENT_URL_BLOCKED_BY_BLACKLIST); |
1241 return false; | 1248 return false; |
1242 } | 1249 } |
1243 | 1250 |
1244 return true; | 1251 return true; |
1245 } | 1252 } |
OLD | NEW |