Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 loader_->OnUpOrDownKeyPressed(count); | 321 loader_->OnUpOrDownKeyPressed(count); |
| 322 return true; | 322 return true; |
| 323 } | 323 } |
| 324 | 324 |
| 325 TabContents* InstantController::GetPreviewContents() const { | 325 TabContents* InstantController::GetPreviewContents() const { |
| 326 return loader_.get() ? loader_->preview_contents() : NULL; | 326 return loader_.get() ? loader_->preview_contents() : NULL; |
| 327 } | 327 } |
| 328 | 328 |
| 329 void InstantController::Hide() { | 329 void InstantController::Hide() { |
| 330 last_active_tab_ = NULL; | 330 last_active_tab_ = NULL; |
| 331 model_.SetDisplayState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT); | 331 |
| 332 // The only time when the model is not already in the desired NOT_READY state | |
| 333 // and GetPreviewContents() returns NULL is when we are in the commit path. | |
| 334 // In that case, don't change the state just yet; otherwise we may cause the | |
| 335 // preview to hide unnecessarily. Instead, the state will be set correctly | |
| 336 // after the commit is done. | |
| 337 if (GetPreviewContents()) | |
| 338 model_.SetDisplayState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT); | |
| 332 | 339 |
| 333 if (GetPreviewContents() && !last_full_text_.empty()) { | 340 if (GetPreviewContents() && !last_full_text_.empty()) { |
| 334 // Send a blank query to ask the preview to clear out old results. | 341 // Send a blank query to ask the preview to clear out old results. |
| 335 last_full_text_.clear(); | 342 last_full_text_.clear(); |
| 336 last_user_text_.clear(); | 343 last_user_text_.clear(); |
| 337 loader_->Update(last_full_text_, true); | 344 loader_->Update(last_full_text_, true); |
| 338 } | 345 } |
| 339 } | 346 } |
| 340 | 347 |
| 341 bool InstantController::IsCurrent() const { | 348 bool InstantController::IsCurrent() const { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 // Add a fake history entry with a non-Instant search URL, so that search | 399 // Add a fake history entry with a non-Instant search URL, so that search |
| 393 // terms extraction (for autocomplete history matches) works. | 400 // terms extraction (for autocomplete history matches) works. |
| 394 if (HistoryService* history = HistoryServiceFactory::GetForProfile( | 401 if (HistoryService* history = HistoryServiceFactory::GetForProfile( |
| 395 preview->profile(), Profile::EXPLICIT_ACCESS)) { | 402 preview->profile(), Profile::EXPLICIT_ACCESS)) { |
| 396 history->AddPage(url_for_history_, base::Time::Now(), NULL, 0, GURL(), | 403 history->AddPage(url_for_history_, base::Time::Now(), NULL, 0, GURL(), |
| 397 history::RedirectList(), last_transition_type_, | 404 history::RedirectList(), last_transition_type_, |
| 398 history::SOURCE_BROWSED, false); | 405 history::SOURCE_BROWSED, false); |
| 399 } | 406 } |
| 400 | 407 |
| 401 AddPreviewUsageForHistogram(mode_, PREVIEW_COMMITTED); | 408 AddPreviewUsageForHistogram(mode_, PREVIEW_COMMITTED); |
| 402 | |
| 403 // We may have gotten here from CommitInstant(), which means the loader may | |
| 404 // still be on the stack. So, schedule a destruction for later. | |
| 405 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); | |
| 406 | |
| 407 // This call is here to reset view state. It won't actually delete |loader_| | |
| 408 // because it was just released to DeleteSoon(). | |
| 409 DeleteLoader(); | 409 DeleteLoader(); |
| 410 | 410 |
| 411 preview->web_contents()->GetController().PruneAllButActive(); | 411 preview->web_contents()->GetController().PruneAllButActive(); |
| 412 | 412 |
| 413 if (type != INSTANT_COMMIT_PRESSED_ALT_ENTER) { | 413 if (type != INSTANT_COMMIT_PRESSED_ALT_ENTER) { |
| 414 const TabContents* active_tab = delegate_->GetActiveTabContents(); | 414 const TabContents* active_tab = delegate_->GetActiveTabContents(); |
| 415 AddSessionStorageHistogram(mode_, active_tab, preview); | 415 AddSessionStorageHistogram(mode_, active_tab, preview); |
| 416 preview->web_contents()->GetController().CopyStateFromAndPrune( | 416 preview->web_contents()->GetController().CopyStateFromAndPrune( |
| 417 &active_tab->web_contents()->GetController()); | 417 &active_tab->web_contents()->GetController()); |
| 418 } | 418 } |
| 419 | 419 |
| 420 // Delegate takes ownership of the preview. | 420 // Delegate takes ownership of the preview. |
| 421 delegate_->CommitInstant(preview, type == INSTANT_COMMIT_PRESSED_ALT_ENTER); | 421 delegate_->CommitInstant(preview, type == INSTANT_COMMIT_PRESSED_ALT_ENTER); |
| 422 | 422 |
| 423 content::NotificationService::current()->Notify( | 423 content::NotificationService::current()->Notify( |
| 424 chrome::NOTIFICATION_INSTANT_COMMITTED, | 424 chrome::NOTIFICATION_INSTANT_COMMITTED, |
| 425 content::Source<content::WebContents>(preview->web_contents()), | 425 content::Source<content::WebContents>(preview->web_contents()), |
| 426 content::NotificationService::NoDetails()); | 426 content::NotificationService::NoDetails()); |
| 427 | 427 |
| 428 model_.SetDisplayState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT); | |
| 429 | |
| 428 // Try to create another loader immediately so that it is ready for the next | 430 // Try to create another loader immediately so that it is ready for the next |
| 429 // user interaction. | 431 // user interaction. |
| 430 CreateDefaultLoader(); | 432 CreateDefaultLoader(); |
| 431 } | 433 } |
| 432 | 434 |
| 433 void InstantController::OnAutocompleteLostFocus( | 435 void InstantController::OnAutocompleteLostFocus( |
| 434 gfx::NativeView view_gaining_focus) { | 436 gfx::NativeView view_gaining_focus) { |
| 435 is_omnibox_focused_ = false; | 437 is_omnibox_focused_ = false; |
| 436 | 438 |
| 437 // If there is no preview, nothing to do. | 439 // If there is no preview, nothing to do. |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 void InstantController::InstantLoaderPreviewLoaded(InstantLoader* loader) { | 613 void InstantController::InstantLoaderPreviewLoaded(InstantLoader* loader) { |
| 612 AddPreviewUsageForHistogram(mode_, PREVIEW_LOADED); | 614 AddPreviewUsageForHistogram(mode_, PREVIEW_LOADED); |
| 613 } | 615 } |
| 614 | 616 |
| 615 void InstantController::InstantSupportDetermined(InstantLoader* loader, | 617 void InstantController::InstantSupportDetermined(InstantLoader* loader, |
| 616 bool supports_instant) { | 618 bool supports_instant) { |
| 617 if (supports_instant) { | 619 if (supports_instant) { |
| 618 blacklisted_urls_.erase(loader->instant_url()); | 620 blacklisted_urls_.erase(loader->instant_url()); |
| 619 } else { | 621 } else { |
| 620 ++blacklisted_urls_[loader->instant_url()]; | 622 ++blacklisted_urls_[loader->instant_url()]; |
| 621 if (loader_ == loader) { | 623 if (loader_ == loader) |
| 622 if (GetPreviewContents()) | |
| 623 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | |
| 624 | |
| 625 // Because of the state of the stack, we can't destroy the loader now. | |
| 626 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); | |
| 627 DeleteLoader(); | 624 DeleteLoader(); |
| 628 } | |
| 629 } | 625 } |
| 630 | 626 |
| 631 content::NotificationService::current()->Notify( | 627 content::NotificationService::current()->Notify( |
| 632 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, | 628 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, |
| 633 content::Source<InstantController>(this), | 629 content::Source<InstantController>(this), |
| 634 content::NotificationService::NoDetails()); | 630 content::NotificationService::NoDetails()); |
| 635 } | 631 } |
| 636 | 632 |
| 637 void InstantController::SwappedTabContents(InstantLoader* loader) { | 633 void InstantController::SwappedTabContents(InstantLoader* loader) { |
| 638 if (loader_ == loader) | 634 if (loader_ == loader) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 663 } | 659 } |
| 664 | 660 |
| 665 void InstantController::ResetLoader(const std::string& instant_url, | 661 void InstantController::ResetLoader(const std::string& instant_url, |
| 666 const TabContents* active_tab) { | 662 const TabContents* active_tab) { |
| 667 if (GetPreviewContents() && loader_->instant_url() != instant_url) | 663 if (GetPreviewContents() && loader_->instant_url() != instant_url) |
| 668 DeleteLoader(); | 664 DeleteLoader(); |
| 669 | 665 |
| 670 if (!GetPreviewContents()) { | 666 if (!GetPreviewContents()) { |
| 671 loader_.reset(new InstantLoader(this, instant_url, active_tab)); | 667 loader_.reset(new InstantLoader(this, instant_url, active_tab)); |
| 672 loader_->Init(); | 668 loader_->Init(); |
| 669 | |
| 673 // Ensure the searchbox API has the correct focus state and context. | 670 // Ensure the searchbox API has the correct focus state and context. |
| 674 if (is_omnibox_focused_) | 671 if (is_omnibox_focused_) |
| 675 loader_->OnAutocompleteGotFocus(); | 672 loader_->OnAutocompleteGotFocus(); |
| 676 else | 673 else |
| 677 loader_->OnAutocompleteLostFocus(); | 674 loader_->OnAutocompleteLostFocus(); |
| 678 loader_->OnActiveTabModeChanged(active_tab_is_ntp_); | 675 loader_->OnActiveTabModeChanged(active_tab_is_ntp_); |
| 676 | |
| 679 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); | 677 AddPreviewUsageForHistogram(mode_, PREVIEW_CREATED); |
| 680 | 678 |
| 681 // Reset the loader timer. | 679 // Reset the loader timer. |
| 682 stale_loader_timer_.Stop(); | 680 stale_loader_timer_.Stop(); |
| 683 stale_loader_timer_.Start( | 681 stale_loader_timer_.Start( |
| 684 FROM_HERE, | 682 FROM_HERE, |
| 685 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, | 683 base::TimeDelta::FromMilliseconds(kStaleLoaderTimeoutMS), this, |
| 686 &InstantController::OnStaleLoader); | 684 &InstantController::OnStaleLoader); |
| 687 } | 685 } |
| 688 } | 686 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 721 OnStaleLoader(); | 719 OnStaleLoader(); |
| 722 } | 720 } |
| 723 | 721 |
| 724 void InstantController::DeleteLoader() { | 722 void InstantController::DeleteLoader() { |
| 725 last_active_tab_ = NULL; | 723 last_active_tab_ = NULL; |
| 726 last_full_text_.clear(); | 724 last_full_text_.clear(); |
| 727 last_user_text_.clear(); | 725 last_user_text_.clear(); |
| 728 last_verbatim_ = false; | 726 last_verbatim_ = false; |
| 729 last_suggestion_ = InstantSuggestion(); | 727 last_suggestion_ = InstantSuggestion(); |
| 730 last_match_was_search_ = false; | 728 last_match_was_search_ = false; |
| 731 model_.SetDisplayState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT); | |
| 732 loader_processed_last_update_ = false; | 729 loader_processed_last_update_ = false; |
| 733 last_omnibox_bounds_ = gfx::Rect(); | 730 last_omnibox_bounds_ = gfx::Rect(); |
| 734 url_for_history_ = GURL(); | 731 url_for_history_ = GURL(); |
| 735 if (GetPreviewContents()) | 732 if (GetPreviewContents()) { |
| 736 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); | 733 AddPreviewUsageForHistogram(mode_, PREVIEW_DELETED); |
| 737 loader_.reset(); | 734 model_.SetDisplayState(InstantModel::NOT_READY, 0, INSTANT_SIZE_PERCENT); |
| 735 } | |
| 736 MessageLoop::current()->DeleteSoon(FROM_HERE, loader_.release()); | |
|
sky
2012/10/24 03:51:16
Add comment as to why delayed delete.
sreeram
2012/10/24 16:46:45
Done.
| |
| 738 } | 737 } |
| 739 | 738 |
| 740 void InstantController::Show(int height, InstantSizeUnits units) { | 739 void InstantController::Show(int height, InstantSizeUnits units) { |
| 741 // Call even if showing in case height changed. | 740 // Call even if showing in case height changed. |
| 742 if (!model_.is_ready()) | 741 if (!model_.is_ready()) |
| 743 AddPreviewUsageForHistogram(mode_, PREVIEW_SHOWED); | 742 AddPreviewUsageForHistogram(mode_, PREVIEW_SHOWED); |
| 744 model_.SetDisplayState(InstantModel::QUERY_RESULTS, height, units); | 743 model_.SetDisplayState(InstantModel::QUERY_RESULTS, height, units); |
| 745 } | 744 } |
| 746 | 745 |
| 747 void InstantController::SendBoundsToPage() { | 746 void InstantController::SendBoundsToPage() { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 822 iter->second > kMaxInstantSupportFailures) | 821 iter->second > kMaxInstantSupportFailures) |
| 823 return false; | 822 return false; |
| 824 | 823 |
| 825 return true; | 824 return true; |
| 826 } | 825 } |
| 827 | 826 |
| 828 bool InstantController::IsOutOfDate() const { | 827 bool InstantController::IsOutOfDate() const { |
| 829 return !last_active_tab_ || | 828 return !last_active_tab_ || |
| 830 last_active_tab_ != delegate_->GetActiveTabContents(); | 829 last_active_tab_ != delegate_->GetActiveTabContents(); |
| 831 } | 830 } |
| OLD | NEW |