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

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

Issue 1951863002: Revert of Convert the utility process JSON parser into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h" 17 #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"
19 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
20 21
21 using content::BrowserThread; 22 using content::BrowserThread;
22 using content::UtilityProcessHost; 23 using content::UtilityProcessHost;
23 24
24 namespace safe_json { 25 namespace safe_json {
25 26
26 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json, 27 SafeJsonParserImpl::SafeJsonParserImpl(const std::string& unsafe_json,
27 const SuccessCallback& success_callback, 28 const SuccessCallback& success_callback,
28 const ErrorCallback& error_callback) 29 const ErrorCallback& error_callback)
29 : unsafe_json_(unsafe_json), 30 : unsafe_json_(unsafe_json),
30 success_callback_(success_callback), 31 success_callback_(success_callback),
31 error_callback_(error_callback) { 32 error_callback_(error_callback) {}
32 io_thread_checker_.DetachFromThread();
33 }
34 33
35 SafeJsonParserImpl::~SafeJsonParserImpl() { 34 SafeJsonParserImpl::~SafeJsonParserImpl() {
36 } 35 }
37 36
38 void SafeJsonParserImpl::StartWorkOnIOThread() { 37 void SafeJsonParserImpl::StartWorkOnIOThread() {
39 DCHECK_CURRENTLY_ON(BrowserThread::IO); 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
40 DCHECK(io_thread_checker_.CalledOnValidThread()); 39 UtilityProcessHost* host = UtilityProcessHost::Create(
41 // TODO(amistry): This UtilityProcessHost dance is likely to be done multiple 40 this, base::ThreadTaskRunnerHandle::Get().get());
42 // times as more tasks are migrated to Mojo. Create some sort of helper class 41 host->SetName(
43 // to eliminate the code duplication.
44 utility_process_host_ = UtilityProcessHost::Create(
45 this, base::ThreadTaskRunnerHandle::Get().get())->AsWeakPtr();
46 utility_process_host_->SetName(
47 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME)); 42 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_JSON_PARSER_NAME));
48 if (!utility_process_host_->Start()) { 43 host->Send(new SafeJsonParserMsg_ParseJSON(unsafe_json_));
49 ReportResults(); 44 }
50 return;
51 }
52 45
53 content::ServiceRegistry* service_registry = 46 void SafeJsonParserImpl::OnJSONParseSucceeded(const base::ListValue& wrapper) {
54 utility_process_host_->GetServiceRegistry(); 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
55 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); 48 const base::Value* value = NULL;
56 service_.set_connection_error_handler( 49 CHECK(wrapper.Get(0, &value));
57 base::Bind(&SafeJsonParserImpl::ReportResults, this));
58 50
59 service_->Parse(unsafe_json_, 51 parsed_json_.reset(value->DeepCopy());
60 base::Bind(&SafeJsonParserImpl::OnParseDone, this)); 52 ReportResults();
53 }
54
55 void SafeJsonParserImpl::OnJSONParseFailed(const std::string& error_message) {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
57 error_ = error_message;
58 ReportResults();
61 } 59 }
62 60
63 void SafeJsonParserImpl::ReportResults() { 61 void SafeJsonParserImpl::ReportResults() {
64 // There should be a DCHECK_CURRENTLY_ON(BrowserThread::IO) here. However, if 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
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();
79 63
80 caller_task_runner_->PostTask( 64 caller_task_runner_->PostTask(
81 FROM_HERE, 65 FROM_HERE,
82 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this)); 66 base::Bind(&SafeJsonParserImpl::ReportResultsOnOriginThread, this));
83 } 67 }
84 68
85 void SafeJsonParserImpl::ReportResultsOnOriginThread() { 69 void SafeJsonParserImpl::ReportResultsOnOriginThread() {
86 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread()); 70 DCHECK(caller_task_runner_->RunsTasksOnCurrentThread());
87 if (error_.empty() && parsed_json_) { 71 if (error_.empty() && parsed_json_) {
88 if (!success_callback_.is_null()) 72 if (!success_callback_.is_null())
89 success_callback_.Run(std::move(parsed_json_)); 73 success_callback_.Run(std::move(parsed_json_));
90 } else { 74 } else {
91 if (!error_callback_.is_null()) 75 if (!error_callback_.is_null())
92 error_callback_.Run(error_); 76 error_callback_.Run(error_);
93 } 77 }
94 } 78 }
95 79
96 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) { 80 bool SafeJsonParserImpl::OnMessageReceived(const IPC::Message& message) {
97 return false; 81 bool handled = true;
98 } 82 IPC_BEGIN_MESSAGE_MAP(SafeJsonParserImpl, message)
99 83 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Succeeded,
100 void SafeJsonParserImpl::OnParseDone(const base::ListValue& wrapper, 84 OnJSONParseSucceeded)
101 mojo::String error) { 85 IPC_MESSAGE_HANDLER(SafeJsonParserHostMsg_ParseJSON_Failed,
102 DCHECK(io_thread_checker_.CalledOnValidThread()); 86 OnJSONParseFailed)
103 if (!wrapper.empty()) { 87 IPC_MESSAGE_UNHANDLED(handled = false)
104 const base::Value* value = NULL; 88 IPC_END_MESSAGE_MAP()
105 CHECK(wrapper.Get(0, &value)); 89 return handled;
106 parsed_json_.reset(value->DeepCopy());
107 } else {
108 error_ = error.get();
109 }
110 ReportResults();
111 } 90 }
112 91
113 void SafeJsonParserImpl::Start() { 92 void SafeJsonParserImpl::Start() {
114 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get(); 93 caller_task_runner_ = base::SequencedTaskRunnerHandle::Get();
115 94
116 BrowserThread::PostTask( 95 BrowserThread::PostTask(
117 BrowserThread::IO, FROM_HERE, 96 BrowserThread::IO, FROM_HERE,
118 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this)); 97 base::Bind(&SafeJsonParserImpl::StartWorkOnIOThread, this));
119 } 98 }
120 99
121 } // namespace safe_json 100 } // 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