OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| 6 #define COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "build/build_config.h" |
| 14 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 15 |
| 16 #if defined(OS_ANDROID) |
| 17 #include "base/android/application_status_listener.h" |
| 18 #endif |
| 19 |
| 20 namespace net { |
| 21 class URLRequest; |
| 22 } |
| 23 |
| 24 namespace data_use_measurement { |
| 25 |
| 26 // Records the data use of user traffic and various services in UMA histograms. |
| 27 // The UMA is broken down by network technology used (Wi-Fi vs cellular). On |
| 28 // Android, the UMA is further broken down by whether the application was in the |
| 29 // background or foreground during the request. |
| 30 class DataUseMeasurement { |
| 31 public: |
| 32 DataUseMeasurement(); |
| 33 ~DataUseMeasurement(); |
| 34 |
| 35 // Records the data use of the |request|, thus |request| must be non-null. |
| 36 void ReportDataUseUMA(const net::URLRequest* request); |
| 37 |
| 38 #if defined(OS_ANDROID) |
| 39 // This function should just be used for testing purposes. A change in |
| 40 // application state can be simulated by calling this function. |
| 41 void OnApplicationStateChangeForTesting( |
| 42 base::android::ApplicationState application_state); |
| 43 #endif |
| 44 |
| 45 private: |
| 46 // Specifies that data is received or sent, respectively. |
| 47 enum TrafficDirection { DOWNSTREAM, UPSTREAM }; |
| 48 |
| 49 // The state of the application. Only available on Android and on other |
| 50 // platforms it is always FOREGROUND. |
| 51 enum AppState { BACKGROUND, FOREGROUND }; |
| 52 |
| 53 // The request is from a user or from a service. |
| 54 enum RequestInitiator { REQUEST_INITIATOR_USER, REQUEST_INITIATOR_SERVICE }; |
| 55 |
| 56 // Returns the current application state (Foreground or Background). It always |
| 57 // returns Foreground if Chrome is not running on Android. |
| 58 AppState CurrentAppState(); |
| 59 |
| 60 // Produces a string representing whether the data use was on the send |
| 61 // ("Upstream") or receive ("Downstream") path, whether the app was in the |
| 62 // "Foreground" or "Background", and whether a "Cellular" or "WiFi" network |
| 63 // was use. For example, "Upstream.Foreground.Cellular" is a possible output. |
| 64 const std::string GetSuffixForHistogramName(TrafficDirection dir); |
| 65 |
| 66 #if defined(OS_ANDROID) |
| 67 // Called whenever the application transitions from foreground to background |
| 68 // and vice versa. |
| 69 void OnApplicationStateChange( |
| 70 base::android::ApplicationState application_state); |
| 71 #endif |
| 72 |
| 73 // A helper function used to record data use of services. It gets the size of |
| 74 // exchanged message, its direction (which is upstream or downstream) and |
| 75 // reports to two histogram groups. DataUse.MessageSize.ServiceName and |
| 76 // DataUse.Services.{Dimensions}. In the second one, services are buckets. |
| 77 void ReportDataUsageServices( |
| 78 data_use_measurement::DataUseUserData::ServiceType service, |
| 79 TrafficDirection dir, |
| 80 int64_t message_size); |
| 81 |
| 82 // A helper function used for reporting data use of user and all the services |
| 83 // as a single group. Two histogram groups are updated by this function. The |
| 84 // first one is DataUse.TrafficSize.User.{Dimensions} and the second one is |
| 85 // DataUse.TrafficSize.System.{Dimensions}. Based on |service_type| one of |
| 86 // these groups is selected and updated. |
| 87 void ReportDataUsageGeneral(RequestInitiator service_type, |
| 88 TrafficDirection dir, |
| 89 int64_t message_size); |
| 90 |
| 91 #if defined(OS_ANDROID) |
| 92 // Application listener store the last known state of the application in this |
| 93 // field. |
| 94 base::android::ApplicationState app_state_; |
| 95 |
| 96 // ApplicationStatusListener used to monitor whether the application is in the |
| 97 // foreground or in the background. It is owned by DataUseMeasurement |
| 98 scoped_ptr<base::android::ApplicationStatusListener> app_listener_; |
| 99 #endif |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement); |
| 102 }; |
| 103 |
| 104 } // namespace data_use_measurement |
| 105 |
| 106 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_ |
OLD | NEW |