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 <set> | 7 #include <set> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 30 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
31 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" | 31 #include "chrome/browser/ui/search/search_ipc_router_policy_impl.h" |
32 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 32 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
33 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" | 33 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" |
34 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" | 34 #include "chrome/browser/ui/webui/ntp/ntp_user_data_logger.h" |
35 #include "chrome/common/url_constants.h" | 35 #include "chrome/common/url_constants.h" |
36 #include "content/public/browser/navigation_controller.h" | 36 #include "content/public/browser/navigation_controller.h" |
37 #include "content/public/browser/navigation_details.h" | 37 #include "content/public/browser/navigation_details.h" |
38 #include "content/public/browser/navigation_entry.h" | 38 #include "content/public/browser/navigation_entry.h" |
39 #include "content/public/browser/navigation_type.h" | 39 #include "content/public/browser/navigation_type.h" |
40 #include "content/public/browser/notification_service.h" | |
41 #include "content/public/browser/notification_types.h" | |
42 #include "content/public/browser/render_process_host.h" | 40 #include "content/public/browser/render_process_host.h" |
43 #include "content/public/browser/user_metrics.h" | 41 #include "content/public/browser/user_metrics.h" |
44 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
45 #include "content/public/browser/web_contents_view.h" | 43 #include "content/public/browser/web_contents_view.h" |
46 #include "content/public/common/page_transition_types.h" | 44 #include "content/public/common/page_transition_types.h" |
47 #include "content/public/common/referrer.h" | 45 #include "content/public/common/referrer.h" |
48 #include "grit/generated_resources.h" | 46 #include "grit/generated_resources.h" |
49 #include "ui/base/l10n/l10n_util.h" | 47 #include "ui/base/l10n/l10n_util.h" |
50 #include "url/gurl.h" | 48 #include "url/gurl.h" |
51 | 49 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), | 130 is_search_enabled_(chrome::IsInstantExtendedAPIEnabled()), |
133 user_input_in_progress_(false), | 131 user_input_in_progress_(false), |
134 web_contents_(web_contents), | 132 web_contents_(web_contents), |
135 ipc_router_(web_contents, this, | 133 ipc_router_(web_contents, this, |
136 make_scoped_ptr(new SearchIPCRouterPolicyImpl(web_contents)) | 134 make_scoped_ptr(new SearchIPCRouterPolicyImpl(web_contents)) |
137 .PassAs<SearchIPCRouter::Policy>()), | 135 .PassAs<SearchIPCRouter::Policy>()), |
138 instant_service_(NULL) { | 136 instant_service_(NULL) { |
139 if (!is_search_enabled_) | 137 if (!is_search_enabled_) |
140 return; | 138 return; |
141 | 139 |
142 // Observe NOTIFICATION_NAV_ENTRY_COMMITTED events so we can reset state | |
143 // associated with the WebContents (such as mode, last known most visited | |
144 // items, instant support state etc). | |
145 registrar_.Add( | |
146 this, | |
147 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
148 content::Source<content::NavigationController>( | |
149 &web_contents->GetController())); | |
150 | |
151 instant_service_ = | 140 instant_service_ = |
152 InstantServiceFactory::GetForProfile( | 141 InstantServiceFactory::GetForProfile( |
153 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); | 142 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
154 if (instant_service_) | 143 if (instant_service_) |
155 instant_service_->AddObserver(this); | 144 instant_service_->AddObserver(this); |
156 } | 145 } |
157 | 146 |
158 SearchTabHelper::~SearchTabHelper() { | 147 SearchTabHelper::~SearchTabHelper() { |
159 if (instant_service_) | 148 if (instant_service_) |
160 instant_service_->RemoveObserver(this); | 149 instant_service_->RemoveObserver(this); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 } | 205 } |
217 | 206 |
218 void SearchTabHelper::OnTabActivated() { | 207 void SearchTabHelper::OnTabActivated() { |
219 ipc_router_.OnTabActivated(); | 208 ipc_router_.OnTabActivated(); |
220 } | 209 } |
221 | 210 |
222 void SearchTabHelper::OnTabDeactivated() { | 211 void SearchTabHelper::OnTabDeactivated() { |
223 ipc_router_.OnTabDeactivated(); | 212 ipc_router_.OnTabDeactivated(); |
224 } | 213 } |
225 | 214 |
226 void SearchTabHelper::Observe( | |
227 int type, | |
228 const content::NotificationSource& source, | |
229 const content::NotificationDetails& details) { | |
230 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type); | |
231 content::LoadCommittedDetails* load_details = | |
232 content::Details<content::LoadCommittedDetails>(details).ptr(); | |
233 if (!load_details->is_main_frame) | |
234 return; | |
235 | |
236 // TODO(kmadhusu): Set the page initial states (such as omnibox margin, etc) | |
237 // from here. Please refer to crbug.com/247517 for more details. | |
238 if (chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), | |
239 profile())) { | |
240 ipc_router_.SetDisplayInstantResults(); | |
241 } | |
242 | |
243 UpdateMode(true, false); | |
244 | |
245 content::NavigationEntry* entry = | |
246 web_contents_->GetController().GetVisibleEntry(); | |
247 DCHECK(entry); | |
248 | |
249 // Already determined the instant support state for this page, do not reset | |
250 // the instant support state. | |
251 // | |
252 // When we get a navigation entry committed event, there seem to be two ways | |
253 // to tell whether the navigation was "in-page". Ideally, when | |
254 // LoadCommittedDetails::is_in_page is true, we should have | |
255 // LoadCommittedDetails::type to be NAVIGATION_TYPE_IN_PAGE. Unfortunately, | |
256 // they are different in some cases. To workaround this bug, we are checking | |
257 // (is_in_page || type == NAVIGATION_TYPE_IN_PAGE). Please refer to | |
258 // crbug.com/251330 for more details. | |
259 if (load_details->is_in_page || | |
260 load_details->type == content::NAVIGATION_TYPE_IN_PAGE) { | |
261 // When an "in-page" navigation happens, we will not receive a | |
262 // DidFinishLoad() event. Therefore, we will not determine the Instant | |
263 // support for the navigated page. So, copy over the Instant support from | |
264 // the previous entry. If the page does not support Instant, update the | |
265 // location bar from here to turn off search terms replacement. | |
266 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), | |
267 entry); | |
268 if (model_.instant_support() == INSTANT_SUPPORT_NO) | |
269 UpdateLocationBar(web_contents_); | |
270 return; | |
271 } | |
272 | |
273 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); | |
274 model_.SetVoiceSearchSupported(false); | |
275 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), | |
276 entry); | |
277 } | |
278 | |
279 void SearchTabHelper::RenderViewCreated( | 215 void SearchTabHelper::RenderViewCreated( |
280 content::RenderViewHost* render_view_host) { | 216 content::RenderViewHost* render_view_host) { |
281 ipc_router_.SetPromoInformation(IsAppLauncherEnabled()); | 217 ipc_router_.SetPromoInformation(IsAppLauncherEnabled()); |
282 } | 218 } |
283 | 219 |
284 void SearchTabHelper::DidNavigateMainFrame( | 220 void SearchTabHelper::DidNavigateMainFrame( |
285 const content::LoadCommittedDetails& details, | 221 const content::LoadCommittedDetails& details, |
286 const content::FrameNavigateParams& params) { | 222 const content::FrameNavigateParams& params) { |
287 if (IsCacheableNTP(web_contents_)) { | 223 if (IsCacheableNTP(web_contents_)) { |
288 if (details.http_status_code == 204 || details.http_status_code >= 400) { | 224 if (details.http_status_code == 204 || details.http_status_code >= 400) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 | 264 |
329 void SearchTabHelper::DidFinishLoad( | 265 void SearchTabHelper::DidFinishLoad( |
330 int64 /* frame_id */, | 266 int64 /* frame_id */, |
331 const GURL& /* validated_url */, | 267 const GURL& /* validated_url */, |
332 bool is_main_frame, | 268 bool is_main_frame, |
333 content::RenderViewHost* /* render_view_host */) { | 269 content::RenderViewHost* /* render_view_host */) { |
334 if (is_main_frame) | 270 if (is_main_frame) |
335 DetermineIfPageSupportsInstant(); | 271 DetermineIfPageSupportsInstant(); |
336 } | 272 } |
337 | 273 |
| 274 void SearchTabHelper::NavigationEntryCommitted( |
| 275 const content::LoadCommittedDetails& load_details) { |
| 276 if (!is_search_enabled_) |
| 277 return; |
| 278 |
| 279 if (!load_details.is_main_frame) |
| 280 return; |
| 281 |
| 282 // TODO(kmadhusu): Set the page initial states (such as omnibox margin, etc) |
| 283 // from here. Please refer to crbug.com/247517 for more details. |
| 284 if (chrome::ShouldAssignURLToInstantRenderer(web_contents_->GetURL(), |
| 285 profile())) { |
| 286 ipc_router_.SetDisplayInstantResults(); |
| 287 } |
| 288 |
| 289 UpdateMode(true, false); |
| 290 |
| 291 content::NavigationEntry* entry = |
| 292 web_contents_->GetController().GetVisibleEntry(); |
| 293 DCHECK(entry); |
| 294 |
| 295 // Already determined the instant support state for this page, do not reset |
| 296 // the instant support state. |
| 297 // |
| 298 // When we get a navigation entry committed event, there seem to be two ways |
| 299 // to tell whether the navigation was "in-page". Ideally, when |
| 300 // LoadCommittedDetails::is_in_page is true, we should have |
| 301 // LoadCommittedDetails::type to be NAVIGATION_TYPE_IN_PAGE. Unfortunately, |
| 302 // they are different in some cases. To workaround this bug, we are checking |
| 303 // (is_in_page || type == NAVIGATION_TYPE_IN_PAGE). Please refer to |
| 304 // crbug.com/251330 for more details. |
| 305 if (load_details.is_in_page || |
| 306 load_details.type == content::NAVIGATION_TYPE_IN_PAGE) { |
| 307 // When an "in-page" navigation happens, we will not receive a |
| 308 // DidFinishLoad() event. Therefore, we will not determine the Instant |
| 309 // support for the navigated page. So, copy over the Instant support from |
| 310 // the previous entry. If the page does not support Instant, update the |
| 311 // location bar from here to turn off search terms replacement. |
| 312 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), |
| 313 entry); |
| 314 if (model_.instant_support() == INSTANT_SUPPORT_NO) |
| 315 UpdateLocationBar(web_contents_); |
| 316 return; |
| 317 } |
| 318 |
| 319 model_.SetInstantSupportState(INSTANT_SUPPORT_UNKNOWN); |
| 320 model_.SetVoiceSearchSupported(false); |
| 321 chrome::SetInstantSupportStateInNavigationEntry(model_.instant_support(), |
| 322 entry); |
| 323 } |
| 324 |
338 void SearchTabHelper::OnInstantSupportDetermined(bool supports_instant) { | 325 void SearchTabHelper::OnInstantSupportDetermined(bool supports_instant) { |
339 InstantSupportChanged(supports_instant); | 326 InstantSupportChanged(supports_instant); |
340 } | 327 } |
341 | 328 |
342 void SearchTabHelper::OnSetVoiceSearchSupport(bool supports_voice_search) { | 329 void SearchTabHelper::OnSetVoiceSearchSupport(bool supports_voice_search) { |
343 model_.SetVoiceSearchSupported(supports_voice_search); | 330 model_.SetVoiceSearchSupported(supports_voice_search); |
344 } | 331 } |
345 | 332 |
346 void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { | 333 void SearchTabHelper::ThemeInfoChanged(const ThemeBackgroundInfo& theme_info) { |
347 ipc_router_.SendThemeBackgroundInfo(theme_info); | 334 ipc_router_.SendThemeBackgroundInfo(theme_info); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 void SearchTabHelper::RedirectToLocalNTP() { | 545 void SearchTabHelper::RedirectToLocalNTP() { |
559 // Extra parentheses to declare a variable. | 546 // Extra parentheses to declare a variable. |
560 content::NavigationController::LoadURLParams load_params( | 547 content::NavigationController::LoadURLParams load_params( |
561 (GURL(chrome::kChromeSearchLocalNtpUrl))); | 548 (GURL(chrome::kChromeSearchLocalNtpUrl))); |
562 load_params.referrer = content::Referrer(); | 549 load_params.referrer = content::Referrer(); |
563 load_params.transition_type = content::PAGE_TRANSITION_SERVER_REDIRECT; | 550 load_params.transition_type = content::PAGE_TRANSITION_SERVER_REDIRECT; |
564 // Don't push a history entry. | 551 // Don't push a history entry. |
565 load_params.should_replace_current_entry = true; | 552 load_params.should_replace_current_entry = true; |
566 web_contents_->GetController().LoadURLWithParams(load_params); | 553 web_contents_->GetController().LoadURLWithParams(load_params); |
567 } | 554 } |
OLD | NEW |