Chromium Code Reviews| Index: chrome/browser/ui/search/search_tab_helper.cc |
| diff --git a/chrome/browser/ui/search/search_tab_helper.cc b/chrome/browser/ui/search/search_tab_helper.cc |
| index f84abbe21c027d19f891a5be143590ff03eeace2..d3a674840742135a55e07940abcd268fe88511b1 100644 |
| --- a/chrome/browser/ui/search/search_tab_helper.cc |
| +++ b/chrome/browser/ui/search/search_tab_helper.cc |
| @@ -4,12 +4,14 @@ |
| #include "chrome/browser/ui/search/search_tab_helper.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/search/search.h" |
| #include "chrome/common/render_messages.h" |
| #include "chrome/common/url_constants.h" |
| #include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/notification_types.h" |
| +#include "content/public/browser/web_contents.h" |
| DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
| @@ -30,6 +32,14 @@ bool IsSearchResults(const content::WebContents* contents) { |
| return !chrome::GetSearchTerms(contents).empty(); |
| } |
| +// TODO(kmadhusu): Move this helper from anonymous namespace to chrome |
| +// namespace and remove InstantPage::IsLocal(). |
| +bool IsLocal(const content::WebContents* contents) { |
| + return contents && |
| + (contents->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || |
| + contents->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); |
| +} |
| + |
| } // namespace |
| SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
| @@ -38,15 +48,33 @@ SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
| user_input_in_progress_(false), |
| popup_is_open_(false), |
| user_text_is_empty_(true), |
| - web_contents_(web_contents) { |
| + web_contents_(web_contents), |
| + instant_support_request_in_progress_(false) { |
| if (!is_search_enabled_) |
| return; |
| + // Observe for NOTIFICATION_NAV_ENTRY_COMMITTED event to reset the local |
| + // states (such as mode, last known most visited items, instant support state, |
| + // etc). |
| registrar_.Add( |
| this, |
| content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| content::Source<content::NavigationController>( |
| &web_contents->GetController())); |
| + |
| + // Observe for NOTIFICATION_LOAD_STOP event to determine the instant support. |
| + // |
| + // Whenever an user switches the search mode in the results page, a |
| + // navigation entry is committed to show the results in the selected mode. |
| + // Once the search results are loaded, we need to dispatch a request to |
| + // determine the instant support. Sometimes, SearchTabHelper::DidFinishLoad() |
| + // is not called after switching the search mode. Therefore, observe for |
| + // NOTIFICATION_LOAD_STOP notification to determine the instant support. |
| + registrar_.Add( |
| + this, |
| + content::NOTIFICATION_LOAD_STOP, |
| + content::Source<content::NavigationController>( |
| + &web_contents->GetController())); |
| } |
| SearchTabHelper::~SearchTabHelper() { |
| @@ -84,12 +112,38 @@ bool SearchTabHelper::UpdateLastKnownMostVisitedItems( |
| return true; |
| } |
| +void SearchTabHelper::InstantSupportChanged(bool instant_support) { |
| + if (!is_search_enabled_) |
| + return; |
| + |
| + model_.SetInstantSupportState(instant_support ? INSTANT_SUPPORT_YES : |
| + INSTANT_SUPPORT_NO); |
| +} |
| + |
| +bool SearchTabHelper::SupportsInstant() const { |
| + return model_.instant_support() == INSTANT_SUPPORT_YES; |
| +} |
| + |
| void SearchTabHelper::Observe( |
| int type, |
| const content::NotificationSource& source, |
| const content::NotificationDetails& details) { |
| - DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
| - UpdateMode(); |
| + if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| + UpdateMode(); |
| + last_known_most_visited_items_.clear(); |
| + |
| + // Reset the instant support state. Once the page contents are loaded, we |
| + // will determine the instant support. |
| + model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); |
| + } else if (type == content::NOTIFICATION_LOAD_STOP) { |
|
samarth
2013/06/14 00:17:10
Does this get fired when you load any resource on
kmadhusu
2013/06/17 16:49:19
Fixed. Reset instant support state iff the committ
|
| + content::NavigationController* controller = |
| + content::Source<content::NavigationController>(source).ptr(); |
| + DCHECK_EQ(controller->GetWebContents(), web_contents_); |
| + |
| + if (!instant_support_request_in_progress_ && |
|
samarth
2013/06/14 00:17:10
This state variable makes me sad. Is it really ne
kmadhusu
2013/06/17 16:49:19
Removed.
|
| + model_.instant_support() == INSTANT_SUPPORT_UNKNOWN) |
| + DetermineIfPageSupportsInstant(); |
| + } |
| } |
| bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { |
| @@ -99,11 +153,22 @@ bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { |
| OnSearchBoxShowBars) |
| IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, |
| OnSearchBoxHideBars) |
| + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, |
| + OnInstantSupportDetermined) |
| IPC_MESSAGE_UNHANDLED(handled = false) |
| IPC_END_MESSAGE_MAP() |
| return handled; |
| } |
| +void SearchTabHelper::DidFinishLoad( |
| + int64 /* frame_id */, |
| + const GURL& /* validated_url */, |
| + bool is_main_frame, |
| + content::RenderViewHost* /* render_view_host */) { |
| + if (is_main_frame) |
| + DetermineIfPageSupportsInstant(); |
| +} |
| + |
| void SearchTabHelper::UpdateMode() { |
| SearchMode::Type type = SearchMode::MODE_DEFAULT; |
| SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; |
| @@ -126,12 +191,40 @@ void SearchTabHelper::UpdateMode() { |
| // OmniboxEditModel::SetInputInProgress() which is called from |
| // OmniboxEditModel::Revert(). |
| model_.SetState(SearchModel::State(SearchMode(type, origin), |
| - model_.state().top_bars_visible)); |
| + model_.state().top_bars_visible, |
| + model_.instant_support())); |
| } else { |
| model_.SetMode(SearchMode(type, origin)); |
| } |
| } |
| +void SearchTabHelper::DetermineIfPageSupportsInstant() { |
| + instant_support_request_in_progress_ = true; |
| + |
| + Profile* profile = |
| + Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| + if (!chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), |
|
samarth
2013/06/14 00:17:10
Can you remind me why this check is necessary? Pr
kmadhusu
2013/06/17 16:49:19
Documented the reason. The page is not in the inst
|
| + profile)) { |
| + InstantSupportChanged(false); |
| + instant_support_request_in_progress_ = false; |
| + } else if (IsLocal(web_contents_)) { |
| + // Local pages always support Instant. |
| + InstantSupportChanged(true); |
| + instant_support_request_in_progress_ = false; |
| + } else { |
| + Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); |
| + } |
| +} |
| + |
| +void SearchTabHelper::OnInstantSupportDetermined(int page_id, |
| + bool instant_support) { |
| + if (!web_contents()->IsActiveEntry(page_id)) |
| + return; |
| + |
| + InstantSupportChanged(instant_support); |
| + instant_support_request_in_progress_ = false; |
| +} |
| + |
| void SearchTabHelper::OnSearchBoxShowBars(int page_id) { |
| if (web_contents()->IsActiveEntry(page_id)) |
| model_.SetTopBarsVisible(true); |