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_impl.h" | 5 #include "components/safe_json/safe_json_parser_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/threading/sequenced_task_runner_handle.h" | 12 #include "base/threading/sequenced_task_runner_handle.h" |
| 13 #include "base/tuple.h" | 13 #include "base/tuple.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "components/safe_json/safe_json_parser_messages.h" | |
| 16 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/utility_process_host.h" | 16 #include "content/public/browser/utility_process_host.h" |
| 17 #include "content/public/common/service_registry.h" | |
| 18 #include "grit/components_strings.h" | 18 #include "grit/components_strings.h" |
| 19 #include "ipc/ipc_message_macros.h" | |
| 20 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 21 | 20 |
| 22 using content::BrowserThread; | 21 using content::BrowserThread; |
| 23 using content::UtilityProcessHost; | 22 using content::UtilityProcessHost; |
| 24 | 23 |
| 25 namespace safe_json { | 24 namespace safe_json { |
| 26 | 25 |
| 27 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, | 26 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, |
| 28 const SuccessCallback& success_callback, | 27 const SuccessCallback& success_callback, |
| 29 const ErrorCallback& error_callback) | 28 const ErrorCallback& error_callback) |
| 30 : unsafe_json_(unsafe_json), | 29 : unsafe_json_(unsafe_json), |
| 31 success_callback_(success_callback), | 30 success_callback_(success_callback), |
| 32 error_callback_(error_callback) {} | 31 error_callback_(error_callback) {} |
| 33 | 32 |
| 34 SafeJsonParserImpl::~SafeJsonParserImpl() { | 33 SafeJsonParserImpl::~SafeJsonParserImpl() { |
| 35 } | 34 } |
| 36 | 35 |
| 37 void SafeJsonParserImpl::StartWorkOnIOThread() { | 36 void SafeJsonParserImpl::StartWorkOnIOThread() { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 37 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 39 UtilityProcessHost* host = UtilityProcessHost::Create( | 38 utility_process_host_ = UtilityProcessHost::Create( |
| 40 this, base::ThreadTaskRunnerHandle::Get().get()); | 39 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); |
| 41 host->SetName( | 40 utility_process_host_->SetName( |
| 42 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | 41 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); |
| 43 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); | 42 if (!utility_process_host_->Start()) { |
| 44 } | 43 ReportResults(); |
| 44 return; | |
| 45 } | |
| 45 | 46 |
| 46 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) { | 47 content::ServiceRegistry* service_registry = |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 48 utility_process_host_->GetServiceRegistry(); |
| 48 const base::Value* value = NULL; | 49 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| 49 CHECK(wrapper.Get(0, &value)); | 50 service_.set_connection_error_handler( |
| 51 base::Bind(&SafeJsonParserImpl::ReportResults, this)); | |
| 50 | 52 |
| 51 parsed_json_.reset(value->DeepCopy()); | 53 service_->Parse(unsafe_json_, |
| 52 ReportResults(); | 54 base::Bind(&SafeJsonParserImpl::OnParseDone, this)); |
| 53 } | |
| 54 | |
| 55 void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) { | |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 57 error_ = error_message; | |
| 58 ReportResults(); | |
| 59 } | 55 } |
| 60 | 56 |
| 61 void SafeJsonParserImpl::ReportResults() { | 57 void SafeJsonParserImpl::ReportResults() { |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 58 // There should be a DCHECK_CURRENTLY_ON(BrowserThread::IO) here. However, if |
| 59 // the parser process is still alive on shutdown, this might run on the IO | |
| 60 // thread while the IO thread message loop is shutting down. This happens | |
|
Bernhard Bauer
2016/04/13 15:46:51
Huh? Isn't the thread destroyed and cleaned up aft
Anand Mistry (off Chromium)
2016/04/14 00:59:49
This case happens due to message loop destruction
Bernhard Bauer
2016/04/14 14:11:38
Hm, could we use a ThreadChecker in this case? Tha
Anand Mistry (off Chromium)
2016/04/18 05:06:45
Done.
| |
| 61 // after the IO thread has unregistered from the BrowserThread list, causing | |
| 62 // the DCHECK to fail. | |
| 63 | |
| 64 // Maintain a reference to |this| since either |utility_process_host_| or | |
| 65 // |service_| may have to last reference and destroying those might delete | |
| 66 // |this|. | |
| 67 scoped_refptr<SafeJsonParserImpl> ref(this); | |
| 68 // Shut down the utility process if it's still running. | |
|
Bernhard Bauer
2016/04/13 15:46:51
Shouldn't the utility process shut itself down whe
Anand Mistry (off Chromium)
2016/04/14 00:59:49
This approach doesn't work well for Mojo. If the p
Bernhard Bauer
2016/04/14 14:11:38
That seems... problematic. This change doesn't rea
Anand Mistry (off Chromium)
2016/04/18 05:06:45
Because inside the utility process, the implementa
Bernhard Bauer
2016/04/20 16:43:15
Thanks for the explanation! I'm still somewhat con
Anand Mistry (off Chromium)
2016/04/21 07:54:12
The good thing about UtilityProcessHost is that it
| |
| 69 delete utility_process_host_.get(); | |
| 70 service_.reset(); | |
| 63 | 71 |
| 64 caller_task_runner_->PostTask( | 72 caller_task_runner_->PostTask( |
| 65 FROM_HERE, | 73 FROM_HERE, |
| 66 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); | 74 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); |
| 67 } | 75 } |
| 68 | 76 |
| 69 void SafeJsonParserImpl::ReportResultsOnOriginThread() { | 77 void SafeJsonParserImpl::ReportResultsOnOriginThread() { |
| 70 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); | 78 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
| 71 if (error_.empty() && parsed_json_) { | 79 if (error_.empty() && parsed_json_) { |
| 72 if (!success_callback_.is_null()) | 80 if (!success_callback_.is_null()) |
| 73 success_callback_.Run(std::move(parsed_json_)); | 81 success_callback_.Run(std::move(parsed_json_)); |
| 74 } else { | 82 } else { |
| 75 if (!error_callback_.is_null()) | 83 if (!error_callback_.is_null()) |
| 76 error_callback_.Run(error_); | 84 error_callback_.Run(error_); |
| 77 } | 85 } |
| 78 } | 86 } |
| 79 | 87 |
| 80 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { | 88 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { |
| 81 bool handled = true; | 89 return false; |
| 82 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message) | 90 } |
| 83 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, | 91 |
| 84 OnJSONParseSucceeded) | 92 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, |
|
Bernhard Bauer
2016/04/13 15:46:51
IIRC, this was a ListValue in the pre-Mojo version
Anand Mistry (off Chromium)
2016/04/14 00:59:49
That still applies because this change doesn't use
| |
| 85 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, | 93 mojo::String error) { |
| 86 OnJSONParseFailed) | 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 87 IPC_MESSAGE_UNHANDLED(handled = false) | 95 if (!wrapper.empty()) { |
| 88 IPC_END_MESSAGE_MAP() | 96 const base::Value* value = NULL; |
| 89 return handled; | 97 CHECK(wrapper.Get(0, &value)); |
| 98 parsed_json_.reset(value->DeepCopy()); | |
| 99 } else { | |
| 100 error_ = error.get(); | |
| 101 } | |
| 102 ReportResults(); | |
| 90 } | 103 } |
| 91 | 104 |
| 92 void SafeJsonParserImpl::Start() { | 105 void SafeJsonParserImpl::Start() { |
| 93 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); | 106 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); |
| 94 | 107 |
| 95 BrowserThread::PostTask( | 108 BrowserThread::PostTask( |
| 96 BrowserThread::IO, FROM_HERE, | 109 BrowserThread::IO, FROM_HERE, |
| 97 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); | 110 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); |
| 98 } | 111 } |
| 99 | 112 |
| 100 } // namespace safe_json | 113 } // namespace safe_json |
| OLD | NEW |