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 GetPreviewsStateFromMainFrameResponse( | |
57 PreviewsState original_state, | |
58 const blink::WebURLResponse& web_url_response) { | |
59 if (original_state == PREVIEWS_UNSPECIFIED) { | |
60 return PREVIEWS_OFF; | |
61 } | |
62 | |
63 // Don't update the state if server previews were not enabled. | |
64 if (!(original_state & SERVER_LITE_PAGE_ON)) { | |
65 return original_state; | |
66 } | |
67 | |
68 // At this point, this is a proxy main frame response for which the | |
69 // PreviewsState needs to be updated from what was enabled/accepted by the | |
70 // client to what the client should actually do based on the server response. | |
71 | |
72 PreviewsState updated_state = original_state; | |
73 | |
74 // Clear the Lite Page bit if Lite Page transformation did not occur. | |
75 if (web_url_response | |
76 .HttpHeaderField( | |
77 blink::WebString::FromUTF8(kChromeProxyContentTransformHeader)) | |
78 .Utf8() != kChromeProxyLitePageDirective) { | |
79 updated_state &= ~(SERVER_LITE_PAGE_ON); | |
80 } | |
81 | |
82 // Determine whether to keep or clear Lo-Fi bits. We need to receive the | |
83 // empty-image policy directive and have SERVER_LOFI_ON in order to retain | |
84 // Lo-Fi bits. | |
85 if (!(original_state & SERVER_LOFI_ON)) { | |
86 // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. | |
87 updated_state &= ~(CLIENT_LOFI_ON); | |
88 } else if (!HasEmptyImageDirective(web_url_response)) { | |
89 updated_state &= ~(SERVER_LOFI_ON | CLIENT_LOFI_ON); | |
90 } | |
91 | |
92 // If we are left with no previews bits set, return the off state. | |
93 if (updated_state == PREVIEWS_UNSPECIFIED) { | |
94 return PREVIEWS_OFF; | |
95 } | |
96 | |
97 return updated_state; | |
98 } | |
99 | |
100 } // namespace content | |
OLD | NEW |