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_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include "base/sequenced_task_runner.h" |
| 8 | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/tuple.h" | 10 #include "base/tuple.h" |
| 13 #include "base/values.h" | 11 #include "base/values.h" |
| 14 #include "components/safe_json/safe_json_parser_messages.h" | 12 #include "components/safe_json/safe_json_parser_messages.h" |
| 15 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/utility_process_host.h" | 14 #include "content/public/browser/utility_process_host.h" |
| 17 #include "grit/components_strings.h" | 15 #include "grit/components_strings.h" |
| 18 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 20 | 18 |
| 21 using content::BrowserThread; | 19 using content::BrowserThread; |
| 22 using content::UtilityProcessHost; | 20 using content::UtilityProcessHost; |
| 23 | 21 |
| 24 namespace safe_json { | 22 namespace safe_json { |
| 25 | 23 |
| 26 SafeJsonParser::SafeJsonParser(const std::string& unsafe_json, | 24 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, |
| 27 const SuccessCallback& success_callback, | 25 const SuccessCallback& success_callback, |
| 28 const ErrorCallback& error_callback) | 26 const ErrorCallback& error_callback) |
| 29 : unsafe_json_(unsafe_json), | 27 : unsafe_json_(unsafe_json), |
| 30 success_callback_(success_callback), | 28 success_callback_(success_callback), |
| 31 error_callback_(error_callback) { | 29 error_callback_(error_callback) {} |
| 30 | |
| 31 SafeJsonParserImpl::~SafeJsonParserImpl() { | |
| 32 } | 32 } |
| 33 | 33 |
| 34 void SafeJsonParser::Start() { | 34 void SafeJsonParserImpl::StartWorkOnIOThread() { |
| 35 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 36 | |
| 37 BrowserThread::PostTask( | |
| 38 BrowserThread::IO, FROM_HERE, | |
| 39 base::Bind(&SafeJsonParser::StartWorkOnIOThread, this)); | |
| 40 } | |
| 41 | |
| 42 SafeJsonParser::~SafeJsonParser() { | |
| 43 } | |
| 44 | |
| 45 void SafeJsonParser::StartWorkOnIOThread() { | |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 47 UtilityProcessHost* host = UtilityProcessHost::Create( | 36 UtilityProcessHost* host = UtilityProcessHost::Create( |
| 48 this, base::ThreadTaskRunnerHandle::Get().get()); | 37 this, base::ThreadTaskRunnerHandle::Get().get()); |
| 49 host->SetName( | 38 host->SetName( |
| 50 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | 39 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); |
| 51 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); | 40 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); |
| 52 } | 41 } |
| 53 | 42 |
| 54 void SafeJsonParser::OnJSONParseSucceeded(const base::ListValue& wrapper) { | 43 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 56 const base::Value* value = NULL; | 45 const base::Value* value = NULL; |
| 57 CHECK(wrapper.Get(0, &value)); | 46 CHECK(wrapper.Get(0, &value)); |
| 58 | 47 |
| 59 parsed_json_.reset(value->DeepCopy()); | 48 parsed_json_.reset(value->DeepCopy()); |
| 60 ReportResults(); | 49 ReportResults(); |
| 61 } | 50 } |
| 62 | 51 |
| 63 void SafeJsonParser::OnJSONParseFailed(const std::string& error_message) { | 52 void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 65 error_ = error_message; | 54 error_ = error_message; |
| 66 ReportResults(); | 55 ReportResults(); |
| 67 } | 56 } |
| 68 | 57 |
| 69 void SafeJsonParser::ReportResults() { | 58 void SafeJsonParserImpl::ReportResults() { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 71 | 60 |
| 72 caller_task_runner_->PostTask( | 61 caller_task_runner_->PostTask( |
| 73 FROM_HERE, | 62 FROM_HERE, |
| 74 base::Bind(&SafeJsonParser::ReportResultsOnOriginThread, this)); | 63 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); |
| 75 } | 64 } |
| 76 | 65 |
| 77 void SafeJsonParser::ReportResultsOnOriginThread() { | 66 void SafeJsonParserImpl::ReportResultsOnOriginThread() { |
| 78 // Current callers of this class are known to either come from | 67 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
| 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_) { | 68 if (error_.empty() && parsed_json_) { |
| 84 if (!success_callback_.is_null()) | 69 if (!success_callback_.is_null()) |
| 85 success_callback_.Run(parsed_json_.Pass()); | 70 success_callback_.Run(parsed_json_.Pass()); |
| 86 } else { | 71 } else { |
| 87 if (!error_callback_.is_null()) | 72 if (!error_callback_.is_null()) |
| 88 error_callback_.Run(error_); | 73 error_callback_.Run(error_); |
| 89 } | 74 } |
| 90 } | 75 } |
| 91 | 76 |
| 92 bool SafeJsonParser::OnMessageReceived(const IPC::Message& message) { | 77 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { |
| 93 bool handled = true; | 78 bool handled = true; |
| 94 IPC_BEGIN_MESSAGE_MAP(SafeJsonParser, message) | 79 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message) |
| 95 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, | 80 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, |
| 96 OnJSONParseSucceeded) | 81 OnJSONParseSucceeded) |
| 97 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, | 82 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, |
| 98 OnJSONParseFailed) | 83 OnJSONParseFailed) |
| 99 IPC_MESSAGE_UNHANDLED(handled = false) | 84 IPC_MESSAGE_UNHANDLED(handled = false) |
| 100 IPC_END_MESSAGE_MAP() | 85 IPC_END_MESSAGE_MAP() |
| 101 return handled; | 86 return handled; |
| 102 } | 87 } |
| 103 | 88 |
| 89 void SafeJsonParserImpl::Start() { | |
| 90 // Current callers of this class are known to either come from | |
| 91 // the UI or IO threads. This DCHECK can be removed/further enhanced | |
| 92 // in case this class is useable outside of these contexts. | |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
|
Robert Sesek
2015/07/02 17:28:19
I don't know if this DCHECK is really that useful.
Bernhard Bauer
2015/07/02 22:38:24
True. Removed.
| |
| 94 BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 95 | |
| 96 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 97 | |
| 98 BrowserThread::PostTask( | |
| 99 BrowserThread::IO, FROM_HERE, | |
| 100 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); | |
| 101 } | |
| 102 | |
| 104 } // namespace safe_json | 103 } // namespace safe_json |
| OLD | NEW |