[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / components / safe_json_parser / safe_json_parser.cc
blob3f6444ff128dccc9df8ad3da3431a2e2e8489313
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_parser/safe_json_parser.h"
7 #include <string>
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/tuple.h"
12 #include "base/values.h"
13 #include "components/safe_json_parser/safe_json_parser_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/utility_process_host.h"
16 #include "grit/components_strings.h"
17 #include "ipc/ipc_message_macros.h"
18 #include "ui/base/l10n/l10n_util.h"
20 using content::BrowserThread;
21 using content::UtilityProcessHost;
23 namespace safe_json_parser {
25 SafeJsonParser::SafeJsonParser(const std::string& unsafe_json,
26 const SuccessCallback& success_callback,
27 const ErrorCallback& error_callback)
28 : unsafe_json_(unsafe_json),
29 success_callback_(success_callback),
30 error_callback_(error_callback) {
33 void SafeJsonParser::Start() {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 BrowserThread::PostTask(
36 BrowserThread::IO, FROM_HERE,
37 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this));
40 SafeJsonParser::~SafeJsonParser() {
43 void SafeJsonParser::StartWorkOnIOThread() {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 UtilityProcessHost* host = UtilityProcessHost::Create(
46 this, base::ThreadTaskRunnerHandle::Get().get());
47 host->SetName(
48 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME));
49 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_));
52 void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 const base::Value* value = NULL;
55 CHECK(wrapper.Get(0, &value));
57 parsed_json_.reset(value->DeepCopy());
58 ReportResults();
61 void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
63 error_ = error_message;
64 ReportResults();
67 void SafeJsonParser::ReportResults() {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
70 BrowserThread::PostTask(
71 BrowserThread::UI, FROM_HERE,
72 base::Bind(&SafeJsonParser::ReportResultOnUIThread, this));
75 void SafeJsonParser::ReportResultOnUIThread() {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 if (error_.empty() && parsed_json_) {
78 if (!success_callback_.is_null())
79 success_callback_.Run(parsed_json_.Pass());
80 } else {
81 if (!error_callback_.is_null())
82 error_callback_.Run(error_);
86 bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) {
87 bool handled = true;
88 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message)
89 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded,
90 OnJSONParseSucceeded)
91 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed,
92 OnJSONParseFailed)
93 IPC_MESSAGE_UNHANDLED(handled = false)
94 IPC_END_MESSAGE_MAP()
95 return handled;
98 } // namespace safe_json_parser