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, | 59 FROM_HERE, |
60 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), | 60 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), |
61 nullptr, "Connection error with the json parser process.")); | 61 nullptr, "Connection error with the json parser process.")); |
62 } | 62 } |
63 | 63 |
64 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, | 64 void SafeJsonParserImpl::OnParseDone(std::unique_ptr<base::Value> result, |
65 const base::Optional<std::string>& error) { | 65 const base::Optional<std::string>& error) { |
66 DCHECK(io_thread_checker_.CalledOnValidThread()); | 66 DCHECK(io_thread_checker_.CalledOnValidThread()); |
67 | 67 |
68 // Shut down the utility process. | 68 // Shut down the utility process. |
69 mojo_json_parser_.reset(); | 69 mojo_json_parser_.reset(); |
70 | 70 |
71 std::unique_ptr<base::Value> parsed_json; | |
72 std::string error_message; | |
73 if (!wrapper.empty()) { | |
74 const base::Value* value = nullptr; | |
75 CHECK(wrapper.Get(0, &value)); | |
76 parsed_json.reset(value->DeepCopy()); | |
77 } else if (error.has_value()) { | |
78 error_message = error.value(); | |
79 } | |
80 | |
81 // Call ReportResults() on caller's thread. | 71 // Call ReportResults() on caller's thread. |
82 caller_task_runner_->PostTask( | 72 caller_task_runner_->PostTask( |
83 FROM_HERE, | 73 FROM_HERE, |
84 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), | 74 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), |
85 base::Passed(&parsed_json), error_message)); | 75 base::Passed(&result), error.value_or(""))); |
86 } | 76 } |
87 | 77 |
88 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, | 78 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, |
89 const std::string& error) { | 79 const std::string& error) { |
90 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); | 80 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
91 if (error.empty() && parsed_json) { | 81 if (error.empty() && parsed_json) { |
92 if (!success_callback_.is_null()) | 82 if (!success_callback_.is_null()) |
93 success_callback_.Run(std::move(parsed_json)); | 83 success_callback_.Run(std::move(parsed_json)); |
94 } else { | 84 } else { |
95 if (!error_callback_.is_null()) | 85 if (!error_callback_.is_null()) |
96 error_callback_.Run(error); | 86 error_callback_.Run(error); |
97 } | 87 } |
98 | 88 |
99 // The parsing is done whether an error occured or not, so this instance can | 89 // The parsing is done whether an error occured or not, so this instance can |
100 // be cleaned up. | 90 // be cleaned up. |
101 delete this; | 91 delete this; |
102 } | 92 } |
103 | 93 |
104 } // namespace safe_json | 94 } // namespace safe_json |
OLD | NEW |