1 // Copyright 2019 The Chromium 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 
5 #ifndef CHROME_SERVICES_CUPS_PROXY_SOCKET_MANAGER_H_
6 #define CHROME_SERVICES_CUPS_PROXY_SOCKET_MANAGER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/callback.h"
12 #include "base/optional.h"
13 
14 #include "chrome/services/cups_proxy/cups_proxy_service_delegate.h"
15 
16 namespace net {
17 class UnixDomainClientSocket;
18 }  // namespace net
19 
20 namespace cups_proxy {
21 
22 using SocketManagerCallback =
23     base::OnceCallback<void(std::unique_ptr<std::vector<uint8_t>>)>;
24 
25 // This manager proxies IPP requests to the CUPS daemon and asynchronously
26 // responds with the IPP response. This class must be created and accessed
27 // from a sequenced context.
28 class SocketManager {
29  public:
30   // Factory function.
31   static std::unique_ptr<SocketManager> Create(
32       CupsProxyServiceDelegate* const delegate);
33 
34   // Factory function that allows injected dependencies, for testing.
35   static std::unique_ptr<SocketManager> CreateForTesting(
36       std::unique_ptr<net::UnixDomainClientSocket> socket,
37       CupsProxyServiceDelegate* const delegate);
38 
39   virtual ~SocketManager() = default;
40 
41   // Attempts to send |request| to the CUPS Daemon, and return its response via
42   // |cb|. |cb| will run on the caller's sequence. Note: Can only handle 1
43   // inflight request at a time; attempts to proxy more will DCHECK.
44   virtual void ProxyToCups(std::vector<uint8_t> request,
45                            SocketManagerCallback cb) = 0;
46 };
47 
48 }  // namespace cups_proxy
49 
50 #endif  // CHROME_SERVICES_CUPS_PROXY_SOCKET_MANAGER_H_
51