OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/safe_json/safe_json_parser_android.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/values.h" |
| 10 #include "components/safe_json/json_sanitizer.h" |
| 11 |
| 12 namespace safe_json { |
| 13 |
| 14 SafeJsonParserAndroid::SafeJsonParserAndroid( |
| 15 const std::string& unsafe_json, |
| 16 const SuccessCallback& success_callback, |
| 17 const ErrorCallback& error_callback) |
| 18 : unsafe_json_(unsafe_json), |
| 19 success_callback_(success_callback), |
| 20 error_callback_(error_callback) {} |
| 21 |
| 22 SafeJsonParserAndroid::~SafeJsonParserAndroid() {} |
| 23 |
| 24 void SafeJsonParserAndroid::Start() { |
| 25 JsonSanitizer::Sanitize( |
| 26 unsafe_json_, |
| 27 base::Bind(&SafeJsonParserAndroid::OnSanitizationSuccess, |
| 28 base::Unretained(this)), |
| 29 base::Bind(&SafeJsonParserAndroid::OnSanitizationError, |
| 30 base::Unretained(this))); |
| 31 } |
| 32 |
| 33 void SafeJsonParserAndroid::OnSanitizationSuccess( |
| 34 const std::string& sanitized_json) { |
| 35 // Self-destruct at the end of this method. |
| 36 scoped_ptr<SafeJsonParserAndroid> deleter(this); |
| 37 |
| 38 int error_code; |
| 39 std::string error; |
| 40 scoped_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( |
| 41 sanitized_json, base::JSON_PARSE_RFC, &error_code, &error); |
| 42 |
| 43 if (!value) { |
| 44 error_callback_.Run(error); |
| 45 return; |
| 46 } |
| 47 |
| 48 success_callback_.Run(value.Pass()); |
| 49 } |
| 50 |
| 51 void SafeJsonParserAndroid::OnSanitizationError(const std::string& error) { |
| 52 error_callback_.Run(error); |
| 53 delete this; |
| 54 } |
| 55 |
| 56 } // namespace safe_json |
OLD | NEW |