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