Chromium Code Reviews| Index: chrome/browser/ui/search/instant_ntp.cc |
| diff --git a/chrome/browser/ui/search/instant_ntp.cc b/chrome/browser/ui/search/instant_ntp.cc |
| index 5813ae00a38ad53c411c66e772060d07384a2dff..56cb5eb4e9042620e09ffa96f42e47133fcdfc72 100644 |
| --- a/chrome/browser/ui/search/instant_ntp.cc |
| +++ b/chrome/browser/ui/search/instant_ntp.cc |
| @@ -4,9 +4,70 @@ |
| #include "chrome/browser/ui/search/instant_ntp.h" |
| +#include "base/metrics/histogram.h" |
| +#include "chrome/browser/search/search.h" |
| #include "chrome/browser/ui/search/search_tab_helper.h" |
| +#include "content/public/browser/navigation_details.h" |
| #include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "content/public/browser/web_contents_observer.h" |
| +#include "content/public/browser/web_contents_user_data.h" |
| +#include "googleurl/src/gurl.h" |
| + |
| +namespace { |
| + |
| +// Helper class for logging data from the NTP. Attached to each InstantNTP |
| +// instance. |
| +class NTPLoggingUserData |
| + : public content::WebContentsObserver, |
| + public content::WebContentsUserData<NTPLoggingUserData> { |
| + public: |
| + virtual ~NTPLoggingUserData() {} |
| + |
| + // Called each time the mouse hovers over an iframe or title. |
| + void increment_number_of_mouseovers() { |
| + number_of_mouseovers_++; |
| + } |
| + |
| + void set_instant_url(const GURL& url) { |
| + instant_url_ = url; |
| + } |
| + |
| + // Logs total number of mouseovers per NTP session to UMA histogram. Called |
| + // when an NTP tab is about to be deactivated (be it by switching tabs, losing |
| + // focus or closing the tab/shutting down Chrome) or when the user navigates |
| + // to a URL. |
| + void EmitMouseoverCount() { |
| + UMA_HISTOGRAM_COUNTS("NewTabPage.NumberOfMouseOvers", |
| + number_of_mouseovers_); |
| + number_of_mouseovers_ = 0; |
| + } |
| + |
| + // content::WebContentsObserver override |
| + virtual void NavigationEntryCommitted OVERRIDE( |
|
beaudoin
2013/07/04 16:12:03
Nit: space between OVERRIDE and (
annark1
2013/07/04 19:25:09
Done.
|
| + const content::LoadCommittedDetails& load_details) { |
| + if (!load_details.previous_url.is_valid()) |
| + return; |
| + |
| + if (chrome::MatchesOriginAndPath(instant_url_, load_details.previous_url)) |
| + EmitMouseoverCount(); |
| + } |
| + |
| + private: |
| + explicit NTPLoggingUserData(content::WebContents* contents) |
| + : content::WebContentsObserver(contents), |
| + number_of_mouseovers_(0) {} |
| + friend class content::WebContentsUserData<NTPLoggingUserData>; |
| + |
| + int number_of_mouseovers_; |
| + GURL instant_url_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NTPLoggingUserData); |
| +}; |
| + |
| +} // namespace |
| + |
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(NTPLoggingUserData); |
| InstantNTP::InstantNTP(InstantPage::Delegate* delegate, |
| const std::string& instant_url, |
| @@ -22,9 +83,15 @@ InstantNTP::~InstantNTP() { |
| void InstantNTP::InitContents(Profile* profile, |
| const content::WebContents* active_tab, |
| const base::Closure& on_stale_callback) { |
| - loader_.Init(GURL(instant_url()), profile, active_tab, on_stale_callback); |
| + GURL instantNTP_url(instant_url()); |
| + loader_.Init(instantNTP_url, profile, active_tab, on_stale_callback); |
| SetContents(loader_.contents()); |
| - SearchTabHelper::FromWebContents(contents())->InitForPreloadedNTP(); |
| + content::WebContents* content = contents(); |
| + SearchTabHelper::FromWebContents(content)->InitForPreloadedNTP(); |
| + |
| + NTPLoggingUserData::CreateForWebContents(content); |
| + NTPLoggingUserData::FromWebContents(content)->set_instant_url(instantNTP_url); |
| + |
| loader_.Load(); |
| } |
| @@ -41,6 +108,22 @@ void InstantNTP::RenderViewGone(base::TerminationStatus /* status */) { |
| delegate()->InstantPageRenderViewGone(contents()); |
| } |
| +// static |
| +void InstantNTP::CountMouseover(content::WebContents* contents) { |
| + if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( |
| + contents)) { |
| + data->increment_number_of_mouseovers(); |
| + } |
|
beaudoin
2013/07/04 16:12:03
Nit: Would be slightly easier to read and shorter
annark1
2013/07/04 19:25:09
Done.
|
| +} |
| + |
| +// static |
| +void InstantNTP::EmitMouseoverCount(content::WebContents* contents) { |
| + if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( |
| + contents)) { |
| + data->EmitMouseoverCount(); |
| + } |
|
beaudoin
2013/07/04 16:12:03
Same here.
annark1
2013/07/04 19:25:09
Done.
|
| +} |
| + |
| void InstantNTP::OnSwappedContents() { |
| SetContents(loader_.contents()); |
| } |