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" |
(...skipping 30 matching lines...) Expand all Loading... |
41 void set_error_callback(const base::Closure& on_error_callback) { | 41 void set_error_callback(const base::Closure& on_error_callback) { |
42 on_error_callback_ = on_error_callback; | 42 on_error_callback_ = on_error_callback; |
43 } | 43 } |
44 | 44 |
45 // Disables the sandbox in the utility process. | 45 // Disables the sandbox in the utility process. |
46 void set_disable_sandbox() { | 46 void set_disable_sandbox() { |
47 DCHECK(!start_called_); | 47 DCHECK(!start_called_); |
48 helper_->set_disable_sandbox(); | 48 helper_->set_disable_sandbox(); |
49 } | 49 } |
50 | 50 |
| 51 #if defined(OS_WIN) |
| 52 // Allow the utility process to run with elevated privileges. |
| 53 void set_run_elevated() { |
| 54 DCHECK(!start_called_); |
| 55 helper_->set_run_elevated(); |
| 56 } |
| 57 #endif // defined(OS_WIN) |
| 58 |
51 // Starts the utility process and connect to the remote Mojo service. | 59 // Starts the utility process and connect to the remote Mojo service. |
52 void Start() { | 60 void Start() { |
53 DCHECK(thread_checker_.CalledOnValidThread()); | 61 DCHECK(thread_checker_.CalledOnValidThread()); |
54 DCHECK(!on_error_callback_.is_null()); | 62 DCHECK(!on_error_callback_.is_null()); |
55 DCHECK(!start_called_); | 63 DCHECK(!start_called_); |
56 | 64 |
57 start_called_ = true; | 65 start_called_ = true; |
58 | 66 |
59 mojo::InterfaceRequest<MojoInterface> req(&service_); | 67 mojo::InterfaceRequest<MojoInterface> req(&service_); |
60 service_.set_connection_error_handler(on_error_callback_); | 68 service_.set_connection_error_handler(on_error_callback_); |
(...skipping 28 matching lines...) Expand all Loading... |
89 void Start(const std::string& mojo_interface_name, | 97 void Start(const std::string& mojo_interface_name, |
90 mojo::ScopedMessagePipeHandle interface_pipe) { | 98 mojo::ScopedMessagePipeHandle interface_pipe) { |
91 BrowserThread::PostTask( | 99 BrowserThread::PostTask( |
92 BrowserThread::IO, FROM_HERE, | 100 BrowserThread::IO, FROM_HERE, |
93 base::Bind(&Helper::StartOnIOThread, base::Unretained(this), | 101 base::Bind(&Helper::StartOnIOThread, base::Unretained(this), |
94 mojo_interface_name, base::Passed(&interface_pipe))); | 102 mojo_interface_name, base::Passed(&interface_pipe))); |
95 } | 103 } |
96 | 104 |
97 void set_disable_sandbox() { disable_sandbox_ = true; } | 105 void set_disable_sandbox() { disable_sandbox_ = true; } |
98 | 106 |
| 107 #if defined(OS_WIN) |
| 108 void set_run_elevated() { |
| 109 disable_sandbox_ = true; |
| 110 run_elevated_ = true; |
| 111 } |
| 112 #endif // defined(OS_WIN) |
| 113 |
99 private: | 114 private: |
100 // Starts the utility process and connects to the remote Mojo service. | 115 // Starts the utility process and connects to the remote Mojo service. |
101 void StartOnIOThread(const std::string& mojo_interface_name, | 116 void StartOnIOThread(const std::string& mojo_interface_name, |
102 mojo::ScopedMessagePipeHandle interface_pipe) { | 117 mojo::ScopedMessagePipeHandle interface_pipe) { |
103 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 |
104 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); | 120 utility_host_ = UtilityProcessHost::Create(nullptr, nullptr)->AsWeakPtr(); |
105 utility_host_->SetName(process_name_); | 121 utility_host_->SetName(process_name_); |
| 122 |
106 if (disable_sandbox_) | 123 if (disable_sandbox_) |
107 utility_host_->DisableSandbox(); | 124 utility_host_->DisableSandbox(); |
| 125 #if defined(OS_WIN) |
| 126 if (run_elevated_) { |
| 127 DCHECK(disable_sandbox_); |
| 128 utility_host_->ElevatePrivileges(); |
| 129 } |
| 130 #endif // defined(OS_WIN) |
108 | 131 |
109 utility_host_->Start(); | 132 utility_host_->Start(); |
110 | 133 |
111 utility_host_->GetRemoteInterfaces()->GetInterface( | 134 utility_host_->GetRemoteInterfaces()->GetInterface( |
112 mojo_interface_name, std::move(interface_pipe)); | 135 mojo_interface_name, std::move(interface_pipe)); |
113 } | 136 } |
114 | 137 |
115 // Properties of the utility process. | 138 // Properties of the utility process. |
116 base::string16 process_name_; | 139 base::string16 process_name_; |
117 bool disable_sandbox_ = false; | 140 bool disable_sandbox_ = false; |
| 141 #if defined(OS_WIN) |
| 142 bool run_elevated_ = false; |
| 143 #endif // defined(OS_WIN) |
118 | 144 |
119 // Must only be accessed on the IO thread. | 145 // Must only be accessed on the IO thread. |
120 base::WeakPtr<UtilityProcessHost> utility_host_; | 146 base::WeakPtr<UtilityProcessHost> utility_host_; |
121 | 147 |
122 DISALLOW_COPY_AND_ASSIGN(Helper); | 148 DISALLOW_COPY_AND_ASSIGN(Helper); |
123 }; | 149 }; |
124 | 150 |
125 std::unique_ptr<Helper> helper_; | 151 std::unique_ptr<Helper> helper_; |
126 | 152 |
127 // Called when a connection error happens or if the process didn't start. | 153 // Called when a connection error happens or if the process didn't start. |
128 base::Closure on_error_callback_; | 154 base::Closure on_error_callback_; |
129 | 155 |
130 mojo::InterfacePtr<MojoInterface> service_; | 156 mojo::InterfacePtr<MojoInterface> service_; |
131 | 157 |
132 // Enforce calling Start() before getting the service. | 158 // Enforce calling Start() before getting the service. |
133 bool start_called_ = false; | 159 bool start_called_ = false; |
134 | 160 |
135 // Checks that this class is always accessed from the same thread. | 161 // Checks that this class is always accessed from the same thread. |
136 base::ThreadChecker thread_checker_; | 162 base::ThreadChecker thread_checker_; |
137 | 163 |
138 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); | 164 DISALLOW_COPY_AND_ASSIGN(UtilityProcessMojoClient); |
139 }; | 165 }; |
140 | 166 |
141 } // namespace content | 167 } // namespace content |
142 | 168 |
143 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ | 169 #endif // CONTENT_PUBLIC_BROWSER_UTILITY_PROCESS_MOJO_CLIENT_H_ |
OLD | NEW |