| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ | 5 #ifndef COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| 6 #define COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ | 6 #define COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // Android, the UMA is further broken down by whether the application was in the | 33 // Android, the UMA is further broken down by whether the application was in the |
| 34 // background or foreground during the request. | 34 // background or foreground during the request. |
| 35 // TODO(amohammadkhan): Complete the layered architecture. | 35 // TODO(amohammadkhan): Complete the layered architecture. |
| 36 // http://crbug.com/527460 | 36 // http://crbug.com/527460 |
| 37 class DataUseMeasurement { | 37 class DataUseMeasurement { |
| 38 public: | 38 public: |
| 39 explicit DataUseMeasurement( | 39 explicit DataUseMeasurement( |
| 40 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder); | 40 const metrics::UpdateUsagePrefCallbackType& metrics_data_use_forwarder); |
| 41 ~DataUseMeasurement(); | 41 ~DataUseMeasurement(); |
| 42 | 42 |
| 43 // Called before a request is sent. |
| 44 void OnBeforeURLRequest(net::URLRequest* request); |
| 45 |
| 43 // Called right after a redirect response code was received for |request|. | 46 // Called right after a redirect response code was received for |request|. |
| 44 void OnBeforeRedirect(const net::URLRequest& request, | 47 void OnBeforeRedirect(const net::URLRequest& request, |
| 45 const GURL& new_location); | 48 const GURL& new_location); |
| 46 | 49 |
| 47 // Called when data is received or sent on the network, respectively. | 50 // Called when data is received or sent on the network, respectively. |
| 48 void OnNetworkBytesReceived(const net::URLRequest& request, | 51 void OnNetworkBytesReceived(const net::URLRequest& request, |
| 49 int64_t bytes_received); | 52 int64_t bytes_received); |
| 50 void OnNetworkBytesSent(const net::URLRequest& request, int64_t bytes_sent); | 53 void OnNetworkBytesSent(const net::URLRequest& request, int64_t bytes_sent); |
| 51 | 54 |
| 52 // Indicates that |request| has been completed or failed. | 55 // Indicates that |request| has been completed or failed. |
| 53 void OnCompleted(const net::URLRequest& request, bool started); | 56 void OnCompleted(const net::URLRequest& request, bool started); |
| 54 | 57 |
| 55 // Returns true if the URLRequest |request| is initiated by user traffic. | 58 // Returns true if the URLRequest |request| is initiated by user traffic. |
| 56 static bool IsUserInitiatedRequest(const net::URLRequest& request); | 59 static bool IsUserInitiatedRequest(const net::URLRequest& request); |
| 57 | 60 |
| 58 #if defined(OS_ANDROID) | 61 #if defined(OS_ANDROID) |
| 59 // This function should just be used for testing purposes. A change in | 62 // This function should just be used for testing purposes. A change in |
| 60 // application state can be simulated by calling this function. | 63 // application state can be simulated by calling this function. |
| 61 void OnApplicationStateChangeForTesting( | 64 void OnApplicationStateChangeForTesting( |
| 62 base::android::ApplicationState application_state); | 65 base::android::ApplicationState application_state); |
| 63 #endif | 66 #endif |
| 64 | 67 |
| 65 private: | 68 private: |
| 69 friend class DataUseMeasurementTest; |
| 70 |
| 66 // Specifies that data is received or sent, respectively. | 71 // Specifies that data is received or sent, respectively. |
| 67 enum TrafficDirection { DOWNSTREAM, UPSTREAM }; | 72 enum TrafficDirection { DOWNSTREAM, UPSTREAM }; |
| 68 | 73 |
| 69 // The state of the application. Only available on Android and on other | |
| 70 // platforms it is always FOREGROUND. | |
| 71 enum AppState { BACKGROUND, FOREGROUND }; | |
| 72 | |
| 73 // Returns the current application state (Foreground or Background). It always | 74 // Returns the current application state (Foreground or Background). It always |
| 74 // returns Foreground if Chrome is not running on Android. | 75 // returns Foreground if Chrome is not running on Android. |
| 75 AppState CurrentAppState() const; | 76 DataUseUserData::AppState CurrentAppState() const; |
| 76 | 77 |
| 77 // Makes the full name of the histogram. It is made from |prefix| and suffix | 78 // Makes the full name of the histogram. It is made from |prefix| and suffix |
| 78 // which is made based on network and application status. suffix is a string | 79 // which is made based on network and application status. suffix is a string |
| 79 // representing whether the data use was on the send ("Upstream") or receive | 80 // representing whether the data use was on the send ("Upstream") or receive |
| 80 // ("Downstream") path, whether the app was in the "Foreground" or | 81 // ("Downstream") path, whether the app was in the "Foreground" or |
| 81 // "Background", and whether a "Cellular" or "WiFi" network was use. For | 82 // "Background", and whether a "Cellular" or "WiFi" network was use. For |
| 82 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output. | 83 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output. |
| 84 // |started_in_foreground| indicates if the request started when the app was |
| 85 // in foreground. |
| 83 std::string GetHistogramName(const char* prefix, | 86 std::string GetHistogramName(const char* prefix, |
| 84 TrafficDirection dir, | 87 TrafficDirection dir, |
| 88 bool started_in_foreground, |
| 85 bool is_connection_cellular) const; | 89 bool is_connection_cellular) const; |
| 86 | 90 |
| 87 #if defined(OS_ANDROID) | 91 #if defined(OS_ANDROID) |
| 88 // Called whenever the application transitions from foreground to background | 92 // Called whenever the application transitions from foreground to background |
| 89 // and vice versa. | 93 // and vice versa. |
| 90 void OnApplicationStateChange( | 94 void OnApplicationStateChange( |
| 91 base::android::ApplicationState application_state); | 95 base::android::ApplicationState application_state); |
| 92 | 96 |
| 93 // Records the count of bytes received and sent by Chrome on the network as | 97 // Records the count of bytes received and sent by Chrome on the network as |
| 94 // reported by the operating system. | 98 // reported by the operating system. |
| 95 void MaybeRecordNetworkBytesOS(); | 99 void MaybeRecordNetworkBytesOS(); |
| 96 #endif | 100 #endif |
| 97 | 101 |
| 98 // Records the data use of the |request|, thus |request| must be non-null. | 102 // Records the data use of the |request|, thus |request| must be non-null. |
| 99 void ReportDataUseUMA(const net::URLRequest& request) const; | 103 void ReportDataUseUMA(const net::URLRequest& request) const; |
| 100 | 104 |
| 101 // A helper function used to record data use of services. It gets the size of | 105 // A helper function used to record data use of services. It gets the size of |
| 102 // exchanged message, its direction (which is upstream or downstream) and | 106 // exchanged message, its direction (which is upstream or downstream) and |
| 103 // reports to two histogram groups. DataUse.MessageSize.ServiceName and | 107 // reports to two histogram groups. DataUse.MessageSize.ServiceName and |
| 104 // DataUse.Services.{Dimensions}. In the second one, services are buckets. | 108 // DataUse.Services.{Dimensions}. In the second one, services are buckets. |
| 109 // |started_in_foreground| indicates if the request started when the app was |
| 110 // in foreground. |
| 105 void ReportDataUsageServices( | 111 void ReportDataUsageServices( |
| 106 data_use_measurement::DataUseUserData::ServiceName service, | 112 data_use_measurement::DataUseUserData::ServiceName service, |
| 107 TrafficDirection dir, | 113 TrafficDirection dir, |
| 114 bool started_in_foreground, |
| 108 bool is_connection_cellular, | 115 bool is_connection_cellular, |
| 109 int64_t message_size) const; | 116 int64_t message_size) const; |
| 110 | 117 |
| 111 // Callback for updating data use prefs. | 118 // Callback for updating data use prefs. |
| 112 // TODO(rajendrant): If a similar mechanism would need be used for components | 119 // TODO(rajendrant): If a similar mechanism would need be used for components |
| 113 // other than metrics, then the better approach would be to refactor this | 120 // other than metrics, then the better approach would be to refactor this |
| 114 // class to support registering arbitrary observers. crbug.com/601185 | 121 // class to support registering arbitrary observers. crbug.com/601185 |
| 115 metrics::UpdateUsagePrefCallbackType metrics_data_use_forwarder_; | 122 metrics::UpdateUsagePrefCallbackType metrics_data_use_forwarder_; |
| 116 | 123 |
| 117 #if defined(OS_ANDROID) | 124 #if defined(OS_ANDROID) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 134 // statistics. | 141 // statistics. |
| 135 int64_t bytes_transferred_since_last_traffic_stats_query_; | 142 int64_t bytes_transferred_since_last_traffic_stats_query_; |
| 136 #endif | 143 #endif |
| 137 | 144 |
| 138 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement); | 145 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement); |
| 139 }; | 146 }; |
| 140 | 147 |
| 141 } // namespace data_use_measurement | 148 } // namespace data_use_measurement |
| 142 | 149 |
| 143 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ | 150 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| OLD | NEW |