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/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "content/public/browser/utility_process_host_client.h" | |
14 | 13 |
15 namespace base { | 14 namespace base { |
16 class ListValue; | |
17 class SingleThreadTaskRunner; | |
18 class Value; | 15 class Value; |
19 } | 16 } |
20 | 17 |
21 namespace IPC { | |
22 class Message; | |
23 } | |
24 | |
25 namespace safe_json { | 18 namespace safe_json { |
26 | 19 |
27 // SafeJsonParser parses a given JSON safely via a utility process. The object | 20 // SafeJsonParser parses a given JSON safely via a platform-dependent mechanism |
28 // is ref-counted and kept alive after Start() is called until one of the two | 21 // (like parsing it in a utility process or in a memory-safe environment). |
29 // callbacks is called. | 22 // Internally, an instance of this class is created when Parse() is called and |
30 class SafeJsonParser : public content::UtilityProcessHostClient { | 23 // is kept alive until one of the two callbacks is called, after which it |
| 24 // deletes itself. |
| 25 class SafeJsonParser { |
31 public: | 26 public: |
32 typedef base::Callback<void(scoped_ptr<base::Value>)> SuccessCallback; | 27 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>; |
33 typedef base::Callback<void(const std::string&)> ErrorCallback; | 28 using ErrorCallback = base::Callback<void(const std::string&)>; |
34 | 29 |
35 SafeJsonParser(const std::string& unsafe_json, | 30 using Factory = SafeJsonParser* (*)(const std::string& unsafe_json, |
36 const SuccessCallback& success_callback, | 31 const SuccessCallback& success_callback, |
37 const ErrorCallback& error_callback); | 32 const ErrorCallback& error_callback); |
38 | 33 |
39 void Start(); | 34 // Starts parsing the passed in |unsafe_json| and calls either |
| 35 // |success_callback| or |error_callback| when finished. |
| 36 static void Parse(const std::string& unsafe_json, |
| 37 const SuccessCallback& success_callback, |
| 38 const ErrorCallback& error_callback); |
| 39 |
| 40 static void SetFactoryForTesting(Factory factory); |
| 41 |
| 42 protected: |
| 43 virtual ~SafeJsonParser() {} |
40 | 44 |
41 private: | 45 private: |
42 ~SafeJsonParser() override; | 46 virtual void Start() = 0; |
43 | |
44 void StartWorkOnIOThread(); | |
45 | |
46 void OnJSONParseSucceeded(const base::ListValue& wrapper); | |
47 void OnJSONParseFailed(const std::string& error_message); | |
48 | |
49 void ReportResults(); | |
50 void ReportResultsOnOriginThread(); | |
51 | |
52 // Implementing pieces of the UtilityProcessHostClient interface. | |
53 bool OnMessageReceived(const IPC::Message& message) override; | |
54 | |
55 const std::string unsafe_json_; | |
56 SuccessCallback success_callback_; | |
57 ErrorCallback error_callback_; | |
58 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
59 | |
60 scoped_ptr<base::Value> parsed_json_; | |
61 std::string error_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(SafeJsonParser); | |
64 }; | 47 }; |
65 | 48 |
66 } // namespace safe_json | 49 } // namespace safe_json |
67 | 50 |
68 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 51 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ |
OLD | NEW |