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 if (!(request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) |
bengr
2017/02/15 00:03:47
Add a comment:
// Since a lite page was not trigge
megjablon
2017/02/16 04:29:09
Done.
| |
115 accept_transform_value = empty_image_directive(); | 116 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); |
117 } else if (lofi_enabled_via_flags_or_field_trial && | |
118 resource_type_supports_empty_image && | |
119 !(request_info->GetPreviewsState() & | |
120 content::SERVER_LITE_PAGE_ON)) { | |
121 accept_transform_value = empty_image_directive(); | |
122 | |
123 if (!(request_info->GetPreviewsState() & content::SERVER_LOFI_ON)) | |
124 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); | |
116 } | 125 } |
117 if (accept_transform_value.empty()) | 126 if (accept_transform_value.empty()) |
118 return; | 127 return; |
119 | 128 |
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(), | 129 headers->SetHeader(chrome_proxy_accept_transform_header(), |
124 accept_transform_value); | 130 accept_transform_value); |
125 } | 131 } |
126 | 132 |
127 bool ContentLoFiDecider::IsSlowPagePreviewRequested( | 133 bool ContentLoFiDecider::IsSlowPagePreviewRequested( |
128 const net::HttpRequestHeaders& headers) const { | 134 const net::HttpRequestHeaders& headers) const { |
129 std::string accept_transform_header_value; | 135 std::string accept_transform_header_value; |
130 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), | 136 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), |
131 &accept_transform_header_value)) { | 137 &accept_transform_header_value)) { |
132 return false; | 138 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); | 188 headers->SetHeader(chrome_proxy_header(), chrome_proxy_header_value); |
183 } | 189 } |
184 | 190 |
185 bool ContentLoFiDecider::ShouldRecordLoFiUMA( | 191 bool ContentLoFiDecider::ShouldRecordLoFiUMA( |
186 const net::URLRequest& request) const { | 192 const net::URLRequest& request) const { |
187 const content::ResourceRequestInfo* request_info = | 193 const content::ResourceRequestInfo* request_info = |
188 content::ResourceRequestInfo::ForRequest(&request); | 194 content::ResourceRequestInfo::ForRequest(&request); |
189 | 195 |
190 // User is not using Lo-Fi. | 196 // User is not using Lo-Fi. |
191 if (!request_info || | 197 if (!request_info || |
192 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON)) { | 198 !(request_info->GetPreviewsState() & content::SERVER_LOFI_ON || |
199 request_info->GetPreviewsState() & content::SERVER_LITE_PAGE_ON)) { | |
193 return false; | 200 return false; |
194 } | 201 } |
195 | 202 |
196 return params::IsIncludedInLoFiEnabledFieldTrial() || | 203 return params::IsIncludedInLoFiEnabledFieldTrial() || |
197 params::IsIncludedInLoFiControlFieldTrial(); | 204 params::IsIncludedInLoFiControlFieldTrial(); |
198 } | 205 } |
199 | 206 |
200 } // namespace data_reduction_proxy | 207 } // namespace data_reduction_proxy |
OLD | NEW |