Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / signaling / xmpp_login_handler.h
blob61f978ae9f068f783b404639cc94610a56eea347
1 // Copyright 2015 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.
5 #ifndef REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_
6 #define REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "remoting/signaling/signal_strategy.h"
13 // Undefine SendMessage and ERROR defined in Windows headers.
14 #ifdef SendMessage
15 #undef SendMessage
16 #endif
18 #ifdef ERROR
19 #undef ERROR
20 #endif
22 namespace remoting {
24 class XmppStreamParser;
26 // XmppLoginHandler handles authentication handshake for XmppSignalStrategy. It
27 // receives incoming data using onDataReceived(), calls Delegate::SendMessage()
28 // to send outgoing messages and calls Delegate::OnHandshakeDone() after
29 // authentication is finished successfully or Delegate::OnError() on error.
31 // See RFC3920 for description of XMPP and authentication handshake.
32 class XmppLoginHandler {
33 public:
34 class Delegate {
35 public:
36 Delegate() {}
38 // All Delegate methods are allowed to destroy XmppLoginHandler.
39 virtual void SendMessage(const std::string& message) = 0;
40 virtual void StartTls() = 0;
41 virtual void OnHandshakeDone(const std::string& jid,
42 scoped_ptr<XmppStreamParser> parser) = 0;
43 virtual void OnLoginHandlerError(SignalStrategy::Error error) = 0;
45 protected:
46 virtual ~Delegate() {}
49 enum class TlsMode {
50 NO_TLS,
51 WITH_HANDSHAKE,
52 WITHOUT_HANDSHAKE,
55 XmppLoginHandler(const std::string& server,
56 const std::string& username,
57 const std::string& auth_token,
58 TlsMode tls_mode,
59 Delegate* delegate);
60 ~XmppLoginHandler();
62 void Start();
63 void OnDataReceived(const std::string& data);
64 void OnTlsStarted();
66 private:
67 // States the handshake goes through. States are iterated from INIT to DONE
68 // sequentially, except for ERROR state which may be accepted at any point.
70 // Following messages are sent/received in each state:
71 // INIT
72 // client -> server: Stream header
73 // client -> server: <starttls>
74 // WAIT_STREAM_HEADER
75 // client <- server: Stream header with list of supported features which
76 // should include starttls.
77 // WAIT_STARTTLS_RESPONSE
78 // client <- server: <proceed>
79 // STARTING_TLS
80 // TLS handshake
81 // client -> server: Stream header
82 // client -> server: <auth> message with the OAuth2 token.
83 // WAIT_STREAM_HEADER_AFTER_TLS
84 // client <- server: Stream header with list of supported authentication
85 // methods which is expected to include X-OAUTH2
86 // WAIT_AUTH_RESULT
87 // client <- server: <success> or <failure>
88 // client -> server: Stream header
89 // client -> server: <bind>
90 // client -> server: <iq><session/></iq> to start the session
91 // WAIT_STREAM_HEADER_AFTER_AUTH
92 // client <- server: Stream header with list of features that should
93 // include <bind>.
94 // WAIT_BIND_RESULT
95 // client <- server: <bind> result with JID.
96 // WAIT_SESSION_IQ_RESULT
97 // client <- server: result for <iq><session/></iq>
98 // DONE
99 enum class State {
100 INIT,
101 WAIT_STREAM_HEADER,
102 WAIT_STARTTLS_RESPONSE,
103 STARTING_TLS,
104 WAIT_STREAM_HEADER_AFTER_TLS,
105 WAIT_AUTH_RESULT,
106 WAIT_STREAM_HEADER_AFTER_AUTH,
107 WAIT_BIND_RESULT,
108 WAIT_SESSION_IQ_RESULT,
109 DONE,
110 ERROR,
113 // Callbacks for XmppStreamParser.
114 void OnStanza(scoped_ptr<buzz::XmlElement> stanza);
115 void OnParserError();
117 // Starts authentication handshake in WAIT_STREAM_HEADER_AFTER_TLS state.
118 void StartAuthHandshake();
120 // Helper used to send stream header.
121 void StartStream(const std::string& first_message);
123 // Report the |error| to the delegate and changes |state_| to ERROR,
124 void OnError(SignalStrategy::Error error);
126 std::string server_;
127 std::string username_;
128 std::string auth_token_;
129 TlsMode tls_mode_;
130 Delegate* delegate_;
132 State state_;
134 std::string jid_;
136 scoped_ptr<XmppStreamParser> stream_parser_;
138 DISALLOW_COPY_AND_ASSIGN(XmppLoginHandler);
141 } // namespace remoting
143 #endif // REMOTING_SIGNALING_XMPP_LOGIN_HANDLER_H_