| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 #include "content/public/common/referrer.h" | 48 #include "content/public/common/referrer.h" |
| 49 #include "net/base/net_errors.h" | 49 #include "net/base/net_errors.h" |
| 50 #include "ui/base/l10n/l10n_util.h" | 50 #include "ui/base/l10n/l10n_util.h" |
| 51 #include "ui/base/page_transition_types.h" | 51 #include "ui/base/page_transition_types.h" |
| 52 #include "url/gurl.h" | 52 #include "url/gurl.h" |
| 53 | 53 |
| 54 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); | 54 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SearchTabHelper); |
| 55 | 55 |
| 56 namespace { | 56 namespace { |
| 57 | 57 |
| 58 // For reporting Cacheable NTP navigations. | |
| 59 enum CacheableNTPLoad { | |
| 60 CACHEABLE_NTP_LOAD_FAILED = 0, | |
| 61 CACHEABLE_NTP_LOAD_SUCCEEDED = 1, | |
| 62 CACHEABLE_NTP_LOAD_MAX = 2 | |
| 63 }; | |
| 64 | |
| 65 void RecordCacheableNTPLoadHistogram(bool succeeded) { | |
| 66 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", | |
| 67 succeeded ? CACHEABLE_NTP_LOAD_SUCCEEDED : | |
| 68 CACHEABLE_NTP_LOAD_FAILED, | |
| 69 CACHEABLE_NTP_LOAD_MAX); | |
| 70 } | |
| 71 | |
| 72 bool IsCacheableNTP(const content::WebContents* contents) { | 58 bool IsCacheableNTP(const content::WebContents* contents) { |
| 73 const content::NavigationEntry* entry = | 59 const content::NavigationEntry* entry = |
| 74 contents->GetController().GetLastCommittedEntry(); | 60 contents->GetController().GetLastCommittedEntry(); |
| 75 return chrome::NavEntryIsInstantNTP(contents, entry) && | 61 return chrome::NavEntryIsInstantNTP(contents, entry) && |
| 76 entry->GetURL() != GURL(chrome::kChromeSearchLocalNtpUrl); | 62 entry->GetURL() != GURL(chrome::kChromeSearchLocalNtpUrl); |
| 77 } | 63 } |
| 78 | 64 |
| 79 bool IsNTP(const content::WebContents* contents) { | 65 bool IsNTP(const content::WebContents* contents) { |
| 80 // We can't use WebContents::GetURL() because that uses the active entry, | 66 // We can't use WebContents::GetURL() because that uses the active entry, |
| 81 // whereas we want the visible entry. | 67 // whereas we want the visible entry. |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 web_contents_->GetController().GetPendingEntry(); | 289 web_contents_->GetController().GetPendingEntry(); |
| 304 if (entry) | 290 if (entry) |
| 305 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 291 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
| 306 } | 292 } |
| 307 } | 293 } |
| 308 | 294 |
| 309 void SearchTabHelper::DidNavigateMainFrame( | 295 void SearchTabHelper::DidNavigateMainFrame( |
| 310 const content::LoadCommittedDetails& details, | 296 const content::LoadCommittedDetails& details, |
| 311 const content::FrameNavigateParams& params) { | 297 const content::FrameNavigateParams& params) { |
| 312 if (IsCacheableNTP(web_contents_)) { | 298 if (IsCacheableNTP(web_contents_)) { |
| 313 if (details.http_status_code == 204 || details.http_status_code >= 400) { | 299 UMA_HISTOGRAM_ENUMERATION("InstantExtended.CacheableNTPLoad", |
| 314 RedirectToLocalNTP(); | 300 chrome::CACHEABLE_NTP_LOAD_SUCCEEDED, |
| 315 RecordCacheableNTPLoadHistogram(false); | 301 chrome::CACHEABLE_NTP_LOAD_MAX); |
| 316 return; | |
| 317 } | |
| 318 RecordCacheableNTPLoadHistogram(true); | |
| 319 } | 302 } |
| 320 | 303 |
| 321 // Always set the title on the new tab page to be the one from our UI | 304 // Always set the title on the new tab page to be the one from our UI |
| 322 // resources. Normally, we set the title when we begin a NTP load, but it can | 305 // resources. Normally, we set the title when we begin a NTP load, but it can |
| 323 // get reset in several places (like when you press Reload). This check | 306 // get reset in several places (like when you press Reload). This check |
| 324 // ensures that the title is properly set to the string defined by the Chrome | 307 // ensures that the title is properly set to the string defined by the Chrome |
| 325 // UI language (rather than the server language) in all cases. | 308 // UI language (rather than the server language) in all cases. |
| 326 // | 309 // |
| 327 // We only override the title when it's nonempty to allow the page to set the | 310 // We only override the title when it's nonempty to allow the page to set the |
| 328 // title if it really wants. An empty title means to use the default. There's | 311 // title if it really wants. An empty title means to use the default. There's |
| 329 // also a race condition between this code and the page's SetTitle call which | 312 // also a race condition between this code and the page's SetTitle call which |
| 330 // this rule avoids. | 313 // this rule avoids. |
| 331 content::NavigationEntry* entry = | 314 content::NavigationEntry* entry = |
| 332 web_contents_->GetController().GetLastCommittedEntry(); | 315 web_contents_->GetController().GetLastCommittedEntry(); |
| 333 if (entry && entry->GetTitle().empty() && | 316 if (entry && entry->GetTitle().empty() && |
| 334 (entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) || | 317 (entry->GetVirtualURL() == GURL(chrome::kChromeUINewTabURL) || |
| 335 chrome::NavEntryIsInstantNTP(web_contents_, entry))) { | 318 chrome::NavEntryIsInstantNTP(web_contents_, entry))) { |
| 336 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 319 entry->SetTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
| 337 } | 320 } |
| 338 } | 321 } |
| 339 | 322 |
| 340 void SearchTabHelper::DidFailProvisionalLoad( | |
| 341 content::RenderFrameHost* render_frame_host, | |
| 342 const GURL& validated_url, | |
| 343 int error_code, | |
| 344 const base::string16& /* error_description */) { | |
| 345 // If error_code is ERR_ABORTED means that the user has canceled this | |
| 346 // navigation so it shouldn't be redirected. | |
| 347 if (!render_frame_host->GetParent() && error_code != net::ERR_ABORTED && | |
| 348 validated_url != GURL(chrome::kChromeSearchLocalNtpUrl) && | |
| 349 chrome::IsNTPURL(validated_url, profile())) { | |
| 350 RedirectToLocalNTP(); | |
| 351 RecordCacheableNTPLoadHistogram(false); | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 void SearchTabHelper::DidFinishLoad(content::RenderFrameHost* render_frame_host, | 323 void SearchTabHelper::DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 356 const GURL& /* validated_url */) { | 324 const GURL& /* validated_url */) { |
| 357 if (!render_frame_host->GetParent()) { | 325 if (!render_frame_host->GetParent()) { |
| 358 if (chrome::IsInstantNTP(web_contents_)) | 326 if (chrome::IsInstantNTP(web_contents_)) |
| 359 RecordNewTabLoadTime(web_contents_); | 327 RecordNewTabLoadTime(web_contents_); |
| 360 | 328 |
| 361 DetermineIfPageSupportsInstant(); | 329 DetermineIfPageSupportsInstant(); |
| 362 } | 330 } |
| 363 } | 331 } |
| 364 | 332 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 InstantSupportChanged(true); | 579 InstantSupportChanged(true); |
| 612 } else { | 580 } else { |
| 613 ipc_router_.DetermineIfPageSupportsInstant(); | 581 ipc_router_.DetermineIfPageSupportsInstant(); |
| 614 } | 582 } |
| 615 } | 583 } |
| 616 | 584 |
| 617 Profile* SearchTabHelper::profile() const { | 585 Profile* SearchTabHelper::profile() const { |
| 618 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 586 return Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 619 } | 587 } |
| 620 | 588 |
| 621 void SearchTabHelper::RedirectToLocalNTP() { | |
| 622 // Extra parentheses to declare a variable. | |
| 623 content::NavigationController::LoadURLParams load_params( | |
| 624 (GURL(chrome::kChromeSearchLocalNtpUrl))); | |
| 625 load_params.referrer = content::Referrer(); | |
| 626 load_params.transition_type = ui::PAGE_TRANSITION_SERVER_REDIRECT; | |
| 627 // Don't push a history entry. | |
| 628 load_params.should_replace_current_entry = true; | |
| 629 web_contents_->GetController().LoadURLWithParams(load_params); | |
| 630 } | |
| 631 | |
| 632 bool SearchTabHelper::IsInputInProgress() const { | 589 bool SearchTabHelper::IsInputInProgress() const { |
| 633 OmniboxView* omnibox = GetOmniboxView(); | 590 OmniboxView* omnibox = GetOmniboxView(); |
| 634 return !model_.mode().is_ntp() && omnibox && | 591 return !model_.mode().is_ntp() && omnibox && |
| 635 omnibox->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; | 592 omnibox->model()->focus_state() == OMNIBOX_FOCUS_VISIBLE; |
| 636 } | 593 } |
| 637 | 594 |
| 638 OmniboxView* SearchTabHelper::GetOmniboxView() const { | 595 OmniboxView* SearchTabHelper::GetOmniboxView() const { |
| 639 return delegate_ ? delegate_->GetOmniboxView() : NULL; | 596 return delegate_ ? delegate_->GetOmniboxView() : NULL; |
| 640 } | 597 } |
| OLD | NEW |