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, | |
| 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 clearing_in_progress_(false), | |
| 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|. | |
| 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 clearing_in_progress_ ? DEFER : PROCEED; | |
| 106 } | |
| 107 | |
| 108 ClearSiteDataThrottle::ThrottleCheckResult | |
| 109 ClearSiteDataThrottle::WillProcessResponse() { | |
| 110 HandleHeader(); | |
| 111 return clearing_in_progress_ ? DEFER : PROCEED; | |
| 112 } | |
| 113 | |
| 114 void ClearSiteDataThrottle::HandleHeader() { | |
| 115 NavigationHandleImpl* handle = | |
| 116 static_cast<NavigationHandleImpl*>(navigation_handle()); | |
| 117 const net::HttpResponseHeaders* headers = handle->GetResponseHeaders(); | |
| 118 | |
| 119 if (!headers || !headers->HasHeader(kClearSiteDataHeader)) | |
| 120 return; | |
| 121 | |
| 122 // Only accept the header on secure origins. | |
| 123 if (!IsOriginSecure(current_url_)) { | |
| 124 ConsoleLog(&messages_, current_url_, "Not supported for insecure origins.", | |
| 125 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 std::string header_value; | |
| 130 headers->EnumerateHeader(nullptr, kClearSiteDataHeader, &header_value); | |
| 131 | |
| 132 bool clear_cookies; | |
| 133 bool clear_storage; | |
| 134 bool clear_cache; | |
| 135 | |
| 136 if (!ParseHeader(header_value, &clear_cookies, &clear_storage, &clear_cache, | |
| 137 &messages_)) { | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 // If the header is valid, clear the data for this browser context and origin. | |
| 142 BrowserContext* browser_context = | |
| 143 navigation_handle()->GetWebContents()->GetBrowserContext(); | |
| 144 url::Origin origin(current_url_); | |
| 145 | |
| 146 if (origin.unique()) { | |
| 147 ConsoleLog(&messages_, current_url_, "Not supported for unique origins.", | |
| 148 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 clearing_in_progress_ = true; | |
| 153 GetContentClient()->browser()->ClearSiteData( | |
| 154 browser_context, origin, clear_cookies, clear_storage, clear_cache, | |
| 155 base::Bind(&ClearSiteDataThrottle::TaskFinished, | |
| 156 weak_ptr_factory_.GetWeakPtr())); | |
| 157 } | |
| 158 | |
| 159 bool ClearSiteDataThrottle::ParseHeader(const std::string& header, | |
| 160 bool* clear_cookies, | |
| 161 bool* clear_storage, | |
| 162 bool* clear_cache, | |
| 163 std::vector<ConsoleMessage>* messages) { | |
| 164 if (!base::IsStringASCII(header)) { | |
| 165 ConsoleLog(messages, current_url_, "Must only contain ASCII characters.", | |
| 166 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 167 return false; | |
| 168 } | |
| 169 | |
| 170 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); | |
| 171 | |
| 172 if (!parsed_header) { | |
| 173 ConsoleLog(messages, current_url_, "Not a valid JSON.", | |
| 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 clearing_in_progress_ = false; | |
|
nasko
2016/08/12 21:44:54
nit: DCHECK that it is true?
msramek
2016/08/16 13:47:37
Done.
| |
| 258 navigation_handle()->Resume(); | |
| 259 } | |
| 260 | |
| 261 } // namespace content | |
| OLD | NEW |