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_IMPL_H_ |
Robert Sesek
2015/07/02 17:28:19
The include guards are swapped for this file and t
Bernhard Bauer
2015/07/02 22:38:23
D'oh! Fixed.
| |
6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 6 #define COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_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 // The object is kept alive after Start() is called until one of the two |
30 class SafeJsonParser : public content::UtilityProcessHostClient { | 23 // callbacks is called, after which it will delete itself. |
24 class SafeJsonParser { | |
31 public: | 25 public: |
32 typedef base::Callback<void(scoped_ptr<base::Value>)> SuccessCallback; | 26 using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>; |
33 typedef base::Callback<void(const std::string&)> ErrorCallback; | 27 using ErrorCallback = base::Callback<void(const std::string&)>; |
34 | 28 |
35 SafeJsonParser(const std::string& unsafe_json, | 29 using Factory = SafeJsonParser* (*)(const std::string& unsafe_json, |
36 const SuccessCallback& success_callback, | 30 const SuccessCallback& success_callback, |
37 const ErrorCallback& error_callback); | 31 const ErrorCallback& error_callback); |
38 | 32 |
39 void Start(); | 33 static void Parse(const std::string& unsafe_json, |
Robert Sesek
2015/07/02 17:28:19
Since this is the only public method, it should ha
Bernhard Bauer
2015/07/02 22:38:24
Done.
| |
34 const SuccessCallback& success_callback, | |
35 const ErrorCallback& error_callback); | |
36 | |
37 static void SetFactoryForTesting(Factory factory); | |
38 | |
39 protected: | |
40 virtual ~SafeJsonParser() {} | |
40 | 41 |
41 private: | 42 private: |
42 ~SafeJsonParser() override; | 43 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 }; | 44 }; |
65 | 45 |
66 } // namespace safe_json | 46 } // namespace safe_json |
67 | 47 |
68 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 48 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_IMPL_H_ |
OLD | NEW |