| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/safe_json_parser_impl.h" | 5 #include "components/safe_json/safe_json_parser_impl.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/sequenced_task_runner.h" | 8 #include "base/sequenced_task_runner.h" |
| 9 #include "base/threading/sequenced_task_runner_handle.h" | 9 #include "base/threading/sequenced_task_runner_handle.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 // Shut down the utility process. | 55 // Shut down the utility process. |
| 56 mojo_json_parser_.reset(); | 56 mojo_json_parser_.reset(); |
| 57 | 57 |
| 58 caller_task_runner_->PostTask( | 58 caller_task_runner_->PostTask( |
| 59 FROM_HERE, base::Bind(&SafeJsonParserImpl::ReportResults, | 59 FROM_HERE, base::Bind(&SafeJsonParserImpl::ReportResults, |
| 60 base::Unretained(this), nullptr, "Json error")); | 60 base::Unretained(this), nullptr, "Json error")); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, | 63 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, |
| 64 mojo::String error) { | 64 const base::Optional<std::string>& error) { |
| 65 DCHECK(io_thread_checker_.CalledOnValidThread()); | 65 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 66 | 66 |
| 67 // Shut down the utility process. | 67 // Shut down the utility process. |
| 68 mojo_json_parser_.reset(); | 68 mojo_json_parser_.reset(); |
| 69 | 69 |
| 70 std::unique_ptr<base::Value> parsed_json; | 70 std::unique_ptr<base::Value> parsed_json; |
| 71 std::string error_message; | 71 std::string error_message; |
| 72 if (!wrapper.empty()) { | 72 if (!wrapper.empty()) { |
| 73 const base::Value* value = nullptr; | 73 const base::Value* value = nullptr; |
| 74 CHECK(wrapper.Get(0, &value)); | 74 CHECK(wrapper.Get(0, &value)); |
| 75 parsed_json.reset(value->DeepCopy()); | 75 parsed_json.reset(value->DeepCopy()); |
| 76 } else { | 76 } else if (error.has_value()) { |
| 77 error_message = error.get(); | 77 error_message = error.value(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 // Call ReportResults() on caller's thread. | 80 // Call ReportResults() on caller's thread. |
| 81 caller_task_runner_->PostTask( | 81 caller_task_runner_->PostTask( |
| 82 FROM_HERE, | 82 FROM_HERE, |
| 83 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), | 83 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), |
| 84 base::Passed(&parsed_json), error_message)); | 84 base::Passed(&parsed_json), error_message)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, | 87 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, |
| 88 const std::string& error) { | 88 const std::string& error) { |
| 89 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); | 89 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
| 90 if (error.empty() && parsed_json) { | 90 if (error.empty() && parsed_json) { |
| 91 if (!success_callback_.is_null()) | 91 if (!success_callback_.is_null()) |
| 92 success_callback_.Run(std::move(parsed_json)); | 92 success_callback_.Run(std::move(parsed_json)); |
| 93 } else { | 93 } else { |
| 94 if (!error_callback_.is_null()) | 94 if (!error_callback_.is_null()) |
| 95 error_callback_.Run(error); | 95 error_callback_.Run(error); |
| 96 } | 96 } |
| 97 | 97 |
| 98 // The parsing is done whether an error occured or not, so this instance can | 98 // The parsing is done whether an error occured or not, so this instance can |
| 99 // be cleaned up. | 99 // be cleaned up. |
| 100 delete this; | 100 delete this; |
| 101 } | 101 } |
| 102 | 102 |
| 103 } // namespace safe_json | 103 } // namespace safe_json |
| OLD | NEW |