Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: chrome/browser/ui/search/instant_controller.cc

Issue 13905008: Merge local_omnibox_popup into local_ntp. Render the Google logo and fakebox if Google is the sear… (Closed) Base URL: https://git.chromium.org/chromium/src.git@master
Patch Set: Addressing comments. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ui/search/instant_controller.h" 5 #include "chrome/browser/ui/search/instant_controller.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 UTF16ToUTF8(user_text).c_str(), UTF16ToUTF8(full_text).c_str(), 261 UTF16ToUTF8(user_text).c_str(), UTF16ToUTF8(full_text).c_str(),
262 static_cast<int>(selection_start), static_cast<int>(selection_end), 262 static_cast<int>(selection_start), static_cast<int>(selection_end),
263 verbatim, user_input_in_progress, omnibox_popup_is_open, escape_pressed, 263 verbatim, user_input_in_progress, omnibox_popup_is_open, escape_pressed,
264 is_keyword_search)); 264 is_keyword_search));
265 265
266 // TODO(dhollowa): Complete keyword match UI. For now just hide suggestions. 266 // TODO(dhollowa): Complete keyword match UI. For now just hide suggestions.
267 // http://crbug.com/153932. Note, this early escape is happens prior to the 267 // http://crbug.com/153932. Note, this early escape is happens prior to the
268 // DCHECKs below because |user_text| and |full_text| have different semantics 268 // DCHECKs below because |user_text| and |full_text| have different semantics
269 // when keyword search is in effect. 269 // when keyword search is in effect.
270 if (is_keyword_search) { 270 if (is_keyword_search) {
271 if (UseInstantTabToShowSuggestions()) 271 if (instant_tab_)
272 instant_tab_->Update(string16(), 0, 0, true); 272 instant_tab_->Update(string16(), 0, 0, true);
273 else 273 else
274 HideOverlay(); 274 HideOverlay();
275 last_match_was_search_ = false; 275 last_match_was_search_ = false;
276 return false; 276 return false;
277 } 277 }
278 278
279 // Ignore spurious updates when the omnibox is blurred; otherwise click 279 // Ignore spurious updates when the omnibox is blurred; otherwise click
280 // targets on the page may vanish before a click event arrives. 280 // targets on the page may vanish before a click event arrives.
281 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE) 281 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 // In non extended mode, Instant is disabled for URLs and keyword mode. 314 // In non extended mode, Instant is disabled for URLs and keyword mode.
315 if (!extended_enabled_ && 315 if (!extended_enabled_ &&
316 (!last_match_was_search_ || 316 (!last_match_was_search_ ||
317 match.type == AutocompleteMatch::SEARCH_OTHER_ENGINE)) { 317 match.type == AutocompleteMatch::SEARCH_OTHER_ENGINE)) {
318 HideOverlay(); 318 HideOverlay();
319 return false; 319 return false;
320 } 320 }
321 321
322 // If we have an |instant_tab_| use it, else ensure we have an overlay that is 322 // If we have an |instant_tab_| use it, else ensure we have an overlay that is
323 // current or is using the local overlay. 323 // current or is using the local overlay.
324 if (!UseInstantTabToShowSuggestions() && 324 if (!instant_tab_ && !(overlay_ && overlay_->IsLocal()) &&
325 !(overlay_ && overlay_->IsLocalOverlay()) &&
326 !EnsureOverlayIsCurrent(false)) { 325 !EnsureOverlayIsCurrent(false)) {
327 HideOverlay(); 326 HideOverlay();
328 return false; 327 return false;
329 } 328 }
330 329
331 if (extended_enabled_) { 330 if (extended_enabled_) {
332 if (!omnibox_popup_is_open) { 331 if (!omnibox_popup_is_open) {
333 if (!user_input_in_progress) { 332 if (!user_input_in_progress) {
334 // If the user isn't typing and the omnibox popup is closed, it means a 333 // If the user isn't typing and the omnibox popup is closed, it means a
335 // regular navigation, tab-switch or the user hitting Escape. 334 // regular navigation, tab-switch or the user hitting Escape.
336 if (UseInstantTabToShowSuggestions()) { 335 if (instant_tab_) {
337 // The user is on a search results page. It may be showing results for 336 // The user is on a search results page. It may be showing results for
338 // a partial query the user typed before they hit Escape. Send the 337 // a partial query the user typed before they hit Escape. Send the
339 // omnibox text to the page to restore the original results. 338 // omnibox text to the page to restore the original results.
340 // 339 //
341 // In a tab switch, |instant_tab_| won't have updated yet, so it may 340 // In a tab switch, |instant_tab_| won't have updated yet, so it may
342 // be pointing to the previous tab (which was a search results page). 341 // be pointing to the previous tab (which was a search results page).
343 // Ensure we don't send the omnibox text to a random webpage (the new 342 // Ensure we don't send the omnibox text to a random webpage (the new
344 // tab), by comparing the old and new WebContents. 343 // tab), by comparing the old and new WebContents.
345 if (escape_pressed && 344 if (escape_pressed &&
346 instant_tab_->contents() == browser_->GetActiveWebContents()) { 345 instant_tab_->contents() == browser_->GetActiveWebContents()) {
(...skipping 12 matching lines...) Expand all
359 // If |full_text| is empty, the user is on the NTP. The overlay may 358 // If |full_text| is empty, the user is on the NTP. The overlay may
360 // be showing custom NTP content; hide only if that's not the case. 359 // be showing custom NTP content; hide only if that's not the case.
361 HideOverlay(); 360 HideOverlay();
362 } 361 }
363 } else if (full_text.empty()) { 362 } else if (full_text.empty()) {
364 // The user is typing, and backspaced away all omnibox text. Clear 363 // The user is typing, and backspaced away all omnibox text. Clear
365 // |last_omnibox_text_| so that we don't attempt to set suggestions. 364 // |last_omnibox_text_| so that we don't attempt to set suggestions.
366 last_omnibox_text_.clear(); 365 last_omnibox_text_.clear();
367 last_user_text_.clear(); 366 last_user_text_.clear();
368 last_suggestion_ = InstantSuggestion(); 367 last_suggestion_ = InstantSuggestion();
369 if (UseInstantTabToShowSuggestions()) { 368 if (instant_tab_) {
370 // On a search results page, tell it to clear old results. 369 // On a search results page, tell it to clear old results.
371 instant_tab_->Update(string16(), 0, 0, true); 370 instant_tab_->Update(string16(), 0, 0, true);
372 } else if (search_mode_.is_origin_ntp()) { 371 } else if (search_mode_.is_origin_ntp()) {
373 // On the NTP, tell the overlay to clear old results. Don't hide the 372 // On the NTP, tell the overlay to clear old results. Don't hide the
374 // overlay so it can show a blank page or logo if it wants. 373 // overlay so it can show a blank page or logo if it wants.
375 overlay_->Update(string16(), 0, 0, true); 374 overlay_->Update(string16(), 0, 0, true);
376 } else { 375 } else {
377 HideOverlay(); 376 HideOverlay();
378 } 377 }
379 } else { 378 } else {
380 // The user switched to a tab with partial text already in the omnibox. 379 // The user switched to a tab with partial text already in the omnibox.
381 HideOverlay(); 380 HideOverlay();
382 381
383 // The new tab may or may not be a search results page; we don't know 382 // The new tab may or may not be a search results page; we don't know
384 // since SearchModeChanged() hasn't been called yet. If it later turns 383 // since SearchModeChanged() hasn't been called yet. If it later turns
385 // out to be, we should store |full_text| now, so that if the user hits 384 // out to be, we should store |full_text| now, so that if the user hits
386 // Enter, we'll send the correct query to instant_tab_->Submit(). If the 385 // Enter, we'll send the correct query to instant_tab_->Submit(). If the
387 // partial text is not a query (|last_match_was_search_| is false), we 386 // partial text is not a query (|last_match_was_search_| is false), we
388 // won't Submit(), so no need to worry about that. 387 // won't Submit(), so no need to worry about that.
389 last_omnibox_text_ = full_text; 388 last_omnibox_text_ = full_text;
390 last_user_text_ = user_text; 389 last_user_text_ = user_text;
391 last_suggestion_ = InstantSuggestion(); 390 last_suggestion_ = InstantSuggestion();
392 } 391 }
393 return false; 392 return false;
394 } else if (full_text.empty()) { 393 } else if (full_text.empty()) {
395 // The user typed a solitary "?". Same as the backspace case above. 394 // The user typed a solitary "?". Same as the backspace case above.
396 last_omnibox_text_.clear(); 395 last_omnibox_text_.clear();
397 last_user_text_.clear(); 396 last_user_text_.clear();
398 last_suggestion_ = InstantSuggestion(); 397 last_suggestion_ = InstantSuggestion();
399 if (UseInstantTabToShowSuggestions()) 398 if (instant_tab_)
400 instant_tab_->Update(string16(), 0, 0, true); 399 instant_tab_->Update(string16(), 0, 0, true);
401 else if (search_mode_.is_origin_ntp()) 400 else if (search_mode_.is_origin_ntp())
402 overlay_->Update(string16(), 0, 0, true); 401 overlay_->Update(string16(), 0, 0, true);
403 else 402 else
404 HideOverlay(); 403 HideOverlay();
405 return false; 404 return false;
406 } 405 }
407 } else if (!omnibox_popup_is_open || full_text.empty()) { 406 } else if (!omnibox_popup_is_open || full_text.empty()) {
408 // In the non-extended case, hide the overlay as long as the user isn't 407 // In the non-extended case, hide the overlay as long as the user isn't
409 // actively typing a non-empty query. 408 // actively typing a non-empty query.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 last_verbatim_ = verbatim; 443 last_verbatim_ = verbatim;
445 444
446 last_transition_type_ = match.transition; 445 last_transition_type_ = match.transition;
447 url_for_history_ = match.destination_url; 446 url_for_history_ = match.destination_url;
448 447
449 // Allow search suggestions. In extended mode, SearchModeChanged() will set 448 // Allow search suggestions. In extended mode, SearchModeChanged() will set
450 // this, but it's not called in non-extended mode, so fake it. 449 // this, but it's not called in non-extended mode, so fake it.
451 if (!extended_enabled_) 450 if (!extended_enabled_)
452 search_mode_.mode = SearchMode::MODE_SEARCH_SUGGESTIONS; 451 search_mode_.mode = SearchMode::MODE_SEARCH_SUGGESTIONS;
453 452
454 if (UseInstantTabToShowSuggestions()) { 453 if (instant_tab_) {
455 // If we have an |instant_tab_| but it doesn't support Instant yet, sever 454 // If we have an |instant_tab_| but it doesn't support Instant yet, sever
456 // the connection to it so we use the overlay instead. This ensures that the 455 // the connection to it so we use the overlay instead. This ensures that the
457 // user interaction will be responsive and handles cases where 456 // user interaction will be responsive and handles cases where
458 // |instant_tab_| never responds about whether it supports Instant. 457 // |instant_tab_| never responds about whether it supports Instant.
459 if (instant_tab_->supports_instant()) 458 if (instant_tab_->supports_instant())
460 instant_tab_->Update(user_text, selection_start, selection_end, verbatim); 459 instant_tab_->Update(user_text, selection_start, selection_end, verbatim);
461 else 460 else
462 instant_tab_.reset(); 461 instant_tab_.reset();
463 } 462 }
464 463
465 if (!UseInstantTabToShowSuggestions()) { 464 if (!instant_tab_) {
466 if (first_interaction_time_.is_null()) 465 if (first_interaction_time_.is_null())
467 first_interaction_time_ = base::Time::Now(); 466 first_interaction_time_ = base::Time::Now();
468 allow_overlay_to_show_search_suggestions_ = true; 467 allow_overlay_to_show_search_suggestions_ = true;
469 468
470 // For extended mode, if the loader is not ready at this point, switch over 469 // For extended mode, if the loader is not ready at this point, switch over
471 // to a backup loader. 470 // to a backup loader.
472 if (extended_enabled_ && !overlay_->supports_instant() && 471 if (extended_enabled_ && !overlay_->supports_instant() &&
473 !overlay_->IsLocalOverlay() && browser_->GetActiveWebContents()) { 472 !overlay_->IsLocal() && browser_->GetActiveWebContents()) {
474 CreateOverlay(chrome::kChromeSearchLocalOmniboxPopupURL, 473 CreateOverlay(chrome::GetLocalInstantURL(browser_->profile()).spec(),
475 browser_->GetActiveWebContents()); 474 browser_->GetActiveWebContents());
476 } 475 }
477 476
478 overlay_->Update(extended_enabled_ ? user_text : full_text, 477 overlay_->Update(extended_enabled_ ? user_text : full_text,
479 selection_start, selection_end, verbatim); 478 selection_start, selection_end, verbatim);
480 } 479 }
481 480
482 content::NotificationService::current()->Notify( 481 content::NotificationService::current()->Notify(
483 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, 482 chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED,
484 content::Source<InstantController>(this), 483 content::Source<InstantController>(this),
485 content::NotificationService::NoDetails()); 484 content::NotificationService::NoDetails());
486 485
487 // We don't have new suggestions yet, but we can either reuse the existing 486 // We don't have new suggestions yet, but we can either reuse the existing
488 // suggestion or reset the existing "gray text". 487 // suggestion or reset the existing "gray text".
489 browser_->SetInstantSuggestion(last_suggestion_); 488 browser_->SetInstantSuggestion(last_suggestion_);
490 489
491 return true; 490 return true;
492 } 491 }
493 492
494 scoped_ptr<content::WebContents> InstantController::ReleaseNTPContents() { 493 scoped_ptr<content::WebContents> InstantController::ReleaseNTPContents() {
495 if (!extended_enabled_ || !browser_->profile() || 494 if (!extended_enabled_ || !browser_->profile() ||
496 browser_->profile()->IsOffTheRecord()) 495 browser_->profile()->IsOffTheRecord())
497 return scoped_ptr<content::WebContents>(NULL); 496 return scoped_ptr<content::WebContents>(NULL);
498 497
499 LOG_INSTANT_DEBUG_EVENT(this, "ReleaseNTPContents"); 498 LOG_INSTANT_DEBUG_EVENT(this, "ReleaseNTPContents");
500 499
501 // Switch to the local NTP unless we're already using one. 500 // TODO(jeremycho): Add tests for this logic.
502 if (!ntp_ || (ShouldSwitchToLocalNTP() && !ntp_->IsLocalNTP())) 501 if (ShouldSwitchToLocalNTP())
503 ResetNTP(false, true); 502 ResetNTP(false, true);
504 503
505 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents(); 504 scoped_ptr<content::WebContents> ntp_contents = ntp_->ReleaseContents();
506 505
507 if (!use_local_page_only_) { 506 if (!use_local_page_only_) {
508 // Preload a new Instant NTP, unless using the local NTP which is not 507 // Preload a new Instant NTP, unless using the local NTP which is not
509 // preloaded to conserve memory. 508 // preloaded to conserve memory.
510 // 509 //
511 // Since this corresponds to a user action, give the remote page another 510 // Since this corresponds to a user action, give the remote page another
512 // try, even if it's blacklisted. We don't want to blacklist pages for the 511 // try, even if it's blacklisted. We don't want to blacklist pages for the
513 // duration of a Browser object (for some people, that's effectively 512 // duration of a Browser object (for some people, that's effectively
514 // "forever"). 513 // "forever").
515 ResetNTP(true, false); 514 ResetNTP(true, false);
515 } else {
516 ntp_.reset();
516 } 517 }
517 return ntp_contents.Pass(); 518 return ntp_contents.Pass();
518 } 519 }
519 520
520 // TODO(tonyg): This method only fires when the omnibox bounds change. It also 521 // TODO(tonyg): This method only fires when the omnibox bounds change. It also
521 // needs to fire when the overlay bounds change (e.g.: open/close info bar). 522 // needs to fire when the overlay bounds change (e.g.: open/close info bar).
522 void InstantController::SetPopupBounds(const gfx::Rect& bounds) { 523 void InstantController::SetPopupBounds(const gfx::Rect& bounds) {
523 if (!extended_enabled_ && !instant_enabled_) 524 if (!extended_enabled_ && !instant_enabled_)
524 return; 525 return;
525 526
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 // as it stops autocomplete. Ignore these. 563 // as it stops autocomplete. Ignore these.
563 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE) 564 if (omnibox_focus_state_ == OMNIBOX_FOCUS_NONE)
564 return; 565 return;
565 566
566 DVLOG(1) << "AutocompleteResults:"; 567 DVLOG(1) << "AutocompleteResults:";
567 std::vector<InstantAutocompleteResult> results; 568 std::vector<InstantAutocompleteResult> results;
568 for (ACProviders::const_iterator provider = providers.begin(); 569 for (ACProviders::const_iterator provider = providers.begin();
569 provider != providers.end(); ++provider) { 570 provider != providers.end(); ++provider) {
570 const bool from_search_provider = 571 const bool from_search_provider =
571 (*provider)->type() == AutocompleteProvider::TYPE_SEARCH; 572 (*provider)->type() == AutocompleteProvider::TYPE_SEARCH;
572 // Unless we are talking to the local overlay, skip SearchProvider, since 573 const bool using_local_page =
573 // it only echoes suggestions. 574 (instant_tab_ && instant_tab_->IsLocal()) ||
574 if (from_search_provider && 575 (!instant_tab_ && overlay_ && overlay_->IsLocal());
575 (UseInstantTabToShowSuggestions() || !overlay_->IsLocalOverlay())) 576 // Unless we are talking to a local page, skip SearchProvider, since it only
577 // echoes suggestions.
578 if (from_search_provider && !using_local_page)
576 continue; 579 continue;
577 // Only send autocomplete results when all the providers are done. Skip 580 // Only send autocomplete results when all the providers are done. Skip
578 // this check for the SearchProvider, since it isn't done until the page 581 // this check for the SearchProvider, since it isn't done until the page
579 // calls SetSuggestions (causing SearchProvider::FinalizeInstantQuery() to 582 // calls SetSuggestions (causing SearchProvider::FinalizeInstantQuery() to
580 // be called), which makes it a chicken-and-egg thing. 583 // be called), which makes it a chicken-and-egg thing.
581 if (!from_search_provider && !(*provider)->done()) { 584 if (!from_search_provider && !(*provider)->done()) {
582 DVLOG(1) << "Waiting for " << (*provider)->GetName(); 585 DVLOG(1) << "Waiting for " << (*provider)->GetName();
583 return; 586 return;
584 } 587 }
585 for (ACMatches::const_iterator match = (*provider)->matches().begin(); 588 for (ACMatches::const_iterator match = (*provider)->matches().begin();
(...skipping 11 matching lines...) Expand all
597 << result.provider << " " << result.destination_url << " '" 600 << result.provider << " " << result.destination_url << " '"
598 << result.description << "' '" << result.search_query << "' " 601 << result.description << "' '" << result.search_query << "' "
599 << result.transition; 602 << result.transition;
600 results.push_back(result); 603 results.push_back(result);
601 } 604 }
602 } 605 }
603 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( 606 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
604 "HandleAutocompleteResults: total_results=%d", 607 "HandleAutocompleteResults: total_results=%d",
605 static_cast<int>(results.size()))); 608 static_cast<int>(results.size())));
606 609
607 if (UseInstantTabToShowSuggestions()) 610 if (instant_tab_)
608 instant_tab_->SendAutocompleteResults(results); 611 instant_tab_->SendAutocompleteResults(results);
609 else 612 else
610 overlay_->SendAutocompleteResults(results); 613 overlay_->SendAutocompleteResults(results);
611 } 614 }
612 615
613 bool InstantController::OnUpOrDownKeyPressed(int count) { 616 bool InstantController::OnUpOrDownKeyPressed(int count) {
614 if (!extended_enabled_) 617 if (!extended_enabled_)
615 return false; 618 return false;
616 619
617 if (!instant_tab_ && !overlay_) 620 if (!instant_tab_ && !overlay_)
618 return false; 621 return false;
619 622
620 if (UseInstantTabToShowSuggestions()) 623 if (instant_tab_)
621 instant_tab_->UpOrDownKeyPressed(count); 624 instant_tab_->UpOrDownKeyPressed(count);
622 else 625 else
623 overlay_->UpOrDownKeyPressed(count); 626 overlay_->UpOrDownKeyPressed(count);
624 627
625 return true; 628 return true;
626 } 629 }
627 630
628 void InstantController::OnCancel(const AutocompleteMatch& match, 631 void InstantController::OnCancel(const AutocompleteMatch& match,
629 const string16& user_text, 632 const string16& user_text,
630 const string16& full_text) { 633 const string16& full_text) {
631 if (!extended_enabled_) 634 if (!extended_enabled_)
632 return; 635 return;
633 636
634 if (!instant_tab_ && !overlay_) 637 if (!instant_tab_ && !overlay_)
635 return; 638 return;
636 639
637 // We manually reset the state here since the JS is not expected to do it. 640 // We manually reset the state here since the JS is not expected to do it.
638 // TODO(sreeram): Handle the case where user_text is now a URL 641 // TODO(sreeram): Handle the case where user_text is now a URL
639 last_match_was_search_ = AutocompleteMatch::IsSearchType(match.type) && 642 last_match_was_search_ = AutocompleteMatch::IsSearchType(match.type) &&
640 !full_text.empty(); 643 !full_text.empty();
641 last_omnibox_text_ = full_text; 644 last_omnibox_text_ = full_text;
642 last_user_text_ = user_text; 645 last_user_text_ = user_text;
643 last_suggestion_ = InstantSuggestion(); 646 last_suggestion_ = InstantSuggestion();
644 647
645 // Say |full_text| is "amazon.com" and |user_text| is "ama". This means the 648 // Say |full_text| is "amazon.com" and |user_text| is "ama". This means the
646 // inline autocompletion is "zon.com"; so the selection should span from 649 // inline autocompletion is "zon.com"; so the selection should span from
647 // user_text.size() to full_text.size(). The selection bounds are inverted 650 // user_text.size() to full_text.size(). The selection bounds are inverted
648 // because the caret is at the end of |user_text|, not |full_text|. 651 // because the caret is at the end of |user_text|, not |full_text|.
649 if (UseInstantTabToShowSuggestions()) { 652 if (instant_tab_) {
650 instant_tab_->CancelSelection(user_text, full_text.size(), user_text.size(), 653 instant_tab_->CancelSelection(user_text, full_text.size(), user_text.size(),
651 last_verbatim_); 654 last_verbatim_);
652 } else { 655 } else {
653 overlay_->CancelSelection(user_text, full_text.size(), user_text.size(), 656 overlay_->CancelSelection(user_text, full_text.size(), user_text.size(),
654 last_verbatim_); 657 last_verbatim_);
655 } 658 }
656 } 659 }
657 660
658 void InstantController::OmniboxNavigateToURL() { 661 void InstantController::OmniboxNavigateToURL() {
659 if (!extended_enabled_) 662 if (!extended_enabled_)
(...skipping 18 matching lines...) Expand all
678 return false; 681 return false;
679 682
680 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( 683 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
681 "CommitIfPossible: type=%d last_omnibox_text_='%s' " 684 "CommitIfPossible: type=%d last_omnibox_text_='%s' "
682 "last_match_was_search_=%d instant_tab_=%d", type, 685 "last_match_was_search_=%d instant_tab_=%d", type,
683 UTF16ToUTF8(last_omnibox_text_).c_str(), last_match_was_search_, 686 UTF16ToUTF8(last_omnibox_text_).c_str(), last_match_was_search_,
684 instant_tab_ != NULL)); 687 instant_tab_ != NULL));
685 688
686 // If we are on an already committed search results page, send a submit event 689 // If we are on an already committed search results page, send a submit event
687 // to the page, but otherwise, nothing else to do. 690 // to the page, but otherwise, nothing else to do.
688 if (UseInstantTabToShowSuggestions()) { 691 if (instant_tab_) {
689 if (type == INSTANT_COMMIT_PRESSED_ENTER && 692 if (type == INSTANT_COMMIT_PRESSED_ENTER &&
693 !instant_tab_->IsLocal() &&
690 (last_match_was_search_ || 694 (last_match_was_search_ ||
691 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER)) { 695 last_suggestion_.behavior == INSTANT_COMPLETE_NEVER)) {
692 last_suggestion_.text.clear(); 696 last_suggestion_.text.clear();
693 instant_tab_->Submit(last_omnibox_text_); 697 instant_tab_->Submit(last_omnibox_text_);
694 instant_tab_->contents()->GetView()->Focus(); 698 instant_tab_->contents()->GetView()->Focus();
695 EnsureSearchTermsAreSet(instant_tab_->contents(), last_omnibox_text_); 699 EnsureSearchTermsAreSet(instant_tab_->contents(), last_omnibox_text_);
696 return true; 700 return true;
697 } 701 }
698 return false; 702 return false;
699 } 703 }
700 704
701 // If the overlay is not showing at all, don't commit it. 705 // If the overlay is not showing at all, don't commit it.
702 if (!model_.mode().is_search_suggestions()) 706 if (!model_.mode().is_search_suggestions())
703 return false; 707 return false;
704 708
705 // If the overlay is showing at full height (with results), commit it. 709 // If the overlay is showing at full height (with results), commit it.
706 // If it's showing at parial height, commit if it's navigating. 710 // If it's showing at parial height, commit if it's navigating.
707 if (!IsOverlayingSearchResults() && type != INSTANT_COMMIT_NAVIGATED) 711 if (!IsOverlayingSearchResults() && type != INSTANT_COMMIT_NAVIGATED)
708 return false; 712 return false;
709 713
710 // There may re-entrance here, from the call to browser_->CommitInstant below, 714 // There may re-entrance here, from the call to browser_->CommitInstant below,
711 // which can cause a TabDeactivated notification which gets back here. 715 // which can cause a TabDeactivated notification which gets back here.
712 // In this case, overlay_->ReleaseContents() was called already. 716 // In this case, overlay_->ReleaseContents() was called already.
713 if (!GetOverlayContents()) 717 if (!GetOverlayContents())
714 return false; 718 return false;
715 719
716 // Never commit the local overlay. 720 // Never commit the local overlay.
717 if (overlay_->IsLocalOverlay()) 721 if (overlay_->IsLocal())
718 return false; 722 return false;
719 723
720 if (type == INSTANT_COMMIT_FOCUS_LOST) { 724 if (type == INSTANT_COMMIT_FOCUS_LOST) {
721 // Extended mode doesn't need or use the Cancel message. 725 // Extended mode doesn't need or use the Cancel message.
722 if (!extended_enabled_) 726 if (!extended_enabled_)
723 overlay_->Cancel(last_omnibox_text_); 727 overlay_->Cancel(last_omnibox_text_);
724 } else if (type != INSTANT_COMMIT_NAVIGATED) { 728 } else if (type != INSTANT_COMMIT_NAVIGATED) {
725 overlay_->Submit(last_omnibox_text_); 729 overlay_->Submit(last_omnibox_text_);
726 } 730 }
727 731
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
946 #if defined(USE_AURA) 950 #if defined(USE_AURA)
947 // On aura the omnibox only receives a focus lost if we initiate the focus 951 // On aura the omnibox only receives a focus lost if we initiate the focus
948 // change. This does that. 952 // change. This does that.
949 if (!model_.mode().is_default()) 953 if (!model_.mode().is_default())
950 browser_->InstantOverlayFocused(); 954 browser_->InstantOverlayFocused();
951 #endif 955 #endif
952 } 956 }
953 957
954 void InstantController::ReloadOverlayIfStale() { 958 void InstantController::ReloadOverlayIfStale() {
955 // The local overlay is never stale. 959 // The local overlay is never stale.
956 if (overlay_ && overlay_->IsLocalOverlay()) 960 if (overlay_ && overlay_->IsLocal())
957 return; 961 return;
958 962
959 // If the overlay is showing or the omnibox has focus, don't delete the 963 // If the overlay is showing or the omnibox has focus, don't delete the
960 // overlay. It will get refreshed the next time the overlay is hidden or the 964 // overlay. It will get refreshed the next time the overlay is hidden or the
961 // omnibox loses focus. 965 // omnibox loses focus.
962 if ((!overlay_ || overlay_->is_stale()) && 966 if ((!overlay_ || overlay_->is_stale()) &&
963 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE && 967 omnibox_focus_state_ == OMNIBOX_FOCUS_NONE &&
964 model_.mode().is_default()) { 968 model_.mode().is_default()) {
965 overlay_.reset(); 969 overlay_.reset();
966 EnsureOverlayIsCurrent(false); 970 EnsureOverlayIsCurrent(false);
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1136 } 1140 }
1137 1141
1138 void InstantController::SetSuggestions( 1142 void InstantController::SetSuggestions(
1139 const content::WebContents* contents, 1143 const content::WebContents* contents,
1140 const std::vector<InstantSuggestion>& suggestions) { 1144 const std::vector<InstantSuggestion>& suggestions) {
1141 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions"); 1145 LOG_INSTANT_DEBUG_EVENT(this, "SetSuggestions");
1142 1146
1143 // Ignore if the message is from an unexpected source. 1147 // Ignore if the message is from an unexpected source.
1144 if (IsContentsFrom(ntp(), contents)) 1148 if (IsContentsFrom(ntp(), contents))
1145 return; 1149 return;
1146 if (UseInstantTabToShowSuggestions() && 1150 if (instant_tab_ && !IsContentsFrom(instant_tab(), contents))
1147 !IsContentsFrom(instant_tab(), contents))
1148 return; 1151 return;
1149 if (IsContentsFrom(overlay(), contents) && 1152 if (IsContentsFrom(overlay(), contents) &&
1150 !allow_overlay_to_show_search_suggestions_) 1153 !allow_overlay_to_show_search_suggestions_)
1151 return; 1154 return;
1152 1155
1153 InstantSuggestion suggestion; 1156 InstantSuggestion suggestion;
1154 if (!suggestions.empty()) 1157 if (!suggestions.empty())
1155 suggestion = suggestions[0]; 1158 suggestion = suggestions[0];
1156 1159
1157 // TODO(samarth): allow InstantTabs to call SetSuggestions() from the NTP once 1160 // TODO(samarth): allow InstantTabs to call SetSuggestions() from the NTP once
1158 // that is better supported. 1161 // that is better supported.
1159 bool can_use_instant_tab = UseInstantTabToShowSuggestions() && 1162 bool can_use_instant_tab = instant_tab_ && search_mode_.is_search();
1160 search_mode_.is_search();
1161 bool can_use_overlay = search_mode_.is_search_suggestions() && 1163 bool can_use_overlay = search_mode_.is_search_suggestions() &&
1162 !last_omnibox_text_.empty(); 1164 !last_omnibox_text_.empty();
1163 if (!can_use_instant_tab && !can_use_overlay) 1165 if (!can_use_instant_tab && !can_use_overlay)
1164 return; 1166 return;
1165 1167
1166 if (suggestion.behavior == INSTANT_COMPLETE_REPLACE) { 1168 if (suggestion.behavior == INSTANT_COMPLETE_REPLACE) {
1167 // We don't get an Update() when changing the omnibox due to a REPLACE 1169 // We don't get an Update() when changing the omnibox due to a REPLACE
1168 // suggestion (so that we don't inadvertently cause the overlay to change 1170 // suggestion (so that we don't inadvertently cause the overlay to change
1169 // what it's showing, as the user arrows up/down through the page-provided 1171 // what it's showing, as the user arrows up/down through the page-provided
1170 // suggestions). So, update these state variables here. 1172 // suggestions). So, update these state variables here.
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
1285 else if (!IsViewInContents(GetViewGainingFocus(view_gaining_focus), 1287 else if (!IsViewInContents(GetViewGainingFocus(view_gaining_focus),
1286 overlay_->contents())) 1288 overlay_->contents()))
1287 HideOverlay(); 1289 HideOverlay();
1288 #endif 1290 #endif
1289 } 1291 }
1290 1292
1291 void InstantController::ResetNTP(bool ignore_blacklist, bool use_local_ntp) { 1293 void InstantController::ResetNTP(bool ignore_blacklist, bool use_local_ntp) {
1292 std::string instant_url; 1294 std::string instant_url;
1293 if (use_local_ntp || 1295 if (use_local_ntp ||
1294 !GetInstantURL(browser_->profile(), ignore_blacklist, &instant_url)) 1296 !GetInstantURL(browser_->profile(), ignore_blacklist, &instant_url))
1295 instant_url = chrome::kChromeSearchLocalNtpUrl; 1297 instant_url = chrome::GetLocalInstantURL(browser_->profile()).spec();
1296 ntp_.reset(new InstantNTP(this, instant_url)); 1298 ntp_.reset(new InstantNTP(this, instant_url));
1297 ntp_->InitContents(browser_->profile(), browser_->GetActiveWebContents(), 1299 ntp_->InitContents(browser_->profile(), browser_->GetActiveWebContents(),
1298 base::Bind(&InstantController::ResetNTP, 1300 base::Bind(&InstantController::ResetNTP,
1299 base::Unretained(this), false, false)); 1301 base::Unretained(this), false, false));
1300 } 1302 }
1301 1303
1302 bool InstantController::EnsureOverlayIsCurrent(bool ignore_blacklist) { 1304 bool InstantController::EnsureOverlayIsCurrent(bool ignore_blacklist) {
1303 // If there's no active tab, the browser is closing. 1305 // If there's no active tab, the browser is closing.
1304 const content::WebContents* active_tab = browser_->GetActiveWebContents(); 1306 const content::WebContents* active_tab = browser_->GetActiveWebContents();
1305 if (!active_tab) 1307 if (!active_tab)
1306 return false; 1308 return false;
1307 1309
1308 Profile* profile = Profile::FromBrowserContext( 1310 Profile* profile = Profile::FromBrowserContext(
1309 active_tab->GetBrowserContext()); 1311 active_tab->GetBrowserContext());
1310 std::string instant_url; 1312 std::string instant_url;
1311 if (!GetInstantURL(profile, ignore_blacklist, &instant_url)) { 1313 if (!GetInstantURL(profile, ignore_blacklist, &instant_url)) {
1312 // If we are in extended mode, fallback to the local overlay. 1314 // If we are in extended mode, fallback to the local overlay.
1313 if (extended_enabled_) 1315 if (extended_enabled_)
1314 instant_url = chrome::kChromeSearchLocalOmniboxPopupURL; 1316 instant_url = chrome::GetLocalInstantURL(browser_->profile()).spec();
1315 else 1317 else
1316 return false; 1318 return false;
1317 } 1319 }
1318 1320
1319 if (!overlay_ || overlay_->instant_url() != instant_url) 1321 if (!overlay_ || overlay_->instant_url() != instant_url)
1320 CreateOverlay(instant_url, active_tab); 1322 CreateOverlay(instant_url, active_tab);
1321 1323
1322 return true; 1324 return true;
1323 } 1325 }
1324 1326
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1386 // Send a message asking the overlay to clear out old results. 1388 // Send a message asking the overlay to clear out old results.
1387 overlay_->Update(string16(), 0, 0, true); 1389 overlay_->Update(string16(), 0, 0, true);
1388 } 1390 }
1389 1391
1390 // Clear the first interaction timestamp for later use. 1392 // Clear the first interaction timestamp for later use.
1391 first_interaction_time_ = base::Time(); 1393 first_interaction_time_ = base::Time();
1392 } 1394 }
1393 1395
1394 void InstantController::ShowOverlay(int height, InstantSizeUnits units) { 1396 void InstantController::ShowOverlay(int height, InstantSizeUnits units) {
1395 // If we are on a committed search results page, the |overlay_| is not in use. 1397 // If we are on a committed search results page, the |overlay_| is not in use.
1396 if (UseInstantTabToShowSuggestions()) 1398 if (instant_tab_)
1397 return; 1399 return;
1398 1400
1399 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf( 1401 LOG_INSTANT_DEBUG_EVENT(this, base::StringPrintf(
1400 "Show: height=%d units=%d", height, units)); 1402 "Show: height=%d units=%d", height, units));
1401 1403
1402 // Must have updated omnibox after the last HideOverlay() to show suggestions. 1404 // Must have updated omnibox after the last HideOverlay() to show suggestions.
1403 if (!allow_overlay_to_show_search_suggestions_) 1405 if (!allow_overlay_to_show_search_suggestions_)
1404 return; 1406 return;
1405 1407
1406 // The page is trying to hide itself. Hide explicitly (i.e., don't use 1408 // The page is trying to hide itself. Hide explicitly (i.e., don't use
1407 // HideOverlay()) so that it can change its mind. 1409 // HideOverlay()) so that it can change its mind.
1408 if (height == 0) { 1410 if (height == 0) {
1409 model_.SetOverlayState(SearchMode(), 0, INSTANT_SIZE_PERCENT); 1411 model_.SetOverlayState(SearchMode(), 0, INSTANT_SIZE_PERCENT);
1410 return; 1412 return;
1411 } 1413 }
1412 1414
1413 // If the overlay is being shown for the first time since the user started 1415 // If the overlay is being shown for the first time since the user started
1414 // typing, record a histogram value. 1416 // typing, record a histogram value.
1415 if (!first_interaction_time_.is_null() && model_.mode().is_default()) { 1417 if (!first_interaction_time_.is_null() && model_.mode().is_default()) {
1416 base::TimeDelta delta = base::Time::Now() - first_interaction_time_; 1418 base::TimeDelta delta = base::Time::Now() - first_interaction_time_;
1417 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta); 1419 UMA_HISTOGRAM_TIMES("Instant.TimeToFirstShow", delta);
1418 } 1420 }
1419 1421
1420 // Show at 100% height except in the following cases: 1422 // Show at 100% height except in the following cases:
1421 // - The local overlay (omnibox popup) is being loaded. 1423 // - The local overlay (omnibox popup) is being loaded.
1422 // - Instant is disabled. The page needs to be able to show only a dropdown. 1424 // - Instant is disabled. The page needs to be able to show only a dropdown.
1423 // - The page is over a website other than search or an NTP, and is not 1425 // - The page is over a website other than search or an NTP, and is not
1424 // already showing at 100% height. 1426 // already showing at 100% height.
1425 if (overlay_->IsLocalOverlay() || !instant_enabled_ || 1427 if (overlay_->IsLocal() || !instant_enabled_ ||
1426 (search_mode_.is_origin_default() && !IsFullHeight(model_))) 1428 (search_mode_.is_origin_default() && !IsFullHeight(model_)))
1427 model_.SetOverlayState(search_mode_, height, units); 1429 model_.SetOverlayState(search_mode_, height, units);
1428 else 1430 else
1429 model_.SetOverlayState(search_mode_, 100, INSTANT_SIZE_PERCENT); 1431 model_.SetOverlayState(search_mode_, 100, INSTANT_SIZE_PERCENT);
1430 1432
1431 // If the overlay is being shown at full height and the omnibox is not 1433 // If the overlay is being shown at full height and the omnibox is not
1432 // focused, commit right away. 1434 // focused, commit right away.
1433 if (IsFullHeight(model_) && omnibox_focus_state_ == OMNIBOX_FOCUS_NONE) 1435 if (IsFullHeight(model_) && omnibox_focus_state_ == OMNIBOX_FOCUS_NONE)
1434 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST); 1436 CommitIfPossible(INSTANT_COMMIT_FOCUS_LOST);
1435 } 1437 }
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 // suggest 'nstant'. Otherwise, the user text really isn't a prefix, so 1633 // suggest 'nstant'. Otherwise, the user text really isn't a prefix, so
1632 // suggest nothing. 1634 // suggest nothing.
1633 // TODO(samarth|jered): revisit this logic. http://crbug.com/196572. 1635 // TODO(samarth|jered): revisit this logic. http://crbug.com/196572.
1634 return true; 1636 return true;
1635 } 1637 }
1636 } 1638 }
1637 1639
1638 return false; 1640 return false;
1639 } 1641 }
1640 1642
1641 bool InstantController::UseInstantTabToShowSuggestions() const { 1643 bool InstantController::ShouldSwitchToLocalNTP() const {
1642 return instant_tab_ && !instant_tab_->IsLocalNTP(); 1644 if (!ntp_)
1643 } 1645 return true;
1644 1646
1645 bool InstantController::ShouldSwitchToLocalNTP() const { 1647 // Don't switch if already using the correct local NTP.
1648 if (ntp_->instant_url() == chrome::GetLocalInstantURL(
1649 browser_->profile()).spec()) {
1650 return false;
1651 }
1652
1646 // If there is no Instant URL or the NTP is stale, switch. 1653 // If there is no Instant URL or the NTP is stale, switch.
1647 std::string instant_url; 1654 std::string instant_url;
1648 if (!GetInstantURL(browser_->profile(), false, &instant_url) || 1655 if (!GetInstantURL(browser_->profile(), false, &instant_url) ||
1649 !chrome::MatchesOriginAndPath(GURL(ntp_->instant_url()), 1656 !chrome::MatchesOriginAndPath(GURL(ntp_->instant_url()),
1650 GURL(instant_url))) { 1657 GURL(instant_url))) {
1651 return true; 1658 return true;
1652 } 1659 }
1653 1660
1654 if (ntp_->supports_instant()) 1661 if (ntp_->supports_instant())
1655 return false; 1662 return false;
1656 1663
1657 // If this is not window startup, switch. 1664 // If this is not window startup, switch.
1658 // TODO(shishir): This is not completely reliable. Find a better way to detect 1665 // TODO(shishir): This is not completely reliable. Find a better way to detect
1659 // startup time. 1666 // startup time.
1660 if (browser_->GetActiveWebContents()) 1667 if (browser_->GetActiveWebContents())
1661 return true; 1668 return true;
1662 1669
1663 return chrome::IsAggressiveLocalNTPFallbackEnabled(); 1670 return chrome::IsAggressiveLocalNTPFallbackEnabled();
1664 } 1671 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698