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_H_ | 5 #ifndef COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ |
6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/callback.h" | |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "components/safe_json/safe_json_parser.h" |
13 #include "content/public/browser/utility_process_host_client.h" | 13 #include "content/public/browser/utility_process_host_client.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 class ListValue; | 16 class ListValue; |
17 class SingleThreadTaskRunner; | 17 class SequencedTaskRunner; |
18 class Value; | 18 class Value; |
19 } | 19 } |
20 | 20 |
21 namespace IPC { | 21 namespace IPC { |
22 class Message; | 22 class Message; |
23 } | 23 } |
24 | 24 |
25 namespace safe_json { | 25 namespace safe_json { |
26 | 26 |
27 // SafeJsonParser parses a given JSON safely via a utility process. The object | 27 class SafeJsonParserImpl : public content::UtilityProcessHostClient, |
28 // is ref-counted and kept alive after Start() is called until one of the two | 28 public SafeJsonParser { |
29 // callbacks is called. | |
30 class SafeJsonParser : public content::UtilityProcessHostClient { | |
31 public: | 29 public: |
32 typedef base::Callback<void(scoped_ptr<base::Value>)> SuccessCallback; | 30 SafeJsonParserImpl(const std::string& unsafe_json, |
33 typedef base::Callback<void(const std::string&)> ErrorCallback; | 31 const SuccessCallback& success_callback, |
34 | 32 const ErrorCallback& error_callback); |
35 SafeJsonParser(const std::string& unsafe_json, | |
36 const SuccessCallback& success_callback, | |
37 const ErrorCallback& error_callback); | |
38 | |
39 void Start(); | |
40 | 33 |
41 private: | 34 private: |
42 ~SafeJsonParser() override; | 35 ~SafeJsonParserImpl() override; |
43 | 36 |
44 void StartWorkOnIOThread(); | 37 void StartWorkOnIOThread(); |
45 | 38 |
46 void OnJSONParseSucceeded(const base::ListValue& wrapper); | 39 void OnJSONParseSucceeded(const base::ListValue& wrapper); |
47 void OnJSONParseFailed(const std::string& error_message); | 40 void OnJSONParseFailed(const std::string& error_message); |
48 | 41 |
49 void ReportResults(); | 42 void ReportResults(); |
50 void ReportResultsOnOriginThread(); | 43 void ReportResultsOnOriginThread(); |
51 | 44 |
52 // Implementing pieces of the UtilityProcessHostClient interface. | 45 // Implementing pieces of the UtilityProcessHostClient interface. |
53 bool OnMessageReceived(const IPC::Message& message) override; | 46 bool OnMessageReceived(const IPC::Message& message) override; |
54 | 47 |
| 48 // SafeJsonParser implementation. |
| 49 void Start() override; |
| 50 |
55 const std::string unsafe_json_; | 51 const std::string unsafe_json_; |
56 SuccessCallback success_callback_; | 52 SuccessCallback success_callback_; |
57 ErrorCallback error_callback_; | 53 ErrorCallback error_callback_; |
58 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | 54 scoped_refptr<base::SequencedTaskRunner> caller_task_runner_; |
59 | 55 |
60 scoped_ptr<base::Value> parsed_json_; | 56 scoped_ptr<base::Value> parsed_json_; |
61 std::string error_; | 57 std::string error_; |
62 | 58 |
63 DISALLOW_COPY_AND_ASSIGN(SafeJsonParser); | 59 DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl); |
64 }; | 60 }; |
65 | 61 |
66 } // namespace safe_json | 62 } // namespace safe_json |
67 | 63 |
68 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 64 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ |
OLD | NEW |