blob: 8a7a4a145329805769c2980f6678581a3d5c8cad [file] [log] [blame]
Andreea Costinas942284d2020-01-28 16:28:40 +01001// 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_SYSTEM_PROXY_ADAPTOR_H_
5#define SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_
6
7#include <memory>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +01008#include <string>
Andreea Costinas942284d2020-01-28 16:28:40 +01009#include <vector>
10
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010011#include <base/memory/weak_ptr.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010012#include <brillo/dbus/async_event_sequencer.h>
Andreea Costinas5862b102020-03-19 14:45:36 +010013#include <brillo/http/http_proxy.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010015#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010016
17#include "system_proxy/org.chromium.SystemProxy.h"
18
19namespace brillo {
20namespace dbus_utils {
21class DBusObject;
22}
23
24} // namespace brillo
25
26namespace system_proxy {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010027
28class SandboxedWorker;
29
Andreea Costinas942284d2020-01-28 16:28:40 +010030// Implementation of the SystemProxy D-Bus interface.
31class SystemProxyAdaptor : public org::chromium::SystemProxyAdaptor,
32 public org::chromium::SystemProxyInterface {
33 public:
34 explicit SystemProxyAdaptor(
35 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object);
36 SystemProxyAdaptor(const SystemProxyAdaptor&) = delete;
37 SystemProxyAdaptor& operator=(const SystemProxyAdaptor&) = delete;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010038 virtual ~SystemProxyAdaptor();
Andreea Costinas942284d2020-01-28 16:28:40 +010039
40 // Registers the D-Bus object and interfaces.
41 void RegisterAsync(
42 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction&
43 completion_callback);
44
45 // org::chromium::SystemProxyInterface: (see org.chromium.SystemProxy.xml).
Andreea Costinas77b180e2020-05-12 15:17:32 +020046 std::vector<uint8_t> SetAuthenticationDetails(
47 const std::vector<uint8_t>& request_blob) override;
Andreea Costinas942284d2020-01-28 16:28:40 +010048 std::vector<uint8_t> SetSystemTrafficCredentials(
49 const std::vector<uint8_t>& request_blob) override;
50 std::vector<uint8_t> ShutDown() override;
51
Andreea Costinas5862b102020-03-19 14:45:36 +010052 void GetChromeProxyServersAsync(
53 const std::string& target_url,
54 const brillo::http::GetChromeProxyServersCallback& callback);
55
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010056 protected:
57 virtual std::unique_ptr<SandboxedWorker> CreateWorker();
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020058 virtual bool ConnectNamespace(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +020059 // Triggers the |WorkerActive| signal.
60 void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010061
Andreea Costinas942284d2020-01-28 16:28:40 +010062 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010063 friend class SystemProxyAdaptorTest;
Andreea Costinas77b180e2020-05-12 15:17:32 +020064 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas41e06442020-03-09 09:41:51 +010065 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
66 FRIEND_TEST(SystemProxyAdaptorTest, ShutDown);
Andreea Costinasa89309d2020-05-08 15:51:12 +020067 FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace);
68 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinas41e06442020-03-09 09:41:51 +010069
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010070 void SetCredentialsTask(SandboxedWorker* worker,
71 const std::string& username,
72 const std::string& password);
73
74 void ShutDownTask();
75
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020076 bool StartWorker(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010077
Andreea Costinas77b180e2020-05-12 15:17:32 +020078 // Checks if a worker process exists and if not creates one and sends a
79 // request to patchpanel to setup the network namespace for it. Returns true
80 // if the worker exists or was created successfully, false otherwise.
81 bool CreateWorkerIfNeeded(bool user_traffic);
82
Andreea Costinasa89309d2020-05-08 15:51:12 +020083 // Called when the patchpanel D-Bus service becomes available.
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020084 void OnPatchpanelServiceAvailable(bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010085
Andreea Costinas5862b102020-03-19 14:45:36 +010086 // The callback of |GetChromeProxyServersAsync|.
87 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
88
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010089 // Worker that authenticates and forwards to a remote web proxy traffic
90 // coming form Chrome OS system services.
91 std::unique_ptr<SandboxedWorker> system_services_worker_;
92 // Worker that authenticates and forwards to a remote web proxy traffic
93 // coming form ARC++ apps.
94 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas942284d2020-01-28 16:28:40 +010095 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010096 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +010097};
98
99} // namespace system_proxy
100#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_