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 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 5 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "components/safe_json/public/interfaces/safe_json.mojom.h" | 15 #include "components/safe_json/public/interfaces/safe_json.mojom.h" |
16 #include "components/safe_json/safe_json_parser.h" | 16 #include "components/safe_json/safe_json_parser.h" |
17 #include "content/public/browser/utility_process_mojo_client.h" | 17 #include "content/public/browser/utility_process_mojo_client.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class ListValue; | |
21 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
22 class Value; | 21 class Value; |
23 } | 22 } |
24 | 23 |
25 namespace safe_json { | 24 namespace safe_json { |
26 | 25 |
27 class SafeJsonParserImpl : public SafeJsonParser { | 26 class SafeJsonParserImpl : public SafeJsonParser { |
28 public: | 27 public: |
29 SafeJsonParserImpl(const std::string& unsafe_json, | 28 SafeJsonParserImpl(const std::string& unsafe_json, |
30 const SuccessCallback& success_callback, | 29 const SuccessCallback& success_callback, |
31 const ErrorCallback& error_callback); | 30 const ErrorCallback& error_callback); |
32 | 31 |
33 private: | 32 private: |
34 ~SafeJsonParserImpl() override; | 33 ~SafeJsonParserImpl() override; |
35 | 34 |
36 // SafeJsonParser implementation. | 35 // SafeJsonParser implementation. |
37 void Start() override; | 36 void Start() override; |
38 | 37 |
39 void StartOnIOThread(); | 38 void StartOnIOThread(); |
40 void OnConnectionError(); | 39 void OnConnectionError(); |
41 | 40 |
42 // mojom::SafeJsonParser::Parse callback. | 41 // mojom::SafeJsonParser::Parse callback. |
43 void OnParseDone(const base::ListValue& wrapper, | 42 void OnParseDone(std::unique_ptr<base::Value> result, |
44 const base::Optional<std::string>& error); | 43 const base::Optional<std::string>& error); |
45 | 44 |
46 // Reports the result on the calling task runner via the |success_callback_| | 45 // Reports the result on the calling task runner via the |success_callback_| |
47 // or the |error_callback_|. | 46 // or the |error_callback_|. |
48 void ReportResults(std::unique_ptr<base::Value> parsed_json, | 47 void ReportResults(std::unique_ptr<base::Value> parsed_json, |
49 const std::string& error); | 48 const std::string& error); |
50 | 49 |
51 const std::string unsafe_json_; | 50 const std::string unsafe_json_; |
52 SuccessCallback success_callback_; | 51 SuccessCallback success_callback_; |
53 ErrorCallback error_callback_; | 52 ErrorCallback error_callback_; |
54 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; | 53 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; |
55 | 54 |
56 std::unique_ptr<content::UtilityProcessMojoClient<mojom::SafeJsonParser>> | 55 std::unique_ptr<content::UtilityProcessMojoClient<mojom::SafeJsonParser>> |
57 mojo_json_parser_; | 56 mojo_json_parser_; |
58 | 57 |
59 // Used instead of DCHECK_CURRENTLY_ON(BrowserThread::IO) because it's | 58 // Used instead of DCHECK_CURRENTLY_ON(BrowserThread::IO) because it's |
60 // posssible that it fails when the IO thread message loop is shutting down. | 59 // posssible that it fails when the IO thread message loop is shutting down. |
61 // This happens after the IO thread has unregistered from the BrowserThread | 60 // This happens after the IO thread has unregistered from the BrowserThread |
62 // list. | 61 // list. |
63 base::ThreadChecker io_thread_checker_; | 62 base::ThreadChecker io_thread_checker_; |
64 | 63 |
65 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); | 64 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); |
66 }; | 65 }; |
67 | 66 |
68 } // namespace safe_json | 67 } // namespace safe_json |
69 | 68 |
70 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ | 69 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
OLD | NEW |