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 "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" |
12 #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" |
13 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
14 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
15 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
17 | 18 |
18 namespace data_reduction_proxy { | 19 namespace data_reduction_proxy { |
19 | 20 |
20 ContentLoFiDecider::ContentLoFiDecider() {} | 21 ContentLoFiDecider::ContentLoFiDecider() {} |
(...skipping 10 matching lines...) Expand all Loading... |
31 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); | 32 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); |
32 | 33 |
33 // Return if the user is using Lo-Fi and not part of the "Control" group. | 34 // Return if the user is using Lo-Fi and not part of the "Control" group. |
34 if (request_info) | 35 if (request_info) |
35 return request_info->IsUsingLoFi() && lofi_enabled_via_flag_or_field_trial; | 36 return request_info->IsUsingLoFi() && lofi_enabled_via_flag_or_field_trial; |
36 return false; | 37 return false; |
37 } | 38 } |
38 | 39 |
39 void ContentLoFiDecider::MaybeSetAcceptTransformHeader( | 40 void ContentLoFiDecider::MaybeSetAcceptTransformHeader( |
40 const net::URLRequest& request, | 41 const net::URLRequest& request, |
| 42 bool is_previews_disabled, |
41 net::HttpRequestHeaders* headers) const { | 43 net::HttpRequestHeaders* headers) const { |
42 const content::ResourceRequestInfo* request_info = | 44 const content::ResourceRequestInfo* request_info = |
43 content::ResourceRequestInfo::ForRequest(&request); | 45 content::ResourceRequestInfo::ForRequest(&request); |
44 | 46 |
45 if (!request_info) | 47 if (!request_info) |
46 return; | 48 return; |
47 | 49 |
48 content::ResourceType resource_type = request_info->GetResourceType(); | |
49 | |
50 // The Lo-Fi and Lite Page directives should not be added for users in the | |
51 // Lo-Fi field trial "Control" group. | |
52 bool lofi_enabled_via_flag_or_field_trial = | |
53 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); | |
54 | |
55 bool lite_page_via_flag_or_field_trial = | |
56 params::AreLitePagesEnabledViaFlags() || | |
57 params::IsIncludedInLitePageFieldTrial(); | |
58 | |
59 // Previews only operate on HTTP. | 50 // Previews only operate on HTTP. |
60 if (!request.url().SchemeIs("http")) | 51 if (!request.url().SchemeIs("http")) |
61 return; | 52 return; |
62 | 53 |
63 // Chrome-Proxy-Accept-Transform takes at most one token. | 54 // Chrome-Proxy-Accept-Transform takes at most one token. |
64 if (headers->HasHeader(chrome_proxy_accept_transform_header())) | 55 if (headers->HasHeader(chrome_proxy_accept_transform_header())) |
65 return; | 56 return; |
66 | 57 |
| 58 content::ResourceType resource_type = request_info->GetResourceType(); |
| 59 |
67 if (resource_type == content::RESOURCE_TYPE_MEDIA) { | 60 if (resource_type == content::RESOURCE_TYPE_MEDIA) { |
68 headers->SetHeader(chrome_proxy_accept_transform_header(), | 61 headers->SetHeader(chrome_proxy_accept_transform_header(), |
69 compressed_video_directive()); | 62 compressed_video_directive()); |
70 return; | 63 return; |
71 } | 64 } |
72 | 65 |
73 // User is not using Lo-Fi or is part of the "Control" group. | 66 // The Lo-Fi and Lite Page directives should not be added for users in the |
74 if (!request_info->IsUsingLoFi() || !lofi_enabled_via_flag_or_field_trial) | 67 // Lo-Fi field trial "Control" group. |
| 68 bool lofi_enabled_via_flags_or_field_trial = |
| 69 params::IsLoFiOnViaFlags() || params::IsIncludedInLoFiEnabledFieldTrial(); |
| 70 |
| 71 bool lite_page_enabled_via_flags_or_field_trial = |
| 72 (params::IsLoFiOnViaFlags() && params::AreLitePagesEnabledViaFlags()) || |
| 73 params::IsIncludedInLitePageFieldTrial(); |
| 74 |
| 75 // User does not have previews enabled. |
| 76 if (!lofi_enabled_via_flags_or_field_trial && |
| 77 !lite_page_enabled_via_flags_or_field_trial) { |
| 78 return; |
| 79 } |
| 80 |
| 81 // Previews has been disabled. |
| 82 if (is_previews_disabled) |
75 return; | 83 return; |
76 | 84 |
77 // LoFi is not allowed on the main frame, stylesheet, script, font resource, | 85 // LoFi is not allowed on the main frame, stylesheet, script, font resource, |
78 // media, service worker, or CSP report. | 86 // media, service worker, or CSP report. |
79 bool resource_type_supports_empty_image = | 87 bool resource_type_supports_empty_image = |
80 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || | 88 !(resource_type == content::RESOURCE_TYPE_MAIN_FRAME || |
81 resource_type == content::RESOURCE_TYPE_STYLESHEET || | 89 resource_type == content::RESOURCE_TYPE_STYLESHEET || |
82 resource_type == content::RESOURCE_TYPE_SCRIPT || | 90 resource_type == content::RESOURCE_TYPE_SCRIPT || |
83 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || | 91 resource_type == content::RESOURCE_TYPE_FONT_RESOURCE || |
84 resource_type == content::RESOURCE_TYPE_MEDIA || | 92 resource_type == content::RESOURCE_TYPE_MEDIA || |
85 resource_type == content::RESOURCE_TYPE_CSP_REPORT); | 93 resource_type == content::RESOURCE_TYPE_CSP_REPORT); |
86 | 94 |
87 // If in the preview field trial or the preview flag is enabled, only add the | 95 // If in the lite page field trial or the lite page flag is enabled, only add |
88 // "lite-page" directive on main frame requests. Do not add "empty-image" | 96 // the "lite-page" directive on main frame requests. Do not add "empty-image" |
89 // directives to other requests when Lite Page previews are enabled. | 97 // directives to other requests when Lite Page previews are enabled. |
| 98 // Add the "if-heavy" qualifier to allow the server to provide a preview when |
| 99 // the page is data heavy on if a preview was not otherwise triggered. |
90 std::string accept_transform_value; | 100 std::string accept_transform_value; |
91 if (lite_page_via_flag_or_field_trial) { | 101 if (lite_page_enabled_via_flags_or_field_trial) { |
92 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 102 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
93 accept_transform_value = lite_page_directive(); | 103 accept_transform_value = lite_page_directive(); |
94 } else if (resource_type_supports_empty_image) { | 104 } else if (lofi_enabled_via_flags_or_field_trial) { |
95 accept_transform_value = empty_image_directive(); | 105 if (resource_type_supports_empty_image) |
| 106 accept_transform_value = empty_image_directive(); |
96 } | 107 } |
97 | |
98 if (accept_transform_value.empty()) | 108 if (accept_transform_value.empty()) |
99 return; | 109 return; |
100 | 110 |
| 111 if (!request_info->IsUsingLoFi()) |
| 112 accept_transform_value += base::StringPrintf(";%s", if_heavy_qualifier()); |
| 113 |
101 headers->SetHeader(chrome_proxy_accept_transform_header(), | 114 headers->SetHeader(chrome_proxy_accept_transform_header(), |
102 accept_transform_value); | 115 accept_transform_value); |
103 } | 116 } |
104 | 117 |
105 bool ContentLoFiDecider::IsSlowPagePreviewRequested( | 118 bool ContentLoFiDecider::IsSlowPagePreviewRequested( |
106 const net::HttpRequestHeaders& headers) const { | 119 const net::HttpRequestHeaders& headers) const { |
107 std::string accept_transform_header_value; | 120 std::string accept_transform_header_value; |
108 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), | 121 if (!headers.GetHeader(chrome_proxy_accept_transform_header(), |
109 &accept_transform_header_value)) { | 122 &accept_transform_header_value)) { |
110 return false; | 123 return false; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 180 |
168 // User is not using Lo-Fi. | 181 // User is not using Lo-Fi. |
169 if (!request_info || !request_info->IsUsingLoFi()) | 182 if (!request_info || !request_info->IsUsingLoFi()) |
170 return false; | 183 return false; |
171 | 184 |
172 return params::IsIncludedInLoFiEnabledFieldTrial() || | 185 return params::IsIncludedInLoFiEnabledFieldTrial() || |
173 params::IsIncludedInLoFiControlFieldTrial(); | 186 params::IsIncludedInLoFiControlFieldTrial(); |
174 } | 187 } |
175 | 188 |
176 } // namespace data_reduction_proxy | 189 } // namespace data_reduction_proxy |
OLD | NEW |