Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" | 5 #include "content/browser/browsing_data/clear_site_data_throttle.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "url/origin.h" | 30 #include "url/origin.h" |
| 31 | 31 |
| 32 namespace content { | 32 namespace content { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char kNameForLogging[] = "ClearSiteDataThrottle"; | 36 const char kNameForLogging[] = "ClearSiteDataThrottle"; |
| 37 | 37 |
| 38 const char kClearSiteDataHeader[] = "Clear-Site-Data"; | 38 const char kClearSiteDataHeader[] = "Clear-Site-Data"; |
| 39 | 39 |
| 40 const char kTypesKey[] = "types"; | |
| 41 | |
| 42 // Datatypes. | 40 // Datatypes. |
| 43 const char kDatatypeCookies[] = "cookies"; | 41 const char kDatatypeCookies[] = "cookies"; |
| 44 const char kDatatypeStorage[] = "storage"; | 42 const char kDatatypeStorage[] = "storage"; |
| 45 const char kDatatypeCache[] = "cache"; | 43 const char kDatatypeCache[] = "cache"; |
| 46 | 44 |
| 47 // Pretty-printed log output. | 45 // Pretty-printed log output. |
| 48 const char kConsoleMessageTemplate[] = "Clear-Site-Data header on '%s': %s"; | 46 const char kConsoleMessageTemplate[] = "Clear-Site-Data header on '%s': %s"; |
| 49 const char kConsoleMessageCleared[] = "Cleared data types: %s."; | 47 const char kConsoleMessageCleared[] = "Cleared data types: %s."; |
| 50 const char kConsoleMessageDatatypeSeparator[] = ", "; | 48 const char kConsoleMessageDatatypeSeparator[] = ", "; |
| 51 | 49 |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 439 bool* clear_storage, | 437 bool* clear_storage, |
| 440 bool* clear_cache, | 438 bool* clear_cache, |
| 441 ConsoleMessagesDelegate* delegate, | 439 ConsoleMessagesDelegate* delegate, |
| 442 const GURL& current_url) { | 440 const GURL& current_url) { |
| 443 if (!base::IsStringASCII(header)) { | 441 if (!base::IsStringASCII(header)) { |
| 444 delegate->AddMessage(current_url, "Must only contain ASCII characters.", | 442 delegate->AddMessage(current_url, "Must only contain ASCII characters.", |
| 445 CONSOLE_MESSAGE_LEVEL_ERROR); | 443 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 446 return false; | 444 return false; |
| 447 } | 445 } |
| 448 | 446 |
| 449 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(header); | 447 // Wrap |header| in `[` and `]`, then process it as a JSON array: |
|
msramek
2017/06/08 09:42:50
JSONReader is discouraged in browser/, and since t
Mike West
2017/06/12 09:21:50
Done.
| |
| 448 std::string wrapped = "[" + header + "]"; | |
| 449 std::unique_ptr<base::Value> parsed_header = base::JSONReader::Read(wrapped); | |
| 450 | 450 |
| 451 if (!parsed_header) { | 451 if (!parsed_header) { |
| 452 delegate->AddMessage(current_url, "Expected valid JSON.", | 452 delegate->AddMessage(current_url, |
| 453 "The header's value does not parse as valid JSON.", | |
| 453 CONSOLE_MESSAGE_LEVEL_ERROR); | 454 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 454 return false; | 455 return false; |
| 455 } | 456 } |
| 456 | 457 |
| 457 const base::DictionaryValue* dictionary = nullptr; | 458 // Expecting a header in the format `"value1", "value2", "value3"`: |
| 458 const base::ListValue* types = nullptr; | 459 const base::ListValue* types = nullptr; |
| 459 if (!parsed_header->GetAsDictionary(&dictionary) || | 460 DCHECK(parsed_header->GetAsList(&types)); |
| 460 !dictionary->GetListWithoutPathExpansion(kTypesKey, &types)) { | |
| 461 delegate->AddMessage(current_url, | |
| 462 "Expected a JSON dictionary with a 'types' field.", | |
| 463 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 464 return false; | |
| 465 } | |
| 466 | |
| 467 DCHECK(types); | 461 DCHECK(types); |
| 468 | 462 |
| 469 *clear_cookies = false; | 463 *clear_cookies = false; |
| 470 *clear_storage = false; | 464 *clear_storage = false; |
| 471 *clear_cache = false; | 465 *clear_cache = false; |
| 472 | 466 |
| 473 std::string type_names; | 467 std::string type_names; |
| 474 for (const base::Value& value : *types) { | 468 for (const base::Value& value : *types) { |
| 475 std::string type; | 469 std::string type; |
| 476 value.GetAsString(&type); | 470 if (value.is_string() && value.GetAsString(&type)) { |
| 471 bool* data_type = nullptr; | |
| 477 | 472 |
| 478 bool* data_type = nullptr; | 473 if (type == kDatatypeCookies) { |
| 474 data_type = clear_cookies; | |
| 475 } else if (type == kDatatypeStorage) { | |
| 476 data_type = clear_storage; | |
| 477 } else if (type == kDatatypeCache) { | |
| 478 data_type = clear_cache; | |
| 479 } else { | |
| 480 delegate->AddMessage( | |
| 481 current_url, | |
| 482 base::StringPrintf("Unrecognized type: \"%s\".", type.c_str()), | |
| 483 CONSOLE_MESSAGE_LEVEL_ERROR); | |
| 484 continue; | |
| 485 } | |
| 479 | 486 |
| 480 if (type == kDatatypeCookies) { | 487 DCHECK(data_type); |
| 481 data_type = clear_cookies; | 488 |
| 482 } else if (type == kDatatypeStorage) { | 489 // Each data type should only be processed once. |
| 483 data_type = clear_storage; | 490 if (*data_type) |
| 484 } else if (type == kDatatypeCache) { | 491 continue; |
| 485 data_type = clear_cache; | 492 |
| 493 *data_type = true; | |
| 494 if (!type_names.empty()) | |
| 495 type_names += kConsoleMessageDatatypeSeparator; | |
| 496 type_names += type; | |
| 486 } else { | 497 } else { |
| 487 std::string serialized_type; | 498 std::string serialized_type; |
| 488 JSONStringValueSerializer serializer(&serialized_type); | 499 JSONStringValueSerializer serializer(&serialized_type); |
| 489 serializer.Serialize(value); | 500 serializer.Serialize(value); |
| 490 delegate->AddMessage( | 501 delegate->AddMessage( |
| 491 current_url, | 502 current_url, |
| 492 base::StringPrintf("Unrecognized type: %s.", serialized_type.c_str()), | 503 base::StringPrintf("Unrecognized type: %s.", serialized_type.c_str()), |
| 493 CONSOLE_MESSAGE_LEVEL_ERROR); | 504 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 494 continue; | |
| 495 } | 505 } |
| 496 | |
| 497 DCHECK(data_type); | |
| 498 | |
| 499 // Each data type should only be processed once. | |
| 500 if (*data_type) | |
| 501 continue; | |
| 502 | |
| 503 *data_type = true; | |
| 504 if (!type_names.empty()) | |
| 505 type_names += kConsoleMessageDatatypeSeparator; | |
| 506 type_names += type; | |
| 507 } | 506 } |
| 508 | 507 |
| 509 if (!*clear_cookies && !*clear_storage && !*clear_cache) { | 508 if (!*clear_cookies && !*clear_storage && !*clear_cache) { |
| 510 delegate->AddMessage(current_url, | 509 delegate->AddMessage(current_url, "No recognized types specified.", |
| 511 "No recognized types specified in the 'types' field.", | |
| 512 CONSOLE_MESSAGE_LEVEL_ERROR); | 510 CONSOLE_MESSAGE_LEVEL_ERROR); |
| 513 return false; | 511 return false; |
| 514 } | 512 } |
| 515 | 513 |
| 516 // Pretty-print which types are to be cleared. | 514 // Pretty-print which types are to be cleared. |
| 517 delegate->AddMessage( | 515 delegate->AddMessage( |
| 518 current_url, | 516 current_url, |
| 519 base::StringPrintf(kConsoleMessageCleared, type_names.c_str()), | 517 base::StringPrintf(kConsoleMessageCleared, type_names.c_str()), |
| 520 CONSOLE_MESSAGE_LEVEL_INFO); | 518 CONSOLE_MESSAGE_LEVEL_INFO); |
| 521 | 519 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 Resume(); | 551 Resume(); |
| 554 } | 552 } |
| 555 | 553 |
| 556 void ClearSiteDataThrottle::OutputConsoleMessages() { | 554 void ClearSiteDataThrottle::OutputConsoleMessages() { |
| 557 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); | 555 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_); |
| 558 if (info) | 556 if (info) |
| 559 delegate_->OutputMessages(info->GetWebContentsGetterForRequest()); | 557 delegate_->OutputMessages(info->GetWebContentsGetterForRequest()); |
| 560 } | 558 } |
| 561 | 559 |
| 562 } // namespace content | 560 } // namespace content |
| OLD | NEW |