| 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> | 10 #include <bitset> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/common/search/ntp_logging_events.h" | 15 #include "chrome/common/search/ntp_logging_events.h" |
| 16 #include "components/ntp_tiles/tile_source.h" | 16 #include "components/ntp_tiles/tile_source.h" |
| 17 #include "components/ntp_tiles/tile_visual_type.h" |
| 17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "content/public/browser/web_contents_user_data.h" | 19 #include "content/public/browser/web_contents_user_data.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 class WebContents; | 22 class WebContents; |
| 22 } | 23 } |
| 23 | 24 |
| 24 // Helper class for logging data from the NTP. Attached to each NTP instance. | 25 // Helper class for logging data from the NTP. Attached to each NTP instance. |
| 25 class NTPUserDataLogger | 26 class NTPUserDataLogger |
| 26 : public content::WebContentsObserver, | 27 : public content::WebContentsObserver, |
| 27 public content::WebContentsUserData<NTPUserDataLogger> { | 28 public content::WebContentsUserData<NTPUserDataLogger> { |
| 28 public: | 29 public: |
| 29 ~NTPUserDataLogger() override; | 30 ~NTPUserDataLogger() override; |
| 30 | 31 |
| 31 // Gets the associated NTPUserDataLogger, creating it if necessary. | 32 // Gets the associated NTPUserDataLogger, creating it if necessary. |
| 32 // | 33 // |
| 33 // MUST be called only when the NTP is active. | 34 // MUST be called only when the NTP is active. |
| 34 static NTPUserDataLogger* GetOrCreateFromWebContents( | 35 static NTPUserDataLogger* GetOrCreateFromWebContents( |
| 35 content::WebContents* content); | 36 content::WebContents* content); |
| 36 | 37 |
| 37 // Called when an event occurs on the NTP that requires a counter to be | 38 // Called when an event occurs on the NTP that requires a counter to be |
| 38 // incremented. |time| is the delta time from navigation start until this | 39 // incremented. |time| is the delta time from navigation start until this |
| 39 // event happened. | 40 // event happened. |
| 40 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); | 41 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); |
| 41 | 42 |
| 42 // Logs an impression on one of the NTP tiles by a given source. | 43 // Logs an impression on one of the NTP tiles by a given source. |
| 43 void LogMostVisitedImpression(int position, | 44 void LogMostVisitedImpression(int position, |
| 44 ntp_tiles::TileSource tile_source); | 45 ntp_tiles::TileSource tile_source, |
| 46 ntp_tiles::TileVisualType tile_type); |
| 45 | 47 |
| 46 // Logs a navigation on one of the NTP tiles by a given source. | 48 // Logs a navigation on one of the NTP tiles by a given source. |
| 47 void LogMostVisitedNavigation(int position, | 49 void LogMostVisitedNavigation(int position, |
| 48 ntp_tiles::TileSource tile_source); | 50 ntp_tiles::TileSource tile_source, |
| 51 ntp_tiles::TileVisualType tile_type); |
| 49 | 52 |
| 50 protected: | 53 protected: |
| 51 explicit NTPUserDataLogger(content::WebContents* contents); | 54 explicit NTPUserDataLogger(content::WebContents* contents); |
| 52 | 55 |
| 53 private: | 56 private: |
| 54 friend class content::WebContentsUserData<NTPUserDataLogger>; | 57 friend class content::WebContentsUserData<NTPUserDataLogger>; |
| 55 | 58 |
| 56 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestLogMostVisitedImpression); | 59 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestLogMostVisitedImpression); |
| 57 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestNumberOfTiles); | 60 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestNumberOfTiles); |
| 58 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestLoadTime); | 61 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, TestLoadTime); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 79 // If something happens that causes the NTP to pull tiles from different | 82 // If something happens that causes the NTP to pull tiles from different |
| 80 // sources, such as signing in (switching from client to server tiles), then | 83 // sources, such as signing in (switching from client to server tiles), then |
| 81 // only the impressions for the first source will be logged, leaving the | 84 // only the impressions for the first source will be logged, leaving the |
| 82 // number of impressions for a source slightly out-of-sync with navigations. | 85 // number of impressions for a source slightly out-of-sync with navigations. |
| 83 std::bitset<kNumMostVisited> impression_was_logged_; | 86 std::bitset<kNumMostVisited> impression_was_logged_; |
| 84 | 87 |
| 85 // Stores the tile source for each impression. Entries are only valid if the | 88 // Stores the tile source for each impression. Entries are only valid if the |
| 86 // corresponding entry in |impression_was_logged_| is true. | 89 // corresponding entry in |impression_was_logged_| is true. |
| 87 std::vector<ntp_tiles::TileSource> impression_tile_source_; | 90 std::vector<ntp_tiles::TileSource> impression_tile_source_; |
| 88 | 91 |
| 92 // Stores the tile type for each impression. Entries are only valid if the |
| 93 // corresponding entry in |impression_was_logged_| is true. |
| 94 std::vector<ntp_tiles::TileVisualType> impression_tile_type_; |
| 95 |
| 89 // The time we received the NTP_ALL_TILES_RECEIVED event. | 96 // The time we received the NTP_ALL_TILES_RECEIVED event. |
| 90 base::TimeDelta tiles_received_time_; | 97 base::TimeDelta tiles_received_time_; |
| 91 | 98 |
| 92 // Whether we have already emitted NTP stats for this web contents. | 99 // Whether we have already emitted NTP stats for this web contents. |
| 93 bool has_emitted_; | 100 bool has_emitted_; |
| 94 | 101 |
| 95 // Are stats being logged during Chrome startup? | 102 // Are stats being logged during Chrome startup? |
| 96 bool during_startup_; | 103 bool during_startup_; |
| 97 | 104 |
| 98 // The URL of this New Tab Page - varies based on NTP version. | 105 // The URL of this New Tab Page - varies based on NTP version. |
| 99 GURL ntp_url_; | 106 GURL ntp_url_; |
| 100 | 107 |
| 101 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); | 108 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); |
| 102 }; | 109 }; |
| 103 | 110 |
| 104 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ | 111 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ |
| OLD | NEW |