Chromium Code Reviews| 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" |
| 11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/domain_reliability/baked_in_configs.h" | 14 #include "components/domain_reliability/baked_in_configs.h" |
| 15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
| 20 | 20 |
| 21 namespace domain_reliability { | 21 namespace domain_reliability { |
| 22 | 22 |
| 23 DomainReliabilityMonitor::DomainReliabilityMonitor( | 23 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 24 const std::string& upload_reporter_string) | 24 const std::string& upload_reporter_string) |
| 25 : time_(new ActualTime()), | 25 : time_(new ActualTime()), |
| 26 upload_reporter_string_(upload_reporter_string), | 26 upload_reporter_string_(upload_reporter_string), |
| 27 scheduler_params_( | 27 scheduler_params_( |
| 28 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 28 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 29 dispatcher_(time_.get()), | 29 dispatcher_(time_.get()), |
| 30 reporting_pref_initialized_(false), | |
| 30 weak_factory_(this) {} | 31 weak_factory_(this) {} |
| 31 | 32 |
| 32 DomainReliabilityMonitor::DomainReliabilityMonitor( | 33 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 33 const std::string& upload_reporter_string, | 34 const std::string& upload_reporter_string, |
| 34 scoped_ptr<MockableTime> time) | 35 scoped_ptr<MockableTime> time) |
| 35 : time_(time.Pass()), | 36 : time_(time.Pass()), |
| 36 upload_reporter_string_(upload_reporter_string), | 37 upload_reporter_string_(upload_reporter_string), |
| 37 scheduler_params_( | 38 scheduler_params_( |
| 38 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 39 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 39 dispatcher_(time_.get()), | 40 dispatcher_(time_.get()), |
| 41 reporting_pref_initialized_(false), | |
| 40 weak_factory_(this) {} | 42 weak_factory_(this) {} |
| 41 | 43 |
| 42 DomainReliabilityMonitor::~DomainReliabilityMonitor() { | 44 DomainReliabilityMonitor::~DomainReliabilityMonitor() { |
| 43 ClearContexts(); | 45 ClearContexts(); |
| 44 } | 46 } |
| 45 | 47 |
| 46 void DomainReliabilityMonitor::Init( | 48 void DomainReliabilityMonitor::InitReportingPref( |
|
davidben
2014/08/20 21:49:02
Is this called anywhere outside of test code yet?
Deprecated (see juliatuttle)
2014/08/21 18:35:28
It is now!
| |
| 49 PrefService* local_state_pref_service, | |
| 50 const char* reporting_pref_name, | |
| 51 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 52 network_message_loop_proxy) { | |
| 53 DCHECK(!reporting_pref_initialized_); | |
| 54 | |
| 55 reporting_pref_.Init( | |
| 56 reporting_pref_name, | |
| 57 local_state_pref_service, | |
| 58 base::Bind(&DomainReliabilityMonitor::OnReportingPrefChanged, | |
| 59 base::Unretained(this))); | |
| 60 reporting_pref_.MoveToThread(network_message_loop_proxy); | |
| 61 | |
| 62 reporting_pref_initialized_ = true; | |
| 63 | |
| 64 OnReportingPrefChanged(); | |
|
davidben
2014/08/20 21:49:03
Doesn't this require that |uploader_| be initializ
Deprecated (see juliatuttle)
2014/08/21 18:35:28
Well-spotted.
| |
| 65 } | |
| 66 | |
| 67 void DomainReliabilityMonitor::InitURLRequestContext( | |
| 47 net::URLRequestContext* url_request_context, | 68 net::URLRequestContext* url_request_context, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 69 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 70 DCHECK(reporting_pref_initialized_); | |
| 49 DCHECK(!thread_checker_); | 71 DCHECK(!thread_checker_); |
| 50 | 72 |
| 51 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = | 73 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
| 52 new net::TrivialURLRequestContextGetter(url_request_context, | 74 new net::TrivialURLRequestContextGetter(url_request_context, |
| 53 task_runner); | 75 task_runner); |
| 54 Init(url_request_context_getter); | 76 InitURLRequestContext(url_request_context_getter); |
| 55 } | 77 } |
| 56 | 78 |
| 57 void DomainReliabilityMonitor::Init( | 79 void DomainReliabilityMonitor::InitURLRequestContext( |
| 58 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { | 80 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
| 81 DCHECK(reporting_pref_initialized_); | |
| 59 DCHECK(!thread_checker_); | 82 DCHECK(!thread_checker_); |
| 60 | 83 |
| 61 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> | 84 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> |
| 62 RunsTasksOnCurrentThread()); | 85 RunsTasksOnCurrentThread()); |
| 63 | 86 |
| 64 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); | 87 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); |
| 65 thread_checker_.reset(new base::ThreadChecker()); | 88 thread_checker_.reset(new base::ThreadChecker()); |
| 66 } | 89 } |
| 67 | 90 |
| 68 void DomainReliabilityMonitor::AddBakedInConfigs() { | 91 void DomainReliabilityMonitor::AddBakedInConfigs() { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 beacon.server_ip.clear(); | 253 beacon.server_ip.clear(); |
| 231 beacon.protocol = GetDomainReliabilityProtocol( | 254 beacon.protocol = GetDomainReliabilityProtocol( |
| 232 request.response_info.connection_info, | 255 request.response_info.connection_info, |
| 233 request.response_info.ssl_info.is_valid()); | 256 request.response_info.ssl_info.is_valid()); |
| 234 beacon.http_response_code = response_code; | 257 beacon.http_response_code = response_code; |
| 235 beacon.start_time = request.load_timing_info.request_start; | 258 beacon.start_time = request.load_timing_info.request_start; |
| 236 beacon.elapsed = time_->NowTicks() - beacon.start_time; | 259 beacon.elapsed = time_->NowTicks() - beacon.start_time; |
| 237 context->OnBeacon(request.url, beacon); | 260 context->OnBeacon(request.url, beacon); |
| 238 } | 261 } |
| 239 | 262 |
| 263 void DomainReliabilityMonitor::OnReportingPrefChanged() { | |
| 264 // When metrics reporting is disabled, discard Domain Reliability uploads. | |
| 265 uploader_->set_discard_uploads(!*reporting_pref_); | |
| 266 } | |
| 267 | |
| 240 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to | 268 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to |
| 241 // prepend '*.' to domains. | 269 // prepend '*.' to domains. |
| 242 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( | 270 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( |
| 243 const std::string& host) const { | 271 const std::string& host) const { |
| 244 ContextMap::const_iterator context_it; | 272 ContextMap::const_iterator context_it; |
| 245 | 273 |
| 246 context_it = contexts_.find(host); | 274 context_it = contexts_.find(host); |
| 247 if (context_it != contexts_.end()) | 275 if (context_it != contexts_.end()) |
| 248 return context_it->second; | 276 return context_it->second; |
| 249 | 277 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 265 | 293 |
| 266 return NULL; | 294 return NULL; |
| 267 } | 295 } |
| 268 | 296 |
| 269 base::WeakPtr<DomainReliabilityMonitor> | 297 base::WeakPtr<DomainReliabilityMonitor> |
| 270 DomainReliabilityMonitor::MakeWeakPtr() { | 298 DomainReliabilityMonitor::MakeWeakPtr() { |
| 271 return weak_factory_.GetWeakPtr(); | 299 return weak_factory_.GetWeakPtr(); |
| 272 } | 300 } |
| 273 | 301 |
| 274 } // namespace domain_reliability | 302 } // namespace domain_reliability |
| OLD | NEW |