OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/domain_reliability/monitor.h" | 5 #include "components/domain_reliability/monitor.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( | 61 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( |
62 new TrivialURLRequestContextGetter( | 62 new TrivialURLRequestContextGetter( |
63 url_request_context, | 63 url_request_context, |
64 content::BrowserThread::GetMessageLoopProxyForThread( | 64 content::BrowserThread::GetMessageLoopProxyForThread( |
65 content::BrowserThread::IO)))), | 65 content::BrowserThread::IO)))), |
66 upload_reporter_string_(upload_reporter_string), | 66 upload_reporter_string_(upload_reporter_string), |
67 scheduler_params_( | 67 scheduler_params_( |
68 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 68 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
69 dispatcher_(time_.get()), | 69 dispatcher_(time_.get()), |
70 uploader_( | 70 uploader_( |
71 DomainReliabilityUploader::Create(url_request_context_getter_)) { | 71 DomainReliabilityUploader::Create(url_request_context_getter_)), |
| 72 was_cleared_(false), |
| 73 cleared_mode_(MAX_CLEAR_MODE) { |
72 DCHECK(OnIOThread()); | 74 DCHECK(OnIOThread()); |
73 } | 75 } |
74 | 76 |
75 DomainReliabilityMonitor::DomainReliabilityMonitor( | 77 DomainReliabilityMonitor::DomainReliabilityMonitor( |
76 net::URLRequestContext* url_request_context, | 78 net::URLRequestContext* url_request_context, |
77 const std::string& upload_reporter_string, | 79 const std::string& upload_reporter_string, |
78 scoped_ptr<MockableTime> time) | 80 scoped_ptr<MockableTime> time) |
79 : time_(time.Pass()), | 81 : time_(time.Pass()), |
80 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( | 82 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( |
81 new TrivialURLRequestContextGetter( | 83 new TrivialURLRequestContextGetter( |
82 url_request_context, | 84 url_request_context, |
83 content::BrowserThread::GetMessageLoopProxyForThread( | 85 content::BrowserThread::GetMessageLoopProxyForThread( |
84 content::BrowserThread::IO)))), | 86 content::BrowserThread::IO)))), |
85 upload_reporter_string_(upload_reporter_string), | 87 upload_reporter_string_(upload_reporter_string), |
86 scheduler_params_( | 88 scheduler_params_( |
87 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 89 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
88 dispatcher_(time_.get()), | 90 dispatcher_(time_.get()), |
89 uploader_( | 91 uploader_( |
90 DomainReliabilityUploader::Create(url_request_context_getter_)) { | 92 DomainReliabilityUploader::Create(url_request_context_getter_)), |
| 93 was_cleared_(false), |
| 94 cleared_mode_(MAX_CLEAR_MODE) { |
91 DCHECK(OnIOThread()); | 95 DCHECK(OnIOThread()); |
92 } | 96 } |
93 | 97 |
94 DomainReliabilityMonitor::~DomainReliabilityMonitor() { | 98 DomainReliabilityMonitor::~DomainReliabilityMonitor() { |
95 DCHECK(OnIOThread()); | 99 DCHECK(OnIOThread()); |
96 STLDeleteContainerPairSecondPointers(contexts_.begin(), contexts_.end()); | 100 ClearContexts(); |
97 } | 101 } |
98 | 102 |
99 void DomainReliabilityMonitor::AddBakedInConfigs() { | 103 void DomainReliabilityMonitor::AddBakedInConfigs() { |
100 base::Time now = base::Time::Now(); | 104 base::Time now = base::Time::Now(); |
101 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { | 105 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { |
102 std::string json(kBakedInJsonConfigs[i]); | 106 std::string json(kBakedInJsonConfigs[i]); |
103 scoped_ptr<const DomainReliabilityConfig> config = | 107 scoped_ptr<const DomainReliabilityConfig> config = |
104 DomainReliabilityConfig::FromJSON(json); | 108 DomainReliabilityConfig::FromJSON(json); |
105 if (config && config->IsExpired(now)) { | 109 if (config && config->IsExpired(now)) { |
106 LOG(WARNING) << "Baked-in Domain Reliability config for " | 110 LOG(WARNING) << "Baked-in Domain Reliability config for " |
(...skipping 17 matching lines...) Expand all Loading... |
124 return; | 128 return; |
125 RequestInfo request_info(*request); | 129 RequestInfo request_info(*request); |
126 if (request_info.AccessedNetwork()) { | 130 if (request_info.AccessedNetwork()) { |
127 OnRequestLegComplete(request_info); | 131 OnRequestLegComplete(request_info); |
128 // A request was just using the network, so now is a good time to run any | 132 // A request was just using the network, so now is a good time to run any |
129 // pending and eligible uploads. | 133 // pending and eligible uploads. |
130 dispatcher_.RunEligibleTasks(); | 134 dispatcher_.RunEligibleTasks(); |
131 } | 135 } |
132 } | 136 } |
133 | 137 |
| 138 void DomainReliabilityMonitor::ClearBrowsingData( |
| 139 DomainReliabilityClearMode mode) { |
| 140 DCHECK(OnIOThread()); |
| 141 |
| 142 was_cleared_ = true; |
| 143 cleared_mode_ = mode; |
| 144 |
| 145 switch (mode) { |
| 146 case CLEAR_BEACONS: { |
| 147 ContextMap::const_iterator it; |
| 148 for (it = contexts_.begin(); it != contexts_.end(); ++it) |
| 149 it->second->ClearBeacons(); |
| 150 break; |
| 151 }; |
| 152 case CLEAR_CONTEXTS: |
| 153 ClearContexts(); |
| 154 break; |
| 155 case MAX_CLEAR_MODE: |
| 156 NOTREACHED(); |
| 157 } |
| 158 } |
| 159 |
134 DomainReliabilityContext* DomainReliabilityMonitor::AddContextForTesting( | 160 DomainReliabilityContext* DomainReliabilityMonitor::AddContextForTesting( |
135 scoped_ptr<const DomainReliabilityConfig> config) { | 161 scoped_ptr<const DomainReliabilityConfig> config) { |
136 return AddContext(config.Pass()); | 162 return AddContext(config.Pass()); |
137 } | 163 } |
138 | 164 |
139 DomainReliabilityMonitor::RequestInfo::RequestInfo() {} | 165 DomainReliabilityMonitor::RequestInfo::RequestInfo() {} |
140 | 166 |
141 DomainReliabilityMonitor::RequestInfo::RequestInfo( | 167 DomainReliabilityMonitor::RequestInfo::RequestInfo( |
142 const net::URLRequest& request) | 168 const net::URLRequest& request) |
143 : url(request.url()), | 169 : url(request.url()), |
(...skipping 28 matching lines...) Expand all Loading... |
172 config.Pass()); | 198 config.Pass()); |
173 | 199 |
174 std::pair<ContextMap::iterator, bool> map_it = | 200 std::pair<ContextMap::iterator, bool> map_it = |
175 contexts_.insert(make_pair(domain, context)); | 201 contexts_.insert(make_pair(domain, context)); |
176 // Make sure the domain wasn't already in the map. | 202 // Make sure the domain wasn't already in the map. |
177 DCHECK(map_it.second); | 203 DCHECK(map_it.second); |
178 | 204 |
179 return map_it.first->second; | 205 return map_it.first->second; |
180 } | 206 } |
181 | 207 |
| 208 void DomainReliabilityMonitor::ClearContexts() { |
| 209 STLDeleteContainerPairSecondPointers( |
| 210 contexts_.begin(), contexts_.end()); |
| 211 contexts_.clear(); |
| 212 } |
| 213 |
182 void DomainReliabilityMonitor::OnRequestLegComplete( | 214 void DomainReliabilityMonitor::OnRequestLegComplete( |
183 const RequestInfo& request) { | 215 const RequestInfo& request) { |
184 int response_code; | 216 int response_code; |
185 if (request.response_info.headers) | 217 if (request.response_info.headers) |
186 response_code = request.response_info.headers->response_code(); | 218 response_code = request.response_info.headers->response_code(); |
187 else | 219 else |
188 response_code = -1; | 220 response_code = -1; |
189 ContextMap::iterator context_it; | 221 ContextMap::iterator context_it; |
190 std::string beacon_status; | 222 std::string beacon_status; |
191 | 223 |
(...skipping 21 matching lines...) Expand all Loading... |
213 beacon.server_ip = request.response_info.socket_address.host(); | 245 beacon.server_ip = request.response_info.socket_address.host(); |
214 else | 246 else |
215 beacon.server_ip.clear(); | 247 beacon.server_ip.clear(); |
216 beacon.http_response_code = response_code; | 248 beacon.http_response_code = response_code; |
217 beacon.start_time = request.load_timing_info.request_start; | 249 beacon.start_time = request.load_timing_info.request_start; |
218 beacon.elapsed = time_->NowTicks() - beacon.start_time; | 250 beacon.elapsed = time_->NowTicks() - beacon.start_time; |
219 context_it->second->OnBeacon(request.url, beacon); | 251 context_it->second->OnBeacon(request.url, beacon); |
220 } | 252 } |
221 | 253 |
222 } // namespace domain_reliability | 254 } // namespace domain_reliability |
OLD | NEW |