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 8b5daec8bec626217914d25000e2709bb8320723..e7ab862dad640fc3c18ff296617f156c7506b8b8 100644 |
| --- a/chrome/browser/ui/search/instant_ntp.cc |
| +++ b/chrome/browser/ui/search/instant_ntp.cc |
| @@ -4,9 +4,69 @@ |
| #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" |
| + |
| +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( |
| + 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, |
| @@ -24,6 +84,11 @@ void InstantNTP::InitContents(Profile* profile, |
| loader_.Init(GURL(instant_url()), profile, active_tab, on_stale_callback); |
|
Alexei Svitkine (slow)
2013/06/28 20:15:34
Nit: Extract GURL(instant_url()) into a variable,
annark1
2013/06/28 20:47:27
Done.
|
| SetContents(loader_.contents()); |
| SearchTabHelper::FromWebContents(contents())->InitForPreloadedNTP(); |
| + |
| + NTPLoggingUserData::CreateForWebContents(contents()); |
| + NTPLoggingUserData::FromWebContents( |
| + contents())->set_instant_url(GURL(instant_url())); |
| + |
| loader_.Load(); |
| } |
| @@ -32,6 +97,22 @@ scoped_ptr<content::WebContents> InstantNTP::ReleaseContents() { |
| return loader_.ReleaseContents(); |
| } |
| +// static |
| +void InstantNTP::CountMouseover(content::WebContents* contents) { |
| + if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( |
| + contents)) { |
|
Alexei Svitkine (slow)
2013/06/28 20:15:34
Nit: Indent 4 more.
annark1
2013/06/28 20:47:27
Done.
|
| + data->increment_number_of_mouseovers(); |
| + } |
| +} |
| + |
| +// static |
| +void InstantNTP::EmitMouseoverCount(content::WebContents* contents) { |
| + if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( |
| + contents)) { |
|
Alexei Svitkine (slow)
2013/06/28 20:15:34
Nit: Indent 4 more.
annark1
2013/06/28 20:47:27
Done.
|
| + data->EmitMouseoverCount(); |
| + } |
| +} |
| + |
| void InstantNTP::OnSwappedContents() { |
| SetContents(loader_.contents()); |
| } |