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