Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/instant_ntp.h" | 5 #include "chrome/browser/ui/search/instant_ntp.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | |
| 8 #include "chrome/browser/search/search.h" | |
| 7 #include "chrome/browser/ui/search/search_tab_helper.h" | 9 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 10 #include "content/public/browser/navigation_details.h" | |
| 8 #include "content/public/browser/navigation_entry.h" | 11 #include "content/public/browser/navigation_entry.h" |
| 9 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_observer.h" | |
| 14 #include "content/public/browser/web_contents_user_data.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Helper class for logging data from the NTP. Attached to each InstantNTP | |
| 19 // instance. | |
| 20 class NTPLoggingUserData | |
| 21 : public content::WebContentsObserver, | |
| 22 public content::WebContentsUserData<NTPLoggingUserData> { | |
| 23 public: | |
| 24 virtual ~NTPLoggingUserData() {} | |
| 25 | |
| 26 // Called each time the mouse hovers over an iframe or title. | |
| 27 void increment_number_of_mouseovers() { | |
| 28 number_of_mouseovers_++; | |
| 29 } | |
| 30 | |
| 31 void set_instant_url(const GURL& url) { | |
| 32 instant_url_ = url; | |
| 33 } | |
| 34 | |
| 35 // Logs total number of mouseovers per NTP session to UMA histogram. Called | |
| 36 // when an NTP tab is about to be deactivated (be it by switching tabs, losing | |
| 37 // focus or closing the tab/shutting down Chrome) or when the user navigates | |
| 38 // to a URL. | |
| 39 void EmitMouseoverCount() { | |
| 40 UMA_HISTOGRAM_COUNTS("NewTabPage.NumberOfMouseOvers", | |
| 41 number_of_mouseovers_); | |
| 42 number_of_mouseovers_ = 0; | |
| 43 } | |
| 44 | |
| 45 // content::WebContentsObserver override | |
| 46 virtual void NavigationEntryCommitted OVERRIDE( | |
| 47 const content::LoadCommittedDetails& load_details) { | |
| 48 if (!load_details.previous_url.is_valid()) | |
| 49 return; | |
| 50 | |
| 51 if (chrome::MatchesOriginAndPath(instant_url_, load_details.previous_url)) | |
| 52 EmitMouseoverCount(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 explicit NTPLoggingUserData(content::WebContents* contents) | |
| 57 : content::WebContentsObserver(contents), | |
| 58 number_of_mouseovers_(0) {} | |
| 59 friend class content::WebContentsUserData<NTPLoggingUserData>; | |
| 60 | |
| 61 int number_of_mouseovers_; | |
| 62 GURL instant_url_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(NTPLoggingUserData); | |
| 65 }; | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NTPLoggingUserData); | |
| 10 | 70 |
| 11 InstantNTP::InstantNTP(InstantPage::Delegate* delegate, | 71 InstantNTP::InstantNTP(InstantPage::Delegate* delegate, |
| 12 const std::string& instant_url, | 72 const std::string& instant_url, |
| 13 bool is_incognito) | 73 bool is_incognito) |
| 14 : InstantPage(delegate, instant_url, is_incognito), | 74 : InstantPage(delegate, instant_url, is_incognito), |
| 15 loader_(this) { | 75 loader_(this) { |
| 16 } | 76 } |
| 17 | 77 |
| 18 InstantNTP::~InstantNTP() { | 78 InstantNTP::~InstantNTP() { |
| 19 } | 79 } |
| 20 | 80 |
| 21 void InstantNTP::InitContents(Profile* profile, | 81 void InstantNTP::InitContents(Profile* profile, |
| 22 const content::WebContents* active_tab, | 82 const content::WebContents* active_tab, |
| 23 const base::Closure& on_stale_callback) { | 83 const base::Closure& on_stale_callback) { |
| 24 loader_.Init(GURL(instant_url()), profile, active_tab, on_stale_callback); | 84 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.
| |
| 25 SetContents(loader_.contents()); | 85 SetContents(loader_.contents()); |
| 26 SearchTabHelper::FromWebContents(contents())->InitForPreloadedNTP(); | 86 SearchTabHelper::FromWebContents(contents())->InitForPreloadedNTP(); |
| 87 | |
| 88 NTPLoggingUserData::CreateForWebContents(contents()); | |
| 89 NTPLoggingUserData::FromWebContents( | |
| 90 contents())->set_instant_url(GURL(instant_url())); | |
| 91 | |
| 27 loader_.Load(); | 92 loader_.Load(); |
| 28 } | 93 } |
| 29 | 94 |
| 30 scoped_ptr<content::WebContents> InstantNTP::ReleaseContents() { | 95 scoped_ptr<content::WebContents> InstantNTP::ReleaseContents() { |
| 31 SetContents(NULL); | 96 SetContents(NULL); |
| 32 return loader_.ReleaseContents(); | 97 return loader_.ReleaseContents(); |
| 33 } | 98 } |
| 34 | 99 |
| 100 // static | |
| 101 void InstantNTP::CountMouseover(content::WebContents* contents) { | |
| 102 if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( | |
| 103 contents)) { | |
|
Alexei Svitkine (slow)
2013/06/28 20:15:34
Nit: Indent 4 more.
annark1
2013/06/28 20:47:27
Done.
| |
| 104 data->increment_number_of_mouseovers(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 // static | |
| 109 void InstantNTP::EmitMouseoverCount(content::WebContents* contents) { | |
| 110 if (NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents( | |
| 111 contents)) { | |
|
Alexei Svitkine (slow)
2013/06/28 20:15:34
Nit: Indent 4 more.
annark1
2013/06/28 20:47:27
Done.
| |
| 112 data->EmitMouseoverCount(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 35 void InstantNTP::OnSwappedContents() { | 116 void InstantNTP::OnSwappedContents() { |
| 36 SetContents(loader_.contents()); | 117 SetContents(loader_.contents()); |
| 37 } | 118 } |
| 38 | 119 |
| 39 void InstantNTP::OnFocus() { | 120 void InstantNTP::OnFocus() { |
| 40 NOTREACHED(); | 121 NOTREACHED(); |
| 41 } | 122 } |
| 42 | 123 |
| 43 void InstantNTP::OnMouseDown() { | 124 void InstantNTP::OnMouseDown() { |
| 44 NOTREACHED(); | 125 NOTREACHED(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 57 void InstantNTP::LoadCompletedMainFrame() { | 138 void InstantNTP::LoadCompletedMainFrame() { |
| 58 } | 139 } |
| 59 | 140 |
| 60 bool InstantNTP::ShouldProcessRenderViewCreated() { | 141 bool InstantNTP::ShouldProcessRenderViewCreated() { |
| 61 return true; | 142 return true; |
| 62 } | 143 } |
| 63 | 144 |
| 64 bool InstantNTP::ShouldProcessRenderViewGone() { | 145 bool InstantNTP::ShouldProcessRenderViewGone() { |
| 65 return true; | 146 return true; |
| 66 } | 147 } |
| OLD | NEW |