blob: d1ca0c83ebaf3a884a305297f895970aad4dd79c [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 Costinasc7d5ad02020-03-09 09:41:51 +010057
Andreea Costinas942284d2020-01-28 16:28:40 +010058 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010059 friend class SystemProxyAdaptorTest;
60 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
61 FRIEND_TEST(SystemProxyAdaptorTest, ShutDown);
62
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010063 void SetCredentialsTask(SandboxedWorker* worker,
64 const std::string& username,
65 const std::string& password);
66
67 void ShutDownTask();
68
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020069 bool StartWorker(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010070
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020071// Called when the patchpanel D-Bus service becomes available.
72 void OnPatchpanelServiceAvailable(bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010073
Andreea Costinas5862b102020-03-19 14:45:36 +010074 // The callback of |GetChromeProxyServersAsync|.
75 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
76
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010077 // Worker that authenticates and forwards to a remote web proxy traffic
78 // coming form Chrome OS system services.
79 std::unique_ptr<SandboxedWorker> system_services_worker_;
80 // Worker that authenticates and forwards to a remote web proxy traffic
81 // coming form ARC++ apps.
82 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas942284d2020-01-28 16:28:40 +010083 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010084 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +010085};
86
87} // namespace system_proxy
88#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_