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 "components/data_reduction_proxy/content/renderer/content_previews_rend
er_frame_observer.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 "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
| 14 #include "content/public/renderer/render_frame.h" |
| 15 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/public/web/WebDocumentLoader.h" |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 18 |
| 19 namespace data_reduction_proxy { |
| 20 |
| 21 namespace { |
| 22 |
| 23 bool HasEmptyImageDirective(const blink::WebURLResponse& web_url_response) { |
| 24 std::string chrome_proxy_value = |
| 25 web_url_response |
| 26 .HttpHeaderField(blink::WebString::FromUTF8(chrome_proxy_header())) |
| 27 .Utf8(); |
| 28 for (const auto& directive : |
| 29 base::SplitStringPiece(chrome_proxy_value, ",", base::TRIM_WHITESPACE, |
| 30 base::SPLIT_WANT_NONEMPTY)) { |
| 31 if (!base::StartsWith(directive, page_policies_directive(), |
| 32 base::CompareCase::INSENSITIVE_ASCII)) { |
| 33 continue; |
| 34 } |
| 35 |
| 36 // Check policy directive for empty-image entry. |
| 37 base::StringPiece page_policies_value = base::StringPiece(directive).substr( |
| 38 strlen(page_policies_directive()) + 1); |
| 39 for (const auto& policy : |
| 40 base::SplitStringPiece(page_policies_value, "|", base::TRIM_WHITESPACE, |
| 41 base::SPLIT_WANT_NONEMPTY)) { |
| 42 if (base::LowerCaseEqualsASCII(policy, empty_image_directive())) { |
| 43 return true; |
| 44 } |
| 45 } |
| 46 } |
| 47 return false; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 ContentPreviewsRenderFrameObserver::ContentPreviewsRenderFrameObserver( |
| 53 content::RenderFrame* render_frame) |
| 54 : content::RenderFrameObserver(render_frame) {} |
| 55 |
| 56 ContentPreviewsRenderFrameObserver::~ContentPreviewsRenderFrameObserver() = |
| 57 default; |
| 58 |
| 59 content::PreviewsState |
| 60 ContentPreviewsRenderFrameObserver::GetPreviewsStateFromResponse( |
| 61 content::PreviewsState original_state, |
| 62 const blink::WebURLResponse& web_url_response) { |
| 63 if (original_state == content::PREVIEWS_UNSPECIFIED) { |
| 64 return content::PREVIEWS_OFF; |
| 65 } |
| 66 |
| 67 // Don't update the state if server previews were not enabled. |
| 68 if (!(original_state & content::SERVER_LITE_PAGE_ON)) { |
| 69 return original_state; |
| 70 } |
| 71 |
| 72 // At this point, this is a proxy main frame response for which the |
| 73 // PreviewsState needs to be updated from what was enabled/accepted by the |
| 74 // client to what the client should actually do based on the server response. |
| 75 |
| 76 content::PreviewsState updated_state = original_state; |
| 77 |
| 78 // Clear the Lite Page bit if Lite Page transformation did not occur. |
| 79 // TODO(megjablon): Leverage common code in drp_headers. |
| 80 if (web_url_response |
| 81 .HttpHeaderField(blink::WebString::FromUTF8( |
| 82 chrome_proxy_content_transform_header())) |
| 83 .Utf8() != lite_page_directive()) { |
| 84 updated_state &= ~(content::SERVER_LITE_PAGE_ON); |
| 85 } |
| 86 |
| 87 // Determine whether to keep or clear Lo-Fi bits. We need to receive the |
| 88 // empty-image policy directive and have SERVER_LOFI_ON in order to retain |
| 89 // Lo-Fi bits. |
| 90 if (!(original_state & content::SERVER_LOFI_ON)) { |
| 91 // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. |
| 92 updated_state &= ~(content::CLIENT_LOFI_ON); |
| 93 } else if (!HasEmptyImageDirective(web_url_response)) { |
| 94 updated_state &= ~(content::SERVER_LOFI_ON | content::CLIENT_LOFI_ON); |
| 95 } |
| 96 |
| 97 // If we are left with no previews bits set, return the off state. |
| 98 if (updated_state == content::PREVIEWS_UNSPECIFIED) { |
| 99 return content::PREVIEWS_OFF; |
| 100 } |
| 101 |
| 102 return updated_state; |
| 103 } |
| 104 |
| 105 void ContentPreviewsRenderFrameObserver::OnDestruct() { |
| 106 delete this; |
| 107 } |
| 108 |
| 109 void ContentPreviewsRenderFrameObserver::DidCommitProvisionalLoad( |
| 110 bool is_new_navigation, |
| 111 bool is_same_document_navigation) { |
| 112 if (is_same_document_navigation) |
| 113 return; |
| 114 |
| 115 content::PreviewsState original_state = render_frame()->GetPreviewsState(); |
| 116 const blink::WebURLResponse& web_url_response = |
| 117 render_frame()->GetWebFrame()->GetDocumentLoader()->GetResponse(); |
| 118 |
| 119 render_frame()->SetPreviewsState( |
| 120 GetPreviewsStateFromResponse(original_state, web_url_response)); |
| 121 } |
| 122 |
| 123 } // namespace data_reduction_proxy |
OLD | NEW |