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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 net::URLRequestContext* context_; | 48 net::URLRequestContext* context_; |
49 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 49 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
50 }; | 50 }; |
51 | 51 |
52 } // namespace | 52 } // namespace |
53 | 53 |
54 namespace domain_reliability { | 54 namespace domain_reliability { |
55 | 55 |
56 DomainReliabilityMonitor::DomainReliabilityMonitor( | 56 DomainReliabilityMonitor::DomainReliabilityMonitor( |
57 net::URLRequestContext* url_request_context) | 57 net::URLRequestContext* url_request_context, |
| 58 const char* upload_reporter_string) |
58 : time_(new ActualTime()), | 59 : time_(new ActualTime()), |
59 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( | 60 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( |
60 new TrivialURLRequestContextGetter( | 61 new TrivialURLRequestContextGetter( |
61 url_request_context, | 62 url_request_context, |
62 content::BrowserThread::GetMessageLoopProxyForThread( | 63 content::BrowserThread::GetMessageLoopProxyForThread( |
63 content::BrowserThread::IO)))), | 64 content::BrowserThread::IO)))), |
| 65 upload_reporter_string_(upload_reporter_string), |
64 scheduler_params_( | 66 scheduler_params_( |
65 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 67 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
66 dispatcher_(time_.get()), | 68 dispatcher_(time_.get()), |
67 uploader_( | 69 uploader_( |
68 DomainReliabilityUploader::Create(url_request_context_getter_)) { | 70 DomainReliabilityUploader::Create(url_request_context_getter_)) { |
69 DCHECK(OnIOThread()); | 71 DCHECK(OnIOThread()); |
70 } | 72 } |
71 | 73 |
72 DomainReliabilityMonitor::DomainReliabilityMonitor( | 74 DomainReliabilityMonitor::DomainReliabilityMonitor( |
73 net::URLRequestContext* url_request_context, | 75 net::URLRequestContext* url_request_context, |
| 76 const char* upload_reporter_string, |
74 scoped_ptr<MockableTime> time) | 77 scoped_ptr<MockableTime> time) |
75 : time_(time.Pass()), | 78 : time_(time.Pass()), |
76 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( | 79 url_request_context_getter_(scoped_refptr<net::URLRequestContextGetter>( |
77 new TrivialURLRequestContextGetter( | 80 new TrivialURLRequestContextGetter( |
78 url_request_context, | 81 url_request_context, |
79 content::BrowserThread::GetMessageLoopProxyForThread( | 82 content::BrowserThread::GetMessageLoopProxyForThread( |
80 content::BrowserThread::IO)))), | 83 content::BrowserThread::IO)))), |
| 84 upload_reporter_string_(upload_reporter_string), |
81 scheduler_params_( | 85 scheduler_params_( |
82 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 86 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
83 dispatcher_(time_.get()), | 87 dispatcher_(time_.get()), |
84 uploader_( | 88 uploader_( |
85 DomainReliabilityUploader::Create(url_request_context_getter_)) { | 89 DomainReliabilityUploader::Create(url_request_context_getter_)) { |
86 DCHECK(OnIOThread()); | 90 DCHECK(OnIOThread()); |
87 } | 91 } |
88 | 92 |
89 DomainReliabilityMonitor::~DomainReliabilityMonitor() { | 93 DomainReliabilityMonitor::~DomainReliabilityMonitor() { |
90 DCHECK(OnIOThread()); | 94 DCHECK(OnIOThread()); |
91 STLDeleteContainerPairSecondPointers( | 95 STLDeleteContainerPairSecondPointers(contexts_.begin(), contexts_.end()); |
92 contexts_.begin(), contexts_.end()); | |
93 } | 96 } |
94 | 97 |
95 void DomainReliabilityMonitor::AddBakedInConfigs() { | 98 void DomainReliabilityMonitor::AddBakedInConfigs() { |
96 base::Time now = base::Time::Now(); | 99 base::Time now = base::Time::Now(); |
97 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { | 100 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { |
98 std::string json(kBakedInJsonConfigs[i]); | 101 std::string json(kBakedInJsonConfigs[i]); |
99 scoped_ptr<const DomainReliabilityConfig> config = | 102 scoped_ptr<const DomainReliabilityConfig> config = |
100 DomainReliabilityConfig::FromJSON(json); | 103 DomainReliabilityConfig::FromJSON(json); |
101 if (config && config->IsExpired(now)) { | 104 if (config && config->IsExpired(now)) { |
102 LOG(WARNING) << "Baked-in Domain Reliability config for " | 105 LOG(WARNING) << "Baked-in Domain Reliability config for " |
103 << config->domain << " is expired."; | 106 << config->domain << " is expired."; |
104 continue; | 107 continue; |
105 } | 108 } |
106 AddContext(config.Pass()); | 109 AddContext(config.Pass()); |
107 } | 110 } |
108 } | 111 } |
109 | 112 |
110 void DomainReliabilityMonitor::OnBeforeRedirect(net::URLRequest* request) { | 113 void DomainReliabilityMonitor::OnBeforeRedirect(net::URLRequest* request) { |
111 DCHECK(OnIOThread()); | 114 DCHECK(OnIOThread()); |
112 RequestInfo request_info(*request); | |
113 // Record the redirect itself in addition to the final request. | 115 // Record the redirect itself in addition to the final request. |
114 OnRequestLegComplete(request_info); | 116 OnRequestLegComplete(RequestInfo(*request)); |
115 } | 117 } |
116 | 118 |
117 void DomainReliabilityMonitor::OnCompleted(net::URLRequest* request, | 119 void DomainReliabilityMonitor::OnCompleted(net::URLRequest* request, |
118 bool started) { | 120 bool started) { |
119 DCHECK(OnIOThread()); | 121 DCHECK(OnIOThread()); |
120 if (!started) | 122 if (!started) |
121 return; | 123 return; |
122 RequestInfo request_info(*request); | 124 RequestInfo request_info(*request); |
123 if (request_info.DefinitelyReachedNetwork()) { | 125 if (request_info.DefinitelyReachedNetwork()) { |
124 OnRequestLegComplete(request_info); | 126 OnRequestLegComplete(request_info); |
(...skipping 29 matching lines...) Expand all Loading... |
154 | 156 |
155 bool DomainReliabilityMonitor::RequestInfo::DefinitelyReachedNetwork() const { | 157 bool DomainReliabilityMonitor::RequestInfo::DefinitelyReachedNetwork() const { |
156 return status.status() != net::URLRequestStatus::CANCELED && !was_cached; | 158 return status.status() != net::URLRequestStatus::CANCELED && !was_cached; |
157 } | 159 } |
158 | 160 |
159 DomainReliabilityContext* DomainReliabilityMonitor::AddContext( | 161 DomainReliabilityContext* DomainReliabilityMonitor::AddContext( |
160 scoped_ptr<const DomainReliabilityConfig> config) { | 162 scoped_ptr<const DomainReliabilityConfig> config) { |
161 DCHECK(config); | 163 DCHECK(config); |
162 DCHECK(config->IsValid()); | 164 DCHECK(config->IsValid()); |
163 | 165 |
164 // Grab domain before we config.Pass(). | 166 // Grab a copy of the domain before transferring ownership of |config|. |
165 std::string domain = config->domain; | 167 std::string domain = config->domain; |
166 | 168 |
167 DomainReliabilityContext* context = new DomainReliabilityContext( | 169 DomainReliabilityContext* context = |
168 time_.get(), | 170 new DomainReliabilityContext(time_.get(), |
169 scheduler_params_, | 171 scheduler_params_, |
170 &dispatcher_, | 172 upload_reporter_string_, |
171 uploader_.get(), | 173 &dispatcher_, |
172 config.Pass()); | 174 uploader_.get(), |
| 175 config.Pass()); |
173 | 176 |
174 std::pair<ContextMap::iterator, bool> map_it = | 177 std::pair<ContextMap::iterator, bool> map_it = |
175 contexts_.insert(make_pair(domain, context)); | 178 contexts_.insert(make_pair(domain, context)); |
176 // Make sure the domain wasn't already in the map. | 179 // Make sure the domain wasn't already in the map. |
177 DCHECK(map_it.second); | 180 DCHECK(map_it.second); |
178 | 181 |
179 return map_it.first->second; | 182 return map_it.first->second; |
180 } | 183 } |
181 | 184 |
182 void DomainReliabilityMonitor::OnRequestLegComplete( | 185 void DomainReliabilityMonitor::OnRequestLegComplete( |
(...skipping 11 matching lines...) Expand all Loading... |
194 // to avoid infinite chains of uploads. | 197 // to avoid infinite chains of uploads. |
195 if (request.is_upload) | 198 if (request.is_upload) |
196 return; | 199 return; |
197 | 200 |
198 ContextMap::iterator it = contexts_.find(request.url.host()); | 201 ContextMap::iterator it = contexts_.find(request.url.host()); |
199 if (it == contexts_.end()) | 202 if (it == contexts_.end()) |
200 return; | 203 return; |
201 DomainReliabilityContext* context = it->second; | 204 DomainReliabilityContext* context = it->second; |
202 | 205 |
203 std::string beacon_status; | 206 std::string beacon_status; |
204 bool got_status = DomainReliabilityUtil::GetBeaconStatus( | 207 bool got_status = GetDomainReliabilityBeaconStatus( |
205 request.status.error(), | 208 request.status.error(), |
206 request.response_code, | 209 request.response_code, |
207 &beacon_status); | 210 &beacon_status); |
208 if (!got_status) | 211 if (!got_status) |
209 return; | 212 return; |
210 | 213 |
211 DomainReliabilityBeacon beacon; | 214 DomainReliabilityBeacon beacon; |
212 beacon.status = beacon_status; | 215 beacon.status = beacon_status; |
213 beacon.chrome_error = request.status.error(); | 216 beacon.chrome_error = request.status.error(); |
214 beacon.server_ip = request.socket_address.host(); | 217 beacon.server_ip = request.socket_address.host(); |
215 beacon.http_response_code = request.response_code; | 218 beacon.http_response_code = request.response_code; |
216 beacon.start_time = request.load_timing_info.request_start; | 219 beacon.start_time = request.load_timing_info.request_start; |
217 beacon.elapsed = time_->NowTicks() - beacon.start_time; | 220 beacon.elapsed = time_->NowTicks() - beacon.start_time; |
218 context->AddBeacon(beacon, request.url); | 221 context->OnBeacon(request.url, beacon); |
219 } | 222 } |
220 | 223 |
221 } // namespace domain_reliability | 224 } // namespace domain_reliability |
OLD | NEW |