| Index: components/safe_json/safe_json_parser.cc
|
| diff --git a/components/safe_json/safe_json_parser.cc b/components/safe_json/safe_json_parser.cc
|
| index af30a19f2d7be77f18052233b91acff618c1e833..4d76c6f81b35554f17ab884023d08fe642e87466 100644
|
| --- a/components/safe_json/safe_json_parser.cc
|
| +++ b/components/safe_json/safe_json_parser.cc
|
| @@ -6,7 +6,7 @@
|
|
|
| #include <string>
|
|
|
| -#include "base/single_thread_task_runner.h"
|
| +#include "base/sequenced_task_runner.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "base/thread_task_runner_handle.h"
|
| #include "base/tuple.h"
|
| @@ -14,6 +14,7 @@
|
| #include "components/safe_json/safe_json_parser_messages.h"
|
| #include "content/public/browser/browser_thread.h"
|
| #include "content/public/browser/utility_process_host.h"
|
| +#include "content/public/browser/utility_process_host_client.h"
|
| #include "grit/components_strings.h"
|
| #include "ipc/ipc_message_macros.h"
|
| #include "ui/base/l10n/l10n_util.h"
|
| @@ -23,26 +24,69 @@ using content::UtilityProcessHost;
|
|
|
| namespace safe_json {
|
|
|
| -SafeJsonParser::SafeJsonParser(const std::string& unsafe_json,
|
| - const SuccessCallback& success_callback,
|
| - const ErrorCallback& error_callback)
|
| +namespace {
|
| +
|
| +SafeJsonParser::Factory g_factory = nullptr;
|
| +
|
| +class SafeJsonParserImpl : public content::UtilityProcessHostClient,
|
| + public SafeJsonParser {
|
| + public:
|
| + SafeJsonParserImpl(const std::string& unsafe_json,
|
| + const SuccessCallback& success_callback,
|
| + const ErrorCallback& error_callback);
|
| +
|
| + void Start() override;
|
| +
|
| + private:
|
| + ~SafeJsonParserImpl() override;
|
| +
|
| + void StartWorkOnIOThread();
|
| +
|
| + void OnJSONParseSucceeded(const base::ListValue& wrapper);
|
| + void OnJSONParseFailed(const std::string& error_message);
|
| +
|
| + void ReportResults();
|
| + void ReportResultsOnOriginThread();
|
| +
|
| + // 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::SequencedTaskRunner> caller_task_runner_;
|
| +
|
| + scoped_ptr<base::Value> parsed_json_;
|
| + std::string error_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SafeJsonParserImpl);
|
| +};
|
| +
|
| +SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json,
|
| + const SuccessCallback& success_callback,
|
| + const ErrorCallback& error_callback)
|
| : unsafe_json_(unsafe_json),
|
| success_callback_(success_callback),
|
| - error_callback_(error_callback) {
|
| -}
|
| + error_callback_(error_callback) {}
|
| +
|
| +void SafeJsonParserImpl::Start() {
|
| + // Current callers of this class are known to either come from
|
| + // the UI or IO threads. This DCHECK can be removed/further enhanced
|
| + // in case this class is useable outside of these contexts.
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
|
| + BrowserThread::CurrentlyOn(BrowserThread::IO));
|
|
|
| -void SafeJsonParser::Start() {
|
| caller_task_runner_ = base::ThreadTaskRunnerHandle::Get();
|
|
|
| BrowserThread::PostTask(
|
| BrowserThread::IO, FROM_HERE,
|
| - base::Bind(&SafeJsonParser::StartWorkOnIOThread, this));
|
| + base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this));
|
| }
|
|
|
| -SafeJsonParser::~SafeJsonParser() {
|
| +SafeJsonParserImpl::~SafeJsonParserImpl() {
|
| }
|
|
|
| -void SafeJsonParser::StartWorkOnIOThread() {
|
| +void SafeJsonParserImpl::StartWorkOnIOThread() {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| UtilityProcessHost* host = UtilityProcessHost::Create(
|
| this, base::ThreadTaskRunnerHandle::Get().get());
|
| @@ -51,7 +95,7 @@ void SafeJsonParser::StartWorkOnIOThread() {
|
| host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_));
|
| }
|
|
|
| -void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) {
|
| +void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| const base::Value* value = NULL;
|
| CHECK(wrapper.Get(0, &value));
|
| @@ -60,26 +104,22 @@ void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) {
|
| ReportResults();
|
| }
|
|
|
| -void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) {
|
| +void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| error_ = error_message;
|
| ReportResults();
|
| }
|
|
|
| -void SafeJsonParser::ReportResults() {
|
| +void SafeJsonParserImpl::ReportResults() {
|
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
|
|
| caller_task_runner_->PostTask(
|
| FROM_HERE,
|
| - base::Bind(&SafeJsonParser::ReportResultsOnOriginThread, this));
|
| + base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this));
|
| }
|
|
|
| -void SafeJsonParser::ReportResultsOnOriginThread() {
|
| - // Current callers of this class are known to either come from
|
| - // the UI or IO threads. This DCHECK can be removed/further enhanced
|
| - // in case this class is useable outside of these contexts.
|
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
|
| - BrowserThread::CurrentlyOn(BrowserThread::IO));
|
| +void SafeJsonParserImpl::ReportResultsOnOriginThread() {
|
| + DCHECK(caller_task_runner_->RunsTasksOnCurrentThread());
|
| if (error_.empty() && parsed_json_) {
|
| if (!success_callback_.is_null())
|
| success_callback_.Run(parsed_json_.Pass());
|
| @@ -89,9 +129,9 @@ void SafeJsonParser::ReportResultsOnOriginThread() {
|
| }
|
| }
|
|
|
| -bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) {
|
| +bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) {
|
| bool handled = true;
|
| - IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message)
|
| + IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message)
|
| IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded,
|
| OnJSONParseSucceeded)
|
| IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed,
|
| @@ -101,4 +141,22 @@ bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) {
|
| return handled;
|
| }
|
|
|
| +} // namespace
|
| +
|
| +// static
|
| +void SafeJsonParser::SetFactoryForTesting(Factory factory) {
|
| + g_factory = factory;
|
| +}
|
| +
|
| +// static
|
| +SafeJsonParser* SafeJsonParser::Create(
|
| + const std::string& unsafe_json,
|
| + const SuccessCallback& success_callback,
|
| + const ErrorCallback& error_callback) {
|
| + if (g_factory)
|
| + return g_factory(unsafe_json, success_callback, error_callback);
|
| +
|
| + return new SafeJsonParserImpl(unsafe_json, success_callback, error_callback);
|
| +}
|
| +
|
| } // namespace safe_json
|
|
|