OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/previews_state_helper.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/strings/string_piece.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "third_party/WebKit/public/platform/WebString.h" | |
14 | |
15 namespace content { | |
16 | |
17 namespace { | |
18 | |
19 // Chrome Proxy Previews header and directives. | |
20 const char kChromeProxyHeader[] = "chrome-proxy"; | |
21 const char kChromeProxyContentTransformHeader[] = | |
22 "chrome-proxy-content-transform"; | |
23 const char kChromeProxyPagePoliciesDirective[] = "page-policies"; | |
24 const char kChromeProxyEmptyImageDirective[] = "empty-image"; | |
25 const char kChromeProxyLitePageDirective[] = "lite-page"; | |
26 | |
27 bool HasEmptyImageDirective(const blink::WebURLResponse& web_url_response) { | |
28 std::string chrome_proxy_value = | |
29 web_url_response | |
30 .HttpHeaderField(blink::WebString::FromUTF8(kChromeProxyHeader)) | |
31 .Utf8(); | |
32 for (const auto& directive : | |
33 base::SplitStringPiece(chrome_proxy_value, ",", base::TRIM_WHITESPACE, | |
34 base::SPLIT_WANT_NONEMPTY)) { | |
35 if (!base::StartsWith(directive, kChromeProxyPagePoliciesDirective, | |
36 base::CompareCase::INSENSITIVE_ASCII)) { | |
37 continue; | |
38 } | |
39 | |
40 // Check policy directive for empty-image entry. | |
41 base::StringPiece page_policies_value = base::StringPiece(directive).substr( | |
42 arraysize(kChromeProxyPagePoliciesDirective)); | |
43 for (const auto& policy : | |
44 base::SplitStringPiece(page_policies_value, "|", base::TRIM_WHITESPACE, | |
45 base::SPLIT_WANT_NONEMPTY)) { | |
46 if (base::LowerCaseEqualsASCII(policy, kChromeProxyEmptyImageDirective)) { | |
47 return true; | |
48 } | |
49 } | |
50 } | |
51 return false; | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 PreviewsState UpdatePreviewsStateFromMainFrameResponse( | |
57 PreviewsState original_state, | |
58 const blink::WebURLResponse& web_url_response) { | |
59 if (original_state == PREVIEWS_UNSPECIFIED) { | |
60 return PREVIEWS_OFF; | |
61 } | |
62 | |
63 // Only update the state if there was a server preview requested. | |
64 if (!(original_state & SERVER_LITE_PAGE_ON) && | |
65 !(original_state & SERVER_LOFI_ON)) { | |
66 return original_state; | |
67 } | |
68 | |
69 // Next handle legacy path where Lite Page not enabled but Lo-Fi is enabled. | |
70 if (!(original_state & SERVER_LITE_PAGE_ON) && | |
71 (original_state & SERVER_LOFI_ON)) { | |
sclittle
2017/06/07 20:31:57
nit: You could combine the previous if statement a
dougarnett
2017/06/07 20:56:11
Good point. I had this separate so that it will be
| |
72 return original_state; | |
73 } | |
74 | |
75 // At this point, this is a proxy main frame response for which the | |
76 // PreviewsState needs to be updated from what was enabled/accepted by the | |
77 // client to what the client should actually do based on the server response. | |
78 | |
79 PreviewsState updated_state = original_state; | |
80 | |
81 // Clear the Lite Page bit if Lite Page transformation did not occur. | |
82 if (web_url_response | |
83 .HttpHeaderField( | |
84 blink::WebString::FromUTF8(kChromeProxyContentTransformHeader)) | |
85 .Utf8() != kChromeProxyLitePageDirective) { | |
86 updated_state &= ~(SERVER_LITE_PAGE_ON); | |
87 } | |
88 | |
89 // Determine whether to keep or clear Lo-Fi bits. We need to receive the | |
90 // empty-image policy directive and have SERVER_LOFI_ON in order to retain | |
91 // Lo-Fi bits. | |
92 if (!(original_state & SERVER_LOFI_ON)) { | |
93 // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. | |
94 updated_state &= ~(CLIENT_LOFI_ON); | |
95 } else if (!HasEmptyImageDirective(web_url_response)) { | |
96 updated_state &= ~(SERVER_LOFI_ON | CLIENT_LOFI_ON); | |
97 } | |
98 | |
99 // If we are left with no previews bits set, return the off state. | |
100 if (updated_state == PREVIEWS_UNSPECIFIED) { | |
101 return PREVIEWS_OFF; | |
102 } | |
103 | |
104 return updated_state; | |
105 } | |
106 | |
107 } // namespace content | |
OLD | NEW |