Chromium Code Reviews| 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..fcbafd496f7d25a1b6f479352a49bdd3f0979bfa |
| --- /dev/null |
| +++ b/content/renderer/previews_state_helper.cc |
| @@ -0,0 +1,80 @@ |
| +// 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 content { |
| + |
| +// 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"; |
| + |
| +PreviewsState UpdatePreviewsStateFromMainFrameResponse( |
| + PreviewsState original_state, |
| + const blink::WebURLResponse& web_url_response) { |
| + // First check if no previews were enabled for this request. |
| + if (original_state == PREVIEWS_UNSPECIFIED) |
| + return original_state; |
| + |
| + // 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)) { |
|
sclittle
2017/06/01 00:59:29
I don't think this is quite right. If Server LoFi
dougarnett
2017/06/01 20:53:11
Adding an early return above if neither server pre
|
| + return original_state; |
| + } |
| + |
| + PreviewsState updated_state = original_state; |
| + |
| + // Clear the Lite Page bit if Lite Page transformation did not occur. |
|
sclittle
2017/06/01 00:59:29
Looking at the code here, it looks like if the DRP
dougarnett
2017/06/01 20:53:11
Let's discuss. It depends on when this method is c
dougarnett
2017/06/01 22:05:10
Done.
|
| + 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)) { |
| + // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. |
|
sclittle
2017/06/01 00:59:29
Client LoFi shouldn't be dependent on Server LoFi
dougarnett
2017/06/01 20:53:11
No, that was not my understanding. I thought that
|
| + updated_state &= ~(CLIENT_LOFI_ON); |
| + } else { |
| + // Keep/clear Lo-Fi bits based on having empty-image policy directive. |
| + bool has_empty_page_directive = false; |
| + std::string chrome_proxy_value = |
| + web_url_response |
| + .HttpHeaderField(blink::WebString::FromUTF8(kChromeProxyHeader)) |
| + .Utf8(); |
| + size_t page_policies_pos = |
|
sclittle
2017/06/01 00:59:29
Could you separate this string searching into a he
dougarnett
2017/06/01 20:53:11
Done.
|
| + chrome_proxy_value.find(kChromeProxyPagePoliciesDirective); |
|
sclittle
2017/06/01 00:59:29
Could you just use base::SplitStringPiece here to
dougarnett
2017/06/01 20:53:11
Done.
|
| + if (page_policies_pos != std::string::npos) { |
| + size_t end_pos = |
| + chrome_proxy_value.find_first_of(" ,", page_policies_pos); |
| + std::string page_policies_value = chrome_proxy_value.substr( |
|
sclittle
2017/06/01 00:59:29
nit: Don't make an unnecessary copy of the std::st
dougarnett
2017/06/01 20:53:11
Done.
|
| + page_policies_pos + strlen(kChromeProxyPagePoliciesDirective) + 1, |
|
sclittle
2017/06/01 00:59:29
nit: you can use arraysize or sizeof instead of st
dougarnett
2017/06/01 20:53:11
Done.
|
| + end_pos); |
|
sclittle
2017/06/01 00:59:29
string::substr takes in a total number of characte
dougarnett
2017/06/01 20:53:11
Acknowledged.
|
| + for (const auto& policy : |
| + base::SplitString(page_policies_value, "|", base::TRIM_WHITESPACE, |
|
sclittle
2017/06/01 00:59:29
nit: use base::SplitStringPiece, not base::SplitSt
dougarnett
2017/06/01 20:53:11
Done.
|
| + base::SPLIT_WANT_NONEMPTY)) { |
| + if (policy == kChromeProxyEmptyImageDirective) { |
|
sclittle
2017/06/01 00:59:29
Use case-insensitive comparison, e.g. base::LowerC
dougarnett
2017/06/01 20:53:11
Done.
|
| + has_empty_page_directive = true; |
| + break; |
| + } |
| + } |
| + } |
| + if (!has_empty_page_directive) { |
| + updated_state &= ~(SERVER_LOFI_ON | CLIENT_LOFI_ON); |
| + } |
| + } |
| + |
| + return updated_state; |
| +} |
| + |
| +} // namespace content |