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/WebDataSource.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) { | |
dougarnett
2017/06/30 21:03:36
Bummer that git didn't identify the move of this c
megjablon
2017/07/06 19:15:00
I couldn't find a sure way to do this.
| |
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 if (web_url_response | |
dougarnett
2017/06/30 21:03:36
Consider adding TODO to leverage common code in dr
megjablon
2017/07/06 19:15:00
Can I put this TODO on you?
| |
80 .HttpHeaderField(blink::WebString::FromUTF8( | |
81 chrome_proxy_content_transform_header())) | |
82 .Utf8() != lite_page_directive()) { | |
83 updated_state &= ~(content::SERVER_LITE_PAGE_ON); | |
84 } | |
85 | |
86 // Determine whether to keep or clear Lo-Fi bits. We need to receive the | |
87 // empty-image policy directive and have SERVER_LOFI_ON in order to retain | |
88 // Lo-Fi bits. | |
89 if (!(original_state & content::SERVER_LOFI_ON)) { | |
90 // Server Lo-Fi not enabled so ensure Client Lo-Fi off for this request. | |
91 updated_state &= ~(content::CLIENT_LOFI_ON); | |
92 } else if (!HasEmptyImageDirective(web_url_response)) { | |
93 updated_state &= ~(content::SERVER_LOFI_ON | content::CLIENT_LOFI_ON); | |
94 } | |
95 | |
96 // If we are left with no previews bits set, return the off state. | |
97 if (updated_state == content::PREVIEWS_UNSPECIFIED) { | |
98 return content::PREVIEWS_OFF; | |
99 } | |
100 | |
101 return updated_state; | |
102 } | |
103 | |
104 void ContentPreviewsRenderFrameObserver::OnDestruct() { | |
105 delete this; | |
106 } | |
107 | |
108 void ContentPreviewsRenderFrameObserver::DidCommitProvisionalLoad( | |
109 bool is_new_navigation, | |
110 bool is_same_document_navigation) { | |
111 if (is_same_document_navigation) | |
112 return; | |
113 | |
114 content::PreviewsState original_state = render_frame()->GetPreviewsState(); | |
115 const blink::WebURLResponse& web_url_response = | |
116 render_frame()->GetWebFrame()->DataSource()->GetResponse(); | |
117 | |
118 render_frame()->SetPreviewsState( | |
119 GetPreviewsStateFromResponse(original_state, web_url_response)); | |
120 } | |
121 | |
122 } // namespace data_reduction_proxy | |
OLD | NEW |