Chromium Code Reviews| 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 // Takes care of starting and stopping the utility process and connecting | |
| 15 // the Mojo InterfaceRequest. | |
| 16 class UtilityProcessMojoClientImpl::Holder | |
| 17 : public base::RefCountedThreadSafe<Holder> { | |
| 18 public: | |
| 19 explicit Holder(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 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 26 BrowserThread::PostTask( | |
| 27 BrowserThread::IO, FROM_HERE, | |
| 28 base::Bind(&Holder::StartOnIOThread, this, mojo_interface_name, | |
| 29 base::Passed(&interface_pipe))); | |
| 30 } | |
| 31 | |
| 32 // Shuts down the utility process. | |
| 33 void Shutdown() { | |
| 34 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 35 base::Bind(&Holder::ShutdownOnIOThread, this)); | |
| 36 } | |
| 37 | |
| 38 void set_disable_sandbox() { disable_sandbox_ = true; } | |
| 39 | |
| 40 private: | |
| 41 friend class base::RefCountedThreadSafe<Holder>; | |
| 42 | |
| 43 ~Holder() = default; | |
| 44 | |
| 45 // Starts the utility process and connects to the remote Mojo service. | |
| 46 void StartOnIOThread(const std::string& mojo_interface_name, | |
| 47 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 49 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); | |
| 50 utility_host_->SetName(process_name_); | |
| 51 if (disable_sandbox_) | |
| 52 utility_host_->DisableSandbox(); | |
| 53 | |
| 54 ServiceRegistry* service_registry = utility_host_->GetServiceRegistry(); | |
| 55 service_registry->ConnectToRemoteService(mojo_interface_name, | |
| 56 std::move(interface_pipe)); | |
| 57 utility_host_->Start(); | |
|
Anand Mistry (off Chromium)
2016/06/10 11:13:54
Ordering doesn't matter, but I think it's more log
Patrick Monette
2016/06/10 18:13:23
Done.
| |
| 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 // Checks that this class is always accessed from the same thread. | |
| 70 base::ThreadChecker thread_checker_; | |
|
Anand Mistry (off Chromium)
2016/06/10 11:13:54
This isn't particularly useful. The thread is only
Patrick Monette
2016/06/10 18:13:23
Removed.
| |
| 71 | |
| 72 // Must only be accessed on the IO thread. | |
| 73 base::WeakPtr<UtilityProcessHost> utility_host_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(Holder); | |
| 76 }; | |
| 77 | |
| 78 UtilityProcessMojoClientImpl::UtilityProcessMojoClientImpl( | |
| 79 const base::string16& process_name) { | |
| 80 holder_ = new Holder(process_name); | |
| 81 } | |
| 82 | |
| 83 UtilityProcessMojoClientImpl::~UtilityProcessMojoClientImpl() { | |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 85 holder_->Shutdown(); | |
| 86 } | |
| 87 | |
| 88 void UtilityProcessMojoClientImpl::set_disable_sandbox() { | |
| 89 holder_->set_disable_sandbox(); | |
| 90 } | |
| 91 | |
| 92 void UtilityProcessMojoClientImpl::StartImpl( | |
| 93 const std::string& mojo_interface_name, | |
| 94 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 95 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 96 holder_->Start(mojo_interface_name, std::move(interface_pipe)); | |
| 97 } | |
| 98 | |
| 99 } // namespace content | |
| OLD | NEW |