Chromium Code Reviews| Index: components/safe_json/safe_json_parser_impl.cc |
| diff --git a/components/safe_json/safe_json_parser_impl.cc b/components/safe_json/safe_json_parser_impl.cc |
| index 40edbc7e112a1a27d6bf6aaf408f2a8f5069d837..504646d2b7bb7f331012cb3f557458a88a3af272 100644 |
| --- a/components/safe_json/safe_json_parser_impl.cc |
| +++ b/components/safe_json/safe_json_parser_impl.cc |
| @@ -4,23 +4,14 @@ |
| #include "components/safe_json/safe_json_parser_impl.h" |
| -#include <utility> |
| - |
| +#include "base/callback.h" |
| #include "base/sequenced_task_runner.h" |
| -#include "base/strings/utf_string_conversions.h" |
| #include "base/threading/sequenced_task_runner_handle.h" |
| -#include "base/threading/thread_task_runner_handle.h" |
| -#include "base/tuple.h" |
| #include "base/values.h" |
| #include "content/public/browser/browser_thread.h" |
| -#include "content/public/browser/utility_process_host.h" |
| -#include "content/public/common/service_registry.h" |
| #include "grit/components_strings.h" |
| #include "ui/base/l10n/l10n_util.h" |
| -using content::BrowserThread; |
| -using content::UtilityProcessHost; |
| - |
| namespace safe_json { |
| SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, |
| @@ -32,90 +23,81 @@ SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, |
| io_thread_checker_.DetachFromThread(); |
| } |
| -SafeJsonParserImpl::~SafeJsonParserImpl() { |
| +SafeJsonParserImpl::~SafeJsonParserImpl() = default; |
| + |
| +void SafeJsonParserImpl::Start() { |
| + caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&SafeJsonParserImpl::StartOnIOThread, base::Unretained(this))); |
| } |
| -void SafeJsonParserImpl::StartWorkOnIOThread() { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| +void SafeJsonParserImpl::StartOnIOThread() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| - // TODO(amistry): This UtilityProcessHost dance is likely to be done multiple |
| - // times as more tasks are migrated to Mojo. Create some sort of helper class |
| - // to eliminate the code duplication. |
| - utility_process_host_ = UtilityProcessHost::Create( |
| - this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); |
| - utility_process_host_->SetName( |
| - l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); |
| - if (!utility_process_host_->Start()) { |
| - ReportResults(); |
| - return; |
| - } |
| + mojo_json_parser_.reset( |
| + new content::UtilityProcessMojoClient<mojom::SafeJsonParser>( |
| + l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME), |
| + base::Bind(&SafeJsonParserImpl::OnConnectionError, |
| + base::Unretained(this)))); |
| - content::ServiceRegistry* service_registry = |
| - utility_process_host_->GetServiceRegistry(); |
| - service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| - service_.set_connection_error_handler( |
| - base::Bind(&SafeJsonParserImpl::ReportResults, this)); |
| + mojo_json_parser_->Start(); |
| - service_->Parse(unsafe_json_, |
| - base::Bind(&SafeJsonParserImpl::OnParseDone, this)); |
| + mojo_json_parser_->service()->Parse( |
| + std::move(unsafe_json_), |
| + base::Bind(&SafeJsonParserImpl::OnParseDone, base::Unretained(this))); |
| } |
| -void SafeJsonParserImpl::ReportResults() { |
| - // There should be a DCHECK_CURRENTLY_ON(BrowserThread::IO) here. However, if |
| - // the parser process is still alive on shutdown, this might run on the IO |
| - // thread while the IO thread message loop is shutting down. This happens |
| - // after the IO thread has unregistered from the BrowserThread list, causing |
| - // the DCHECK to fail. |
| +void SafeJsonParserImpl::OnConnectionError() { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| - io_thread_checker_.DetachFromThread(); |
| - // Maintain a reference to |this| since either |utility_process_host_| or |
| - // |service_| may have the last reference and destroying those might delete |
| - // |this|. |
| - scoped_refptr<SafeJsonParserImpl> ref(this); |
| - // Shut down the utility process if it's still running. |
| - delete utility_process_host_.get(); |
| - service_.reset(); |
| + // Shut down the utility process. |
| + mojo_json_parser_.reset(); |
| caller_task_runner_->PostTask( |
| - FROM_HERE, |
| - base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); |
| -} |
| - |
| -void SafeJsonParserImpl::ReportResultsOnOriginThread() { |
| - DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
| - if (error_.empty() && parsed_json_) { |
| - if (!success_callback_.is_null()) |
| - success_callback_.Run(std::move(parsed_json_)); |
| - } else { |
| - if (!error_callback_.is_null()) |
| - error_callback_.Run(error_); |
| - } |
| -} |
| - |
| -bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { |
| - return false; |
| + FROM_HERE, base::Bind(&SafeJsonParserImpl::ReportResults, |
| + base::Unretained(this), nullptr, "Json error")); |
| } |
| void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, |
| - mojo::String error) { |
| + const mojo::String& error) { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| + |
| + // Shut down the utility process. |
| + mojo_json_parser_.reset(); |
| + |
| + std::unique_ptr<base::Value> parsed_json; |
| + std::string error_message; |
| if (!wrapper.empty()) { |
| const base::Value* value = NULL; |
| CHECK(wrapper.Get(0, &value)); |
| - parsed_json_.reset(value->DeepCopy()); |
| + parsed_json.reset(value->DeepCopy()); |
| } else { |
| - error_ = error.get(); |
| + error_message = error.get(); |
| } |
| - ReportResults(); |
| + |
| + // Call ReportResults() on caller's thread. |
| + caller_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&SafeJsonParserImpl::ReportResults, base::Unretained(this), |
| + base::Passed(&parsed_json), error_message)); |
| } |
| -void SafeJsonParserImpl::Start() { |
| - caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); |
| +void SafeJsonParserImpl::ReportResults(std::unique_ptr<base::Value> parsed_json, |
| + const std::string& error) { |
| + DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
| + if (error.empty() && parsed_json) { |
| + if (!success_callback_.is_null()) |
| + success_callback_.Run(std::move(parsed_json)); |
| + } else { |
| + if (!error_callback_.is_null()) |
| + error_callback_.Run(error); |
| + } |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); |
| + // The parsing is done whether an error occured or not and this instance can |
|
grt (UTC plus 2)
2016/06/10 19:03:06
language nit: "whether or not an error occurred, s
Patrick Monette
2016/06/10 21:29:46
Done.
|
| + // be cleaned up. |
| + delete this; |
| } |
| } // namespace safe_json |