1 // Copyright 2018 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 SERVICES_NETWORK_WEBSOCKET_FACTORY_H_
6 #define SERVICES_NETWORK_WEBSOCKET_FACTORY_H_
7 
8 #include <set>
9 #include <vector>
10 
11 #include "base/callback_forward.h"
12 #include "base/containers/unique_ptr_adapters.h"
13 #include "base/memory/weak_ptr.h"
14 #include "services/network/public/mojom/network_context.mojom.h"
15 #include "services/network/public/mojom/websocket.mojom.h"
16 #include "services/network/websocket.h"
17 #include "services/network/websocket_throttler.h"
18 
19 class GURL;
20 
21 namespace net {
22 class SiteForCookies;
23 class SSLInfo;
24 }  // namespace net
25 
26 namespace url {
27 class Origin;
28 }  // namespace url
29 
30 namespace network {
31 
32 class NetworkContext;
33 class WebSocket;
34 
35 class WebSocketFactory final {
36  public:
37   explicit WebSocketFactory(NetworkContext* context);
38   ~WebSocketFactory();
39 
40   void CreateWebSocket(
41       const GURL& url,
42       const std::vector<std::string>& requested_protocols,
43       const net::SiteForCookies& site_for_cookies,
44       const net::NetworkIsolationKey& network_isolation_key,
45       std::vector<mojom::HttpHeaderPtr> additional_headers,
46       int32_t process_id,
47       int32_t render_frame_id,
48       const url::Origin& origin,
49       uint32_t options,
50       mojo::PendingRemote<mojom::WebSocketHandshakeClient> handshake_client,
51       mojo::PendingRemote<mojom::AuthenticationHandler> auth_handler,
52       mojo::PendingRemote<mojom::TrustedHeaderClient> header_client);
53 
54   // Returns a URLRequestContext associated with this factory.
55   net::URLRequestContext* GetURLRequestContext();
56 
57   // Called when a WebSocket sees a SSL certificate error.
58   void OnSSLCertificateError(base::OnceCallback<void(int)> callback,
59                              const GURL& url,
60                              int process_id,
61                              int render_frame_id,
62                              int net_error,
63                              const net::SSLInfo& ssl_info,
64                              bool fatal);
65 
66   // Removes and deletes |impl|.
67   void Remove(WebSocket* impl);
68 
69  private:
70   // The connections held by this factory.
71   std::set<std::unique_ptr<WebSocket>, base::UniquePtrComparator> connections_;
72 
73   WebSocketThrottler throttler_;
74 
75   // |context_| outlives this object.
76   NetworkContext* const context_;
77 
78   DISALLOW_COPY_AND_ASSIGN(WebSocketFactory);
79 };
80 
81 }  // namespace network
82 
83 #endif  // SERVICES_NETWORK_WEBSOCKET_FACTORY_H_
84