Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: components/safe_json/safe_json_parser_impl.cc

Issue 1861573002: Convert the utility process JSON parser into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove from IPC message start. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // TODO(amistry): This UtilityProcessHost dance is likely to be done multiple
41 host->SetName( 42 // times as more tasks are migrated to Mojo. Create some sort of helper class
43 // to eliminate the code duplication.
44 utility_process_host_ = UtilityProcessHost::Create(
45 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr();
46 utility_process_host_->SetName(
42 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); 47 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME));
43 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_)); 48 if (!utility_process_host_->Start()) {
44 } 49 ReportResults();
50 return;
51 }
45 52
46 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) { 53 content::ServiceRegistry* service_registry =
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 54 utility_process_host_->GetServiceRegistry();
48 const base::Value* value = NULL; 55 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_));
49 CHECK(wrapper.Get(0, &value)); 56 service_.set_connection_error_handler(
57 base::Bind(&SafeJsonParserImpl::ReportResults, this));
50 58
51 parsed_json_.reset(value->DeepCopy()); 59 service_->Parse(unsafe_json_,
52 ReportResults(); 60 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 } 61 }
60 62
61 void SafeJsonParserImpl::ReportResults() { 63 void SafeJsonParserImpl::ReportResults() {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 64 // There should be a DCHECK_CURRENTLY_ON(BrowserThread::IO) here. However, if
65 // the parser process is still alive on shutdown, this might run on the IO
66 // thread while the IO thread message loop is shutting down. This happens
67 // after the IO thread has unregistered from the BrowserThread list, causing
68 // the DCHECK to fail.
69 DCHECK(io_thread_checker_.CalledOnValidThread());
70 io_thread_checker_.DetachFromThread();
71
72 // Maintain a reference to |this| since either |utility_process_host_| or
73 // |service_| may have the last reference and destroying those might delete
74 // |this|.
75 scoped_refptr<SafeJsonParserImpl> ref(this);
76 // Shut down the utility process if it's still running.
77 delete utility_process_host_.get();
78 service_.reset();
63 79
64 caller_task_runner_->PostTask( 80 caller_task_runner_->PostTask(
65 FROM_HERE, 81 FROM_HERE,
66 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); 82 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this));
67 } 83 }
68 84
69 void SafeJsonParserImpl::ReportResultsOnOriginThread() { 85 void SafeJsonParserImpl::ReportResultsOnOriginThread() {
70 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); 86 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread());
71 if (error_.empty() && parsed_json_) { 87 if (error_.empty() && parsed_json_) {
72 if (!success_callback_.is_null()) 88 if (!success_callback_.is_null())
73 success_callback_.Run(std::move(parsed_json_)); 89 success_callback_.Run(std::move(parsed_json_));
74 } else { 90 } else {
75 if (!error_callback_.is_null()) 91 if (!error_callback_.is_null())
76 error_callback_.Run(error_); 92 error_callback_.Run(error_);
77 } 93 }
78 } 94 }
79 95
80 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { 96 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) {
81 bool handled = true; 97 return false;
82 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message) 98 }
83 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded, 99
84 OnJSONParseSucceeded) 100 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper,
85 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed, 101 mojo::String error) {
86 OnJSONParseFailed) 102 DCHECK(io_thread_checker_.CalledOnValidThread());
87 IPC_MESSAGE_UNHANDLED(handled = false) 103 if (!wrapper.empty()) {
88 IPC_END_MESSAGE_MAP() 104 const base::Value* value = NULL;
89 return handled; 105 CHECK(wrapper.Get(0, &value));
106 parsed_json_.reset(value->DeepCopy());
107 } else {
108 error_ = error.get();
109 }
110 ReportResults();
90 } 111 }
91 112
92 void SafeJsonParserImpl::Start() { 113 void SafeJsonParserImpl::Start() {
93 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); 114 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get();
94 115
95 BrowserThread::PostTask( 116 BrowserThread::PostTask(
96 BrowserThread::IO, FROM_HERE, 117 BrowserThread::IO, FROM_HERE,
97 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); 118 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this));
98 } 119 }
99 120
100 } // namespace safe_json 121 } // namespace safe_json
OLDNEW
« no previous file with comments | « components/safe_json/safe_json_parser_impl.h ('k') | components/safe_json/safe_json_parser_message_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698