| 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_macros.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" |
| 14 #include "base/time/default_clock.h" | 15 #include "base/time/default_clock.h" |
| 15 #include "components/previews/core/previews_black_list.h" | 16 #include "components/previews/core/previews_black_list.h" |
| 17 #include "components/previews/core/previews_experiments.h" |
| 16 #include "components/previews/core/previews_opt_out_store.h" | 18 #include "components/previews/core/previews_opt_out_store.h" |
| 17 #include "components/previews/core/previews_ui_service.h" | 19 #include "components/previews/core/previews_ui_service.h" |
| 18 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
| 19 #include "net/nqe/network_quality_estimator.h" | 21 #include "net/nqe/network_quality_estimator.h" |
| 20 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 21 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 namespace previews { | 26 namespace previews { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 void LogPreviewsEligibilityReason(PreviewsEligibilityReason status, | 30 void LogPreviewsEligibilityReason(PreviewsEligibilityReason status, |
| 29 PreviewsType type) { | 31 PreviewsType type) { |
| 30 switch (type) { | 32 int32_t max_limit = static_cast<int32_t>(PreviewsEligibilityReason::LAST); |
| 31 case PreviewsType::OFFLINE: | 33 base::LinearHistogram::FactoryGet( |
| 32 UMA_HISTOGRAM_ENUMERATION( | 34 base::StringPrintf("Previews.EligibilityReason.%s", |
| 33 "Previews.EligibilityReason.Offline", static_cast<int>(status), | 35 GetStringNameForType(type).c_str()), |
| 34 static_cast<int>(PreviewsEligibilityReason::LAST)); | 36 1, max_limit, max_limit + 1, |
| 35 break; | 37 base::HistogramBase::kUmaTargetedHistogramFlag) |
| 36 case PreviewsType::CLIENT_LOFI: | 38 ->Add(static_cast<int>(status)); |
| 37 UMA_HISTOGRAM_ENUMERATION( | |
| 38 "Previews.EligibilityReason.ClientLoFi", static_cast<int>(status), | |
| 39 static_cast<int>(PreviewsEligibilityReason::LAST)); | |
| 40 break; | |
| 41 default: | |
| 42 NOTREACHED(); | |
| 43 } | |
| 44 } | 39 } |
| 45 | 40 |
| 46 net::EffectiveConnectionType GetEffectiveConnectionTypeThresholdForPreviewsType( | 41 net::EffectiveConnectionType GetEffectiveConnectionTypeThresholdForPreviewsType( |
| 47 PreviewsType type) { | 42 PreviewsType type) { |
| 48 switch (type) { | 43 switch (type) { |
| 44 // These types check network quality on their own. |
| 45 case PreviewsType::LITE_PAGE: |
| 46 case PreviewsType::SERVER_LOFI: |
| 47 return net::EFFECTIVE_CONNECTION_TYPE_4G; |
| 49 case PreviewsType::OFFLINE: | 48 case PreviewsType::OFFLINE: |
| 50 return params::EffectiveConnectionTypeThresholdForOffline(); | 49 return params::EffectiveConnectionTypeThresholdForOffline(); |
| 51 case PreviewsType::CLIENT_LOFI: | 50 case PreviewsType::CLIENT_LOFI: |
| 52 return params::EffectiveConnectionTypeThresholdForClientLoFi(); | 51 return params::EffectiveConnectionTypeThresholdForClientLoFi(); |
| 53 case PreviewsType::NONE: | 52 case PreviewsType::NONE: |
| 54 case PreviewsType::LAST: | 53 case PreviewsType::LAST: |
| 55 break; | 54 break; |
| 56 } | 55 } |
| 57 NOTREACHED(); | 56 NOTREACHED(); |
| 58 return net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | 57 return net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; |
| 59 } | 58 } |
| 60 | 59 |
| 60 bool AllowedOnReload(PreviewsType type) { |
| 61 switch (type) { |
| 62 // These types return new content on refresh. |
| 63 case PreviewsType::LITE_PAGE: |
| 64 case PreviewsType::SERVER_LOFI: |
| 65 case PreviewsType::CLIENT_LOFI: |
| 66 return true; |
| 67 // Loading these types will always be stale when refreshed. |
| 68 case PreviewsType::OFFLINE: |
| 69 return false; |
| 70 case PreviewsType::NONE: |
| 71 case PreviewsType::LAST: |
| 72 break; |
| 73 } |
| 74 NOTREACHED(); |
| 75 return false; |
| 76 } |
| 77 |
| 61 } // namespace | 78 } // namespace |
| 62 | 79 |
| 63 PreviewsIOData::PreviewsIOData( | 80 PreviewsIOData::PreviewsIOData( |
| 64 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | 81 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
| 65 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 82 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 66 : ui_task_runner_(ui_task_runner), | 83 : ui_task_runner_(ui_task_runner), |
| 67 io_task_runner_(io_task_runner), | 84 io_task_runner_(io_task_runner), |
| 68 weak_factory_(this) {} | 85 weak_factory_(this) {} |
| 69 | 86 |
| 70 PreviewsIOData::~PreviewsIOData() {} | 87 PreviewsIOData::~PreviewsIOData() {} |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 PreviewsType type) const { | 129 PreviewsType type) const { |
| 113 if (is_enabled_callback_.is_null() || !previews_black_list_) { | 130 if (is_enabled_callback_.is_null() || !previews_black_list_) { |
| 114 LogPreviewsEligibilityReason( | 131 LogPreviewsEligibilityReason( |
| 115 PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE, type); | 132 PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE, type); |
| 116 return false; | 133 return false; |
| 117 } | 134 } |
| 118 if (!is_enabled_callback_.Run(type)) | 135 if (!is_enabled_callback_.Run(type)) |
| 119 return false; | 136 return false; |
| 120 | 137 |
| 121 // The blacklist will disallow certain hosts for periods of time based on | 138 // The blacklist will disallow certain hosts for periods of time based on |
| 122 // user's opting out of the preview | 139 // user's opting out of the preview. |
| 123 PreviewsEligibilityReason status = | 140 PreviewsEligibilityReason status = |
| 124 previews_black_list_->IsLoadedAndAllowed(request.url(), type); | 141 previews_black_list_->IsLoadedAndAllowed(request.url(), type); |
| 125 if (status != PreviewsEligibilityReason::ALLOWED) { | 142 if (status != PreviewsEligibilityReason::ALLOWED) { |
| 126 LogPreviewsEligibilityReason(status, type); | 143 LogPreviewsEligibilityReason(status, type); |
| 127 return false; | 144 return false; |
| 128 } | 145 } |
| 129 net::NetworkQualityEstimator* network_quality_estimator = | 146 net::NetworkQualityEstimator* network_quality_estimator = |
| 130 request.context()->network_quality_estimator(); | 147 request.context()->network_quality_estimator(); |
| 131 if (!network_quality_estimator || | 148 if (!network_quality_estimator || |
| 132 network_quality_estimator->GetEffectiveConnectionType() < | 149 network_quality_estimator->GetEffectiveConnectionType() < |
| 133 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { | 150 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { |
| 134 LogPreviewsEligibilityReason( | 151 LogPreviewsEligibilityReason( |
| 135 PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, type); | 152 PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, type); |
| 136 return false; | 153 return false; |
| 137 } | 154 } |
| 138 | 155 |
| 139 if (network_quality_estimator->GetEffectiveConnectionType() > | 156 if (network_quality_estimator->GetEffectiveConnectionType() > |
| 140 GetEffectiveConnectionTypeThresholdForPreviewsType(type)) { | 157 GetEffectiveConnectionTypeThresholdForPreviewsType(type)) { |
| 141 LogPreviewsEligibilityReason(PreviewsEligibilityReason::NETWORK_NOT_SLOW, | 158 LogPreviewsEligibilityReason(PreviewsEligibilityReason::NETWORK_NOT_SLOW, |
| 142 type); | 159 type); |
| 143 return false; | 160 return false; |
| 144 } | 161 } |
| 145 // LOAD_VALIDATE_CACHE or LOAD_BYPASS_CACHE mean the user reloaded the page. | 162 // LOAD_VALIDATE_CACHE or LOAD_BYPASS_CACHE mean the user reloaded the page. |
| 146 // If this is a query for offline previews, reloads should be disallowed. | 163 // If this is a query for offline previews, reloads should be disallowed. |
| 147 if (type == PreviewsType::OFFLINE && | 164 if (!AllowedOnReload(type) && |
| 148 (request.load_flags() & | 165 request.load_flags() & |
| 149 (net::LOAD_VALIDATE_CACHE | net::LOAD_BYPASS_CACHE))) { | 166 (net::LOAD_VALIDATE_CACHE | net::LOAD_BYPASS_CACHE)) { |
| 150 LogPreviewsEligibilityReason( | 167 LogPreviewsEligibilityReason(PreviewsEligibilityReason::RELOAD_DISALLOWED, |
| 151 PreviewsEligibilityReason::RELOAD_DISALLOWED_FOR_OFFLINE, type); | 168 type); |
| 152 return false; | 169 return false; |
| 153 } | 170 } |
| 171 |
| 154 LogPreviewsEligibilityReason(PreviewsEligibilityReason::ALLOWED, type); | 172 LogPreviewsEligibilityReason(PreviewsEligibilityReason::ALLOWED, type); |
| 155 return true; | 173 return true; |
| 156 } | 174 } |
| 157 | 175 |
| 158 } // namespace previews | 176 } // namespace previews |
| OLD | NEW |