| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/data_reduction_proxy/content/browser/content_lofi_decider.h
" | 5 #include "components/data_reduction_proxy/content/browser/content_lofi_decider.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" | 12 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
| 13 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" | 13 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" |
| 14 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 15 #include "content/public/common/previews_state.h" | 15 #include "content/public/common/previews_state.h" |
| 16 #include "content/public/common/resource_type.h" | 16 #include "content/public/common/resource_type.h" |
| 17 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 18 #include "net/http/http_request_headers.h" | 18 #include "net/http/http_request_headers.h" |
| 19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
| 20 | 20 |
| 21 namespace data_reduction_proxy { | 21 namespace data_reduction_proxy { |
| 22 | 22 |
| 23 ContentLoFiDecider::ContentLoFiDecider() {} | 23 ContentLoFiDecider::ContentLoFiDecider() {} |
| 24 | 24 |
| 25 ContentLoFiDecider::~ContentLoFiDecider() {} | 25 ContentLoFiDecider::~ContentLoFiDecider() {} |
| 26 | 26 |
| 27 bool ContentLoFiDecider::IsUsingLoFiMode(const net::URLRequest& request) const { | 27 bool ContentLoFiDecider::IsUsingLoFi(const net::URLRequest& request) const { |
| 28 const content::ResourceRequestInfo* request_info = | 28 const content::ResourceRequestInfo* request_info = |
| 29 content::ResourceRequestInfo::ForRequest(&request); | 29 content::ResourceRequestInfo::ForRequest(&request); |
| 30 // The Lo-Fi directive should not be added for users in the Lo-Fi field | 30 // The Lo-Fi directive should not be added for users in the Lo-Fi field |
| 31 // trial "Control" group. Check that the user is in a group that can get | 31 // trial "Control" group. Check that the user is in a group that can get |
| 32 // "q=low". | 32 // "q=low". |
| 33 bool lofi_enabled_via_flag_or_field_trial = | 33 bool lofi_enabled_via_flag_or_field_trial = |
| 34 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); | 34 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); |
| 35 | 35 |
| 36 // Return if the user is using Lo-Fi and not part of the "Control" group. | 36 // Return if the user is using Lo-Fi and not part of the "Control" group. |
| 37 if (request_info) { | 37 if (request_info) { |
| 38 return (request_info->GetPreviewsState() & content::SERVER_LOFI_ON) && | 38 return (request_info->GetPreviewsState() & content::SERVER_LOFI_ON) && |
| 39 lofi_enabled_via_flag_or_field_trial; | 39 lofi_enabled_via_flag_or_field_trial; |
| 40 } | 40 } |
| 41 return false; | 41 return false; |
| 42 } | 42 } |
| 43 | 43 |
| 44 void ContentLoFiDecider::MaybeSetAcceptTransformHeader( | 44 void ContentLoFiDecider::MaybeSetAcceptTransformHeader( |
| 45 const net::URLRequest& request, | 45 const net::URLRequest& request, |
| 46 bool is_previews_disabled, | 46 bool are_previews_disabled, |
| 47 net::HttpRequestHeaders* headers) const { | 47 net::HttpRequestHeaders* headers) const { |
| 48 const content::ResourceRequestInfo* request_info = | 48 const content::ResourceRequestInfo* request_info = |
| 49 content::ResourceRequestInfo::ForRequest(&request); | 49 content::ResourceRequestInfo::ForRequest(&request); |
| 50 | 50 |
| 51 if (!request_info) | 51 if (!request_info) |
| 52 return; | 52 return; |
| 53 | 53 |
| 54 // Previews only operate on HTTP. | 54 // Previews only operate on HTTP. |
| 55 if (!request.url().SchemeIs("http")) | 55 if (!request.url().SchemeIs("http")) |
| 56 return; | 56 return; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 76 (params::IsLoFiOnViaFlags() && params::AreLitePagesEnabledViaFlags()) || | 76 (params::IsLoFiOnViaFlags() && params::AreLitePagesEnabledViaFlags()) || |
| 77 params::IsIncludedInLitePageFieldTrial(); | 77 params::IsIncludedInLitePageFieldTrial(); |
| 78 | 78 |
| 79 // User does not have previews enabled. | 79 // User does not have previews enabled. |
| 80 if (!lofi_enabled_via_flags_or_field_trial && | 80 if (!lofi_enabled_via_flags_or_field_trial && |
| 81 !lite_page_enabled_via_flags_or_field_trial) { | 81 !lite_page_enabled_via_flags_or_field_trial) { |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Previews has been disabled. | 85 // Previews has been disabled. |
| 86 if (is_previews_disabled) | 86 if (are_previews_disabled) |
| 87 return; | 87 return; |
| 88 | 88 |
| 89 // Do not add the Chrome-Proxy-Accept-Transform header when the page load | 89 // Do not add the Chrome-Proxy-Accept-Transform header when the page load |
| 90 // explicitly forbids previews transformations. | 90 // explicitly forbids previews transformations. |
| 91 if (request_info->GetPreviewsState() & content::PREVIEWS_NO_TRANSFORM) | 91 if (request_info->GetPreviewsState() & content::PREVIEWS_NO_TRANSFORM) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 // LoFi is not allowed on the main frame, stylesheet, script, font resource, | 94 // Lo-Fi is not allowed on the main frame, stylesheet, script, font resource, |
| 95 // media, service worker, or CSP report. | 95 // media, service worker, or CSP report. |
| 96 bool resource_type_supports_empty_image = | 96 bool resource_type_supports_empty_image = |
| 97 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || | 97 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
| 98 resource_type == content::RESOURCE_TYPE_STYLESHEET || | 98 resource_type == content::RESOURCE_TYPE_STYLESHEET || |
| 99 resource_type == content::RESOURCE_TYPE_SCRIPT || | 99 resource_type == content::RESOURCE_TYPE_SCRIPT || |
| 100 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || | 100 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || |
| 101 resource_type == content::RESOURCE_TYPE_MEDIA || | 101 resource_type == content::RESOURCE_TYPE_MEDIA || |
| 102 resource_type == content::RESOURCE_TYPE_CSP_REPORT); | 102 resource_type == content::RESOURCE_TYPE_CSP_REPORT); |
| 103 | 103 |
| 104 // If in the lite page field trial or the lite page flag is enabled, only add | 104 // If the Lite Page field trial or flag is enabled, only add the "lite-page" |
| 105 // the "lite-page" directive on main frame requests. Do not add "empty-image" | 105 // directive on main frame requests. Only add "empty-image" directives to |
| 106 // directives to other requests when Lite Page previews are enabled. | 106 // other requests when Lite Page previews are not enabled after the main |
| 107 // Add the "if-heavy" qualifier to allow the server to provide a preview when | 107 // frame. Add the "if-heavy" qualifier to allow the server to provide a |
| 108 // the page is data heavy on if a preview was not otherwise triggered. | 108 // preview when the page is data heavy on if a preview was not otherwise |
| 109 // triggered. |
| 109 std::string accept_transform_value; | 110 std::string accept_transform_value; |
| 110 if (lite_page_enabled_via_flags_or_field_trial) { | 111 if (lite_page_enabled_via_flags_or_field_trial && |
| 111 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 112 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) { |
| 112 accept_transform_value = lite_page_directive(); | 113 accept_transform_value = lite_page_directive(); |
| 113 } else if (lofi_enabled_via_flags_or_field_trial) { | 114 |
| 114 if (resource_type_supports_empty_image) | 115 // Since a Lite Page was not triggered client side, ask the server to |
| 115 accept_transform_value = empty_image_directive(); | 116 // provide a Lite Page only if the page is otherwise data-heavy. |
| 117 if (!(request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) |
| 118 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); |
| 119 } else if (lofi_enabled_via_flags_or_field_trial && |
| 120 // Only use Lo-Fi if Lite Pages aren't enabled or fallback from |
| 121 // Lite Pages to Lo-Fi is enabled. |
| 122 (!lite_page_enabled_via_flags_or_field_trial || |
| 123 params::IsLitePageFallbackEnabled()) && |
| 124 resource_type_supports_empty_image && |
| 125 !(request_info->GetPreviewsState() & |
| 126 content::SERVER_LITE_PAGE_ON)) { |
| 127 accept_transform_value = empty_image_directive(); |
| 128 |
| 129 // Since Lo-Fi was not triggered client side, ask the server to provide |
| 130 // Lo-Fi only if the page is otherwise data-heavy. |
| 131 if (!(request_info->GetPreviewsState() & content::SERVER_LOFI_ON)) |
| 132 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); |
| 116 } | 133 } |
| 117 if (accept_transform_value.empty()) | 134 if (accept_transform_value.empty()) |
| 118 return; | 135 return; |
| 119 | 136 |
| 120 if (!(request_info->GetPreviewsState() & content::SERVER_LOFI_ON)) | |
| 121 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); | |
| 122 | |
| 123 headers->SetHeader(chrome_proxy_accept_transform_header(), | 137 headers->SetHeader(chrome_proxy_accept_transform_header(), |
| 124 accept_transform_value); | 138 accept_transform_value); |
| 125 } | 139 } |
| 126 | 140 |
| 127 bool ContentLoFiDecider::IsSlowPagePreviewRequested( | 141 bool ContentLoFiDecider::IsSlowPagePreviewRequested( |
| 128 const net::HttpRequestHeaders& headers) const { | 142 const net::HttpRequestHeaders& headers) const { |
| 129 std::string accept_transform_header_value; | 143 std::string accept_transform_header_value; |
| 130 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), | 144 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), |
| 131 &accept_transform_header_value)) { | 145 &accept_transform_header_value)) { |
| 132 return false; | 146 return false; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 headers->SetHeader(chrome_proxy_header(), chrome_proxy_header_value); | 196 headers->SetHeader(chrome_proxy_header(), chrome_proxy_header_value); |
| 183 } | 197 } |
| 184 | 198 |
| 185 bool ContentLoFiDecider::ShouldRecordLoFiUMA( | 199 bool ContentLoFiDecider::ShouldRecordLoFiUMA( |
| 186 const net::URLRequest& request) const { | 200 const net::URLRequest& request) const { |
| 187 const content::ResourceRequestInfo* request_info = | 201 const content::ResourceRequestInfo* request_info = |
| 188 content::ResourceRequestInfo::ForRequest(&request); | 202 content::ResourceRequestInfo::ForRequest(&request); |
| 189 | 203 |
| 190 // User is not using Lo-Fi. | 204 // User is not using Lo-Fi. |
| 191 if (!request_info || | 205 if (!request_info || |
| 192 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON)) { | 206 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON || |
| 207 request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) { |
| 193 return false; | 208 return false; |
| 194 } | 209 } |
| 195 | 210 |
| 196 return params::IsIncludedInLoFiEnabledFieldTrial() || | 211 return params::IsIncludedInLoFiEnabledFieldTrial() || |
| 197 params::IsIncludedInLoFiControlFieldTrial(); | 212 params::IsIncludedInLoFiControlFieldTrial(); |
| 198 } | 213 } |
| 199 | 214 |
| 200 } // namespace data_reduction_proxy | 215 } // namespace data_reduction_proxy |
| OLD | NEW |