OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | 5 #ifndef CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
6 #define CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | 6 #define CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.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/browser/utility_process_host_client.h" | 18 #include "content/public/browser/utility_process_host_client.h" |
18 #include "mojo/public/cpp/bindings/interface_ptr.h" | 19 #include "mojo/public/cpp/bindings/interface_ptr.h" |
19 #include "services/service_manager/public/cpp/interface_provider.h" | 20 #include "services/service_manager/public/cpp/interface_provider.h" |
20 | 21 |
(...skipping 10 matching lines...) Expand all Loading... |
31 explicit UtilityProcessMojoClient(const base::string16& process_name) { | 32 explicit UtilityProcessMojoClient(const base::string16& process_name) { |
32 helper_.reset(new Helper(process_name)); | 33 helper_.reset(new Helper(process_name)); |
33 } | 34 } |
34 | 35 |
35 ~UtilityProcessMojoClient() { | 36 ~UtilityProcessMojoClient() { |
36 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, helper_.release()); | 37 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, helper_.release()); |
37 } | 38 } |
38 | 39 |
39 // Sets the error callback. A valid callback must be set before calling | 40 // Sets the error callback. A valid callback must be set before calling |
40 // Start(). | 41 // Start(). |
41 void set_error_callback(const base::Closure& on_error_callback) { | 42 void set_error_callback(const base::Closure& error_callback) { |
42 on_error_callback_ = on_error_callback; | 43 error_callback_ = error_callback; |
| 44 } |
| 45 |
| 46 // Allows a directory to be opened through the utility process sandbox. |
| 47 void set_exposed_directory(const base::FilePath& directory) { |
| 48 DCHECK(!start_called_); |
| 49 DCHECK(!directory.empty()); |
| 50 helper_->set_exposed_directory(directory); |
43 } | 51 } |
44 | 52 |
45 // Disables the sandbox in the utility process. | 53 // Disables the sandbox in the utility process. |
46 void set_disable_sandbox() { | 54 void set_disable_sandbox() { |
47 DCHECK(!start_called_); | 55 DCHECK(!start_called_); |
48 helper_->set_disable_sandbox(); | 56 helper_->set_disable_sandbox(); |
49 } | 57 } |
50 | 58 |
51 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
52 // Allow the utility process to run with elevated privileges. | 60 // Allows the utility process to run with elevated privileges. |
53 void set_run_elevated() { | 61 void set_run_elevated() { |
54 DCHECK(!start_called_); | 62 DCHECK(!start_called_); |
55 helper_->set_run_elevated(); | 63 helper_->set_run_elevated(); |
56 } | 64 } |
57 #endif // defined(OS_WIN) | 65 #endif // defined(OS_WIN) |
58 | 66 |
59 // Starts the utility process and connect to the remote Mojo service. | 67 // Starts the utility process and connects to the remote Mojo service. |
60 void Start() { | 68 void Start() { |
61 DCHECK(thread_checker_.CalledOnValidThread()); | 69 DCHECK(thread_checker_.CalledOnValidThread()); |
62 DCHECK(!on_error_callback_.is_null()); | 70 DCHECK(error_callback_); |
63 DCHECK(!start_called_); | 71 DCHECK(!start_called_); |
64 | 72 |
65 start_called_ = true; | 73 start_called_ = true; |
66 | 74 |
67 mojo::InterfaceRequest<MojoInterface> req(&service_); | 75 mojo::InterfaceRequest<MojoInterface> request(&service_); |
68 service_.set_connection_error_handler(on_error_callback_); | 76 service_.set_connection_error_handler(error_callback_); |
69 helper_->Start(MojoInterface::Name_, req.PassMessagePipe()); | 77 helper_->Start(MojoInterface::Name_, request.PassMessagePipe()); |
70 } | 78 } |
71 | 79 |
72 // Returns the Mojo service used to make calls to the utility process. | 80 // Returns the Mojo service used to make calls to the utility process. |
73 MojoInterface* service() WARN_UNUSED_RESULT { | 81 MojoInterface* service() WARN_UNUSED_RESULT { |
74 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
75 DCHECK(start_called_); | 83 DCHECK(start_called_); |
76 | 84 |
77 return service_.get(); | 85 return service_.get(); |
78 } | 86 } |
79 | 87 |
(...skipping 15 matching lines...) Expand all Loading... |
95 | 103 |
96 // Starts the utility process on the IO thread. | 104 // Starts the utility process on the IO thread. |
97 void Start(const std::string& mojo_interface_name, | 105 void Start(const std::string& mojo_interface_name, |
98 mojo::ScopedMessagePipeHandle interface_pipe) { | 106 mojo::ScopedMessagePipeHandle interface_pipe) { |
99 BrowserThread::PostTask( | 107 BrowserThread::PostTask( |
100 BrowserThread::IO, FROM_HERE, | 108 BrowserThread::IO, FROM_HERE, |
101 base::Bind(&Helper::StartOnIOThread, base::Unretained(this), | 109 base::Bind(&Helper::StartOnIOThread, base::Unretained(this), |
102 mojo_interface_name, base::Passed(&interface_pipe))); | 110 mojo_interface_name, base::Passed(&interface_pipe))); |
103 } | 111 } |
104 | 112 |
| 113 void set_exposed_directory(const base::FilePath& directory) { |
| 114 exposed_directory_ = directory; |
| 115 } |
| 116 |
105 void set_disable_sandbox() { disable_sandbox_ = true; } | 117 void set_disable_sandbox() { disable_sandbox_ = true; } |
106 | 118 |
107 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
108 void set_run_elevated() { | 120 void set_run_elevated() { |
109 disable_sandbox_ = true; | 121 disable_sandbox_ = true; |
110 run_elevated_ = true; | 122 run_elevated_ = true; |
111 } | 123 } |
112 #endif // defined(OS_WIN) | 124 #endif // defined(OS_WIN) |
113 | 125 |
114 private: | 126 private: |
115 // Starts the utility process and connects to the remote Mojo service. | 127 // Starts the utility process and connects to the remote Mojo service. |
116 void StartOnIOThread(const std::string& mojo_interface_name, | 128 void StartOnIOThread(const std::string& mojo_interface_name, |
117 mojo::ScopedMessagePipeHandle interface_pipe) { | 129 mojo::ScopedMessagePipeHandle interface_pipe) { |
118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
119 | 131 |
120 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); | 132 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); |
121 utility_host_->SetName(process_name_); | 133 utility_host_->SetName(process_name_); |
122 | 134 |
| 135 if (!exposed_directory_.empty()) |
| 136 utility_host_->SetExposedDir(exposed_directory_); |
| 137 |
123 if (disable_sandbox_) | 138 if (disable_sandbox_) |
124 utility_host_->DisableSandbox(); | 139 utility_host_->DisableSandbox(); |
125 #if defined(OS_WIN) | 140 #if defined(OS_WIN) |
126 if (run_elevated_) { | 141 if (run_elevated_) { |
127 DCHECK(disable_sandbox_); | 142 DCHECK(disable_sandbox_); |
128 utility_host_->ElevatePrivileges(); | 143 utility_host_->ElevatePrivileges(); |
129 } | 144 } |
130 #endif // defined(OS_WIN) | 145 #endif // defined(OS_WIN) |
131 | 146 |
132 utility_host_->Start(); | 147 utility_host_->Start(); |
133 | 148 |
134 utility_host_->GetRemoteInterfaces()->GetInterface( | 149 utility_host_->GetRemoteInterfaces()->GetInterface( |
135 mojo_interface_name, std::move(interface_pipe)); | 150 mojo_interface_name, std::move(interface_pipe)); |
136 } | 151 } |
137 | 152 |
138 // Properties of the utility process. | 153 // Properties of the utility process. |
139 base::string16 process_name_; | 154 base::string16 process_name_; |
| 155 base::FilePath exposed_directory_; |
140 bool disable_sandbox_ = false; | 156 bool disable_sandbox_ = false; |
141 #if defined(OS_WIN) | 157 #if defined(OS_WIN) |
142 bool run_elevated_ = false; | 158 bool run_elevated_ = false; |
143 #endif // defined(OS_WIN) | 159 #endif // defined(OS_WIN) |
144 | 160 |
145 // Must only be accessed on the IO thread. | 161 // Must only be accessed on the IO thread. |
146 base::WeakPtr<UtilityProcessHost> utility_host_; | 162 base::WeakPtr<UtilityProcessHost> utility_host_; |
147 | 163 |
148 DISALLOW_COPY_AND_ASSIGN(Helper); | 164 DISALLOW_COPY_AND_ASSIGN(Helper); |
149 }; | 165 }; |
150 | 166 |
151 std::unique_ptr<Helper> helper_; | 167 std::unique_ptr<Helper> helper_; |
152 | 168 |
153 // Called when a connection error happens or if the process didn't start. | 169 // Called when a connection error happens or if the process didn't start. |
154 base::Closure on_error_callback_; | 170 base::Closure error_callback_; |
155 | 171 |
156 mojo::InterfacePtr<MojoInterface> service_; | 172 mojo::InterfacePtr<MojoInterface> service_; |
157 | 173 |
158 // Enforce calling Start() before getting the service. | 174 // Enforce calling Start() before getting the service. |
159 bool start_called_ = false; | 175 bool start_called_ = false; |
160 | 176 |
161 // Checks that this class is always accessed from the same thread. | 177 // Checks that this class is always accessed from the same thread. |
162 base::ThreadChecker thread_checker_; | 178 base::ThreadChecker thread_checker_; |
163 | 179 |
164 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); | 180 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); |
165 }; | 181 }; |
166 | 182 |
167 } // namespace content | 183 } // namespace content |
168 | 184 |
169 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | 185 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
OLD | NEW |