| 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" | |
| 8 #include "chrome/browser/search/search.h" | 7 #include "chrome/browser/search/search.h" |
| 9 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/common/url_constants.h" | 9 #include "chrome/common/url_constants.h" |
| 11 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
| 12 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 13 #include "content/public/browser/notification_types.h" | 12 #include "content/public/browser/notification_types.h" |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 | 13 |
| 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); | 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
| 17 | 15 |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 bool IsNTP(const content::WebContents* contents) { | 18 bool IsNTP(const content::WebContents* contents) { |
| 21 // We can't use WebContents::GetURL() because that uses the active entry, | 19 // We can't use WebContents::GetURL() because that uses the active entry, |
| 22 // whereas we want the visible entry. | 20 // whereas we want the visible entry. |
| 23 const content::NavigationEntry* entry = | 21 const content::NavigationEntry* entry = |
| 24 contents->GetController().GetVisibleEntry(); | 22 contents->GetController().GetVisibleEntry(); |
| 25 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) | 23 if (entry && entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL)) |
| 26 return true; | 24 return true; |
| 27 | 25 |
| 28 return chrome::IsInstantNTP(contents); | 26 return chrome::IsInstantNTP(contents); |
| 29 } | 27 } |
| 30 | 28 |
| 31 bool IsSearchResults(const content::WebContents* contents) { | 29 bool IsSearchResults(const content::WebContents* contents) { |
| 32 return !chrome::GetSearchTerms(contents).empty(); | 30 return !chrome::GetSearchTerms(contents).empty(); |
| 33 } | 31 } |
| 34 | 32 |
| 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 | |
| 43 } // namespace | 33 } // namespace |
| 44 | 34 |
| 45 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) | 35 SearchTabHelper::SearchTabHelper(content::WebContents* web_contents) |
| 46 : WebContentsObserver(web_contents), | 36 : WebContentsObserver(web_contents), |
| 47 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), | 37 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
| 48 user_input_in_progress_(false), | 38 user_input_in_progress_(false), |
| 49 popup_is_open_(false), | 39 popup_is_open_(false), |
| 50 user_text_is_empty_(true), | 40 user_text_is_empty_(true), |
| 51 web_contents_(web_contents) { | 41 web_contents_(web_contents) { |
| 52 if (!is_search_enabled_) | 42 if (!is_search_enabled_) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 78 UpdateMode(); | 68 UpdateMode(); |
| 79 } | 69 } |
| 80 | 70 |
| 81 void SearchTabHelper::NavigationEntryUpdated() { | 71 void SearchTabHelper::NavigationEntryUpdated() { |
| 82 if (!is_search_enabled_) | 72 if (!is_search_enabled_) |
| 83 return; | 73 return; |
| 84 | 74 |
| 85 UpdateMode(); | 75 UpdateMode(); |
| 86 } | 76 } |
| 87 | 77 |
| 88 void SearchTabHelper::InstantSupportChanged(bool supports_instant) { | |
| 89 if (!is_search_enabled_) | |
| 90 return; | |
| 91 | |
| 92 model_.SetSupportsInstant(supports_instant); | |
| 93 } | |
| 94 | |
| 95 void SearchTabHelper::Observe( | 78 void SearchTabHelper::Observe( |
| 96 int type, | 79 int type, |
| 97 const content::NotificationSource& source, | 80 const content::NotificationSource& source, |
| 98 const content::NotificationDetails& details) { | 81 const content::NotificationDetails& details) { |
| 99 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | 82 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); |
| 100 UpdateMode(); | 83 UpdateMode(); |
| 101 } | 84 } |
| 102 | 85 |
| 103 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { | 86 bool SearchTabHelper::OnMessageReceived(const IPC::Message& message) { |
| 104 bool handled = true; | 87 bool handled = true; |
| 105 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) | 88 IPC_BEGIN_MESSAGE_MAP(SearchTabHelper, message) |
| 106 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, | 89 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxShowBars, |
| 107 OnSearchBoxShowBars) | 90 OnSearchBoxShowBars) |
| 108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, | 91 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_SearchBoxHideBars, |
| 109 OnSearchBoxHideBars) | 92 OnSearchBoxHideBars) |
| 110 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_InstantSupportDetermined, | |
| 111 OnInstantSupportDetermined) | |
| 112 IPC_MESSAGE_UNHANDLED(handled = false) | 93 IPC_MESSAGE_UNHANDLED(handled = false) |
| 113 IPC_END_MESSAGE_MAP() | 94 IPC_END_MESSAGE_MAP() |
| 114 return handled; | 95 return handled; |
| 115 } | 96 } |
| 116 | 97 |
| 117 void SearchTabHelper::DidFinishLoad( | |
| 118 int64 /* frame_id */, | |
| 119 const GURL& /* validated_url */, | |
| 120 bool is_main_frame, | |
| 121 content::RenderViewHost* /* render_view_host */) { | |
| 122 if (is_main_frame) | |
| 123 DetermineIfPageSupportsInstant(); | |
| 124 } | |
| 125 | |
| 126 void SearchTabHelper::UpdateMode() { | 98 void SearchTabHelper::UpdateMode() { |
| 127 SearchMode::Type type = SearchMode::MODE_DEFAULT; | 99 SearchMode::Type type = SearchMode::MODE_DEFAULT; |
| 128 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; | 100 SearchMode::Origin origin = SearchMode::ORIGIN_DEFAULT; |
| 129 if (IsNTP(web_contents_)) { | 101 if (IsNTP(web_contents_)) { |
| 130 type = SearchMode::MODE_NTP; | 102 type = SearchMode::MODE_NTP; |
| 131 origin = SearchMode::ORIGIN_NTP; | 103 origin = SearchMode::ORIGIN_NTP; |
| 132 } else if (IsSearchResults(web_contents_)) { | 104 } else if (IsSearchResults(web_contents_)) { |
| 133 type = SearchMode::MODE_SEARCH_RESULTS; | 105 type = SearchMode::MODE_SEARCH_RESULTS; |
| 134 origin = SearchMode::ORIGIN_SEARCH; | 106 origin = SearchMode::ORIGIN_SEARCH; |
| 135 } | 107 } |
| 136 if (user_input_in_progress_) | 108 if (user_input_in_progress_) |
| 137 type = SearchMode::MODE_SEARCH_SUGGESTIONS; | 109 type = SearchMode::MODE_SEARCH_SUGGESTIONS; |
| 138 | 110 |
| 139 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && | 111 if (type == SearchMode::MODE_NTP && origin == SearchMode::ORIGIN_NTP && |
| 140 !popup_is_open_ && !user_text_is_empty_) { | 112 !popup_is_open_ && !user_text_is_empty_) { |
| 141 // We're switching back (|popup_is_open_| is false) to an NTP (type and | 113 // We're switching back (|popup_is_open_| is false) to an NTP (type and |
| 142 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't | 114 // mode are |NTP|) with suggestions (|user_text_is_empty_| is false), don't |
| 143 // modify visibility of top bars. This specific omnibox state is set when | 115 // modify visibility of top bars. This specific omnibox state is set when |
| 144 // OmniboxEditModelChanged() is called from | 116 // OmniboxEditModelChanged() is called from |
| 145 // OmniboxEditModel::SetInputInProgress() which is called from | 117 // OmniboxEditModel::SetInputInProgress() which is called from |
| 146 // OmniboxEditModel::Revert(). | 118 // OmniboxEditModel::Revert(). |
| 147 model_.SetState(SearchModel::State(SearchMode(type, origin), | 119 model_.SetState(SearchModel::State(SearchMode(type, origin), |
| 148 model_.state().top_bars_visible, | 120 model_.state().top_bars_visible)); |
| 149 model_.instant_support())); | |
| 150 } else { | 121 } else { |
| 151 model_.SetMode(SearchMode(type, origin)); | 122 model_.SetMode(SearchMode(type, origin)); |
| 152 } | 123 } |
| 153 } | 124 } |
| 154 | 125 |
| 155 void SearchTabHelper::DetermineIfPageSupportsInstant() { | |
| 156 Profile* profile = | |
| 157 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
| 158 if (!chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), | |
| 159 profile)) { | |
| 160 InstantSupportChanged(false); | |
| 161 } else if (IsLocal(web_contents_)) { | |
| 162 // Local pages always support Instant. | |
| 163 InstantSupportChanged(true); | |
| 164 } else { | |
| 165 Send(new ChromeViewMsg_DetermineIfPageSupportsInstant(routing_id())); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 void SearchTabHelper::OnInstantSupportDetermined(int page_id, | |
| 170 bool supports_instant) { | |
| 171 if (web_contents()->IsActiveEntry(page_id)) | |
| 172 InstantSupportChanged(supports_instant); | |
| 173 } | |
| 174 | |
| 175 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { | 126 void SearchTabHelper::OnSearchBoxShowBars(int page_id) { |
| 176 if (web_contents()->IsActiveEntry(page_id)) | 127 if (web_contents()->IsActiveEntry(page_id)) |
| 177 model_.SetTopBarsVisible(true); | 128 model_.SetTopBarsVisible(true); |
| 178 } | 129 } |
| 179 | 130 |
| 180 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { | 131 void SearchTabHelper::OnSearchBoxHideBars(int page_id) { |
| 181 if (web_contents()->IsActiveEntry(page_id)) { | 132 if (web_contents()->IsActiveEntry(page_id)) { |
| 182 model_.SetTopBarsVisible(false); | 133 model_.SetTopBarsVisible(false); |
| 183 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); | 134 Send(new ChromeViewMsg_SearchBoxBarsHidden(routing_id())); |
| 184 } | 135 } |
| 185 } | 136 } |
| OLD | NEW |