| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/previews/core/previews_io_data.h" | 5 #include "components/previews/core/previews_io_data.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/metrics/histogram.h" | |
| 13 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 14 #include "base/time/default_clock.h" | 13 #include "base/time/default_clock.h" |
| 15 #include "components/previews/core/previews_black_list.h" | 14 #include "components/previews/core/previews_black_list.h" |
| 16 #include "components/previews/core/previews_opt_out_store.h" | 15 #include "components/previews/core/previews_opt_out_store.h" |
| 17 #include "components/previews/core/previews_ui_service.h" | 16 #include "components/previews/core/previews_ui_service.h" |
| 18 #include "net/nqe/network_quality_estimator.h" | 17 #include "net/nqe/network_quality_estimator.h" |
| 19 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 20 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 21 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 22 | 21 |
| 23 namespace previews { | 22 namespace previews { |
| 24 | 23 |
| 25 namespace { | |
| 26 | |
| 27 void LogPreviewsEligibilityReason(PreviewsEligibilityReason status, | |
| 28 PreviewsType type) { | |
| 29 switch (type) { | |
| 30 case PreviewsType::OFFLINE: | |
| 31 UMA_HISTOGRAM_ENUMERATION( | |
| 32 "Previews.EligibilityReason.Offline", static_cast<int>(status), | |
| 33 static_cast<int>(PreviewsEligibilityReason::LAST)); | |
| 34 break; | |
| 35 default: | |
| 36 NOTREACHED(); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 PreviewsIOData::PreviewsIOData( | 24 PreviewsIOData::PreviewsIOData( |
| 43 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | 25 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 26 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 45 : ui_task_runner_(ui_task_runner), | 27 : ui_task_runner_(ui_task_runner), |
| 46 io_task_runner_(io_task_runner), | 28 io_task_runner_(io_task_runner), |
| 47 weak_factory_(this) {} | 29 weak_factory_(this) {} |
| 48 | 30 |
| 49 PreviewsIOData::~PreviewsIOData() {} | 31 PreviewsIOData::~PreviewsIOData() {} |
| 50 | 32 |
| 51 void PreviewsIOData::Initialize( | 33 void PreviewsIOData::Initialize( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 66 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 85 previews_black_list_->ClearBlackList(begin_time, end_time); | 67 previews_black_list_->ClearBlackList(begin_time, end_time); |
| 86 } | 68 } |
| 87 | 69 |
| 88 bool PreviewsIOData::ShouldAllowPreview(const net::URLRequest& request, | 70 bool PreviewsIOData::ShouldAllowPreview(const net::URLRequest& request, |
| 89 PreviewsType type) const { | 71 PreviewsType type) const { |
| 90 if (!IsPreviewsTypeEnabled(type)) | 72 if (!IsPreviewsTypeEnabled(type)) |
| 91 return false; | 73 return false; |
| 92 // The blacklist will disallow certain hosts for periods of time based on | 74 // The blacklist will disallow certain hosts for periods of time based on |
| 93 // user's opting out of the preview | 75 // user's opting out of the preview |
| 94 if (!previews_black_list_) { | 76 if (!previews_black_list_ || |
| 95 LogPreviewsEligibilityReason( | 77 !previews_black_list_->IsLoadedAndAllowed(request.url(), type)) { |
| 96 PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE, type); | |
| 97 return false; | |
| 98 } | |
| 99 PreviewsEligibilityReason status = | |
| 100 previews_black_list_->IsLoadedAndAllowed(request.url(), type); | |
| 101 if (status != PreviewsEligibilityReason::ALLOWED) { | |
| 102 LogPreviewsEligibilityReason(status, type); | |
| 103 return false; | 78 return false; |
| 104 } | 79 } |
| 105 net::NetworkQualityEstimator* network_quality_estimator = | 80 net::NetworkQualityEstimator* network_quality_estimator = |
| 106 request.context()->network_quality_estimator(); | 81 request.context()->network_quality_estimator(); |
| 107 if (!network_quality_estimator || | 82 if (!network_quality_estimator) |
| 108 network_quality_estimator->GetEffectiveConnectionType() < | |
| 109 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { | |
| 110 LogPreviewsEligibilityReason( | |
| 111 PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, type); | |
| 112 return false; | 83 return false; |
| 113 } | 84 |
| 114 if (network_quality_estimator->GetEffectiveConnectionType() > | 85 net::EffectiveConnectionType effective_connection_type = |
| 115 net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G) { | 86 network_quality_estimator->GetEffectiveConnectionType(); |
| 116 LogPreviewsEligibilityReason(PreviewsEligibilityReason::NETWORK_NOT_SLOW, | 87 return effective_connection_type >= net::EFFECTIVE_CONNECTION_TYPE_OFFLINE && |
| 117 type); | 88 effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; |
| 118 return false; | |
| 119 } | |
| 120 LogPreviewsEligibilityReason(PreviewsEligibilityReason::ALLOWED, type); | |
| 121 return true; | |
| 122 } | 89 } |
| 123 | 90 |
| 124 } // namespace previews | 91 } // namespace previews |
| OLD | NEW |