Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(901)

Unified Diff: components/safe_json/safe_json_parser.h

Issue 1214903010: Make JSONParser a pure interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: review Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/safe_json/safe_json_parser.h
diff --git a/components/safe_json/safe_json_parser.h b/components/safe_json/safe_json_parser.h
index 8fb703b3e9a619f630906418841a8c87bd6d395f..704f0680cb39b2b7edf0311964f4860a31377acc 100644
--- a/components/safe_json/safe_json_parser.h
+++ b/components/safe_json/safe_json_parser.h
@@ -10,57 +10,40 @@
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
-#include "content/public/browser/utility_process_host_client.h"
namespace base {
-class ListValue;
-class SingleThreadTaskRunner;
class Value;
}
-namespace IPC {
-class Message;
-}
-
namespace safe_json {
-// SafeJsonParser parses a given JSON safely via a utility process. The object
-// is ref-counted and kept alive after Start() is called until one of the two
-// callbacks is called.
-class SafeJsonParser : public content::UtilityProcessHostClient {
+// SafeJsonParser parses a given JSON safely via a platform-dependent mechanism
+// (like parsing it in a utility process or in a memory-safe environment).
+// Internally, an instance of this class is created when Parse() is called and
+// is kept alive until one of the two callbacks is called, after which it
+// deletes itself.
+class SafeJsonParser {
public:
- typedef base::Callback<void(scoped_ptr<base::Value>)> SuccessCallback;
- typedef base::Callback<void(const std::string&)> ErrorCallback;
-
- SafeJsonParser(const std::string& unsafe_json,
- const SuccessCallback& success_callback,
- const ErrorCallback& error_callback);
+ using SuccessCallback = base::Callback<void(scoped_ptr<base::Value>)>;
+ using ErrorCallback = base::Callback<void(const std::string&)>;
- void Start();
-
- private:
- ~SafeJsonParser() override;
+ using Factory = SafeJsonParser* (*)(const std::string& unsafe_json,
+ const SuccessCallback& success_callback,
+ const ErrorCallback& error_callback);
- void StartWorkOnIOThread();
+ // Starts parsing the passed in |unsafe_json| and calls either
+ // |success_callback| or |error_callback| when finished.
+ static void Parse(const std::string& unsafe_json,
+ const SuccessCallback& success_callback,
+ const ErrorCallback& error_callback);
- void OnJSONParseSucceeded(const base::ListValue& wrapper);
- void OnJSONParseFailed(const std::string& error_message);
+ static void SetFactoryForTesting(Factory factory);
- void ReportResults();
- void ReportResultsOnOriginThread();
+ protected:
+ virtual ~SafeJsonParser() {}
- // Implementing pieces of the UtilityProcessHostClient interface.
- bool OnMessageReceived(const IPC::Message& message) override;
-
- const std::string unsafe_json_;
- SuccessCallback success_callback_;
- ErrorCallback error_callback_;
- scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
-
- scoped_ptr<base::Value> parsed_json_;
- std::string error_;
-
- DISALLOW_COPY_AND_ASSIGN(SafeJsonParser);
+ private:
+ virtual void Start() = 0;
};
} // namespace safe_json

Powered by Google App Engine
This is Rietveld 408576698