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 #ifndef CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | |
| 6 #define CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/threading/thread_checker.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/utility_process_host.h" | |
| 17 #include "content/public/browser/utility_process_host_client.h" | |
| 18 #include "content/public/common/service_registry.h" | |
| 19 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 // Implements a client to a mojo service running on a utility process. Takes | |
|
jam
2016/06/17 15:47:34
nit: Mojo
Patrick Monette
2016/06/17 15:58:32
Done twice
| |
| 24 // care of starting the utility process and connecting to the remote mojo | |
| 25 // service. The utility process is terminated in the destructor. | |
| 26 // Note: This class is not thread-safe. It is bound to the | |
| 27 // SingleThreadTaskRunner it is created on. | |
| 28 template <class MojoInterface> | |
| 29 class UtilityProcessMojoClient { | |
| 30 public: | |
| 31 UtilityProcessMojoClient(const base::string16& process_name, | |
| 32 const base::Closure& on_error_callback) | |
| 33 : on_error_callback_(on_error_callback) { | |
| 34 DCHECK(!on_error_callback_.is_null()); | |
| 35 helper_.reset(new Helper(process_name)); | |
| 36 } | |
| 37 | |
| 38 ~UtilityProcessMojoClient() { | |
| 39 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, helper_.release()); | |
| 40 } | |
| 41 | |
| 42 // Disables the sandbox in the utility process. | |
| 43 void set_disable_sandbox() { | |
| 44 DCHECK(!start_called_); | |
| 45 helper_->set_disable_sandbox(); | |
| 46 } | |
| 47 | |
| 48 // Starts the utility process and connect to the remote Mojo service. | |
| 49 void Start() { | |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 51 DCHECK(!start_called_); | |
| 52 | |
| 53 start_called_ = true; | |
| 54 | |
| 55 mojo::InterfaceRequest<MojoInterface> req = mojo::GetProxy(&service_); | |
| 56 | |
| 57 service_.set_connection_error_handler(on_error_callback_); | |
| 58 | |
| 59 helper_->Start(MojoInterface::Name_, req.PassMessagePipe()); | |
| 60 } | |
| 61 | |
| 62 // Returns the Mojo service used to make calls to the utility process. | |
| 63 MojoInterface* service() WARN_UNUSED_RESULT { | |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 65 DCHECK(start_called_); | |
| 66 | |
| 67 return service_.get(); | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 // Helper class that takes care of managing the lifetime of the utility | |
| 72 // process on the IO thread. | |
| 73 class Helper { | |
| 74 public: | |
| 75 explicit Helper(const base::string16& process_name) | |
| 76 : process_name_(process_name) {} | |
| 77 | |
| 78 ~Helper() { | |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 80 | |
| 81 // |utility_host_| manages its own lifetime but this forces the process to | |
| 82 // terminate if it's still alive. | |
| 83 delete utility_host_.get(); | |
| 84 } | |
| 85 | |
| 86 // Starts the utility process on the IO thread. | |
| 87 void Start(const std::string& mojo_interface_name, | |
| 88 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 89 BrowserThread::PostTask( | |
| 90 BrowserThread::IO, FROM_HERE, | |
| 91 base::Bind(&Helper::StartOnIOThread, base::Unretained(this), | |
| 92 mojo_interface_name, base::Passed(&interface_pipe))); | |
| 93 } | |
| 94 | |
| 95 void set_disable_sandbox() { disable_sandbox_ = true; } | |
| 96 | |
| 97 private: | |
| 98 // Starts the utility process and connects to the remote Mojo service. | |
| 99 void StartOnIOThread(const std::string& mojo_interface_name, | |
| 100 mojo::ScopedMessagePipeHandle interface_pipe) { | |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 102 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); | |
| 103 utility_host_->SetName(process_name_); | |
| 104 if (disable_sandbox_) | |
| 105 utility_host_->DisableSandbox(); | |
| 106 | |
| 107 utility_host_->Start(); | |
| 108 | |
| 109 ServiceRegistry* service_registry = utility_host_->GetServiceRegistry(); | |
| 110 service_registry->ConnectToRemoteService(mojo_interface_name, | |
| 111 std::move(interface_pipe)); | |
| 112 } | |
| 113 | |
| 114 // Properties of the utility process. | |
| 115 base::string16 process_name_; | |
| 116 bool disable_sandbox_ = false; | |
| 117 | |
| 118 // Must only be accessed on the IO thread. | |
| 119 base::WeakPtr<UtilityProcessHost> utility_host_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(Helper); | |
| 122 }; | |
| 123 | |
| 124 std::unique_ptr<Helper> helper_; | |
| 125 | |
| 126 // Called when a connection error happens or if the process didn't start. | |
| 127 base::Closure on_error_callback_; | |
| 128 | |
| 129 mojo::InterfacePtr<MojoInterface> service_; | |
| 130 | |
| 131 // Enforce calling Start() before getting the service. | |
| 132 bool start_called_ = false; | |
| 133 | |
| 134 // Checks that this class is always accessed from the same thread. | |
| 135 base::ThreadChecker thread_checker_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); | |
| 138 }; | |
| 139 | |
| 140 } // namespace content | |
| 141 | |
| 142 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | |
| OLD | NEW |