| OLD | NEW |
| (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 |
| 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; } |
| 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(); |
| 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 |
| OLD | NEW |