| 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/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "components/domain_reliability/baked_in_configs.h" | 12 #include "components/domain_reliability/baked_in_configs.h" |
| 13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 17 #include "net/url_request/url_request_context_getter.h" |
| 18 | 18 |
| 19 namespace domain_reliability { | 19 namespace domain_reliability { |
| 20 | 20 |
| 21 DomainReliabilityMonitor::DomainReliabilityMonitor( | 21 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 22 const std::string& upload_reporter_string, | 22 const std::string& upload_reporter_string, |
| 23 scoped_refptr<base::SingleThreadTaskRunner> pref_thread, | 23 scoped_refptr<base::SingleThreadTaskRunner> pref_thread, |
| 24 scoped_refptr<base::SingleThreadTaskRunner> network_thread, | 24 scoped_refptr<base::SingleThreadTaskRunner> network_thread) |
| 25 PrefService* local_state_pref_service, | |
| 26 const char* reporting_pref_name) | |
| 27 : time_(new ActualTime()), | 25 : time_(new ActualTime()), |
| 28 upload_reporter_string_(upload_reporter_string), | 26 upload_reporter_string_(upload_reporter_string), |
| 29 scheduler_params_( | 27 scheduler_params_( |
| 30 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 28 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 31 dispatcher_(time_.get()), | 29 dispatcher_(time_.get()), |
| 32 pref_task_runner_(pref_thread), | 30 pref_task_runner_(pref_thread), |
| 33 network_task_runner_(network_thread), | 31 network_task_runner_(network_thread), |
| 34 moved_to_network_thread_(false), | 32 moved_to_network_thread_(false), |
| 33 discard_uploads_set_(false), |
| 35 weak_factory_(this) { | 34 weak_factory_(this) { |
| 36 DCHECK(OnPrefThread()); | 35 DCHECK(OnPrefThread()); |
| 37 InitReportingPref(local_state_pref_service, reporting_pref_name); | |
| 38 } | 36 } |
| 39 | 37 |
| 40 DomainReliabilityMonitor::DomainReliabilityMonitor( | 38 DomainReliabilityMonitor::DomainReliabilityMonitor( |
| 41 const std::string& upload_reporter_string, | 39 const std::string& upload_reporter_string, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> pref_thread, | 40 scoped_refptr<base::SingleThreadTaskRunner> pref_thread, |
| 43 scoped_refptr<base::SingleThreadTaskRunner> network_thread, | 41 scoped_refptr<base::SingleThreadTaskRunner> network_thread, |
| 44 PrefService* local_state_pref_service, | |
| 45 const char* reporting_pref_name, | |
| 46 scoped_ptr<MockableTime> time) | 42 scoped_ptr<MockableTime> time) |
| 47 : time_(time.Pass()), | 43 : time_(time.Pass()), |
| 48 upload_reporter_string_(upload_reporter_string), | 44 upload_reporter_string_(upload_reporter_string), |
| 49 scheduler_params_( | 45 scheduler_params_( |
| 50 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), | 46 DomainReliabilityScheduler::Params::GetFromFieldTrialsOrDefaults()), |
| 51 dispatcher_(time_.get()), | 47 dispatcher_(time_.get()), |
| 52 pref_task_runner_(pref_thread), | 48 pref_task_runner_(pref_thread), |
| 53 network_task_runner_(network_thread), | 49 network_task_runner_(network_thread), |
| 54 moved_to_network_thread_(false), | 50 moved_to_network_thread_(false), |
| 51 discard_uploads_set_(false), |
| 55 weak_factory_(this) { | 52 weak_factory_(this) { |
| 56 DCHECK(OnPrefThread()); | 53 DCHECK(OnPrefThread()); |
| 57 InitReportingPref(local_state_pref_service, reporting_pref_name); | |
| 58 } | 54 } |
| 59 | 55 |
| 60 DomainReliabilityMonitor::~DomainReliabilityMonitor() { | 56 DomainReliabilityMonitor::~DomainReliabilityMonitor() { |
| 61 if (moved_to_network_thread_) | 57 if (moved_to_network_thread_) |
| 62 DCHECK(OnNetworkThread()); | 58 DCHECK(OnNetworkThread()); |
| 63 else | 59 else |
| 64 DCHECK(OnPrefThread()); | 60 DCHECK(OnPrefThread()); |
| 65 | 61 |
| 66 ClearContexts(); | 62 ClearContexts(); |
| 67 } | 63 } |
| 68 | 64 |
| 69 void DomainReliabilityMonitor::MoveToNetworkThread() { | 65 void DomainReliabilityMonitor::MoveToNetworkThread() { |
| 70 DCHECK(OnPrefThread()); | 66 DCHECK(OnPrefThread()); |
| 71 DCHECK(!moved_to_network_thread_); | 67 DCHECK(!moved_to_network_thread_); |
| 72 | 68 |
| 73 reporting_pref_.MoveToThread(network_task_runner_); | |
| 74 moved_to_network_thread_ = true; | 69 moved_to_network_thread_ = true; |
| 75 } | 70 } |
| 76 | 71 |
| 77 void DomainReliabilityMonitor::DestroyReportingPref() { | |
| 78 DCHECK(OnPrefThread()); | |
| 79 | |
| 80 reporting_pref_.Destroy(); | |
| 81 } | |
| 82 | |
| 83 void DomainReliabilityMonitor::InitURLRequestContext( | 72 void DomainReliabilityMonitor::InitURLRequestContext( |
| 84 net::URLRequestContext* url_request_context) { | 73 net::URLRequestContext* url_request_context) { |
| 85 DCHECK(OnNetworkThread()); | 74 DCHECK(OnNetworkThread()); |
| 86 DCHECK(moved_to_network_thread_); | 75 DCHECK(moved_to_network_thread_); |
| 87 | 76 |
| 88 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = | 77 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter = |
| 89 new net::TrivialURLRequestContextGetter(url_request_context, | 78 new net::TrivialURLRequestContextGetter(url_request_context, |
| 90 network_task_runner_); | 79 network_task_runner_); |
| 91 InitURLRequestContext(url_request_context_getter); | 80 InitURLRequestContext(url_request_context_getter); |
| 92 } | 81 } |
| 93 | 82 |
| 94 void DomainReliabilityMonitor::InitURLRequestContext( | 83 void DomainReliabilityMonitor::InitURLRequestContext( |
| 95 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { | 84 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { |
| 96 DCHECK(OnNetworkThread()); | 85 DCHECK(OnNetworkThread()); |
| 97 DCHECK(moved_to_network_thread_); | 86 DCHECK(moved_to_network_thread_); |
| 98 | 87 |
| 99 // Make sure the URLRequestContext actually lives on what was declared to be | 88 // Make sure the URLRequestContext actually lives on what was declared to be |
| 100 // the network thread. | 89 // the network thread. |
| 101 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> | 90 DCHECK(url_request_context_getter->GetNetworkTaskRunner()-> |
| 102 RunsTasksOnCurrentThread()); | 91 RunsTasksOnCurrentThread()); |
| 103 | 92 |
| 104 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); | 93 uploader_ = DomainReliabilityUploader::Create(url_request_context_getter); |
| 105 // Make sure the uploader is sending or discarding uploads according to pref. | |
| 106 OnReportingPrefChanged(); | |
| 107 } | 94 } |
| 108 | 95 |
| 109 void DomainReliabilityMonitor::AddBakedInConfigs() { | 96 void DomainReliabilityMonitor::AddBakedInConfigs() { |
| 110 DCHECK(OnNetworkThread()); | 97 DCHECK(OnNetworkThread()); |
| 98 DCHECK(moved_to_network_thread_); |
| 111 | 99 |
| 112 base::Time now = base::Time::Now(); | 100 base::Time now = base::Time::Now(); |
| 113 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { | 101 for (size_t i = 0; kBakedInJsonConfigs[i]; ++i) { |
| 114 std::string json(kBakedInJsonConfigs[i]); | 102 std::string json(kBakedInJsonConfigs[i]); |
| 115 scoped_ptr<const DomainReliabilityConfig> config = | 103 scoped_ptr<const DomainReliabilityConfig> config = |
| 116 DomainReliabilityConfig::FromJSON(json); | 104 DomainReliabilityConfig::FromJSON(json); |
| 117 if (config && config->IsExpired(now)) { | 105 if (config && config->IsExpired(now)) { |
| 118 LOG(WARNING) << "Baked-in Domain Reliability config for " | 106 LOG(WARNING) << "Baked-in Domain Reliability config for " |
| 119 << config->domain << " is expired."; | 107 << config->domain << " is expired."; |
| 120 continue; | 108 continue; |
| 121 } | 109 } |
| 122 AddContext(config.Pass()); | 110 AddContext(config.Pass()); |
| 123 } | 111 } |
| 124 } | 112 } |
| 125 | 113 |
| 114 void DomainReliabilityMonitor::SetDiscardUploads(bool discard_uploads) { |
| 115 DCHECK(OnNetworkThread()); |
| 116 DCHECK(moved_to_network_thread_); |
| 117 DCHECK(uploader_); |
| 118 |
| 119 uploader_->set_discard_uploads(discard_uploads); |
| 120 discard_uploads_set_ = true; |
| 121 } |
| 122 |
| 126 void DomainReliabilityMonitor::OnBeforeRedirect(net::URLRequest* request) { | 123 void DomainReliabilityMonitor::OnBeforeRedirect(net::URLRequest* request) { |
| 127 DCHECK(OnNetworkThread()); | 124 DCHECK(OnNetworkThread()); |
| 125 DCHECK(discard_uploads_set_); |
| 128 | 126 |
| 129 // Record the redirect itself in addition to the final request. | 127 // Record the redirect itself in addition to the final request. |
| 130 OnRequestLegComplete(RequestInfo(*request)); | 128 OnRequestLegComplete(RequestInfo(*request)); |
| 131 } | 129 } |
| 132 | 130 |
| 133 void DomainReliabilityMonitor::OnCompleted(net::URLRequest* request, | 131 void DomainReliabilityMonitor::OnCompleted(net::URLRequest* request, |
| 134 bool started) { | 132 bool started) { |
| 135 DCHECK(OnNetworkThread()); | 133 DCHECK(OnNetworkThread()); |
| 134 DCHECK(discard_uploads_set_); |
| 136 | 135 |
| 137 if (!started) | 136 if (!started) |
| 138 return; | 137 return; |
| 139 RequestInfo request_info(*request); | 138 RequestInfo request_info(*request); |
| 140 if (request_info.AccessedNetwork()) { | 139 if (request_info.AccessedNetwork()) { |
| 141 OnRequestLegComplete(request_info); | 140 OnRequestLegComplete(request_info); |
| 142 // A request was just using the network, so now is a good time to run any | 141 // A request was just using the network, so now is a good time to run any |
| 143 // pending and eligible uploads. | 142 // pending and eligible uploads. |
| 144 dispatcher_.RunEligibleTasks(); | 143 dispatcher_.RunEligibleTasks(); |
| 145 } | 144 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 231 } |
| 233 | 232 |
| 234 void DomainReliabilityMonitor::ClearContexts() { | 233 void DomainReliabilityMonitor::ClearContexts() { |
| 235 STLDeleteContainerPairSecondPointers( | 234 STLDeleteContainerPairSecondPointers( |
| 236 contexts_.begin(), contexts_.end()); | 235 contexts_.begin(), contexts_.end()); |
| 237 contexts_.clear(); | 236 contexts_.clear(); |
| 238 } | 237 } |
| 239 | 238 |
| 240 void DomainReliabilityMonitor::OnRequestLegComplete( | 239 void DomainReliabilityMonitor::OnRequestLegComplete( |
| 241 const RequestInfo& request) { | 240 const RequestInfo& request) { |
| 241 // Check these again because unit tests call this directly. |
| 242 DCHECK(OnNetworkThread()); |
| 243 DCHECK(discard_uploads_set_); |
| 244 |
| 242 int response_code; | 245 int response_code; |
| 243 if (request.response_info.headers.get()) | 246 if (request.response_info.headers.get()) |
| 244 response_code = request.response_info.headers->response_code(); | 247 response_code = request.response_info.headers->response_code(); |
| 245 else | 248 else |
| 246 response_code = -1; | 249 response_code = -1; |
| 247 std::string beacon_status; | 250 std::string beacon_status; |
| 248 | 251 |
| 249 int error_code = net::OK; | 252 int error_code = net::OK; |
| 250 if (request.status.status() == net::URLRequestStatus::FAILED) | 253 if (request.status.status() == net::URLRequestStatus::FAILED) |
| 251 error_code = request.status.error(); | 254 error_code = request.status.error(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 279 beacon.protocol = GetDomainReliabilityProtocol( | 282 beacon.protocol = GetDomainReliabilityProtocol( |
| 280 request.response_info.connection_info, | 283 request.response_info.connection_info, |
| 281 request.response_info.ssl_info.is_valid()); | 284 request.response_info.ssl_info.is_valid()); |
| 282 beacon.http_response_code = response_code; | 285 beacon.http_response_code = response_code; |
| 283 beacon.start_time = request.load_timing_info.request_start; | 286 beacon.start_time = request.load_timing_info.request_start; |
| 284 beacon.elapsed = time_->NowTicks() - beacon.start_time; | 287 beacon.elapsed = time_->NowTicks() - beacon.start_time; |
| 285 beacon.domain = request.url.host(); | 288 beacon.domain = request.url.host(); |
| 286 context->OnBeacon(request.url, beacon); | 289 context->OnBeacon(request.url, beacon); |
| 287 } | 290 } |
| 288 | 291 |
| 289 void DomainReliabilityMonitor::InitReportingPref( | |
| 290 PrefService* local_state_pref_service, | |
| 291 const char* reporting_pref_name) { | |
| 292 reporting_pref_.Init( | |
| 293 reporting_pref_name, | |
| 294 local_state_pref_service, | |
| 295 base::Bind(&DomainReliabilityMonitor::OnReportingPrefChanged, | |
| 296 base::Unretained(this))); | |
| 297 } | |
| 298 | |
| 299 void DomainReliabilityMonitor::OnReportingPrefChanged() { | |
| 300 DCHECK(OnNetworkThread()); | |
| 301 | |
| 302 // When metrics reporting is disabled, discard Domain Reliability uploads. | |
| 303 if (uploader_) | |
| 304 uploader_->set_discard_uploads(!*reporting_pref_); | |
| 305 } | |
| 306 | |
| 307 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to | 292 // TODO(ttuttle): Keep a separate wildcard_contexts_ map to avoid having to |
| 308 // prepend '*.' to domains. | 293 // prepend '*.' to domains. |
| 309 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( | 294 DomainReliabilityContext* DomainReliabilityMonitor::GetContextForHost( |
| 310 const std::string& host) const { | 295 const std::string& host) const { |
| 311 DCHECK(OnNetworkThread()); | 296 DCHECK(OnNetworkThread()); |
| 312 | 297 |
| 313 ContextMap::const_iterator context_it; | 298 ContextMap::const_iterator context_it; |
| 314 | 299 |
| 315 context_it = contexts_.find(host); | 300 context_it = contexts_.find(host); |
| 316 if (context_it != contexts_.end()) | 301 if (context_it != contexts_.end()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 334 | 319 |
| 335 return NULL; | 320 return NULL; |
| 336 } | 321 } |
| 337 | 322 |
| 338 base::WeakPtr<DomainReliabilityMonitor> | 323 base::WeakPtr<DomainReliabilityMonitor> |
| 339 DomainReliabilityMonitor::MakeWeakPtr() { | 324 DomainReliabilityMonitor::MakeWeakPtr() { |
| 340 return weak_factory_.GetWeakPtr(); | 325 return weak_factory_.GetWeakPtr(); |
| 341 } | 326 } |
| 342 | 327 |
| 343 } // namespace domain_reliability | 328 } // namespace domain_reliability |
| OLD | NEW |