Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / safe_json / safe_json_parser_impl.cc
blob0b4823e43b883d686650e034e7c2e90d77a37c22
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
3 // found in the LICENSE file.
5 #include "components/safe_json/safe_json_parser_impl.h"
7 #include "base/sequenced_task_runner.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "base/tuple.h"
11 #include "base/values.h"
12 #include "components/safe_json/safe_json_parser_messages.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/utility_process_host.h"
15 #include "grit/components_strings.h"
16 #include "ipc/ipc_message_macros.h"
17 #include "ui/base/l10n/l10n_util.h"
19 using content::BrowserThread;
20 using content::UtilityProcessHost;
22 namespace safe_json {
24 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json,
25 const SuccessCallback& success_callback,
26 const ErrorCallback& error_callback)
27 : unsafe_json_(unsafe_json),
28 success_callback_(success_callback),
29 error_callback_(error_callback) {}
31 SafeJsonParserImpl::~SafeJsonParserImpl() {
34 void SafeJsonParserImpl::StartWorkOnIOThread() {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
36 UtilityProcessHost* host = UtilityProcessHost::Create(
37 this, base::ThreadTaskRunnerHandle::Get().get());
38 host->SetName(
39 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME));
40 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_));
43 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 const base::Value* value = NULL;
46 CHECK(wrapper.Get(0, &value));
48 parsed_json_.reset(value->DeepCopy());
49 ReportResults();
52 void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 error_ = error_message;
55 ReportResults();
58 void SafeJsonParserImpl::ReportResults() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
61 caller_task_runner_->PostTask(
62 FROM_HERE,
63 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this));
66 void SafeJsonParserImpl::ReportResultsOnOriginThread() {
67 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread());
68 if (error_.empty() && parsed_json_) {
69 if (!success_callback_.is_null())
70 success_callback_.Run(parsed_json_.Pass());
71 } else {
72 if (!error_callback_.is_null())
73 error_callback_.Run(error_);
77 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) {
78 bool handled = true;
79 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message)
80 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded,
81 OnJSONParseSucceeded)
82 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed,
83 OnJSONParseFailed)
84 IPC_MESSAGE_UNHANDLED(handled = false)
85 IPC_END_MESSAGE_MAP()
86 return handled;
89 void SafeJsonParserImpl::Start() {
90 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get();
92 BrowserThread::PostTask(
93 BrowserThread::IO, FROM_HERE,
94 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this));
97 } // namespace safe_json