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.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/tuple.h"
13 #include "base/values.h"
14 #include "components/safe_json/safe_json_parser_messages.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "grit/components_strings.h"
18 #include "ipc/ipc_message_macros.h"
19 #include "ui/base/l10n/l10n_util.h"
21 using content::BrowserThread
;
22 using content::UtilityProcessHost
;
26 SafeJsonParser::SafeJsonParser(const std::string
& unsafe_json
,
27 const SuccessCallback
& success_callback
,
28 const ErrorCallback
& error_callback
)
29 : unsafe_json_(unsafe_json
),
30 success_callback_(success_callback
),
31 error_callback_(error_callback
) {
34 void SafeJsonParser::Start() {
35 caller_task_runner_
= base::ThreadTaskRunnerHandle::Get();
37 BrowserThread::PostTask(
38 BrowserThread::IO
, FROM_HERE
,
39 base::Bind(&SafeJsonParser::StartWorkOnIOThread
, this));
42 SafeJsonParser::~SafeJsonParser() {
45 void SafeJsonParser::StartWorkOnIOThread() {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
47 UtilityProcessHost
* host
= UtilityProcessHost::Create(
48 this, base::ThreadTaskRunnerHandle::Get().get());
50 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME
));
51 host
->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_
));
54 void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue
& wrapper
) {
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
56 const base::Value
* value
= NULL
;
57 CHECK(wrapper
.Get(0, &value
));
59 parsed_json_
.reset(value
->DeepCopy());
63 void SafeJsonParser::OnJSONParseFailed(const std::string
& error_message
) {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
65 error_
= error_message
;
69 void SafeJsonParser::ReportResults() {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
72 caller_task_runner_
->PostTask(
74 base::Bind(&SafeJsonParser::ReportResultsOnOriginThread
, this));
77 void SafeJsonParser::ReportResultsOnOriginThread() {
78 // Current callers of this class are known to either come from
79 // the UI or IO threads. This DCHECK can be removed/further enhanced
80 // in case this class is useable outside of these contexts.
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
) ||
82 BrowserThread::CurrentlyOn(BrowserThread::IO
));
83 if (error_
.empty() && parsed_json_
) {
84 if (!success_callback_
.is_null())
85 success_callback_
.Run(parsed_json_
.Pass());
87 if (!error_callback_
.is_null())
88 error_callback_
.Run(error_
);
92 bool SafeJsonParser::OnMessageReceived(const IPC::Message
& message
) {
94 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser
, message
)
95 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded
,
97 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed
,
99 IPC_MESSAGE_UNHANDLED(handled
= false)
100 IPC_END_MESSAGE_MAP()
104 } // namespace safe_json