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