Index: content/renderer/previews_state_helper.cc |
diff --git a/content/renderer/previews_state_helper.cc b/content/renderer/previews_state_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..808ccc3eb39a2a8407f013b319a6915fc990d6ed |
--- /dev/null |
+++ b/content/renderer/previews_state_helper.cc |
@@ -0,0 +1,91 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/renderer/previews_state_helper.h" |
+ |
+#include "base/strings/string_split.h" |
+ |
+namespace { |
megjablon
2017/06/05 23:47:58
move the anonymous namespace inside content namesp
dougarnett
2017/06/06 16:49:46
Done.
|
+ |
+// Chrome Proxy Previews header and directives. |
+const char kChromeProxyHeader[] = "chrome-proxy"; |
+const char kChromeProxyContentTransformHeader[] = |
+ "chrome-proxy-content-transform"; |
+const char kChromeProxyPagePoliciesDirective[] = "page-policies"; |
+const char kChromeProxyEmptyImageDirective[] = "empty-image"; |
+const char kChromeProxyLitePageDirective[] = "lite-page"; |
+ |
+bool HasEmptyPageDirective(const blink::WebURLResponse& web_url_response) { |
megjablon
2017/06/05 23:47:59
s/HasEmptyPageDirective/HasEmptyImageDirective/?
dougarnett
2017/06/06 16:49:46
Yuck! Thanks!
|
+ std::string chrome_proxy_value = |
megjablon
2017/06/05 23:47:59
#include <string>
dougarnett
2017/06/06 16:49:46
Done.
|
+ web_url_response |
+ .HttpHeaderField(blink::WebString::FromUTF8(kChromeProxyHeader)) |
megjablon
2017/06/05 23:47:58
#include "third_party/WebKit/public/platform/WebSt
dougarnett
2017/06/06 16:49:46
Done.
|
+ .Utf8(); |
+ for (const auto& directive : |
+ base::SplitStringPiece(chrome_proxy_value, ",", base::TRIM_WHITESPACE, |
+ base::SPLIT_WANT_NONEMPTY)) { |
+ if (!base::StartsWith(directive, kChromeProxyPagePoliciesDirective, |
megjablon
2017/06/05 23:47:58
#include "base/strings/string_util.cc"
dougarnett
2017/06/06 16:49:46
Done.
|
+ base::CompareCase::INSENSITIVE_ASCII)) { |
+ continue; |
+ } |
+ |
+ // Check policy directive for empty-image entry. |
+ base::StringPiece page_policies_value = base::StringPiece(directive).substr( |
megjablon
2017/06/05 23:47:59
#include "base/strings/string_piece.h"
dougarnett
2017/06/06 16:49:46
Done.
|
+ arraysize(kChromeProxyPagePoliciesDirective)); |
megjablon
2017/06/05 23:47:58
#include "src/base/macros.h"
dougarnett
2017/06/06 16:49:46
Done.
|
+ for (const auto& policy : |
+ base::SplitStringPiece(page_policies_value, "|", base::TRIM_WHITESPACE, |
+ base::SPLIT_WANT_NONEMPTY)) { |
+ if (base::LowerCaseEqualsASCII(policy, kChromeProxyEmptyImageDirective)) { |
+ return true; |
+ } |
+ } |
+ } |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+PreviewsState UpdatePreviewsStateFromMainFrameResponse( |
+ PreviewsState original_state, |
+ const blink::WebURLResponse& web_url_response) { |
+ // Only update the state if there was a server preview requested. |
+ if (!(original_state & SERVER_LITE_PAGE_ON) && |
+ !(original_state & SERVER_LOFI_ON)) |
+ return original_state; |
megjablon
2017/06/05 23:47:58
if statement is more than two lines, add braces.
dougarnett
2017/06/06 16:49:46
Done.
|
+ |
+ // Next handle legacy path where Lite Page not enabled but Lo-Fi is enabled. |
+ if (!(original_state & SERVER_LITE_PAGE_ON) && |
+ (original_state & SERVER_LOFI_ON)) { |
megjablon
2017/06/05 23:47:58
Just want to make sure, in the updated protocol ca
dougarnett
2017/06/06 16:49:46
We don't want to since SERVER_LITE_PAGE_ON => acce
|
+ return original_state; |
+ } |
+ |
+ // At this point, we have a proxy main frame response for which we want |
megjablon
2017/06/05 23:47:58
Don't use we.
dougarnett
2017/06/06 16:49:46
Done.
|
+ // to update the PreviewsState from what was enabled/accepted by the client |
+ // to what the client should actually do based on the server response. |
+ |
+ PreviewsState updated_state = original_state; |
+ |
+ // Clear the Lite Page bit if Lite Page transformation did not occur. |
+ if (web_url_response |
+ .HttpHeaderField( |
+ blink::WebString::FromUTF8(kChromeProxyContentTransformHeader)) |
+ .Utf8() != kChromeProxyLitePageDirective) { |
+ updated_state &= ~(SERVER_LITE_PAGE_ON); |
+ } |
+ |
+ // Determine whether to keep or clear Lo-Fi bits. We need to receive the |
+ // empty-image policy directive and have SERVER_LOFI_ON in order to retain |
+ // Lo-Fi bits. |
+ if (!(updated_state & SERVER_LOFI_ON)) { |
megjablon
2017/06/05 23:47:59
Even though it's the same, I'd use original_state
dougarnett
2017/06/06 16:49:46
Done.
|
+ // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. |
+ updated_state &= ~(CLIENT_LOFI_ON); |
+ } else if (!HasEmptyPageDirective(web_url_response)) { |
+ updated_state &= ~(SERVER_LOFI_ON | CLIENT_LOFI_ON); |
+ } |
+ |
megjablon
2017/06/05 23:47:58
Can we check here if we cleared all the bits and i
dougarnett
2017/06/06 16:49:46
Done.
|
+ return updated_state; |
+} |
+ |
+} // namespace content |