| 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 "base/containers/hash_tables.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // can make multiple URLRequests, so there is a one:many relationship between | 23 // can make multiple URLRequests, so there is a one:many relationship between |
| 24 // DataUseRecorders and URLRequests. Data used by each URLRequest will be | 24 // DataUseRecorders and URLRequests. Data used by each URLRequest will be |
| 25 // tracked by exactly one DataUseRecorder. | 25 // tracked by exactly one DataUseRecorder. |
| 26 class DataUseRecorder { | 26 class DataUseRecorder { |
| 27 public: | 27 public: |
| 28 DataUseRecorder(); | 28 DataUseRecorder(); |
| 29 ~DataUseRecorder(); | 29 ~DataUseRecorder(); |
| 30 | 30 |
| 31 // Returns the actual data used by the entity being tracked. | 31 // Returns the actual data used by the entity being tracked. |
| 32 DataUse& data_use() { return data_use_; } | 32 DataUse& data_use() { return data_use_; } |
| 33 const base::hash_set<net::URLRequest*>& pending_url_requests() const { |
| 34 return pending_url_requests_; |
| 35 } |
| 36 const net::URLRequest* main_url_request() const { return main_url_request_; } |
| 37 |
| 38 void set_main_url_request(const net::URLRequest* request) { |
| 39 main_url_request_ = request; |
| 40 } |
| 33 | 41 |
| 34 // Returns whether data use is complete and no additional data can be used | 42 // Returns whether data use is complete and no additional data can be used |
| 35 // by the entity tracked by this recorder. For example, | 43 // by the entity tracked by this recorder. For example, |
| 36 bool IsDataUseComplete(); | 44 bool IsDataUseComplete(); |
| 37 | 45 |
| 46 // Adds |request| to the list of pending URLRequests that ascribe data use to |
| 47 // this recorder. |
| 48 void AddPendingURLRequest(net::URLRequest* request); |
| 49 |
| 50 // Clears the list of pending URLRequests that ascribe data use to this |
| 51 // recorder. |
| 52 void RemoveAllPendingURLRequests(); |
| 53 |
| 54 // Returns whether there are any pending URLRequests whose data use is tracked |
| 55 // by this DataUseRecorder. |
| 56 bool HasPendingURLRequest(net::URLRequest* request); |
| 57 |
| 58 // Merge another DataUseRecorder to this instance. |
| 59 void MergeFrom(DataUseRecorder* other); |
| 60 |
| 61 private: |
| 62 friend class DataUseAscriber; |
| 63 |
| 38 // Methods for tracking data use sources. These sources can initiate | 64 // Methods for tracking data use sources. These sources can initiate |
| 39 // URLRequests directly or indirectly. The entity whose data use is being | 65 // URLRequests directly or indirectly. The entity whose data use is being |
| 40 // tracked by this recorder may comprise of sub-entities each of which use | 66 // tracked by this recorder may comprise of sub-entities each of which use |
| 41 // network data. These helper methods help track these sub-entities. | 67 // network data. These helper methods help track these sub-entities. |
| 42 // A recorder will not be marked as having completed data use as long as it | 68 // A recorder will not be marked as having completed data use as long as it |
| 43 // has pending data sources. | 69 // has pending data sources. |
| 44 void AddPendingDataSource(void* source); | 70 void AddPendingDataSource(void* source); |
| 45 bool HasPendingDataSource(void* source); | 71 bool HasPendingDataSource(void* source); |
| 46 void RemovePendingDataSource(void* source); | 72 void RemovePendingDataSource(void* source); |
| 47 | 73 |
| 48 // Returns whether there are any pending URLRequests whose data use is tracked | |
| 49 // by this DataUseRecorder. | |
| 50 bool HasPendingURLRequest(const net::URLRequest* request); | |
| 51 | |
| 52 // Method to merge another DataUseRecorder to this instance. | |
| 53 void MergeWith(DataUseRecorder* other); | |
| 54 | |
| 55 private: | |
| 56 friend class DataUseAscriber; | |
| 57 | |
| 58 // Network Delegate methods: | 74 // Network Delegate methods: |
| 59 void OnBeforeUrlRequest(net::URLRequest* request); | 75 void OnBeforeUrlRequest(net::URLRequest* request); |
| 60 void OnUrlRequestDestroyed(net::URLRequest* request); | 76 void OnUrlRequestDestroyed(net::URLRequest* request); |
| 61 void OnNetworkBytesSent(net::URLRequest* request, int64_t bytes_sent); | 77 void OnNetworkBytesSent(net::URLRequest* request, int64_t bytes_sent); |
| 62 void OnNetworkBytesReceived(net::URLRequest* request, int64_t bytes_received); | 78 void OnNetworkBytesReceived(net::URLRequest* request, int64_t bytes_received); |
| 63 | 79 |
| 64 // Pending URLRequests whose data is being tracked by this DataUseRecorder. | 80 // Pending URLRequests whose data is being tracked by this DataUseRecorder. |
| 65 base::hash_set<const net::URLRequest*> pending_url_requests_; | 81 base::hash_set<net::URLRequest*> pending_url_requests_; |
| 66 | 82 |
| 67 // Data sources other than URLRequests, whose data is being tracked by this | 83 // Data sources other than URLRequests, whose data is being tracked by this |
| 68 // DataUseRecorder. | 84 // DataUseRecorder. |
| 69 base::hash_set<const void*> pending_data_sources_; | 85 base::hash_set<const void*> pending_data_sources_; |
| 70 | 86 |
| 87 // The main frame URLRequest for page loads. Null if this is not tracking a |
| 88 // page load. |
| 89 const net::URLRequest* main_url_request_; |
| 90 |
| 71 // The network data use measured by this DataUseRecorder. | 91 // The network data use measured by this DataUseRecorder. |
| 72 DataUse data_use_; | 92 DataUse data_use_; |
| 73 | 93 |
| 74 DISALLOW_COPY_AND_ASSIGN(DataUseRecorder); | 94 DISALLOW_COPY_AND_ASSIGN(DataUseRecorder); |
| 75 }; | 95 }; |
| 76 | 96 |
| 77 } // namespace data_use_measurement | 97 } // namespace data_use_measurement |
| 78 | 98 |
| 79 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ | 99 #endif // COMPONENTS_DATA_USE_MEASUREMENT_CORE_DATA_USE_RECORDER_H_ |
| OLD | NEW |