OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/safe_json/json_sanitizer.h" | 5 #include "components/safe_json/json_sanitizer.h" |
6 | 6 |
7 #if defined(OS_ANDROID) | 7 #if defined(OS_ANDROID) |
8 #error Build json_sanitizer_android.cc instead of this file on Android. | 8 #error Build json_sanitizer_android.cc instead of this file on Android. |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 base::Unretained(this))); | 54 base::Unretained(this))); |
55 } | 55 } |
56 | 56 |
57 void OopJsonSanitizer::OnParseSuccess(std::unique_ptr<base::Value> value) { | 57 void OopJsonSanitizer::OnParseSuccess(std::unique_ptr<base::Value> value) { |
58 // Self-destruct at the end of this method. | 58 // Self-destruct at the end of this method. |
59 std::unique_ptr<OopJsonSanitizer> deleter(this); | 59 std::unique_ptr<OopJsonSanitizer> deleter(this); |
60 | 60 |
61 // A valid JSON document may only have a dictionary or list as its top-level | 61 // A valid JSON document may only have a dictionary or list as its top-level |
62 // type, but the JSON parser also accepts other types, so we filter them out. | 62 // type, but the JSON parser also accepts other types, so we filter them out. |
63 base::Value::Type type = value->GetType(); | 63 base::Value::Type type = value->GetType(); |
64 if (type != base::Value::TYPE_DICTIONARY && type != base::Value::TYPE_LIST) { | 64 if (type != base::Value::Type::DICTIONARY && |
| 65 type != base::Value::Type::LIST) { |
65 error_callback_.Run("Invalid top-level type"); | 66 error_callback_.Run("Invalid top-level type"); |
66 return; | 67 return; |
67 } | 68 } |
68 | 69 |
69 std::string json; | 70 std::string json; |
70 if (!base::JSONWriter::Write(*value, &json)) { | 71 if (!base::JSONWriter::Write(*value, &json)) { |
71 error_callback_.Run("Encoding error"); | 72 error_callback_.Run("Encoding error"); |
72 return; | 73 return; |
73 } | 74 } |
74 | 75 |
75 success_callback_.Run(json); | 76 success_callback_.Run(json); |
76 } | 77 } |
77 | 78 |
78 void OopJsonSanitizer::OnParseError(const std::string& error) { | 79 void OopJsonSanitizer::OnParseError(const std::string& error) { |
79 error_callback_.Run("Parse error: " + error); | 80 error_callback_.Run("Parse error: " + error); |
80 delete this; | 81 delete this; |
81 } | 82 } |
82 | 83 |
83 } // namespace | 84 } // namespace |
84 | 85 |
85 // static | 86 // static |
86 void JsonSanitizer::Sanitize(const std::string& unsafe_json, | 87 void JsonSanitizer::Sanitize(const std::string& unsafe_json, |
87 const StringCallback& success_callback, | 88 const StringCallback& success_callback, |
88 const StringCallback& error_callback) { | 89 const StringCallback& error_callback) { |
89 // OopJsonSanitizer destroys itself when it is finished. | 90 // OopJsonSanitizer destroys itself when it is finished. |
90 new OopJsonSanitizer(unsafe_json, success_callback, error_callback); | 91 new OopJsonSanitizer(unsafe_json, success_callback, error_callback); |
91 } | 92 } |
92 | 93 |
93 } // namespace safe_json | 94 } // namespace safe_json |
OLD | NEW |