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" | |
| 10 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 11 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| 12 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "components/domain_reliability/baked_in_configs.h" | 13 #include "components/domain_reliability/baked_in_configs.h" |
| 15 #include "net/base/load_flags.h" | 14 #include "net/base/load_flags.h" |
| 16 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 17 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 20 | 19 |
| 21 namespace domain_reliability { | 20 namespace domain_reliability { |
| 22 | 21 |
| 23 DomainReliabilityMonitor::DomainReliabilityMonitor( | 22 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 24 const std::string& upload_reporter_string) | 23 const std::string& upload_reporter_string) |
| 25 : time_(new ActualTime()), | 24 : time_(new ActualTime()), |
| 26 upload_reporter_string_(upload_reporter_string), | 25 upload_reporter_string_(upload_reporter_string), |
| 27 scheduler_params_( | 26 scheduler_params_( |
| 28 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 27 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 29 dispatcher_(time_.get()), | 28 dispatcher_(time_.get()), |
| 29 reporting_pref_initialized_(false), | |
| 30 weak_factory_(this) {} | 30 weak_factory_(this) {} |
| 31 | 31 |
| 32 DomainReliabilityMonitor::DomainReliabilityMonitor( | 32 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 33 const std::string& upload_reporter_string, | 33 const std::string& upload_reporter_string, |
| 34 scoped_ptr<MockableTime> time) | 34 scoped_ptr<MockableTime> time) |
| 35 : time_(time.Pass()), | 35 : time_(time.Pass()), |
| 36 upload_reporter_string_(upload_reporter_string), | 36 upload_reporter_string_(upload_reporter_string), |
| 37 scheduler_params_( | 37 scheduler_params_( |
| 38 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 38 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 39 dispatcher_(time_.get()), | 39 dispatcher_(time_.get()), |
| 40 reporting_pref_initialized_(false), | |
| 40 weak_factory_(this) {} | 41 weak_factory_(this) {} |
| 41 | 42 |
| 42 DomainReliabilityMonitor::~DomainReliabilityMonitor() { | 43 DomainReliabilityMonitor::~DomainReliabilityMonitor() { |
| 44 DCHECK(!thread_checker_ || thread_checker_->CalledOnValidThread()); | |
| 45 | |
| 43 ClearContexts(); | 46 ClearContexts(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 void DomainReliabilityMonitor::Init( | 49 void DomainReliabilityMonitor::InitReportingPref( |
| 50 PrefService* local_state_pref_service, | |
| 51 const char* reporting_pref_name, | |
| 52 const scoped_refptr<base::SingleThreadTaskRunner>& | |
| 53 network_task_runner) { | |
| 54 DCHECK(!reporting_pref_initialized_); | |
| 55 | |
| 56 reporting_pref_.Init( | |
| 57 reporting_pref_name, | |
| 58 local_state_pref_service, | |
| 59 base::Bind(&DomainReliabilityMonitor::OnReportingPrefChanged, | |
| 60 base::Unretained(this))); | |
| 61 reporting_pref_initialized_ = true; | |
| 62 reporting_pref_.MoveToThread(network_task_runner); | |
| 63 } | |
| 64 | |
| 65 void DomainReliabilityMonitor::InitURLRequestContext( | |
| 47 net::URLRequestContext* url_request_context, | 66 net::URLRequestContext* url_request_context, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 67 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
|
davidben
2014/08/22 19:00:40
I wonder if the thread_checker_ should be replaced
davidben
2014/08/22 19:02:10
Er, that got lost in rewording. By 'add DCHECK', I
| |
| 68 DCHECK(reporting_pref_initialized_); | |
| 49 DCHECK(!thread_checker_); | 69 DCHECK(!thread_checker_); |
| 50 | 70 |
| 51 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = | 71 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
| 52 new net::TrivialURLRequestContextGetter(url_request_context, | 72 new net::TrivialURLRequestContextGetter(url_request_context, |
| 53 task_runner); | 73 task_runner); |
| 54 Init(url_request_context_getter); | 74 InitURLRequestContext(url_request_context_getter); |
| 55 } | 75 } |
| 56 | 76 |
| 57 void DomainReliabilityMonitor::Init( | 77 void DomainReliabilityMonitor::InitURLRequestContext( |
| 58 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { | 78 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
| 79 DCHECK(reporting_pref_initialized_); | |
| 59 DCHECK(!thread_checker_); | 80 DCHECK(!thread_checker_); |
| 60 | 81 |
| 61 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> | 82 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> |
| 62 RunsTasksOnCurrentThread()); | 83 RunsTasksOnCurrentThread()); |
| 63 | 84 |
| 85 thread_checker_.reset(new base::ThreadChecker()); | |
| 64 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); | 86 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); |
| 65 thread_checker_.reset(new base::ThreadChecker()); | 87 OnReportingPrefChanged(); |
| 66 } | 88 } |
| 67 | 89 |
| 68 void DomainReliabilityMonitor::AddBakedInConfigs() { | 90 void DomainReliabilityMonitor::AddBakedInConfigs() { |
| 69 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 91 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 70 base::Time now = base::Time::Now(); | 92 base::Time now = base::Time::Now(); |
| 71 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { | 93 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { |
| 72 std::string json(kBakedInJsonConfigs[i]); | 94 std::string json(kBakedInJsonConfigs[i]); |
| 73 scoped_ptr<const DomainReliabilityConfig> config = | 95 scoped_ptr<const DomainReliabilityConfig> config = |
| 74 DomainReliabilityConfig::FromJSON(json); | 96 DomainReliabilityConfig::FromJSON(json); |
| 75 if (config && config->IsExpired(now)) { | 97 if (config && config->IsExpired(now)) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 beacon.server_ip.clear(); | 252 beacon.server_ip.clear(); |
| 231 beacon.protocol = GetDomainReliabilityProtocol( | 253 beacon.protocol = GetDomainReliabilityProtocol( |
| 232 request.response_info.connection_info, | 254 request.response_info.connection_info, |
| 233 request.response_info.ssl_info.is_valid()); | 255 request.response_info.ssl_info.is_valid()); |
| 234 beacon.http_response_code = response_code; | 256 beacon.http_response_code = response_code; |
| 235 beacon.start_time = request.load_timing_info.request_start; | 257 beacon.start_time = request.load_timing_info.request_start; |
| 236 beacon.elapsed = time_->NowTicks() - beacon.start_time; | 258 beacon.elapsed = time_->NowTicks() - beacon.start_time; |
| 237 context->OnBeacon(request.url, beacon); | 259 context->OnBeacon(request.url, beacon); |
| 238 } | 260 } |
| 239 | 261 |
| 262 void DomainReliabilityMonitor::OnReportingPrefChanged() { | |
|
davidben
2014/08/22 19:00:40
Probably want to add a check for uploader_ being N
Deprecated (see juliatuttle)
2014/08/23 00:10:29
Done.
| |
| 263 // When metrics reporting is disabled, discard Domain Reliability uploads. | |
| 264 uploader_->set_discard_uploads(!*reporting_pref_); | |
| 265 } | |
| 266 | |
| 267 void DomainReliabilityMonitor::DestroyReportingPref() { | |
| 268 reporting_pref_.Destroy(); | |
| 269 } | |
| 270 | |
| 240 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to | 271 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to |
| 241 // prepend '*.' to domains. | 272 // prepend '*.' to domains. |
| 242 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( | 273 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( |
| 243 const std::string& host) const { | 274 const std::string& host) const { |
| 244 ContextMap::const_iterator context_it; | 275 ContextMap::const_iterator context_it; |
| 245 | 276 |
| 246 context_it = contexts_.find(host); | 277 context_it = contexts_.find(host); |
| 247 if (context_it != contexts_.end()) | 278 if (context_it != contexts_.end()) |
| 248 return context_it->second; | 279 return context_it->second; |
| 249 | 280 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 265 | 296 |
| 266 return NULL; | 297 return NULL; |
| 267 } | 298 } |
| 268 | 299 |
| 269 base::WeakPtr<DomainReliabilityMonitor> | 300 base::WeakPtr<DomainReliabilityMonitor> |
| 270 DomainReliabilityMonitor::MakeWeakPtr() { | 301 DomainReliabilityMonitor::MakeWeakPtr() { |
| 271 return weak_factory_.GetWeakPtr(); | 302 return weak_factory_.GetWeakPtr(); |
| 272 } | 303 } |
| 273 | 304 |
| 274 } // namespace domain_reliability | 305 } // namespace domain_reliability |
| OLD | NEW |