Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/safe_json/safe_json_parser.h" | 5 #include "components/safe_json/safe_json_parser.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include "components/safe_json/safe_json_parser_impl.h" |
| 8 | |
| 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" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 using content::UtilityProcessHost; | |
| 23 | 8 |
| 24 namespace safe_json { | 9 namespace safe_json { |
| 25 | 10 |
| 26 SafeJsonParser::SafeJsonParser(const std::string& unsafe_json, | 11 namespace { |
| 27 const SuccessCallback& success_callback, | 12 |
| 28 const ErrorCallback& error_callback) | 13 SafeJsonParser::Factory g_factory = nullptr; |
| 29 : unsafe_json_(unsafe_json), | 14 |
| 30 success_callback_(success_callback), | 15 } // namespace |
| 31 error_callback_(error_callback) { | 16 |
| 17 // static | |
| 18 void SafeJsonParser::SetFactoryForTesting(Factory factory) { | |
| 19 g_factory = factory; | |
| 32 } | 20 } |
| 33 | 21 |
| 34 void SafeJsonParser::Start() { | 22 // static |
| 35 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 23 void SafeJsonParser::Parse(const std::string& unsafe_json, |
| 36 | 24 const SuccessCallback& success_callback, |
| 37 BrowserThread::PostTask( | 25 const ErrorCallback& error_callback) { |
| 38 BrowserThread::IO, FROM_HERE, | 26 SafeJsonParser* parser = |
| 39 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this)); | 27 g_factory ? g_factory(unsafe_json, success_callback, error_callback) |
| 40 } | 28 : new SafeJsonParserImpl(unsafe_json, success_callback, |
|
Robert Sesek
2015/07/02 17:28:19
Is this going to get released propery? Callers put
Bernhard Bauer
2015/07/02 22:38:23
SafeJsonParserImpl is still refcounted, by virtue
Robert Sesek
2015/07/06 17:31:35
Ah, right. Our C++ RefCounted impl starts the ref-
| |
| 41 | 29 error_callback); |
| 42 SafeJsonParser::~SafeJsonParser() { | 30 parser->Start(); |
| 43 } | |
| 44 | |
| 45 void SafeJsonParser::StartWorkOnIOThread() { | |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 47 UtilityProcessHost* host = UtilityProcessHost::Create( | |
| 48 this, base::ThreadTaskRunnerHandle::Get().get()); | |
| 49 host->SetName( | |
| 50 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | |
| 51 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); | |
| 52 } | |
| 53 | |
| 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)); | |
| 58 | |
| 59 parsed_json_.reset(value->DeepCopy()); | |
| 60 ReportResults(); | |
| 61 } | |
| 62 | |
| 63 void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 65 error_ = error_message; | |
| 66 ReportResults(); | |
| 67 } | |
| 68 | |
| 69 void SafeJsonParser::ReportResults() { | |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 71 | |
| 72 caller_task_runner_->PostTask( | |
| 73 FROM_HERE, | |
| 74 base::Bind(&SafeJsonParser::ReportResultsOnOriginThread, this)); | |
| 75 } | |
| 76 | |
| 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()); | |
| 86 } else { | |
| 87 if (!error_callback_.is_null()) | |
| 88 error_callback_.Run(error_); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) { | |
| 93 bool handled = true; | |
| 94 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message) | |
| 95 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, | |
| 96 OnJSONParseSucceeded) | |
| 97 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, | |
| 98 OnJSONParseFailed) | |
| 99 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 100 IPC_END_MESSAGE_MAP() | |
| 101 return handled; | |
| 102 } | 31 } |
| 103 | 32 |
| 104 } // namespace safe_json | 33 } // namespace safe_json |
| OLD | NEW |