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" |
(...skipping 25 matching lines...) Expand all Loading... | |
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 are_previews_disabled, | |
47 net::HttpRequestHeaders* headers) const { | 46 net::HttpRequestHeaders* headers) const { |
48 const content::ResourceRequestInfo* request_info = | 47 const content::ResourceRequestInfo* request_info = |
49 content::ResourceRequestInfo::ForRequest(&request); | 48 content::ResourceRequestInfo::ForRequest(&request); |
50 | 49 |
51 if (!request_info) | 50 if (!request_info) |
52 return; | 51 return; |
53 | 52 |
54 // Previews only operate on HTTP. | 53 // Previews only operate on HTTP. |
55 if (!request.url().SchemeIs("http")) | 54 if (!request.url().SchemeIs("http")) |
56 return; | 55 return; |
57 | 56 |
58 // Chrome-Proxy-Accept-Transform takes at most one token. | 57 // Chrome-Proxy-Accept-Transform takes at most one token. |
59 if (headers->HasHeader(chrome_proxy_accept_transform_header())) | 58 if (headers->HasHeader(chrome_proxy_accept_transform_header())) |
60 return; | 59 return; |
61 | 60 |
62 content::ResourceType resource_type = request_info->GetResourceType(); | 61 content::ResourceType resource_type = request_info->GetResourceType(); |
63 | 62 |
64 if (resource_type == content::RESOURCE_TYPE_MEDIA) { | 63 if (resource_type == content::RESOURCE_TYPE_MEDIA) { |
65 headers->SetHeader(chrome_proxy_accept_transform_header(), | 64 headers->SetHeader(chrome_proxy_accept_transform_header(), |
66 compressed_video_directive()); | 65 compressed_video_directive()); |
67 return; | 66 return; |
68 } | 67 } |
69 | 68 |
70 // The Lo-Fi and Lite Page directives should not be added for users in the | 69 // Do not add the Chrome-Proxy-Accept-Transform header if the request |
71 // Lo-Fi field trial "Control" group. | 70 // has previews turned off. |
72 bool lofi_enabled_via_flags_or_field_trial = | 71 content::PreviewsState previews_state = request_info->GetPreviewsState(); |
73 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); | 72 if (previews_state & content::PREVIEWS_OFF) |
74 | |
75 bool lite_page_enabled_via_flags_or_field_trial = | |
76 (params::IsLoFiOnViaFlags() && params::AreLitePagesEnabledViaFlags()) || | |
77 params::IsIncludedInLitePageFieldTrial(); | |
78 | |
79 // User does not have previews enabled. | |
80 if (!lofi_enabled_via_flags_or_field_trial && | |
81 !lite_page_enabled_via_flags_or_field_trial) { | |
82 return; | |
83 } | |
84 | |
85 // Previews has been disabled. | |
86 if (are_previews_disabled) | |
87 return; | 73 return; |
88 | 74 |
75 // TODO(dougarnett): Remove once blink uses Off instead of NoTransform. | |
89 // Do not add the Chrome-Proxy-Accept-Transform header when the page load | 76 // Do not add the Chrome-Proxy-Accept-Transform header when the page load |
90 // explicitly forbids previews transformations. | 77 // explicitly forbids previews transformations. |
91 if (request_info->GetPreviewsState() & content::PREVIEWS_NO_TRANSFORM) | 78 if (previews_state & content::PREVIEWS_NO_TRANSFORM) |
92 return; | 79 return; |
93 | 80 |
94 // Lo-Fi is not allowed on the main frame, stylesheet, script, font resource, | 81 // Lo-Fi is not allowed on the main frame, stylesheet, script, font resource, |
95 // media, service worker, or CSP report. | 82 // media, service worker, or CSP report. |
96 bool resource_type_supports_empty_image = | 83 bool resource_type_supports_empty_image = |
bengr
2017/05/01 16:53:13
Hmm. I don't see this being used anymore, but it s
| |
97 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || | 84 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
98 resource_type == content::RESOURCE_TYPE_STYLESHEET || | 85 resource_type == content::RESOURCE_TYPE_STYLESHEET || |
99 resource_type == content::RESOURCE_TYPE_SCRIPT || | 86 resource_type == content::RESOURCE_TYPE_SCRIPT || |
100 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || | 87 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || |
101 resource_type == content::RESOURCE_TYPE_MEDIA || | 88 resource_type == content::RESOURCE_TYPE_MEDIA || |
102 resource_type == content::RESOURCE_TYPE_CSP_REPORT); | 89 resource_type == content::RESOURCE_TYPE_CSP_REPORT); |
103 | 90 |
104 // If the Lite Page field trial or flag is enabled, only add the "lite-page" | 91 // If the Lite Page field trial or flag is enabled, only add the "lite-page" |
105 // directive on main frame requests. Only add "empty-image" directives to | 92 // directive on main frame requests. Only add "empty-image" directives to |
106 // other requests when Lite Page previews are not enabled after the main | 93 // other requests when Lite Page previews are not enabled after the main |
107 // frame. Add the "if-heavy" qualifier to allow the server to provide a | 94 // frame. |
108 // preview when the page is data heavy on if a preview was not otherwise | |
109 // triggered. | |
110 std::string accept_transform_value; | 95 std::string accept_transform_value; |
111 if (lite_page_enabled_via_flags_or_field_trial && | 96 if ((previews_state & content::SERVER_LITE_PAGE_ON) && |
112 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) { | 97 resource_type == content::RESOURCE_TYPE_MAIN_FRAME) { |
113 accept_transform_value = lite_page_directive(); | 98 accept_transform_value = lite_page_directive(); |
114 | 99 } else if ((previews_state & content::SERVER_LOFI_ON) && |
115 // Since a Lite Page was not triggered client side, ask the server to | 100 resource_type_supports_empty_image) { |
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(); | 101 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()); | |
133 } | 102 } |
134 if (accept_transform_value.empty()) | 103 if (accept_transform_value.empty()) |
135 return; | 104 return; |
136 | 105 |
137 headers->SetHeader(chrome_proxy_accept_transform_header(), | 106 headers->SetHeader(chrome_proxy_accept_transform_header(), |
138 accept_transform_value); | 107 accept_transform_value); |
139 } | 108 } |
140 | 109 |
141 bool ContentLoFiDecider::IsSlowPagePreviewRequested( | 110 bool ContentLoFiDecider::IsSlowPagePreviewRequested( |
142 const net::HttpRequestHeaders& headers) const { | 111 const net::HttpRequestHeaders& headers) const { |
(...skipping 30 matching lines...) Expand all Loading... | |
173 std::string transform_type; | 142 std::string transform_type; |
174 base::TrimWhitespaceASCII(tokens[0], base::TRIM_ALL, &transform_type); | 143 base::TrimWhitespaceASCII(tokens[0], base::TRIM_ALL, &transform_type); |
175 return transform_type == lite_page_directive(); | 144 return transform_type == lite_page_directive(); |
176 } | 145 } |
177 | 146 |
178 void ContentLoFiDecider::RemoveAcceptTransformHeader( | 147 void ContentLoFiDecider::RemoveAcceptTransformHeader( |
179 net::HttpRequestHeaders* headers) const { | 148 net::HttpRequestHeaders* headers) const { |
180 headers->RemoveHeader(chrome_proxy_accept_transform_header()); | 149 headers->RemoveHeader(chrome_proxy_accept_transform_header()); |
181 } | 150 } |
182 | 151 |
183 void ContentLoFiDecider::MaybeSetIgnorePreviewsBlacklistDirective( | 152 void ContentLoFiDecider::MaybeSetForceLitePageDirective( |
184 net::HttpRequestHeaders* headers) const { | 153 net::HttpRequestHeaders* headers) const { |
154 // Ensure lite page is requested in headers and enabled via flag. | |
bengr
2017/05/01 16:53:13
This is very confusing. If lite pages are requeste
| |
185 if (!headers || !params::AreLitePagesEnabledViaFlags() || | 155 if (!headers || !params::AreLitePagesEnabledViaFlags() || |
186 !IsLitePagePreviewRequested(*headers)) { | 156 !IsLitePagePreviewRequested(*headers)) { |
187 return; | 157 return; |
188 } | 158 } |
159 | |
189 std::string chrome_proxy_header_value; | 160 std::string chrome_proxy_header_value; |
190 headers->GetHeader(chrome_proxy_header(), &chrome_proxy_header_value); | 161 headers->GetHeader(chrome_proxy_header(), &chrome_proxy_header_value); |
191 headers->RemoveHeader(chrome_proxy_header()); | 162 headers->RemoveHeader(chrome_proxy_header()); |
192 if (!chrome_proxy_header_value.empty()) | 163 if (!chrome_proxy_header_value.empty()) |
193 chrome_proxy_header_value += ", "; | 164 chrome_proxy_header_value += ", "; |
194 chrome_proxy_header_value += | 165 chrome_proxy_header_value += chrome_proxy_force_lite_page_directive(); |
195 chrome_proxy_lite_page_ignore_blacklist_directive(); | |
196 headers->SetHeader(chrome_proxy_header(), chrome_proxy_header_value); | 166 headers->SetHeader(chrome_proxy_header(), chrome_proxy_header_value); |
197 } | 167 } |
198 | 168 |
199 bool ContentLoFiDecider::ShouldRecordLoFiUMA( | 169 bool ContentLoFiDecider::ShouldRecordPreviewsUMA( |
200 const net::URLRequest& request) const { | 170 const net::URLRequest& request) const { |
201 const content::ResourceRequestInfo* request_info = | 171 const content::ResourceRequestInfo* request_info = |
202 content::ResourceRequestInfo::ForRequest(&request); | 172 content::ResourceRequestInfo::ForRequest(&request); |
203 | 173 |
204 // User is not using Lo-Fi. | 174 // User is not using Lo-Fi. |
205 if (!request_info || | 175 if (!request_info || |
206 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON || | 176 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON || |
207 request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) { | 177 request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) { |
208 return false; | 178 return false; |
209 } | 179 } |
210 | 180 |
211 return params::IsIncludedInLoFiEnabledFieldTrial() || | 181 return params::IsIncludedInLoFiEnabledFieldTrial() || |
212 params::IsIncludedInLoFiControlFieldTrial(); | 182 params::IsIncludedInLoFiControlFieldTrial(); |
213 } | 183 } |
214 | 184 |
215 } // namespace data_reduction_proxy | 185 } // namespace data_reduction_proxy |
OLD | NEW |