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 #include "googleurl/src/gurl.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Helper class for logging data from the NTP. Attached to each InstantNTP |
| 20 // instance. |
| 21 class NTPLoggingUserData |
| 22 : public content::WebContentsObserver, |
| 23 public content::WebContentsUserData<NTPLoggingUserData> { |
| 24 public: |
| 25 virtual ~NTPLoggingUserData() {} |
| 26 |
| 27 // Called each time the mouse hovers over an iframe or title. |
| 28 void increment_number_of_mouseovers() { |
| 29 number_of_mouseovers_++; |
| 30 } |
| 31 |
| 32 void set_instant_url(const GURL& url) { |
| 33 instant_url_ = url; |
| 34 } |
| 35 |
| 36 // Logs total number of mouseovers per NTP session to UMA histogram. Called |
| 37 // when an NTP tab is about to be deactivated (be it by switching tabs, losing |
| 38 // focus or closing the tab/shutting down Chrome) or when the user navigates |
| 39 // to a URL. |
| 40 void EmitMouseoverCount() { |
| 41 UMA_HISTOGRAM_COUNTS("NewTabPage.NumberOfMouseOvers", |
| 42 number_of_mouseovers_); |
| 43 number_of_mouseovers_ = 0; |
| 44 } |
| 45 |
| 46 // content::WebContentsObserver override |
| 47 virtual void NavigationEntryCommitted( |
| 48 const content::LoadCommittedDetails& load_details) OVERRIDE { |
| 49 if (!load_details.previous_url.is_valid()) |
| 50 return; |
| 51 |
| 52 if (chrome::MatchesOriginAndPath(instant_url_, load_details.previous_url)) |
| 53 EmitMouseoverCount(); |
| 54 } |
| 55 |
| 56 private: |
| 57 explicit NTPLoggingUserData(content::WebContents* contents) |
| 58 : content::WebContentsObserver(contents), |
| 59 number_of_mouseovers_(0) {} |
| 60 friend class content::WebContentsUserData<NTPLoggingUserData>; |
| 61 |
| 62 int number_of_mouseovers_; |
| 63 GURL instant_url_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(NTPLoggingUserData); |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 DEFINE_WEB_CONTENTS_USER_DATA_KEY(NTPLoggingUserData); |
10 | 71 |
11 InstantNTP::InstantNTP(InstantPage::Delegate* delegate, | 72 InstantNTP::InstantNTP(InstantPage::Delegate* delegate, |
12 const std::string& instant_url, | 73 const std::string& instant_url, |
13 bool is_incognito) | 74 bool is_incognito) |
14 : InstantPage(delegate, instant_url, is_incognito), | 75 : InstantPage(delegate, instant_url, is_incognito), |
15 loader_(this) { | 76 loader_(this) { |
16 DCHECK(delegate); | 77 DCHECK(delegate); |
17 } | 78 } |
18 | 79 |
19 InstantNTP::~InstantNTP() { | 80 InstantNTP::~InstantNTP() { |
20 } | 81 } |
21 | 82 |
22 void InstantNTP::InitContents(Profile* profile, | 83 void InstantNTP::InitContents(Profile* profile, |
23 const content::WebContents* active_tab, | 84 const content::WebContents* active_tab, |
24 const base::Closure& on_stale_callback) { | 85 const base::Closure& on_stale_callback) { |
25 loader_.Init(GURL(instant_url()), profile, active_tab, on_stale_callback); | 86 GURL instantNTP_url(instant_url()); |
| 87 loader_.Init(instantNTP_url, profile, active_tab, on_stale_callback); |
26 SetContents(loader_.contents()); | 88 SetContents(loader_.contents()); |
27 SearchTabHelper::FromWebContents(contents())->InitForPreloadedNTP(); | 89 content::WebContents* content = contents(); |
| 90 SearchTabHelper::FromWebContents(content)->InitForPreloadedNTP(); |
| 91 |
| 92 NTPLoggingUserData::CreateForWebContents(content); |
| 93 NTPLoggingUserData::FromWebContents(content)->set_instant_url(instantNTP_url); |
| 94 |
28 loader_.Load(); | 95 loader_.Load(); |
29 } | 96 } |
30 | 97 |
31 scoped_ptr<content::WebContents> InstantNTP::ReleaseContents() { | 98 scoped_ptr<content::WebContents> InstantNTP::ReleaseContents() { |
32 SetContents(NULL); | 99 SetContents(NULL); |
33 return loader_.ReleaseContents(); | 100 return loader_.ReleaseContents(); |
34 } | 101 } |
35 | 102 |
36 void InstantNTP::RenderViewCreated(content::RenderViewHost* render_view_host) { | 103 void InstantNTP::RenderViewCreated(content::RenderViewHost* render_view_host) { |
37 delegate()->InstantPageRenderViewCreated(contents()); | 104 delegate()->InstantPageRenderViewCreated(contents()); |
38 } | 105 } |
39 | 106 |
40 void InstantNTP::RenderViewGone(base::TerminationStatus /* status */) { | 107 void InstantNTP::RenderViewGone(base::TerminationStatus /* status */) { |
41 delegate()->InstantPageRenderViewGone(contents()); | 108 delegate()->InstantPageRenderViewGone(contents()); |
42 } | 109 } |
43 | 110 |
| 111 // static |
| 112 void InstantNTP::CountMouseover(content::WebContents* contents) { |
| 113 NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents(contents); |
| 114 if (data) |
| 115 data->increment_number_of_mouseovers(); |
| 116 } |
| 117 |
| 118 // static |
| 119 void InstantNTP::EmitMouseoverCount(content::WebContents* contents) { |
| 120 NTPLoggingUserData* data = NTPLoggingUserData::FromWebContents(contents); |
| 121 if (data) |
| 122 data->EmitMouseoverCount(); |
| 123 } |
| 124 |
44 void InstantNTP::OnSwappedContents() { | 125 void InstantNTP::OnSwappedContents() { |
45 SetContents(loader_.contents()); | 126 SetContents(loader_.contents()); |
46 } | 127 } |
47 | 128 |
48 void InstantNTP::OnFocus() { | 129 void InstantNTP::OnFocus() { |
49 NOTREACHED(); | 130 NOTREACHED(); |
50 } | 131 } |
51 | 132 |
52 void InstantNTP::OnMouseDown() { | 133 void InstantNTP::OnMouseDown() { |
53 NOTREACHED(); | 134 NOTREACHED(); |
54 } | 135 } |
55 | 136 |
56 void InstantNTP::OnMouseUp() { | 137 void InstantNTP::OnMouseUp() { |
57 NOTREACHED(); | 138 NOTREACHED(); |
58 } | 139 } |
59 | 140 |
60 content::WebContents* InstantNTP::OpenURLFromTab( | 141 content::WebContents* InstantNTP::OpenURLFromTab( |
61 content::WebContents* source, | 142 content::WebContents* source, |
62 const content::OpenURLParams& params) { | 143 const content::OpenURLParams& params) { |
63 return NULL; | 144 return NULL; |
64 } | 145 } |
65 | 146 |
66 void InstantNTP::LoadCompletedMainFrame() { | 147 void InstantNTP::LoadCompletedMainFrame() { |
67 } | 148 } |
OLD | NEW |