| 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 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <bitset> |
| 11 |
| 10 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "chrome/common/search/ntp_logging_events.h" | 15 #include "chrome/common/search/ntp_logging_events.h" |
| 16 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/browser/web_contents_user_data.h" | 17 #include "content/public/browser/web_contents_user_data.h" |
| 15 | 18 |
| 16 namespace content { | 19 namespace content { |
| 17 class WebContents; | 20 class WebContents; |
| 18 } | 21 } |
| 19 | 22 |
| 20 // Helper class for logging data from the NTP. Attached to each NTP instance. | 23 // Helper class for logging data from the NTP. Attached to each NTP instance. |
| 21 class NTPUserDataLogger | 24 class NTPUserDataLogger |
| 22 : public content::WebContentsUserData<NTPUserDataLogger> { | 25 : public content::WebContentsObserver, |
| 26 public content::WebContentsUserData<NTPUserDataLogger> { |
| 23 public: | 27 public: |
| 24 ~NTPUserDataLogger() override; | 28 ~NTPUserDataLogger() override; |
| 25 | 29 |
| 30 // Gets the associated NTPUserDataLogger, creating it if necessary. |
| 31 // |
| 32 // MUST be called only when the NTP is active. |
| 26 static NTPUserDataLogger* GetOrCreateFromWebContents( | 33 static NTPUserDataLogger* GetOrCreateFromWebContents( |
| 27 content::WebContents* content); | 34 content::WebContents* content); |
| 28 | 35 |
| 29 // Called when an event occurs on the NTP that requires a counter to be | 36 // Called when an event occurs on the NTP that requires a counter to be |
| 30 // incremented. |time| is the delta time in ms from navigation start until | 37 // incremented. |time| is the delta time in ms from navigation start until |
| 31 // this event happened. | 38 // this event happened. |
| 32 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); | 39 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); |
| 33 | 40 |
| 34 // Logs an impression on one of the NTP tiles by a given source. | 41 // Logs an impression on one of the NTP tiles by a given source. |
| 35 void LogMostVisitedImpression(int position, NTPLoggingTileSource tile_source); | 42 void LogMostVisitedImpression(int position, NTPLoggingTileSource tile_source); |
| 36 | 43 |
| 37 // Logs a navigation on one of the NTP tiles by a given source. | 44 // Logs a navigation on one of the NTP tiles by a given source. |
| 38 void LogMostVisitedNavigation(int position, NTPLoggingTileSource tile_source); | 45 void LogMostVisitedNavigation(int position, NTPLoggingTileSource tile_source); |
| 39 | 46 |
| 40 protected: | 47 protected: |
| 41 explicit NTPUserDataLogger(content::WebContents* contents); | 48 explicit NTPUserDataLogger(content::WebContents* contents); |
| 42 | 49 |
| 43 private: | 50 private: |
| 44 friend class content::WebContentsUserData<NTPUserDataLogger>; | 51 friend class content::WebContentsUserData<NTPUserDataLogger>; |
| 45 | 52 |
| 46 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, | 53 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, |
| 47 OnMostVisitedItemsChangedFromServer); | 54 OnMostVisitedItemsChangedFromServer); |
| 48 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, | 55 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, |
| 49 OnMostVisitedItemsChangedFromClient); | 56 OnMostVisitedItemsChangedFromClient); |
| 50 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, | 57 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, |
| 51 TestLogging); | 58 TestLogging); |
| 59 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestLogMostVisitedImpression); |
| 60 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestNumberOfTiles); |
| 61 |
| 62 // Number of Most Visited elements on the NTP for logging purposes. |
| 63 static const int kNumMostVisited = 8; |
| 64 |
| 65 // content::WebContentsObserver override |
| 66 void NavigationEntryCommitted( |
| 67 const content::LoadCommittedDetails& load_details) override; |
| 68 |
| 69 // Implementation of NavigationEntryCommitted; separate for test. |
| 70 void NavigatedFromURLToURL(const GURL& from, const GURL& to); |
| 52 | 71 |
| 53 // Logs a number of statistics regarding the NTP. Called when an NTP tab is | 72 // Logs a number of statistics regarding the NTP. Called when an NTP tab is |
| 54 // about to be deactivated (be it by switching tabs, losing focus or closing | 73 // about to be deactivated (be it by switching tabs, losing focus or closing |
| 55 // the tab/shutting down Chrome), or when the user navigates to a URL. | 74 // the tab/shutting down Chrome), or when the user navigates to a URL. |
| 56 void EmitNtpStatistics(base::TimeDelta load_time); | 75 void EmitNtpStatistics(base::TimeDelta load_time); |
| 57 | 76 |
| 77 // Records whether we have yet logged an impression for the tile at a given |
| 78 // index. A typical NTP will log 8 impressions, but could record fewer for new |
| 79 // users that haven't built up a history yet. |
| 80 // |
| 81 // If something happens that causes the NTP to pull tiles from different |
| 82 // sources, such as signing in (switching from client to server tiles), then |
| 83 // only the impressions for the first source will be logged, leaving the |
| 84 // number of impressions for a source slightly out-of-sync with navigations. |
| 85 std::bitset<kNumMostVisited> impression_was_logged_; |
| 86 |
| 58 // True if at least one iframe came from a server-side suggestion. | 87 // True if at least one iframe came from a server-side suggestion. |
| 59 bool has_server_side_suggestions_; | 88 bool has_server_side_suggestions_; |
| 60 | 89 |
| 61 // True if at least one iframe came from a client-side suggestion. | 90 // True if at least one iframe came from a client-side suggestion. |
| 62 bool has_client_side_suggestions_; | 91 bool has_client_side_suggestions_; |
| 63 | 92 |
| 64 // Total number of tiles rendered, no matter if it's a thumbnail, a gray tile | 93 // Total number of tiles rendered, no matter if it's a thumbnail, a gray tile |
| 65 // or an external tile. | 94 // or an external tile. |
| 66 size_t number_of_tiles_; | 95 size_t number_of_tiles_; |
| 67 | 96 |
| 68 // Whether we have already emitted NTP stats for this web contents. | 97 // Whether we have already emitted NTP stats for this web contents. |
| 69 bool has_emitted_; | 98 bool has_emitted_; |
| 70 | 99 |
| 71 // Are stats being logged during Chrome startup? | 100 // Are stats being logged during Chrome startup? |
| 72 bool during_startup_; | 101 bool during_startup_; |
| 73 | 102 |
| 74 // The URL of this New Tab Page - varies based on NTP version. | 103 // The URL of this New Tab Page - varies based on NTP version. |
| 75 GURL ntp_url_; | 104 GURL ntp_url_; |
| 76 | 105 |
| 77 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); | 106 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); |
| 78 }; | 107 }; |
| 79 | 108 |
| 80 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ | 109 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ |
| OLD | NEW |