Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: components/data_use_measurement/core/data_use_recorder.cc

Issue 2947973002: Support moving pending requests from one DataUseRecorder to another (Closed)
Patch Set: rebase Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/data_use_measurement/core/data_use_recorder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "components/data_use_measurement/core/data_use_recorder.h" 5 #include "components/data_use_measurement/core/data_use_recorder.h"
6 6
7 #include "net/url_request/url_request.h" 7 #include "net/url_request/url_request.h"
8 8
9 namespace data_use_measurement { 9 namespace data_use_measurement {
10 10
11 DataUseRecorder::DataUseRecorder(DataUse::TrafficType traffic_type) 11 DataUseRecorder::DataUseRecorder(DataUse::TrafficType traffic_type)
12 : main_url_request_(nullptr), data_use_(traffic_type), is_visible_(false) {} 12 : main_url_request_(nullptr), data_use_(traffic_type), is_visible_(false) {}
13 13
14 DataUseRecorder::~DataUseRecorder() {} 14 DataUseRecorder::~DataUseRecorder() {}
15 15
16 bool DataUseRecorder::IsDataUseComplete() { 16 bool DataUseRecorder::IsDataUseComplete() {
17 return pending_url_requests_.empty() && pending_data_sources_.empty(); 17 return pending_url_requests_.empty();
18 } 18 }
19 19
20 void DataUseRecorder::AddPendingURLRequest(net::URLRequest* request) { 20 void DataUseRecorder::AddPendingURLRequest(net::URLRequest* request) {
21 pending_url_requests_.insert(request); 21 pending_url_requests_.emplace(std::piecewise_construct,
22 std::forward_as_tuple(request),
23 std::forward_as_tuple());
22 } 24 }
23 25
24 void DataUseRecorder::OnUrlRequestDestroyed(net::URLRequest* request) { 26 void DataUseRecorder::OnUrlRequestDestroyed(net::URLRequest* request) {
25 pending_url_requests_.erase(request); 27 pending_url_requests_.erase(request);
26 } 28 }
27 29
30 void DataUseRecorder::MovePendingURLRequest(DataUseRecorder* other,
31 net::URLRequest* request) {
32 auto request_it = pending_url_requests_.find(request);
33 DCHECK(request_it != pending_url_requests_.end());
34 DCHECK(other->pending_url_requests_.find(request) ==
35 other->pending_url_requests_.end());
36
37 // Increment the bytes of the request in |other|, and decrement the bytes in
38 // |this|.
39 // TODO(rajendrant): Check if the moving the bytes in |data_use_| needs to be
40 // propogated to observers, which could store per-request user data.
41 other->AddPendingURLRequest(request);
42 other->UpdateNetworkByteCounts(request, request_it->second.bytes_received,
43 request_it->second.bytes_sent);
44 data_use_.IncrementTotalBytes(-request_it->second.bytes_received,
45 -request_it->second.bytes_sent);
46 pending_url_requests_.erase(request_it);
47 }
48
28 void DataUseRecorder::RemoveAllPendingURLRequests() { 49 void DataUseRecorder::RemoveAllPendingURLRequests() {
29 pending_url_requests_.clear(); 50 pending_url_requests_.clear();
30 } 51 }
31 52
32 void DataUseRecorder::OnBeforeUrlRequest(net::URLRequest* request) {} 53 void DataUseRecorder::OnBeforeUrlRequest(net::URLRequest* request) {}
33 54
34 void DataUseRecorder::OnNetworkBytesReceived(net::URLRequest* request, 55 void DataUseRecorder::OnNetworkBytesReceived(net::URLRequest* request,
35 int64_t bytes_received) { 56 int64_t bytes_received) {
36 data_use_.IncrementTotalBytes(bytes_received, 0); 57 UpdateNetworkByteCounts(request, bytes_received, 0);
37 } 58 }
38 59
39 void DataUseRecorder::OnNetworkBytesSent(net::URLRequest* request, 60 void DataUseRecorder::OnNetworkBytesSent(net::URLRequest* request,
40 int64_t bytes_sent) { 61 int64_t bytes_sent) {
41 data_use_.IncrementTotalBytes(0, bytes_sent); 62 UpdateNetworkByteCounts(request, 0, bytes_sent);
42 } 63 }
43 64
44 void DataUseRecorder::MergeFrom(DataUseRecorder* other) { 65 void DataUseRecorder::UpdateNetworkByteCounts(net::URLRequest* request,
45 data_use_.MergeFrom(other->data_use()); 66 int64_t bytes_received,
67 int64_t bytes_sent) {
68 data_use_.IncrementTotalBytes(bytes_received, bytes_sent);
69 auto request_it = pending_url_requests_.find(request);
70 request_it->second.bytes_received += bytes_received;
71 request_it->second.bytes_sent += bytes_sent;
46 } 72 }
47 73
48 } // namespace data_use_measurement 74 } // namespace data_use_measurement
OLDNEW
« no previous file with comments | « components/data_use_measurement/core/data_use_recorder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698