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

Side by Side Diff: content/browser/utility_process_mojo_client_impl.cc

Issue 2049303002: Add the UtilityProcessMojoClient class and convert SafeJsonParser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: bauerb@ comment Created 4 years, 6 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/public/browser/utility_process_mojo_client_impl.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/utility_process_host.h"
9 #include "content/public/browser/utility_process_host_client.h"
10 #include "content/public/common/service_registry.h"
11
12 namespace content {
13
14 // Helper ref-counted class that takes care of managing the lifetime of the
michaeln 2016/06/14 18:37:27 Generally we like to avoid refcounting. This class
Patrick Monette 2016/06/14 21:23:32 Good idea. Done.
15 // utility process on the IO thread.
16 class UtilityProcessMojoClientImpl::Helper
17 : public base::RefCountedThreadSafe<Helper> {
18 public:
19 explicit Helper(const base::string16& process_name)
20 : process_name_(process_name) {}
21
22 // Starts the utility process on the IO thread.
23 void Start(const std::string& mojo_interface_name,
24 mojo::ScopedMessagePipeHandle interface_pipe) {
25 BrowserThread::PostTask(
26 BrowserThread::IO, FROM_HERE,
27 base::Bind(&Helper::StartOnIOThread, this, mojo_interface_name,
28 base::Passed(&interface_pipe)));
29 }
30
31 // Shuts down the utility process.
32 void Shutdown() {
33 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
34 base::Bind(&Helper::ShutdownOnIOThread, this));
35 }
36
37 void set_disable_sandbox() { disable_sandbox_ = true; }
michaeln 2016/06/14 18:37:26 Presumably the setter is for use on the main threa
Patrick Monette 2016/06/14 21:23:32 The contract of the class is explicit about callin
38
39 private:
40 friend class base::RefCountedThreadSafe<Helper>;
41
42 ~Helper() = default;
43
44 // Starts the utility process and connects to the remote Mojo service.
45 void StartOnIOThread(const std::string& mojo_interface_name,
46 mojo::ScopedMessagePipeHandle interface_pipe) {
47 DCHECK_CURRENTLY_ON(BrowserThread::IO);
48 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr();
michaeln 2016/06/14 18:37:27 What is the reason to use a WeakPtr<> here? It loo
Patrick Monette 2016/06/14 21:23:32 utility_host_ manages its own lifetime. The WeakPt
michaeln 2016/06/14 22:10:50 i see, thnx, you might want a comment about that t
Patrick Monette 2016/06/15 15:28:09 Done.
49 utility_host_->SetName(process_name_);
50 if (disable_sandbox_)
51 utility_host_->DisableSandbox();
52
53 utility_host_->Start();
54
55 ServiceRegistry* service_registry = utility_host_->GetServiceRegistry();
56 service_registry->ConnectToRemoteService(mojo_interface_name,
57 std::move(interface_pipe));
58 }
59
60 void ShutdownOnIOThread() {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO);
62 delete utility_host_.get();
63 }
64
65 // Properties of the utility process.
66 base::string16 process_name_;
67 bool disable_sandbox_ = false;
68
69 // Must only be accessed on the IO thread.
70 base::WeakPtr<UtilityProcessHost> utility_host_;
71
72 DISALLOW_COPY_AND_ASSIGN(Helper);
73 };
74
75 UtilityProcessMojoClientImpl::UtilityProcessMojoClientImpl(
76 const base::string16& process_name) {
77 helper_ = new Helper(process_name);
78 }
79
80 UtilityProcessMojoClientImpl::~UtilityProcessMojoClientImpl() {
81 helper_->Shutdown();
82 }
83
84 void UtilityProcessMojoClientImpl::set_disable_sandbox() {
85 helper_->set_disable_sandbox();
86 }
87
88 void UtilityProcessMojoClientImpl::Start(
89 const std::string& mojo_interface_name,
90 mojo::ScopedMessagePipeHandle interface_pipe) {
91 helper_->Start(mojo_interface_name, std::move(interface_pipe));
92 }
93
94 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698