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) { |
32 io_thread_checker_.DetachFromThread(); | |
33 } | |
33 | 34 |
34 SafeJsonParserImpl::~SafeJsonParserImpl() { | 35 SafeJsonParserImpl::~SafeJsonParserImpl() { |
35 } | 36 } |
36 | 37 |
37 void SafeJsonParserImpl::StartWorkOnIOThread() { | 38 void SafeJsonParserImpl::StartWorkOnIOThread() { |
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
39 UtilityProcessHost* host = UtilityProcessHost::Create( | 40 DCHECK(io_thread_checker_.CalledOnValidThread()); |
40 this, base::ThreadTaskRunnerHandle::Get().get()); | 41 utility_process_host_ = UtilityProcessHost::Create( |
41 host->SetName( | 42 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr(); |
43 utility_process_host_->SetName( | |
42 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); | 44 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); |
43 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); | 45 if (!utility_process_host_->Start()) { |
44 } | 46 ReportResults(); |
47 return; | |
48 } | |
45 | 49 |
46 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) { | 50 content::ServiceRegistry* service_registry = |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 51 utility_process_host_->GetServiceRegistry(); |
48 const base::Value* value = NULL; | 52 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
49 CHECK(wrapper.Get(0, &value)); | 53 service_.set_connection_error_handler( |
54 base::Bind(&SafeJsonParserImpl::ReportResults, this)); | |
50 | 55 |
51 parsed_json_.reset(value->DeepCopy()); | 56 service_->Parse(unsafe_json_, |
52 ReportResults(); | 57 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 } | 58 } |
60 | 59 |
61 void SafeJsonParserImpl::ReportResults() { | 60 void SafeJsonParserImpl::ReportResults() { |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 61 // There should be a DCHECK_CURRENTLY_ON(BrowserThread::IO) here. However, if |
62 // the parser process is still alive on shutdown, this might run on the IO | |
63 // thread while the IO thread message loop is shutting down. This happens | |
64 // after the IO thread has unregistered from the BrowserThread list, causing | |
65 // the DCHECK to fail. | |
66 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
67 io_thread_checker_.DetachFromThread(); | |
68 | |
69 // Maintain a reference to |this| since either |utility_process_host_| or | |
70 // |service_| may have to last reference and destroying those might delete | |
Bernhard Bauer
2016/04/28 16:12:41
Nit: "the" last reference
Anand Mistry (off Chromium)
2016/05/02 06:44:33
Done.
| |
71 // |this|. | |
72 scoped_refptr<SafeJsonParserImpl> ref(this); | |
73 // Shut down the utility process if it's still running. | |
74 delete utility_process_host_.get(); | |
75 service_.reset(); | |
63 | 76 |
64 caller_task_runner_->PostTask( | 77 caller_task_runner_->PostTask( |
65 FROM_HERE, | 78 FROM_HERE, |
66 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); | 79 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); |
67 } | 80 } |
68 | 81 |
69 void SafeJsonParserImpl::ReportResultsOnOriginThread() { | 82 void SafeJsonParserImpl::ReportResultsOnOriginThread() { |
70 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); | 83 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); |
71 if (error_.empty() && parsed_json_) { | 84 if (error_.empty() && parsed_json_) { |
72 if (!success_callback_.is_null()) | 85 if (!success_callback_.is_null()) |
73 success_callback_.Run(std::move(parsed_json_)); | 86 success_callback_.Run(std::move(parsed_json_)); |
74 } else { | 87 } else { |
75 if (!error_callback_.is_null()) | 88 if (!error_callback_.is_null()) |
76 error_callback_.Run(error_); | 89 error_callback_.Run(error_); |
77 } | 90 } |
78 } | 91 } |
79 | 92 |
80 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { | 93 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { |
81 bool handled = true; | 94 return false; |
82 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message) | 95 } |
83 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, | 96 |
84 OnJSONParseSucceeded) | 97 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, |
85 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, | 98 mojo::String error) { |
86 OnJSONParseFailed) | 99 DCHECK(io_thread_checker_.CalledOnValidThread()); |
87 IPC_MESSAGE_UNHANDLED(handled = false) | 100 if (!wrapper.empty()) { |
88 IPC_END_MESSAGE_MAP() | 101 const base::Value* value = NULL; |
89 return handled; | 102 CHECK(wrapper.Get(0, &value)); |
103 parsed_json_.reset(value->DeepCopy()); | |
104 } else { | |
105 error_ = error.get(); | |
106 } | |
107 ReportResults(); | |
90 } | 108 } |
91 | 109 |
92 void SafeJsonParserImpl::Start() { | 110 void SafeJsonParserImpl::Start() { |
93 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); | 111 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); |
94 | 112 |
95 BrowserThread::PostTask( | 113 BrowserThread::PostTask( |
96 BrowserThread::IO, FROM_HERE, | 114 BrowserThread::IO, FROM_HERE, |
97 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); | 115 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); |
98 } | 116 } |
99 | 117 |
100 } // namespace safe_json | 118 } // namespace safe_json |
OLD | NEW |