blob: 18db8e24c9237e708064f291ee433a0e3d8cd496 [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).
46 std::vector<uint8_t> SetSystemTrafficCredentials(
47 const std::vector<uint8_t>& request_blob) override;
48 std::vector<uint8_t> ShutDown() override;
49
Andreea Costinas5862b102020-03-19 14:45:36 +010050 void GetChromeProxyServersAsync(
51 const std::string& target_url,
52 const brillo::http::GetChromeProxyServersCallback& callback);
53
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010054 protected:
55 virtual std::unique_ptr<SandboxedWorker> CreateWorker();
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020056 virtual bool ConnectNamespace(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +020057 // Triggers the |WorkerActive| signal.
58 void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010059
Andreea Costinas942284d2020-01-28 16:28:40 +010060 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010061 friend class SystemProxyAdaptorTest;
62 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
63 FRIEND_TEST(SystemProxyAdaptorTest, ShutDown);
Andreea Costinasa89309d2020-05-08 15:51:12 +020064 FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace);
65 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinas41e06442020-03-09 09:41:51 +010066
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010067 void SetCredentialsTask(SandboxedWorker* worker,
68 const std::string& username,
69 const std::string& password);
70
71 void ShutDownTask();
72
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020073 bool StartWorker(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010074
Andreea Costinasa89309d2020-05-08 15:51:12 +020075 // Called when the patchpanel D-Bus service becomes available.
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020076 void OnPatchpanelServiceAvailable(bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010077
Andreea Costinas5862b102020-03-19 14:45:36 +010078 // The callback of |GetChromeProxyServersAsync|.
79 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
80
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010081 // Worker that authenticates and forwards to a remote web proxy traffic
82 // coming form Chrome OS system services.
83 std::unique_ptr<SandboxedWorker> system_services_worker_;
84 // Worker that authenticates and forwards to a remote web proxy traffic
85 // coming form ARC++ apps.
86 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas942284d2020-01-28 16:28:40 +010087 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010088 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +010089};
90
91} // namespace system_proxy
92#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_