| 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 // Indicates that |request| has been completed or failed. | 50 // Indicates that |request| has been completed or failed. |
| 48 void OnCompleted(const net::URLRequest& request, bool started); | 51 void OnCompleted(const net::URLRequest& request, bool started); |
| 49 | 52 |
| 50 // Returns true if the URLRequest |request| is initiated by user traffic. | 53 // Returns true if the URLRequest |request| is initiated by user traffic. |
| 51 static bool IsUserInitiatedRequest(const net::URLRequest& request); | 54 static bool IsUserInitiatedRequest(const net::URLRequest& request); |
| 52 | 55 |
| 53 #if defined(OS_ANDROID) | 56 #if defined(OS_ANDROID) |
| 54 // This function should just be used for testing purposes. A change in | 57 // This function should just be used for testing purposes. A change in |
| 55 // application state can be simulated by calling this function. | 58 // application state can be simulated by calling this function. |
| 56 void OnApplicationStateChangeForTesting( | 59 void OnApplicationStateChangeForTesting( |
| 57 base::android::ApplicationState application_state); | 60 base::android::ApplicationState application_state); |
| 58 #endif | 61 #endif |
| 59 | 62 |
| 60 private: | 63 private: |
| 64 friend class DataUseMeasurementTest; |
| 65 |
| 61 // Specifies that data is received or sent, respectively. | 66 // Specifies that data is received or sent, respectively. |
| 62 enum TrafficDirection { DOWNSTREAM, UPSTREAM }; | 67 enum TrafficDirection { DOWNSTREAM, UPSTREAM }; |
| 63 | 68 |
| 64 // The state of the application. Only available on Android and on other | 69 // The state of the application. Only available on Android and on other |
| 65 // platforms it is always FOREGROUND. | 70 // platforms it is always FOREGROUND. |
| 66 enum AppState { BACKGROUND, FOREGROUND }; | 71 enum AppState { BACKGROUND, FOREGROUND }; |
| 67 | 72 |
| 68 // Returns the current application state (Foreground or Background). It always | 73 // Returns the current application state (Foreground or Background). It always |
| 69 // returns Foreground if Chrome is not running on Android. | 74 // returns Foreground if Chrome is not running on Android. |
| 70 AppState CurrentAppState() const; | 75 AppState CurrentAppState() const; |
| 71 | 76 |
| 72 // Makes the full name of the histogram. It is made from |prefix| and suffix | 77 // Makes the full name of the histogram. It is made from |prefix| and suffix |
| 73 // which is made based on network and application status. suffix is a string | 78 // which is made based on network and application status. suffix is a string |
| 74 // representing whether the data use was on the send ("Upstream") or receive | 79 // representing whether the data use was on the send ("Upstream") or receive |
| 75 // ("Downstream") path, whether the app was in the "Foreground" or | 80 // ("Downstream") path, whether the app was in the "Foreground" or |
| 76 // "Background", and whether a "Cellular" or "WiFi" network was use. For | 81 // "Background", and whether a "Cellular" or "WiFi" network was use. For |
| 77 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output. | 82 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output. |
| 83 // |started_in_foreground| indicates if the request started when the app was |
| 84 // in foreground. |
| 78 std::string GetHistogramName(const char* prefix, | 85 std::string GetHistogramName(const char* prefix, |
| 79 TrafficDirection dir, | 86 TrafficDirection dir, |
| 87 bool started_in_foreground, |
| 80 bool is_connection_cellular) const; | 88 bool is_connection_cellular) const; |
| 81 | 89 |
| 82 #if defined(OS_ANDROID) | 90 #if defined(OS_ANDROID) |
| 83 // Called whenever the application transitions from foreground to background | 91 // Called whenever the application transitions from foreground to background |
| 84 // and vice versa. | 92 // and vice versa. |
| 85 void OnApplicationStateChange( | 93 void OnApplicationStateChange( |
| 86 base::android::ApplicationState application_state); | 94 base::android::ApplicationState application_state); |
| 87 #endif | 95 #endif |
| 88 | 96 |
| 89 // Records the data use of the |request|, thus |request| must be non-null. | 97 // Records the data use of the |request|, thus |request| must be non-null. |
| 90 void ReportDataUseUMA(const net::URLRequest& request) const; | 98 void ReportDataUseUMA(const net::URLRequest& request) const; |
| 91 | 99 |
| 92 // A helper function used to record data use of services. It gets the size of | 100 // A helper function used to record data use of services. It gets the size of |
| 93 // exchanged message, its direction (which is upstream or downstream) and | 101 // exchanged message, its direction (which is upstream or downstream) and |
| 94 // reports to two histogram groups. DataUse.MessageSize.ServiceName and | 102 // reports to two histogram groups. DataUse.MessageSize.ServiceName and |
| 95 // DataUse.Services.{Dimensions}. In the second one, services are buckets. | 103 // DataUse.Services.{Dimensions}. In the second one, services are buckets. |
| 104 // |started_in_foreground| indicates if the request started when the app was |
| 105 // in foreground. |
| 96 void ReportDataUsageServices( | 106 void ReportDataUsageServices( |
| 97 data_use_measurement::DataUseUserData::ServiceName service, | 107 data_use_measurement::DataUseUserData::ServiceName service, |
| 98 TrafficDirection dir, | 108 TrafficDirection dir, |
| 109 bool started_in_foreground, |
| 99 bool is_connection_cellular, | 110 bool is_connection_cellular, |
| 100 int64_t message_size) const; | 111 int64_t message_size) const; |
| 101 | 112 |
| 102 // Callback for updating data use prefs. | 113 // Callback for updating data use prefs. |
| 103 // TODO(rajendrant): If a similar mechanism would need be used for components | 114 // TODO(rajendrant): If a similar mechanism would need be used for components |
| 104 // other than metrics, then the better approach would be to refactor this | 115 // other than metrics, then the better approach would be to refactor this |
| 105 // class to support registering arbitrary observers. crbug.com/601185 | 116 // class to support registering arbitrary observers. crbug.com/601185 |
| 106 metrics::UpdateUsagePrefCallbackType metrics_data_use_forwarder_; | 117 metrics::UpdateUsagePrefCallbackType metrics_data_use_forwarder_; |
| 107 | 118 |
| 108 #if defined(OS_ANDROID) | 119 #if defined(OS_ANDROID) |
| 109 // Application listener store the last known state of the application in this | 120 // Application listener store the last known state of the application in this |
| 110 // field. | 121 // field. |
| 111 base::android::ApplicationState app_state_; | 122 base::android::ApplicationState app_state_; |
| 112 | 123 |
| 113 // ApplicationStatusListener used to monitor whether the application is in the | 124 // ApplicationStatusListener used to monitor whether the application is in the |
| 114 // foreground or in the background. It is owned by DataUseMeasurement. | 125 // foreground or in the background. It is owned by DataUseMeasurement. |
| 115 std::unique_ptr<base::android::ApplicationStatusListener> app_listener_; | 126 std::unique_ptr<base::android::ApplicationStatusListener> app_listener_; |
| 116 #endif | 127 #endif |
| 117 | 128 |
| 118 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement); | 129 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement); |
| 119 }; | 130 }; |
| 120 | 131 |
| 121 } // namespace data_use_measurement | 132 } // namespace data_use_measurement |
| 122 | 133 |
| 123 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ | 134 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| OLD | NEW |