OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/utility/safe_json_parser_mojo_impl.h" | 5 #include "components/safe_json/utility/safe_json_parser_mojo_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 | 13 |
13 namespace safe_json { | 14 namespace safe_json { |
14 | 15 |
| 16 SafeJsonParserMojoImpl::SafeJsonParserMojoImpl() = default; |
| 17 |
| 18 SafeJsonParserMojoImpl::~SafeJsonParserMojoImpl() = default; |
| 19 |
15 // static | 20 // static |
16 void SafeJsonParserMojoImpl::Create( | 21 void SafeJsonParserMojoImpl::Create( |
17 mojo::InterfaceRequest<mojom::SafeJsonParser> request) { | 22 mojo::InterfaceRequest<mojom::SafeJsonParser> request) { |
18 new SafeJsonParserMojoImpl(std::move(request)); | 23 mojo::MakeStrongBinding(base::MakeUnique<SafeJsonParserMojoImpl>(), |
19 } | 24 std::move(request)); |
20 | |
21 SafeJsonParserMojoImpl::SafeJsonParserMojoImpl( | |
22 mojo::InterfaceRequest<mojom::SafeJsonParser> request) | |
23 : binding_(this, std::move(request)) {} | |
24 | |
25 SafeJsonParserMojoImpl::~SafeJsonParserMojoImpl() { | |
26 } | 25 } |
27 | 26 |
28 void SafeJsonParserMojoImpl::Parse(const mojo::String& json, | 27 void SafeJsonParserMojoImpl::Parse(const mojo::String& json, |
29 const ParseCallback& callback) { | 28 const ParseCallback& callback) { |
30 int error_code; | 29 int error_code; |
31 std::string error; | 30 std::string error; |
32 std::unique_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( | 31 std::unique_ptr<base::Value> value = base::JSONReader::ReadAndReturnError( |
33 json.get(), base::JSON_PARSE_RFC, &error_code, &error); | 32 json.get(), base::JSON_PARSE_RFC, &error_code, &error); |
34 base::ListValue wrapper; | 33 base::ListValue wrapper; |
35 if (value) { | 34 if (value) { |
36 wrapper.Append(std::move(value)); | 35 wrapper.Append(std::move(value)); |
37 callback.Run(wrapper, nullptr); | 36 callback.Run(wrapper, nullptr); |
38 } else { | 37 } else { |
39 callback.Run(wrapper, error); | 38 callback.Run(wrapper, error); |
40 } | 39 } |
41 } | 40 } |
42 | 41 |
43 } // namespace safe_json | 42 } // namespace safe_json |
OLD | NEW |