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/ui/search/search_tab_helper.h" | 5 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" | |
| 7 #include "chrome/browser/search/search.h" | 8 #include "chrome/browser/search/search.h" |
| 8 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 9 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 10 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
| 11 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
| 14 #include "content/public/browser/web_contents.h" | |
| 13 | 15 |
| 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); | 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 bool IsNTP(const content::WebContents* contents) { | 20 bool IsNTP(const content::WebContents* contents) { |
| 19 // We can't use WebContents::GetURL() because that uses the active entry, | 21 // We can't use WebContents::GetURL() because that uses the active entry, |
| 20 // whereas we want the visible entry. | 22 // whereas we want the visible entry. |
| 21 const content::NavigationEntry* entry = | 23 const content::NavigationEntry* entry = |
| 22 contents->GetController().GetVisibleEntry(); | 24 contents->GetController().GetVisibleEntry(); |
| 23 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) | 25 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) |
| 24 return true; | 26 return true; |
| 25 | 27 |
| 26 return chrome::IsInstantNTP(contents); | 28 return chrome::IsInstantNTP(contents); |
| 27 } | 29 } |
| 28 | 30 |
| 29 bool IsSearchResults(const content::WebContents* contents) { | 31 bool IsSearchResults(const content::WebContents* contents) { |
| 30 return !chrome::GetSearchTerms(contents).empty(); | 32 return !chrome::GetSearchTerms(contents).empty(); |
| 31 } | 33 } |
| 32 | 34 |
| 35 // TODO(kmadhusu): Move this helper from anonymous namespace to chrome | |
| 36 // namespace and remove InstantPage::IsLocal(). | |
| 37 bool IsLocal(const content::WebContents* contents) { | |
| 38 return contents && | |
| 39 (contents->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl) || | |
| 40 contents->GetURL() == GURL(chrome::kChromeSearchLocalGoogleNtpUrl)); | |
| 41 } | |
| 42 | |
| 33 } // namespace | 43 } // namespace |
| 34 | 44 |
| 35 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 45 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
| 36 : WebContentsObserver(web_contents), | 46 : WebContentsObserver(web_contents), |
| 37 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), | 47 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
| 38 user_input_in_progress_(false), | 48 user_input_in_progress_(false), |
| 39 popup_is_open_(false), | 49 popup_is_open_(false), |
| 40 user_text_is_empty_(true), | 50 user_text_is_empty_(true), |
| 41 web_contents_(web_contents) { | 51 web_contents_(web_contents), |
| 52 instant_support_request_in_progress_(false) { | |
| 42 if (!is_search_enabled_) | 53 if (!is_search_enabled_) |
| 43 return; | 54 return; |
| 44 | 55 |
| 56 // Observe for NOTIFICATION_NAV_ENTRY_COMMITTED event to reset the local | |
| 57 // states (such as mode, last known most visited items, instant support state, | |
| 58 // etc). | |
| 45 registrar_.Add( | 59 registrar_.Add( |
| 46 this, | 60 this, |
| 47 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 61 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 48 content::Source<content::NavigationController>( | 62 content::Source<content::NavigationController>( |
| 49 &web_contents->GetController())); | 63 &web_contents->GetController())); |
| 64 | |
| 65 // Observe for NOTIFICATION_LOAD_STOP event to determine the instant support. | |
| 66 // | |
| 67 // Whenever an user switches the search mode in the results page, a | |
| 68 // navigation entry is committed to show the results in the selected mode. | |
| 69 // Once the search results are loaded, we need to dispatch a request to | |
| 70 // determine the instant support. Sometimes, SearchTabHelper::DidFinishLoad() | |
| 71 // is not called after switching the search mode. Therefore, observe for | |
| 72 // NOTIFICATION_LOAD_STOP notification to determine the instant support. | |
| 73 registrar_.Add( | |
| 74 this, | |
| 75 content::NOTIFICATION_LOAD_STOP, | |
| 76 content::Source<content::NavigationController>( | |
| 77 &web_contents->GetController())); | |
| 50 } | 78 } |
| 51 | 79 |
| 52 SearchTabHelper::~SearchTabHelper() { | 80 SearchTabHelper::~SearchTabHelper() { |
| 53 } | 81 } |
| 54 | 82 |
| 55 void SearchTabHelper::OmniboxEditModelChanged(bool user_input_in_progress, | 83 void SearchTabHelper::OmniboxEditModelChanged(bool user_input_in_progress, |
| 56 bool cancelling, | 84 bool cancelling, |
| 57 bool popup_is_open, | 85 bool popup_is_open, |
| 58 bool user_text_is_empty) { | 86 bool user_text_is_empty) { |
| 59 if (!is_search_enabled_) | 87 if (!is_search_enabled_) |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 77 | 105 |
| 78 bool SearchTabHelper::UpdateLastKnownMostVisitedItems( | 106 bool SearchTabHelper::UpdateLastKnownMostVisitedItems( |
| 79 const std::vector<InstantMostVisitedItem>& items) { | 107 const std::vector<InstantMostVisitedItem>& items) { |
| 80 if (chrome::AreMostVisitedItemsEqual(items, last_known_most_visited_items_)) | 108 if (chrome::AreMostVisitedItemsEqual(items, last_known_most_visited_items_)) |
| 81 return false; | 109 return false; |
| 82 | 110 |
| 83 last_known_most_visited_items_ = items; | 111 last_known_most_visited_items_ = items; |
| 84 return true; | 112 return true; |
| 85 } | 113 } |
| 86 | 114 |
| 115 void SearchTabHelper::InstantSupportChanged(bool instant_support) { | |
| 116 if (!is_search_enabled_) | |
| 117 return; | |
| 118 | |
| 119 model_.SetInstantSupportState(instant_support ? INSTANT_SUPPORT_YES : | |
| 120 INSTANT_SUPPORT_NO); | |
| 121 } | |
| 122 | |
| 123 bool SearchTabHelper::SupportsInstant() const { | |
| 124 return model_.instant_support() == INSTANT_SUPPORT_YES; | |
| 125 } | |
| 126 | |
| 87 void SearchTabHelper::Observe( | 127 void SearchTabHelper::Observe( |
| 88 int type, | 128 int type, |
| 89 const content::NotificationSource& source, | 129 const content::NotificationSource& source, |
| 90 const content::NotificationDetails& details) { | 130 const content::NotificationDetails& details) { |
| 91 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 131 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 92 UpdateMode(); | 132 UpdateMode(); |
| 133 last_known_most_visited_items_.clear(); | |
| 134 | |
| 135 // Reset the instant support state. Once the page contents are loaded, we | |
| 136 // will determine the instant support. | |
| 137 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); | |
| 138 } 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
| |
| 139 content::NavigationController* controller = | |
| 140 content::Source<content::NavigationController>(source).ptr(); | |
| 141 DCHECK_EQ(controller->GetWebContents(), web_contents_); | |
| 142 | |
| 143 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.
| |
| 144 model_.instant_support() == INSTANT_SUPPORT_UNKNOWN) | |
| 145 DetermineIfPageSupportsInstant(); | |
| 146 } | |
| 93 } | 147 } |
| 94 | 148 |
| 95 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { | 149 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 96 bool handled = true; | 150 bool handled = true; |
| 97 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) | 151 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) |
| 98 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, | 152 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, |
| 99 OnSearchBoxShowBars) | 153 OnSearchBoxShowBars) |
| 100 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, | 154 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, |
| 101 OnSearchBoxHideBars) | 155 OnSearchBoxHideBars) |
| 156 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 157 OnInstantSupportDetermined) | |
| 102 IPC_MESSAGE_UNHANDLED(handled = false) | 158 IPC_MESSAGE_UNHANDLED(handled = false) |
| 103 IPC_END_MESSAGE_MAP() | 159 IPC_END_MESSAGE_MAP() |
| 104 return handled; | 160 return handled; |
| 105 } | 161 } |
| 106 | 162 |
| 163 void SearchTabHelper::DidFinishLoad( | |
| 164 int64 /* frame_id */, | |
| 165 const GURL& /* validated_url */, | |
| 166 bool is_main_frame, | |
| 167 content::RenderViewHost* /* render_view_host */) { | |
| 168 if (is_main_frame) | |
| 169 DetermineIfPageSupportsInstant(); | |
| 170 } | |
| 171 | |
| 107 void SearchTabHelper::UpdateMode() { | 172 void SearchTabHelper::UpdateMode() { |
| 108 SearchMode::Type type = SearchMode::MODE_DEFAULT; | 173 SearchMode::Type type = SearchMode::MODE_DEFAULT; |
| 109 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; | 174 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; |
| 110 if (IsNTP(web_contents_)) { | 175 if (IsNTP(web_contents_)) { |
| 111 type = SearchMode::MODE_NTP; | 176 type = SearchMode::MODE_NTP; |
| 112 origin = SearchMode::ORIGIN_NTP; | 177 origin = SearchMode::ORIGIN_NTP; |
| 113 } else if (IsSearchResults(web_contents_)) { | 178 } else if (IsSearchResults(web_contents_)) { |
| 114 type = SearchMode::MODE_SEARCH_RESULTS; | 179 type = SearchMode::MODE_SEARCH_RESULTS; |
| 115 origin = SearchMode::ORIGIN_SEARCH; | 180 origin = SearchMode::ORIGIN_SEARCH; |
| 116 } | 181 } |
| 117 if (user_input_in_progress_) | 182 if (user_input_in_progress_) |
| 118 type = SearchMode::MODE_SEARCH_SUGGESTIONS; | 183 type = SearchMode::MODE_SEARCH_SUGGESTIONS; |
| 119 | 184 |
| 120 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && | 185 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && |
| 121 !popup_is_open_ && !user_text_is_empty_) { | 186 !popup_is_open_ && !user_text_is_empty_) { |
| 122 // We're switching back (|popup_is_open_| is false) to an NTP (type and | 187 // We're switching back (|popup_is_open_| is false) to an NTP (type and |
| 123 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't | 188 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't |
| 124 // modify visibility of top bars. This specific omnibox state is set when | 189 // modify visibility of top bars. This specific omnibox state is set when |
| 125 // OmniboxEditModelChanged() is called from | 190 // OmniboxEditModelChanged() is called from |
| 126 // OmniboxEditModel::SetInputInProgress() which is called from | 191 // OmniboxEditModel::SetInputInProgress() which is called from |
| 127 // OmniboxEditModel::Revert(). | 192 // OmniboxEditModel::Revert(). |
| 128 model_.SetState(SearchModel::State(SearchMode(type, origin), | 193 model_.SetState(SearchModel::State(SearchMode(type, origin), |
| 129 model_.state().top_bars_visible)); | 194 model_.state().top_bars_visible, |
| 195 model_.instant_support())); | |
| 130 } else { | 196 } else { |
| 131 model_.SetMode(SearchMode(type, origin)); | 197 model_.SetMode(SearchMode(type, origin)); |
| 132 } | 198 } |
| 133 } | 199 } |
| 134 | 200 |
| 201 void SearchTabHelper::DetermineIfPageSupportsInstant() { | |
| 202 instant_support_request_in_progress_ = true; | |
| 203 | |
| 204 Profile* profile = | |
| 205 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
| 206 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
| |
| 207 profile)) { | |
| 208 InstantSupportChanged(false); | |
| 209 instant_support_request_in_progress_ = false; | |
| 210 } else if (IsLocal(web_contents_)) { | |
| 211 // Local pages always support Instant. | |
| 212 InstantSupportChanged(true); | |
| 213 instant_support_request_in_progress_ = false; | |
| 214 } else { | |
| 215 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void SearchTabHelper::OnInstantSupportDetermined(int page_id, | |
| 220 bool instant_support) { | |
| 221 if (!web_contents()->IsActiveEntry(page_id)) | |
| 222 return; | |
| 223 | |
| 224 InstantSupportChanged(instant_support); | |
| 225 instant_support_request_in_progress_ = false; | |
| 226 } | |
| 227 | |
| 135 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { | 228 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { |
| 136 if (web_contents()->IsActiveEntry(page_id)) | 229 if (web_contents()->IsActiveEntry(page_id)) |
| 137 model_.SetTopBarsVisible(true); | 230 model_.SetTopBarsVisible(true); |
| 138 } | 231 } |
| 139 | 232 |
| 140 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { | 233 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { |
| 141 if (web_contents()->IsActiveEntry(page_id)) { | 234 if (web_contents()->IsActiveEntry(page_id)) { |
| 142 model_.SetTopBarsVisible(false); | 235 model_.SetTopBarsVisible(false); |
| 143 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); | 236 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); |
| 144 } | 237 } |
| 145 } | 238 } |
| OLD | NEW |