Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: content/browser/browsing_data/clear_site_data_throttle.cc

Issue 2025683003: First experimental implementation of the Clear-Site-Data header (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Compilation fixes. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/browser/browsing_data/clear_site_data_throttle.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/values.h"
11 #include "content/browser/frame_host/navigation_handle_impl.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/content_browser_client.h"
14 #include "content/public/browser/navigation_handle.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_client.h"
17 #include "content/public/common/origin_util.h"
18 #include "net/http/http_response_headers.h"
19 #include "url/gurl.h"
20 #include "url/origin.h"
21
22 namespace content {
23
24 namespace {
25
26 static const char* kClearSiteDataHeader = "Clear-Site-Data";
27
28 static const char* kTypesKey = "types";
29
30 // Pretty-printed log output.
31 static const char* kConsoleMessageFormat =
32 "Clear-Site-Data header on '%s': %s";
33 static const char* kClearingOneType = "Clearing %s.";
34 static const char* kClearingTwoTypes = "Clearing %s and %s.";
35 static const char* kClearingThreeTypes = "Clearing %s, %s and %s.";
36
37 } // namespace
38
39 // static
40 std::unique_ptr<NavigationThrottle>
41 ClearSiteDataThrottle::CreateThrottleFor(NavigationHandle* handle) {
42 return std::unique_ptr<NavigationThrottle>(new ClearSiteDataThrottle(handle));
43 }
44
45 ClearSiteDataThrottle::~ClearSiteDataThrottle() {}
46
47 ClearSiteDataThrottle::ThrottleCheckResult
48 ClearSiteDataThrottle::WillStartRequest() {
49 current_url_ = navigation_handle()->GetURL();
50 return PROCEED;
51 }
52
53 ClearSiteDataThrottle::ThrottleCheckResult
54 ClearSiteDataThrottle::WillRedirectRequest() {
55 // We are processing a redirect from url1 to url2. GetResponseHeaders()
56 // contains headers from url1, but GetURL() is already equal to url2. Handle
57 // the headers before updating the URL, so that |current_url_| corresponds
58 // to the URL that sent the headers.
59 HandleHeader();
60 current_url_ = navigation_handle()->GetURL();
61
62 return PROCEED;
63 }
64
65 ClearSiteDataThrottle::ThrottleCheckResult
66 ClearSiteDataThrottle::WillProcessResponse() {
67 HandleHeader();
68
69 // Now that RenderFrameHost is ready, output the console messages.
70 for (const ConsoleMessage& message : messages_) {
71 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole(
72 message.level,
73 base::StringPrintf(
74 kConsoleMessageFormat, navigation_handle()->GetURL().spec().c_str(),
75 message.text.c_str()));
76 }
77
78 return PROCEED;
79 }
80
81 ClearSiteDataThrottle::ClearSiteDataThrottle(NavigationHandle* handle)
82 : NavigationThrottle(handle) {}
83
84 void ClearSiteDataThrottle::HandleHeader() {
85 NavigationHandleImpl* handle =
86 static_cast<NavigationHandleImpl*>(navigation_handle());
87
88 // Only accept the header on secure origins.
89 if (!IsOriginSecure(current_url_)) {
90 ConsoleLog(&messages_,
91 "Not supported for insecure origins.",
92 CONSOLE_MESSAGE_LEVEL_ERROR);
93 return;
94 }
95
96 // Extract the instances of the header and parse them.
97 size_t iter = 0;
98 std::string header_name;
99 std::string header_value;
100 while (handle->GetResponseHeaders()->EnumerateHeaderLines(
101 &iter, &header_name, &header_value)) {
102 if (header_name != kClearSiteDataHeader)
103 continue;
104
105 bool clear_cookies;
106 bool clear_storage;
107 bool clear_cache;
108
109 bool valid_header = ParseHeader(
110 header_value, &clear_cookies, &clear_storage, &clear_cache, &messages_);
111
112 // If the header is valid, clear the data for this browser context
113 // and origin.
114 if (!valid_header)
115 continue;
116
117 BrowserContext* browser_context =
118 handle->GetWebContents()->GetBrowserContext();
119 url::Origin origin(current_url_);
120
121 GetContentClient()->browser()->ClearSiteData(
122 browser_context, origin, clear_cookies, clear_storage, clear_cache);
123 }
124 }
125
126 bool ClearSiteDataThrottle::ParseHeader(
127 const std::string& header,
128 bool* clear_cookies, bool* clear_storage, bool* clear_cache,
129 std::vector<ConsoleMessage>* messages) {
130 std::unique_ptr<base::Value> parsed_header =
131 base::JSONReader::Read(header);
132
133 if (!parsed_header) {
134 ConsoleLog(messages,
135 base::StringPrintf("%s is not a valid JSON.", header.c_str()),
136 CONSOLE_MESSAGE_LEVEL_ERROR);
137 return false;
138 }
139
140 if (!parsed_header->GetAsDictionary(nullptr)) {
141 ConsoleLog(messages,
142 base::StringPrintf("%s is not a dictionary.", header.c_str()),
143 CONSOLE_MESSAGE_LEVEL_ERROR);
144 return false;
145 }
146
147 const base::ListValue* types;
148 if (!static_cast<base::DictionaryValue*>(parsed_header.get())
149 ->GetListWithoutPathExpansion(kTypesKey, &types)) {
150 ConsoleLog(messages,
151 base::StringPrintf(
152 "No 'types' field present in %s.", header.c_str()),
153 CONSOLE_MESSAGE_LEVEL_ERROR);
154 return false;
155 }
156
157 *clear_cookies = false;
158 *clear_storage = false;
159 *clear_cache = false;
160
161 std::vector<std::string> type_names;
162 for (const std::unique_ptr<base::Value>& value : *types) {
163 std::string type;
164 value->GetAsString(&type);
165
166 bool* datatype = nullptr;
167
168 if (type == "cookies") {
169 datatype = clear_cookies;
170 } else if (type == "storage") {
171 datatype = clear_storage;
172 } else if (type == "cache") {
173 datatype = clear_cache;
174 } else {
175 std::string serialized_type;
176 JSONStringValueSerializer serializer(&serialized_type);
177 serializer.Serialize(*value);
178 ConsoleLog(messages,
179 base::StringPrintf("Invalid type: '%s'.", type.c_str()),
180 CONSOLE_MESSAGE_LEVEL_ERROR);
181 continue;
182 }
183
184 // Each data type should only be
185 DCHECK(datatype != nullptr);
186 if (*datatype)
187 continue;
188
189 *datatype = true;
190 type_names.push_back(type);
191 }
192
193 if (!*clear_cookies && !*clear_storage && !*clear_cache) {
194 ConsoleLog(messages,
195 base::StringPrintf(
196 "No valid types specified in %s.", header.c_str()),
197 CONSOLE_MESSAGE_LEVEL_ERROR);
198 return false;
199 }
200
201 // Pretty-print which types are to be cleared.
202 std::string output;
203 switch (type_names.size()) {
204 case 1:
205 output = base::StringPrintf(kClearingOneType, type_names[0].c_str());
206 break;
207 case 2:
208 output = base::StringPrintf(
209 kClearingTwoTypes, type_names[0].c_str(), type_names[1].c_str());
210 break;
211 case 3:
212 output = base::StringPrintf(
213 kClearingThreeTypes,
214 type_names[0].c_str(), type_names[1].c_str(), type_names[2].c_str());
215 break;
216 default:
217 NOTREACHED();
218 }
219 ConsoleLog(messages, output, CONSOLE_MESSAGE_LEVEL_LOG);
220
221 return true;
222 }
223
224 void ClearSiteDataThrottle::ConsoleLog(
225 std::vector<ConsoleMessage>* messages,
226 const std::string& text, ConsoleMessageLevel level) {
227 messages->push_back({text, level});
228 }
229
230 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698