Chromium Code Reviews| 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 bool AllowedOnReload(PreviewsType type) { |
| 47 PreviewsType type) { | |
| 48 switch (type) { | 42 switch (type) { |
| 43 // These types return new content on refresh. | |
| 44 case PreviewsType::LITE_PAGE: | |
| 45 case PreviewsType::LOFI: | |
| 46 return true; | |
| 47 // Loading these types will always be stale when refreshed. | |
| 49 case PreviewsType::OFFLINE: | 48 case PreviewsType::OFFLINE: |
| 50 return params::EffectiveConnectionTypeThresholdForOffline(); | 49 return false; |
| 51 case PreviewsType::CLIENT_LOFI: | |
| 52 return params::EffectiveConnectionTypeThresholdForClientLoFi(); | |
| 53 case PreviewsType::NONE: | 50 case PreviewsType::NONE: |
| 54 case PreviewsType::LAST: | 51 case PreviewsType::LAST: |
| 55 break; | 52 break; |
| 56 } | 53 } |
| 57 NOTREACHED(); | 54 NOTREACHED(); |
| 58 return net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | 55 return false; |
| 59 } | 56 } |
| 60 | 57 |
| 61 } // namespace | 58 } // namespace |
| 62 | 59 |
| 63 PreviewsIOData::PreviewsIOData( | 60 PreviewsIOData::PreviewsIOData( |
| 64 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | 61 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
| 65 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 62 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 66 : ui_task_runner_(ui_task_runner), | 63 : ui_task_runner_(ui_task_runner), |
| 67 io_task_runner_(io_task_runner), | 64 io_task_runner_(io_task_runner), |
| 68 weak_factory_(this) {} | 65 weak_factory_(this) {} |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 } | 100 } |
| 104 | 101 |
| 105 void PreviewsIOData::ClearBlackList(base::Time begin_time, | 102 void PreviewsIOData::ClearBlackList(base::Time begin_time, |
| 106 base::Time end_time) { | 103 base::Time end_time) { |
| 107 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 104 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 108 previews_black_list_->ClearBlackList(begin_time, end_time); | 105 previews_black_list_->ClearBlackList(begin_time, end_time); |
| 109 } | 106 } |
| 110 | 107 |
| 111 bool PreviewsIOData::ShouldAllowPreview(const net::URLRequest& request, | 108 bool PreviewsIOData::ShouldAllowPreview(const net::URLRequest& request, |
| 112 PreviewsType type) const { | 109 PreviewsType type) const { |
| 110 return ShouldAllowPreview(request, type, | |
| 111 params::EffectiveConnectionTypeThreshold()); | |
| 112 } | |
| 113 | |
| 114 bool PreviewsIOData::ShouldAllowPreview( | |
| 115 const net::URLRequest& request, | |
| 116 PreviewsType type, | |
| 117 net::EffectiveConnectionType effective_connection_type_threshold) const { | |
| 113 if (is_enabled_callback_.is_null() || !previews_black_list_) { | 118 if (is_enabled_callback_.is_null() || !previews_black_list_) { |
| 114 LogPreviewsEligibilityReason( | 119 LogPreviewsEligibilityReason( |
| 115 PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE, type); | 120 PreviewsEligibilityReason::BLACKLIST_UNAVAILABLE, type); |
| 116 return false; | 121 return false; |
| 117 } | 122 } |
| 118 if (!is_enabled_callback_.Run(type)) | 123 if (!is_enabled_callback_.Run(type)) |
| 119 return false; | 124 return false; |
| 120 | 125 |
| 121 // The blacklist will disallow certain hosts for periods of time based on | 126 // The blacklist will disallow certain hosts for periods of time based on |
| 122 // user's opting out of the preview | 127 // user's opting out of the preview. |
| 123 PreviewsEligibilityReason status = | 128 PreviewsEligibilityReason status = |
| 124 previews_black_list_->IsLoadedAndAllowed(request.url(), type); | 129 previews_black_list_->IsLoadedAndAllowed(request.url(), type); |
| 125 if (status != PreviewsEligibilityReason::ALLOWED) { | 130 if (status != PreviewsEligibilityReason::ALLOWED) { |
| 126 LogPreviewsEligibilityReason(status, type); | 131 LogPreviewsEligibilityReason(status, type); |
| 127 return false; | 132 return false; |
| 128 } | 133 } |
| 129 net::NetworkQualityEstimator* network_quality_estimator = | 134 net::NetworkQualityEstimator* network_quality_estimator = |
| 130 request.context()->network_quality_estimator(); | 135 request.context()->network_quality_estimator(); |
| 131 if (!network_quality_estimator || | 136 if (!network_quality_estimator || |
| 132 network_quality_estimator->GetEffectiveConnectionType() < | 137 network_quality_estimator->GetEffectiveConnectionType() < |
| 133 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { | 138 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) { |
| 134 LogPreviewsEligibilityReason( | 139 LogPreviewsEligibilityReason( |
| 135 PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, type); | 140 PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE, type); |
| 136 return false; | 141 return false; |
| 137 } | 142 } |
| 138 | 143 |
| 139 if (network_quality_estimator->GetEffectiveConnectionType() > | 144 if (network_quality_estimator->GetEffectiveConnectionType() > |
| 140 GetEffectiveConnectionTypeThresholdForPreviewsType(type)) { | 145 effective_connection_type_threshold) { |
|
tbansal1
2017/05/05 20:34:39
Why was the API changed?
RyanSturm
2017/05/05 20:51:35
Because the LoFis have different params despite sh
| |
| 141 LogPreviewsEligibilityReason(PreviewsEligibilityReason::NETWORK_NOT_SLOW, | 146 LogPreviewsEligibilityReason(PreviewsEligibilityReason::NETWORK_NOT_SLOW, |
| 142 type); | 147 type); |
| 143 return false; | 148 return false; |
| 144 } | 149 } |
| 145 // LOAD_VALIDATE_CACHE or LOAD_BYPASS_CACHE mean the user reloaded the page. | 150 // 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. | 151 // If this is a query for offline previews, reloads should be disallowed. |
| 147 if (type == PreviewsType::OFFLINE && | 152 if (!AllowedOnReload(type) && |
| 148 (request.load_flags() & | 153 request.load_flags() & |
| 149 (net::LOAD_VALIDATE_CACHE | net::LOAD_BYPASS_CACHE))) { | 154 (net::LOAD_VALIDATE_CACHE | net::LOAD_BYPASS_CACHE)) { |
| 150 LogPreviewsEligibilityReason( | 155 LogPreviewsEligibilityReason(PreviewsEligibilityReason::RELOAD_DISALLOWED, |
| 151 PreviewsEligibilityReason::RELOAD_DISALLOWED_FOR_OFFLINE, type); | 156 type); |
| 152 return false; | 157 return false; |
| 153 } | 158 } |
| 159 | |
| 154 LogPreviewsEligibilityReason(PreviewsEligibilityReason::ALLOWED, type); | 160 LogPreviewsEligibilityReason(PreviewsEligibilityReason::ALLOWED, type); |
| 155 return true; | 161 return true; |
| 156 } | 162 } |
| 157 | 163 |
| 158 } // namespace previews | 164 } // namespace previews |
| OLD | NEW |