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
;
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());
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());
52 void SafeJsonParserImpl::OnJSONParseFailed(const std::string
& error_message
) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
54 error_
= error_message
;
58 void SafeJsonParserImpl::ReportResults() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
61 caller_task_runner_
->PostTask(
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());
72 if (!error_callback_
.is_null())
73 error_callback_
.Run(error_
);
77 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message
& message
) {
79 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl
, message
)
80 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded
,
82 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed
,
84 IPC_MESSAGE_UNHANDLED(handled
= false)
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