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(std::unique_ptr<base::Value> result, | 64 void SafeJsonParserImpl::OnParseDone(const base::Optional<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 // Call ReportResults() on caller's thread. | 71 // Call ReportResults() on caller's thread. |
72 caller_task_runner_->PostTask( | 72 caller_task_runner_->PostTask( |
73 FROM_HERE, | 73 FROM_HERE, |
74 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), | 74 base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), |
75 base::Passed(&result), error.value_or(""))); | 75 base::Passed(result ? result->CreateDeepCopy() : nullptr), |
| 76 error.value_or(""))); |
76 } | 77 } |
77 | 78 |
78 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, | 79 void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, |
79 const std::string& error) { | 80 const std::string& error) { |
80 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); | 81 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
81 if (error.empty() && parsed_json) { | 82 if (error.empty() && parsed_json) { |
82 if (!success_callback_.is_null()) | 83 if (!success_callback_.is_null()) |
83 success_callback_.Run(std::move(parsed_json)); | 84 success_callback_.Run(std::move(parsed_json)); |
84 } else { | 85 } else { |
85 if (!error_callback_.is_null()) | 86 if (!error_callback_.is_null()) |
86 error_callback_.Run(error); | 87 error_callback_.Run(error); |
87 } | 88 } |
88 | 89 |
89 // The parsing is done whether an error occured or not, so this instance can | 90 // The parsing is done whether an error occured or not, so this instance can |
90 // be cleaned up. | 91 // be cleaned up. |
91 delete this; | 92 delete this; |
92 } | 93 } |
93 | 94 |
94 } // namespace safe_json | 95 } // namespace safe_json |
OLD | NEW |