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.
5 #ifndef COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_
6 #define COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "build/build_config.h"
15 #include "components/data_use_measurement/core/data_use_user_data.h"
17 #if defined(OS_ANDROID)
18 #include "base/android/application_status_listener.h"
25 namespace data_use_measurement
{
27 // Records the data use of user traffic and various services in UMA histograms.
28 // The UMA is broken down by network technology used (Wi-Fi vs cellular). On
29 // Android, the UMA is further broken down by whether the application was in the
30 // background or foreground during the request.
31 // TODO(amohammadkhan): Complete the layered architecture.
32 // http://crbug.com/527460
33 class DataUseMeasurement
{
36 ~DataUseMeasurement();
38 // Records the data use of the |request|, thus |request| must be non-null.
39 void ReportDataUseUMA(const net::URLRequest
* request
) const;
41 #if defined(OS_ANDROID)
42 // This function should just be used for testing purposes. A change in
43 // application state can be simulated by calling this function.
44 void OnApplicationStateChangeForTesting(
45 base::android::ApplicationState application_state
);
49 // Specifies that data is received or sent, respectively.
50 enum TrafficDirection
{ DOWNSTREAM
, UPSTREAM
};
52 // The state of the application. Only available on Android and on other
53 // platforms it is always FOREGROUND.
54 enum AppState
{ BACKGROUND
, FOREGROUND
};
56 // Returns the current application state (Foreground or Background). It always
57 // returns Foreground if Chrome is not running on Android.
58 AppState
CurrentAppState() const;
60 // Makes the full name of the histogram. It is made from |prefix| and suffix
61 // which is made based on network and application status. suffix is a string
62 // representing whether the data use was on the send ("Upstream") or receive
63 // ("Downstream") path, whether the app was in the "Foreground" or
64 // "Background", and whether a "Cellular" or "WiFi" network was use. For
65 // example, "Prefix.Upstream.Foreground.Cellular" is a possible output.
66 std::string
GetHistogramName(const char* prefix
, TrafficDirection dir
) const;
68 #if defined(OS_ANDROID)
69 // Called whenever the application transitions from foreground to background
71 void OnApplicationStateChange(
72 base::android::ApplicationState application_state
);
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::ServiceName service
,
82 int64_t message_size
) const;
84 #if defined(OS_ANDROID)
85 // Application listener store the last known state of the application in this
87 base::android::ApplicationState app_state_
;
89 // ApplicationStatusListener used to monitor whether the application is in the
90 // foreground or in the background. It is owned by DataUseMeasurement.
91 scoped_ptr
<base::android::ApplicationStatusListener
> app_listener_
;
94 DISALLOW_COPY_AND_ASSIGN(DataUseMeasurement
);
97 } // namespace data_use_measurement
99 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CONTENT_DATA_USE_MEASUREMENT_H_