Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef API_DTLS_TRANSPORT_INTERFACE_H_ |
| 12 | #define API_DTLS_TRANSPORT_INTERFACE_H_ |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 13 | |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame^] | 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 17 | #include "api/ice_transport_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "api/rtc_error.h" |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 19 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/ref_count.h" |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame^] | 21 | #include "rtc_base/ssl_certificate.h" |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 25 | // States of a DTLS transport, corresponding to the JS API specification. |
| 26 | // http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate |
| 27 | enum class DtlsTransportState { |
| 28 | kNew, // Has not started negotiating yet. |
| 29 | kConnecting, // In the process of negotiating a secure connection. |
| 30 | kConnected, // Completed negotiation and verified fingerprints. |
| 31 | kClosed, // Intentionally closed. |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 32 | kFailed, // Failure due to an error or failing to verify a remote |
| 33 | // fingerprint. |
| 34 | kNumValues |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | // This object gives snapshot information about the changeable state of a |
| 38 | // DTLSTransport. |
| 39 | class DtlsTransportInformation { |
| 40 | public: |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame^] | 41 | DtlsTransportInformation(); |
| 42 | explicit DtlsTransportInformation(DtlsTransportState state); |
| 43 | DtlsTransportInformation( |
| 44 | DtlsTransportState state, |
| 45 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates); |
| 46 | // Copy and assign |
| 47 | DtlsTransportInformation(const DtlsTransportInformation& c); |
| 48 | DtlsTransportInformation& operator=(const DtlsTransportInformation& c); |
| 49 | // Move |
| 50 | DtlsTransportInformation(DtlsTransportInformation&& other) = default; |
| 51 | DtlsTransportInformation& operator=(DtlsTransportInformation&& other) = |
| 52 | default; |
| 53 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 54 | DtlsTransportState state() const { return state_; } |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame^] | 55 | // The accessor returns a temporary pointer, it does not release ownership. |
| 56 | const rtc::SSLCertChain* remote_ssl_certificates() const { |
| 57 | return remote_ssl_certificates_.get(); |
| 58 | } |
| 59 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 60 | private: |
| 61 | DtlsTransportState state_; |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame^] | 62 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_; |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class DtlsTransportObserverInterface { |
| 66 | public: |
| 67 | // This callback carries information about the state of the transport. |
| 68 | // The argument is a pass-by-value snapshot of the state. |
| 69 | virtual void OnStateChange(DtlsTransportInformation info) = 0; |
| 70 | // This callback is called when an error occurs, causing the transport |
| 71 | // to go to the kFailed state. |
| 72 | virtual void OnError(RTCError error) = 0; |
| 73 | |
| 74 | protected: |
| 75 | virtual ~DtlsTransportObserverInterface() = default; |
| 76 | }; |
| 77 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 78 | // A DTLS transport, as represented to the outside world. |
Harald Alvestrand | 69fb6c8 | 2019-02-13 19:40:11 +0100 | [diff] [blame] | 79 | // This object is created on the network thread, and can only be |
| 80 | // accessed on that thread, except for functions explicitly marked otherwise. |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 81 | // References can be held by other threads, and destruction can therefore |
| 82 | // be initiated by other threads. |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 83 | class DtlsTransportInterface : public rtc::RefCountInterface { |
| 84 | public: |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 85 | // Returns a pointer to the ICE transport that is owned by the DTLS transport. |
| 86 | virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0; |
Harald Alvestrand | 69fb6c8 | 2019-02-13 19:40:11 +0100 | [diff] [blame] | 87 | // Returns information on the state of the DtlsTransport. |
| 88 | // This function can be called from other threads. |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 89 | virtual DtlsTransportInformation Information() = 0; |
| 90 | // Observer management. |
| 91 | virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0; |
| 92 | virtual void UnregisterObserver() = 0; |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | } // namespace webrtc |
| 96 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 97 | #endif // API_DTLS_TRANSPORT_INTERFACE_H_ |