Chromium Code Reviews| 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 // 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 SafeJsonParser* Create(const std::string& unsafe_json, |
|
Robert Sesek
2015/07/01 22:19:04
At minium, ditto the comment about ownership here.
Bernhard Bauer
2015/07/02 14:16:01
Done.
I also moved the Impl class to a separate f
| |
| 34 const SuccessCallback& success_callback, | |
|
Robert Sesek
2015/07/01 22:19:04
nit: alignment
Bernhard Bauer
2015/07/02 14:16:01
Done.
| |
| 35 const ErrorCallback& error_callback); | |
| 40 | 36 |
| 41 private: | 37 virtual void Start() = 0; |
| 42 ~SafeJsonParser() override; | |
| 43 | 38 |
| 44 void StartWorkOnIOThread(); | 39 static void SetFactoryForTesting(Factory factory); |
| 45 | 40 |
| 46 void OnJSONParseSucceeded(const base::ListValue& wrapper); | 41 protected: |
| 47 void OnJSONParseFailed(const std::string& error_message); | 42 virtual ~SafeJsonParser() {} |
| 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 }; | 43 }; |
| 65 | 44 |
| 66 } // namespace safe_json | 45 } // namespace safe_json |
| 67 | 46 |
| 68 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ | 47 #endif // COMPONENTS_SAFE_JSON_SAFE_JSON_PARSER_H_ |
| OLD | NEW |