| 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 "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/common/search/ntp_logging_events.h" | 13 #include "chrome/common/search/ntp_logging_events.h" |
| 14 #include "content/public/browser/web_contents_observer.h" | |
| 15 #include "content/public/browser/web_contents_user_data.h" | 14 #include "content/public/browser/web_contents_user_data.h" |
| 16 | 15 |
| 17 namespace content { | 16 namespace content { |
| 18 class WebContents; | 17 class WebContents; |
| 19 } | 18 } |
| 20 | 19 |
| 21 // Helper class for logging data from the NTP. Attached to each NTP instance. | 20 // Helper class for logging data from the NTP. Attached to each NTP instance. |
| 22 class NTPUserDataLogger | 21 class NTPUserDataLogger |
| 23 : public content::WebContentsObserver, | 22 : public content::WebContentsUserData<NTPUserDataLogger> { |
| 24 public content::WebContentsUserData<NTPUserDataLogger> { | |
| 25 public: | 23 public: |
| 26 ~NTPUserDataLogger() override; | 24 ~NTPUserDataLogger() override; |
| 27 | 25 |
| 28 static NTPUserDataLogger* GetOrCreateFromWebContents( | 26 static NTPUserDataLogger* GetOrCreateFromWebContents( |
| 29 content::WebContents* content); | 27 content::WebContents* content); |
| 30 | 28 |
| 31 // Called when an event occurs on the NTP that requires a counter to be | 29 // Called when an event occurs on the NTP that requires a counter to be |
| 32 // incremented. |time| is the delta time in ms from navigation start until | 30 // incremented. |time| is the delta time in ms from navigation start until |
| 33 // this event happened. | 31 // this event happened. |
| 34 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); | 32 void LogEvent(NTPLoggingEventType event, base::TimeDelta time); |
| 35 | 33 |
| 36 // Logs an impression on one of the NTP tiles by a given source. | 34 // Logs an impression on one of the NTP tiles by a given source. |
| 37 void LogMostVisitedImpression(int position, NTPLoggingTileSource tile_source); | 35 void LogMostVisitedImpression(int position, NTPLoggingTileSource tile_source); |
| 38 | 36 |
| 39 // Logs a navigation on one of the NTP tiles by a given source. | 37 // Logs a navigation on one of the NTP tiles by a given source. |
| 40 void LogMostVisitedNavigation(int position, NTPLoggingTileSource tile_source); | 38 void LogMostVisitedNavigation(int position, NTPLoggingTileSource tile_source); |
| 41 | 39 |
| 42 // Called when the tab is closed. Logs statistics. | |
| 43 void TabDeactivated(); | |
| 44 | |
| 45 // Called when the set of most visited sites changes. Flushes statistics. | |
| 46 void MostVisitedItemsChanged(); | |
| 47 | |
| 48 protected: | 40 protected: |
| 49 explicit NTPUserDataLogger(content::WebContents* contents); | 41 explicit NTPUserDataLogger(content::WebContents* contents); |
| 50 | 42 |
| 51 private: | 43 private: |
| 52 friend class content::WebContentsUserData<NTPUserDataLogger>; | 44 friend class content::WebContentsUserData<NTPUserDataLogger>; |
| 53 | 45 |
| 54 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, | 46 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, |
| 55 OnMostVisitedItemsChangedFromServer); | 47 OnMostVisitedItemsChangedFromServer); |
| 56 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, | 48 FRIEND_TEST_ALL_PREFIXES(SearchTabHelperTest, |
| 57 OnMostVisitedItemsChangedFromClient); | 49 OnMostVisitedItemsChangedFromClient); |
| 58 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, | 50 FRIEND_TEST_ALL_PREFIXES(NTPUserDataLoggerTest, |
| 59 TestLogging); | 51 TestLogging); |
| 60 | 52 |
| 61 // content::WebContentsObserver override | |
| 62 void NavigationEntryCommitted( | |
| 63 const content::LoadCommittedDetails& load_details) override; | |
| 64 | |
| 65 // To clarify at the call site whether we are calling EmitNtpStatistics() for | |
| 66 // a good reason (CLOSED, NAVIGATED_AWAY) or a questionable one (MV_CHANGED, | |
| 67 // INTERNAL_FLUSH). | |
| 68 // TODO(sfiera): remove MV_CHANGED, INTERNAL_FLUSH. | |
| 69 enum class EmitReason { CLOSED, NAVIGATED_AWAY, MV_CHANGED, INTERNAL_FLUSH }; | |
| 70 | |
| 71 // Logs a number of statistics regarding the NTP. Called when an NTP tab is | 53 // Logs a number of statistics regarding the NTP. Called when an NTP tab is |
| 72 // about to be deactivated (be it by switching tabs, losing focus or closing | 54 // about to be deactivated (be it by switching tabs, losing focus or closing |
| 73 // the tab/shutting down Chrome), or when the user navigates to a URL. | 55 // the tab/shutting down Chrome), or when the user navigates to a URL. |
| 74 void EmitNtpStatistics(EmitReason reason); | 56 void EmitNtpStatistics(base::TimeDelta load_time); |
| 75 | 57 |
| 76 // True if at least one iframe came from a server-side suggestion. | 58 // True if at least one iframe came from a server-side suggestion. |
| 77 bool has_server_side_suggestions_; | 59 bool has_server_side_suggestions_; |
| 78 | 60 |
| 79 // True if at least one iframe came from a client-side suggestion. | 61 // True if at least one iframe came from a client-side suggestion. |
| 80 bool has_client_side_suggestions_; | 62 bool has_client_side_suggestions_; |
| 81 | 63 |
| 82 // Total number of tiles rendered, no matter if it's a thumbnail, a gray tile | 64 // Total number of tiles rendered, no matter if it's a thumbnail, a gray tile |
| 83 // or an external tile. | 65 // or an external tile. |
| 84 size_t number_of_tiles_; | 66 size_t number_of_tiles_; |
| 85 | 67 |
| 86 // Time from navigation start it took to load the NTP in milliseconds. | |
| 87 base::TimeDelta load_time_; | |
| 88 | |
| 89 // Whether we have already emitted NTP stats for this web contents. | 68 // Whether we have already emitted NTP stats for this web contents. |
| 90 bool has_emitted_; | 69 bool has_emitted_; |
| 91 | 70 |
| 92 // Are stats being logged during Chrome startup? | 71 // Are stats being logged during Chrome startup? |
| 93 bool during_startup_; | 72 bool during_startup_; |
| 94 | 73 |
| 95 // The URL of this New Tab Page - varies based on NTP version. | 74 // The URL of this New Tab Page - varies based on NTP version. |
| 96 GURL ntp_url_; | 75 GURL ntp_url_; |
| 97 | 76 |
| 98 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); | 77 DISALLOW_COPY_AND_ASSIGN(NTPUserDataLogger); |
| 99 }; | 78 }; |
| 100 | 79 |
| 101 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ | 80 #endif // CHROME_BROWSER_UI_WEBUI_NTP_NTP_USER_DATA_LOGGER_H_ |
| OLD | NEW |