Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/command_line.h" | |
| 8 #include "base/json/json_reader.h" | |
| 9 #include "base/json/json_string_value_serializer.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "base/values.h" | |
| 14 #include "content/browser/frame_host/navigation_handle_impl.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/browser/content_browser_client.h" | |
| 17 #include "content/public/browser/navigation_handle.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "content/public/common/content_client.h" | |
| 20 #include "content/public/common/content_switches.h" | |
| 21 #include "content/public/common/origin_util.h" | |
| 22 #include "net/http/http_response_headers.h" | |
| 23 #include "url/gurl.h" | |
| 24 #include "url/origin.h" | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 static const char* kClearSiteDataHeader = "Clear-Site-Data"; | |
| 31 | |
| 32 static const char* kTypesKey = "types"; | |
| 33 | |
| 34 // Pretty-printed log output. | |
| 35 static const char* kConsoleMessagePrefix = "Clear-Site-Data header on '%s': %s"; | |
| 36 static const char* kClearingOneType = "Clearing %s."; | |
| 37 static const char* kClearingTwoTypes = "Clearing %s and %s."; | |
| 38 static const char* kClearingThreeTypes = "Clearing %s, %s, and %s."; | |
| 39 | |
| 40 // Console logging. Adds a |text| message with |level| to |messages|. | |
| 41 void ConsoleLog(std::vector<ClearSiteDataThrottle::ConsoleMessage>* messages, | |
|
nasko
2016/08/11 20:07:22
nit: Seems an overkill to have a full on method fo
msramek
2016/08/12 15:06:27
Hmm, here I would argue that ConsoleLog(where, wha
nasko
2016/08/12 21:44:53
I'm fine as it is.
| |
| 42 const GURL& url, | |
| 43 const std::string& text, | |
| 44 ConsoleMessageLevel level) { | |
| 45 messages->push_back({url, text, level}); | |
| 46 } | |
| 47 | |
| 48 bool AreExperimentalFeaturesEnabled() { | |
| 49 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 50 switches::kEnableExperimentalWebPlatformFeatures); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 // static | |
| 56 std::unique_ptr<NavigationThrottle> | |
| 57 ClearSiteDataThrottle::CreateThrottleForNavigation(NavigationHandle* handle) { | |
| 58 if (AreExperimentalFeaturesEnabled()) | |
| 59 return base::WrapUnique(new ClearSiteDataThrottle(handle)); | |
| 60 | |
| 61 return std::unique_ptr<NavigationThrottle>(); | |
| 62 } | |
| 63 | |
| 64 ClearSiteDataThrottle::ClearSiteDataThrottle( | |
| 65 NavigationHandle* navigation_handle) | |
| 66 : NavigationThrottle(navigation_handle), | |
| 67 active_tasks_(0), | |
| 68 weak_ptr_factory_(this) {} | |
| 69 | |
| 70 ClearSiteDataThrottle::~ClearSiteDataThrottle() { | |
| 71 // At the end of the navigation we finally have access to the correct | |
| 72 // RenderFrameHost. Output the cached console messages. Prefix each sequence | |
| 73 // of messages belonging to the same URL with |kConsoleMessagePrefix|. | |
|
nasko
2016/08/11 20:07:21
|messages| is a vector and there is no sorting per
msramek
2016/08/12 15:06:27
If we're going through a redirect url1->url2, then
nasko
2016/08/12 21:44:53
Yes, I had the url1->url2->url1 pattern in mind. I
| |
| 74 GURL last_seen_url; | |
| 75 for (const ConsoleMessage& message : messages_) { | |
| 76 if (message.url == last_seen_url) { | |
| 77 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | |
| 78 message.level, message.text); | |
| 79 } else { | |
| 80 navigation_handle()->GetRenderFrameHost()->AddMessageToConsole( | |
| 81 message.level, | |
| 82 base::StringPrintf(kConsoleMessagePrefix, message.url.spec().c_str(), | |
| 83 message.text.c_str())); | |
| 84 } | |
| 85 | |
| 86 last_seen_url = message.url; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 ClearSiteDataThrottle::ThrottleCheckResult | |
| 91 ClearSiteDataThrottle::WillStartRequest() { | |
| 92 current_url_ = navigation_handle()->GetURL(); | |
| 93 return PROCEED; | |
| 94 } | |
| 95 | |
| 96 ClearSiteDataThrottle::ThrottleCheckResult | |
| 97 ClearSiteDataThrottle::WillRedirectRequest() { | |
| 98 // We are processing a redirect from url1 to url2. GetResponseHeaders() | |
| 99 // contains headers from url1, but GetURL() is already equal to url2. Handle | |
| 100 // the headers before updating the URL, so that |current_url_| corresponds | |
| 101 // to the URL that sent the headers. | |
| 102 HandleHeader(); | |
| 103 current_url_ = navigation_handle()->GetURL(); | |
| 104 | |
| 105 return active_tasks_ ? DEFER : PROCEED; | |
| 106 } | |
| 107 | |
| 108 ClearSiteDataThrottle::ThrottleCheckResult | |
| 109 ClearSiteDataThrottle::WillProcessResponse() { | |
| 110 HandleHeader(); | |
| 111 return active_tasks_ ? DEFER : PROCEED; | |
| 112 } | |
| 113 | |
| 114 void ClearSiteDataThrottle::HandleHeader() { | |
| 115 NavigationHandleImpl* handle = | |
| 116 static_cast<NavigationHandleImpl*>(navigation_handle()); | |
| 117 | |
| 118 // Some navigations don't have response headers. | |
| 119 if (!handle->GetResponseHeaders()) | |
| 120 return; | |
| 121 | |
|
nasko
2016/08/11 20:07:22
The spec calls out that Clear-Site-Data is only va
msramek
2016/08/12 15:06:27
You're correct to expect the call to IsOriginSecur
nasko
2016/08/12 21:44:53
Hmm, I wrote the comment and then discarded it. I
| |
| 122 // Extract the instances of the header and parse them. | |
| 123 size_t iter = 0; | |
| 124 std::string header_name; | |
| 125 std::string header_value; | |
| 126 while (handle->GetResponseHeaders()->EnumerateHeaderLines(&iter, &header_name, | |
|
nasko
2016/08/11 20:07:21
Do we want to allow multiple instances of the head
msramek
2016/08/12 15:06:27
Done.
It's true that currently there's no reason
nasko
2016/08/12 21:44:53
Doesn't GetNormalizedHeader work? It will likely a
msramek
2016/08/16 13:47:37
Done.
Ok, let's do it that way. We'll probably up
| |
| 127 &header_value)) { | |
| 128 if (header_name != kClearSiteDataHeader) | |
| 129 continue; | |
| 130 | |
| 131 // Only accept the header on secure origins. | |
| 132 if (!IsOriginSecure(current_url_)) { | |
| 133 ConsoleLog(&messages_, current_url_, | |
| 134 "Not supported for insecure origins.", | |
| 135 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 136 return; | |
| 137 } | |
| 138 | |
| 139 bool clear_cookies; | |
| 140 bool clear_storage; | |
| 141 bool clear_cache; | |
| 142 | |
| 143 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, | |
| 144 &messages_)) { | |
| 145 continue; | |
| 146 } | |
| 147 | |
| 148 // If the header is valid, clear the data for this browser context | |
| 149 // and origin. | |
| 150 BrowserContext* browser_context = | |
| 151 navigation_handle()->GetWebContents()->GetBrowserContext(); | |
| 152 url::Origin origin(current_url_); | |
|
nasko
2016/08/11 20:07:21
Do we honor unique origins for deletion too? The s
msramek
2016/08/12 15:06:27
That's a good question.
The current implementatio
nasko
2016/08/12 21:44:53
Maybe not right now with the current state of affa
| |
| 153 | |
| 154 ++active_tasks_; | |
|
nasko
2016/08/11 20:07:21
Honoring only single instance of the header will a
msramek
2016/08/12 15:06:27
Done. But we'll still need at least a boolean.
nasko
2016/08/12 21:44:53
Acknowledged.
| |
| 155 GetContentClient()->browser()->ClearSiteData( | |
| 156 browser_context, origin, clear_cookies, clear_storage, clear_cache, | |
| 157 base::Bind(&ClearSiteDataThrottle::TaskFinished, | |
| 158 weak_ptr_factory_.GetWeakPtr())); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, | |
| 163 bool* clear_cookies, | |
| 164 bool* clear_storage, | |
| 165 bool* clear_cache, | |
| 166 std::vector<ConsoleMessage>* messages) { | |
| 167 std::unique_ptr<base::Value> parsed_header; | |
| 168 if (base::IsStringASCII(header)) | |
|
nasko
2016/08/11 20:07:22
nit: Invert the check and early return.
msramek
2016/08/12 15:06:27
Done.
| |
| 169 parsed_header = base::JSONReader::Read(header); | |
| 170 | |
| 171 if (!parsed_header) { | |
| 172 ConsoleLog(messages, current_url_, | |
| 173 "Not a valid JSON or non-ASCII characters present.", | |
|
nasko
2016/08/11 20:07:21
Wouldn't we want to be more precise as to what wen
msramek
2016/08/12 15:06:27
Done.
| |
| 174 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 175 return false; | |
| 176 } | |
| 177 | |
| 178 const base::DictionaryValue* dictionary = nullptr; | |
| 179 const base::ListValue* types = nullptr; | |
| 180 if (!parsed_header->GetAsDictionary(&dictionary) || | |
| 181 !dictionary->GetListWithoutPathExpansion(kTypesKey, &types)) { | |
| 182 ConsoleLog(messages, current_url_, | |
| 183 "Expecting a JSON dictionary with a 'types' field.", | |
| 184 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 DCHECK(types); | |
| 189 | |
| 190 *clear_cookies = false; | |
| 191 *clear_storage = false; | |
| 192 *clear_cache = false; | |
| 193 | |
| 194 std::vector<std::string> type_names; | |
| 195 for (const std::unique_ptr<base::Value>& value : *types) { | |
| 196 std::string type; | |
| 197 value->GetAsString(&type); | |
| 198 | |
| 199 bool* datatype = nullptr; | |
| 200 | |
| 201 if (type == "cookies") { | |
| 202 datatype = clear_cookies; | |
| 203 } else if (type == "storage") { | |
| 204 datatype = clear_storage; | |
| 205 } else if (type == "cache") { | |
| 206 datatype = clear_cache; | |
| 207 } else { | |
| 208 std::string serialized_type; | |
| 209 JSONStringValueSerializer serializer(&serialized_type); | |
| 210 serializer.Serialize(*value); | |
| 211 ConsoleLog( | |
| 212 messages, current_url_, | |
| 213 base::StringPrintf("Invalid type: %s.", serialized_type.c_str()), | |
| 214 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 215 continue; | |
| 216 } | |
| 217 | |
| 218 // Each data type should only be processed once. | |
| 219 DCHECK(datatype); | |
| 220 if (*datatype) | |
| 221 continue; | |
| 222 | |
| 223 *datatype = true; | |
| 224 type_names.push_back(type); | |
| 225 } | |
| 226 | |
| 227 if (!*clear_cookies && !*clear_storage && !*clear_cache) { | |
| 228 ConsoleLog(messages, current_url_, | |
| 229 "No valid types specified in the 'types' field.", | |
| 230 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 231 return false; | |
| 232 } | |
| 233 | |
| 234 // Pretty-print which types are to be cleared. | |
| 235 std::string output; | |
| 236 switch (type_names.size()) { | |
| 237 case 1: | |
| 238 output = base::StringPrintf(kClearingOneType, type_names[0].c_str()); | |
| 239 break; | |
| 240 case 2: | |
| 241 output = base::StringPrintf(kClearingTwoTypes, type_names[0].c_str(), | |
| 242 type_names[1].c_str()); | |
| 243 break; | |
| 244 case 3: | |
| 245 output = base::StringPrintf(kClearingThreeTypes, type_names[0].c_str(), | |
| 246 type_names[1].c_str(), type_names[2].c_str()); | |
| 247 break; | |
| 248 default: | |
| 249 NOTREACHED(); | |
| 250 } | |
| 251 ConsoleLog(messages, current_url_, output, CONSOLE_MESSAGE_LEVEL_LOG); | |
| 252 | |
| 253 return true; | |
| 254 } | |
| 255 | |
| 256 void ClearSiteDataThrottle::TaskFinished() { | |
| 257 DCHECK(active_tasks_); | |
| 258 if (!--active_tasks_) | |
| 259 navigation_handle()->Resume(); | |
| 260 } | |
| 261 | |
| 262 } // namespace content | |
| OLD | NEW |