Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS 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 | #ifndef SYSTEM_PROXY_SANDBOXED_WORKER_H_ |
| 5 | #define SYSTEM_PROXY_SANDBOXED_WORKER_H_ |
| 6 | |
| 7 | #include <array> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 8 | #include <map> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 11 | #include <vector> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 12 | |
| 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 | |
| 15 | #include <base/files/file_descriptor_watcher_posix.h> |
| 16 | #include <base/files/scoped_file.h> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 17 | #include <base/memory/weak_ptr.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 18 | #include <chromeos/scoped_minijail.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 19 | |
| 20 | namespace system_proxy { |
| 21 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 22 | class SystemProxyAdaptor; |
| 23 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 24 | class SandboxedWorker { |
| 25 | public: |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 26 | explicit SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 27 | SandboxedWorker(const SandboxedWorker&) = delete; |
| 28 | SandboxedWorker& operator=(const SandboxedWorker&) = delete; |
| 29 | virtual ~SandboxedWorker() = default; |
| 30 | |
| 31 | // Starts a sandboxed worker with pipes. |
| 32 | virtual void Start(); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 33 | // Sends the username and password to the worker via communication pipes. |
| 34 | void SetUsernameAndPassword(const std::string& username, |
| 35 | const std::string& password); |
| 36 | // Sends the listening address and port to the worker via communication |
| 37 | // pipes. |
| 38 | void SetListeningAddress(uint32_t addr, int port); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 39 | |
| 40 | // Terminates the child process by sending a SIGTERM signal. |
| 41 | virtual bool Stop(); |
| 42 | |
| 43 | virtual bool IsRunning(); |
| 44 | |
| 45 | pid_t pid() { return pid_; } |
| 46 | |
| 47 | private: |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 48 | friend class SystemProxyAdaptorTest; |
| 49 | FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials); |
| 50 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 51 | void OnMessageReceived(); |
| 52 | void OnErrorReceived(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 53 | // Called when a proxy resolver job is resolved. |proxy_servers| is the |
| 54 | // ordered list of proxies returned by Chrome. In case of failure it will be |
| 55 | // the direct proxy. |
| 56 | void OnProxyResolved(const std::string& target_url, |
| 57 | bool success, |
| 58 | const std::vector<std::string>& proxy_servers); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 59 | |
| 60 | bool is_being_terminated_ = false; |
| 61 | ScopedMinijail jail_; |
| 62 | base::ScopedFD stdin_pipe_; |
| 63 | base::ScopedFD stdout_pipe_; |
| 64 | base::ScopedFD stderr_pipe_; |
| 65 | |
| 66 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdout_watcher_; |
| 67 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stderr_watcher_; |
| 68 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 69 | // The adaptor that owns this worker. |
| 70 | base::WeakPtr<SystemProxyAdaptor> adaptor_; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 71 | pid_t pid_; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame^] | 72 | base::WeakPtrFactory<SandboxedWorker> weak_ptr_factory_{this}; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace system_proxy |
| 76 | |
| 77 | #endif // SYSTEM_PROXY_SANDBOXED_WORKER_H_ |