OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_CORE_DATA_USE_RECORDER_H_ | 5 #ifndef COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ |
6 #define COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ | 6 #define COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include <map> |
| 11 |
11 #include "base/macros.h" | 12 #include "base/macros.h" |
12 #include "base/supports_user_data.h" | 13 #include "base/supports_user_data.h" |
13 #include "components/data_use_measurement/core/data_use.h" | 14 #include "components/data_use_measurement/core/data_use.h" |
14 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 class URLRequest; | 18 class URLRequest; |
18 } | 19 } |
19 | 20 |
20 namespace data_use_measurement { | 21 namespace data_use_measurement { |
21 | 22 |
22 // Tracks all network data used by a single high level entity. An entity | 23 // Tracks all network data used by a single high level entity. An entity |
23 // can make multiple URLRequests, so there is a one:many relationship between | 24 // can make multiple URLRequests, so there is a one:many relationship between |
24 // DataUseRecorders and URLRequests. Data used by each URLRequest will be | 25 // DataUseRecorders and URLRequests. Data used by each URLRequest will be |
25 // tracked by exactly one DataUseRecorder. | 26 // tracked by exactly one DataUseRecorder. |
26 class DataUseRecorder { | 27 class DataUseRecorder { |
27 public: | 28 public: |
| 29 // Stores network data used by a URLRequest. |
| 30 struct URLRequestDataUse { |
| 31 URLRequestDataUse() : bytes_received(0), bytes_sent(0) {} |
| 32 |
| 33 int64_t bytes_received; |
| 34 int64_t bytes_sent; |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(URLRequestDataUse); |
| 38 }; |
| 39 |
28 explicit DataUseRecorder(DataUse::TrafficType traffic_type); | 40 explicit DataUseRecorder(DataUse::TrafficType traffic_type); |
29 virtual ~DataUseRecorder(); | 41 virtual ~DataUseRecorder(); |
30 | 42 |
31 // Returns the actual data used by the entity being tracked. | 43 // Returns the actual data used by the entity being tracked. |
32 DataUse& data_use() { return data_use_; } | 44 DataUse& data_use() { return data_use_; } |
33 const base::hash_set<net::URLRequest*>& pending_url_requests() const { | 45 const std::map<net::URLRequest*, URLRequestDataUse>& pending_url_requests() |
| 46 const { |
34 return pending_url_requests_; | 47 return pending_url_requests_; |
35 } | 48 } |
36 const net::URLRequest* main_url_request() const { return main_url_request_; } | 49 const net::URLRequest* main_url_request() const { return main_url_request_; } |
37 | 50 |
38 void set_main_url_request(const net::URLRequest* request) { | 51 void set_main_url_request(const net::URLRequest* request) { |
39 main_url_request_ = request; | 52 main_url_request_ = request; |
40 } | 53 } |
41 | 54 |
42 bool is_visible() const { return is_visible_; } | 55 bool is_visible() const { return is_visible_; } |
43 | 56 |
44 void set_is_visible(bool is_visible) { is_visible_ = is_visible; } | 57 void set_is_visible(bool is_visible) { is_visible_ = is_visible; } |
45 | 58 |
46 uint64_t page_transition() const { return page_transition_; } | 59 uint64_t page_transition() const { return page_transition_; } |
47 | 60 |
48 void set_page_transition(uint64_t page_transition) { | 61 void set_page_transition(uint64_t page_transition) { |
49 page_transition_ = page_transition; | 62 page_transition_ = page_transition; |
50 } | 63 } |
51 | 64 |
52 // Returns whether data use is complete and no additional data can be used | 65 // Returns whether data use is complete and no additional data can be used |
53 // by the entity tracked by this recorder. For example, | 66 // by the entity tracked by this recorder. For example, |
54 bool IsDataUseComplete(); | 67 bool IsDataUseComplete(); |
55 | 68 |
56 // Adds |request| to the list of pending URLRequests that ascribe data use to | 69 // Adds |request| to the list of pending URLRequests that ascribe data use to |
57 // this recorder. | 70 // this recorder. |
58 void AddPendingURLRequest(net::URLRequest* request); | 71 void AddPendingURLRequest(net::URLRequest* request); |
59 | 72 |
| 73 // Moves pending |request| from |this| recorder to |other| recorder, and |
| 74 // updates the data use for the recorders. |
| 75 void MovePendingURLRequest(DataUseRecorder* other, net::URLRequest* request); |
| 76 |
60 // Clears the list of pending URLRequests that ascribe data use to this | 77 // Clears the list of pending URLRequests that ascribe data use to this |
61 // recorder. | 78 // recorder. |
62 void RemoveAllPendingURLRequests(); | 79 void RemoveAllPendingURLRequests(); |
63 | 80 |
64 // Merge another DataUseRecorder to this instance. | |
65 void MergeFrom(DataUseRecorder* other); | |
66 | |
67 // Network Delegate methods: | 81 // Network Delegate methods: |
68 void OnBeforeUrlRequest(net::URLRequest* request); | 82 void OnBeforeUrlRequest(net::URLRequest* request); |
69 void OnUrlRequestDestroyed(net::URLRequest* request); | 83 void OnUrlRequestDestroyed(net::URLRequest* request); |
70 void OnNetworkBytesSent(net::URLRequest* request, int64_t bytes_sent); | 84 void OnNetworkBytesSent(net::URLRequest* request, int64_t bytes_sent); |
71 void OnNetworkBytesReceived(net::URLRequest* request, int64_t bytes_received); | 85 void OnNetworkBytesReceived(net::URLRequest* request, int64_t bytes_received); |
72 | 86 |
73 private: | 87 private: |
| 88 // Updates the network data use for the url request. |
| 89 void UpdateNetworkByteCounts(net::URLRequest* request, |
| 90 int64_t bytes_received, |
| 91 int64_t bytes_sent); |
| 92 |
74 // Pending URLRequests whose data is being tracked by this DataUseRecorder. | 93 // Pending URLRequests whose data is being tracked by this DataUseRecorder. |
75 base::hash_set<net::URLRequest*> pending_url_requests_; | 94 std::map<net::URLRequest*, URLRequestDataUse> pending_url_requests_; |
76 | |
77 // Data sources other than URLRequests, whose data is being tracked by this | |
78 // DataUseRecorder. | |
79 base::hash_set<const void*> pending_data_sources_; | |
80 | 95 |
81 // The main frame URLRequest for page loads. Null if this is not tracking a | 96 // The main frame URLRequest for page loads. Null if this is not tracking a |
82 // page load. | 97 // page load. |
83 const net::URLRequest* main_url_request_; | 98 const net::URLRequest* main_url_request_; |
84 | 99 |
85 // The network data use measured by this DataUseRecorder. | 100 // The network data use measured by this DataUseRecorder. |
86 DataUse data_use_; | 101 DataUse data_use_; |
87 | 102 |
88 // Whether the entity that owns this data use is currently visible. | 103 // Whether the entity that owns this data use is currently visible. |
89 bool is_visible_; | 104 bool is_visible_; |
90 | 105 |
91 // ui::PageTransition casted as a uint64_t. | 106 // ui::PageTransition casted as a uint64_t. |
92 uint64_t page_transition_; | 107 uint64_t page_transition_; |
93 | 108 |
94 DISALLOW_COPY_AND_ASSIGN(DataUseRecorder); | 109 DISALLOW_COPY_AND_ASSIGN(DataUseRecorder); |
95 }; | 110 }; |
96 | 111 |
97 } // namespace data_use_measurement | 112 } // namespace data_use_measurement |
98 | 113 |
99 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ | 114 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ |
OLD | NEW |