Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
| 5 | #ifndef BUFFET_NOTIFICATION_XMPP_CHANNEL_H_ |
| 6 | #define BUFFET_NOTIFICATION_XMPP_CHANNEL_H_ |
| 7 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 8 | #include <map> |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <base/callback_forward.h> |
| 14 | #include <base/macros.h> |
| 15 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 16 | #include <base/single_thread_task_runner.h> |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 17 | #include <base/timer/timer.h> |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 18 | #include <chromeos/backoff_entry.h> |
| 19 | #include <chromeos/streams/stream.h> |
| 20 | |
| 21 | #include "buffet/notification/notification_channel.h" |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 22 | #include "buffet/notification/xmpp_iq_stanza_handler.h" |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 23 | #include "buffet/notification/xmpp_stream_parser.h" |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace buffet { |
| 26 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 27 | // Simple interface to abstract XmppChannel's SendMessage() method. |
| 28 | class XmppChannelInterface { |
| 29 | public: |
| 30 | virtual void SendMessage(const std::string& message) = 0; |
| 31 | |
| 32 | protected: |
| 33 | virtual ~XmppChannelInterface() = default; |
| 34 | }; |
| 35 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 36 | class XmppChannel : public NotificationChannel, |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 37 | public XmppStreamParser::Delegate, |
| 38 | public XmppChannelInterface { |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 39 | public: |
| 40 | // |account| is the robot account for buffet and |access_token| |
| 41 | // it the OAuth token. Note that the OAuth token expires fairly frequently |
| 42 | // so you will need to reset the XmppClient every time this happens. |
| 43 | XmppChannel(const std::string& account, |
| 44 | const std::string& access_token, |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 45 | const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 46 | ~XmppChannel() override = default; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 47 | |
| 48 | // Overrides from NotificationChannel. |
| 49 | std::string GetName() const override; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 50 | bool IsConnected() const override; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 51 | void AddChannelParameters(base::DictionaryValue* channel_json) override; |
| 52 | void Start(NotificationDelegate* delegate) override; |
| 53 | void Stop() override; |
| 54 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 55 | const std::string& jid() const { return jid_; } |
| 56 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 57 | // Internal states for the XMPP stream. |
| 58 | enum class XmppState { |
| 59 | kNotStarted, |
| 60 | kStarted, |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 61 | kTlsStarted, |
| 62 | kTlsCompleted, |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 63 | kAuthenticationStarted, |
| 64 | kAuthenticationFailed, |
| 65 | kStreamRestartedPostAuthentication, |
| 66 | kBindSent, |
| 67 | kSessionStarted, |
| 68 | kSubscribeStarted, |
| 69 | kSubscribed, |
| 70 | }; |
| 71 | |
| 72 | protected: |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 73 | // These methods are internal helpers that can be overloaded by unit tests |
| 74 | // to help provide unit-test-specific functionality. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 75 | virtual void Connect(const std::string& host, uint16_t port, |
| 76 | const base::Closure& callback); |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 77 | virtual void StartPingTimer(); |
| 78 | virtual void StopPingTimer(); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 79 | |
| 80 | XmppState state_{XmppState::kNotStarted}; |
| 81 | |
| 82 | // The connection socket stream to the XMPP server. |
| 83 | chromeos::Stream* stream_{nullptr}; |
| 84 | |
| 85 | private: |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 86 | friend class IqStanzaHandler; |
| 87 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 88 | // Overrides from XmppStreamParser::Delegate. |
| 89 | void OnStreamStart(const std::string& node_name, |
| 90 | std::map<std::string, std::string> attributes) override; |
| 91 | void OnStreamEnd(const std::string& node_name) override; |
| 92 | void OnStanza(std::unique_ptr<XmlNode> stanza) override; |
| 93 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 94 | // Overrides from XmppChannelInterface. |
| 95 | void SendMessage(const std::string& message) override; |
| 96 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 97 | void HandleStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 98 | void HandleMessageStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 99 | void RestartXmppStream(); |
| 100 | |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 101 | void StartTlsHandshake(); |
| 102 | void OnTlsHandshakeComplete(chromeos::StreamPtr tls_stream); |
| 103 | void OnTlsError(const chromeos::Error* error); |
| 104 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 105 | void WaitForMessage(); |
| 106 | |
| 107 | void OnConnected(); |
| 108 | void OnMessageRead(size_t size); |
| 109 | void OnMessageSent(); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 110 | void OnReadError(const chromeos::Error* error); |
| 111 | void OnWriteError(const chromeos::Error* error); |
| 112 | void Restart(); |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 113 | void CloseStream(); |
| 114 | |
| 115 | // XMPP connection state machine's state handlers. |
| 116 | void OnBindCompleted(std::unique_ptr<XmlNode> reply); |
| 117 | void OnSessionEstablished(std::unique_ptr<XmlNode> reply); |
| 118 | void OnSubscribed(std::unique_ptr<XmlNode> reply); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 119 | |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 120 | // Sends a ping request to the server to check if the connection is still |
| 121 | // valid. |
| 122 | void PingServer(); |
| 123 | void OnPingResponse(std::unique_ptr<XmlNode> reply); |
| 124 | void OnPingTimeout(); |
| 125 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 126 | // Robot account name for the device. |
| 127 | std::string account_; |
| 128 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 129 | // Full JID of this device. |
| 130 | std::string jid_; |
| 131 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 132 | // OAuth access token for the account. Expires fairly frequently. |
| 133 | std::string access_token_; |
| 134 | |
| 135 | chromeos::StreamPtr raw_socket_; |
Alex Vakulenko | 26f557b | 2015-05-26 16:47:40 -0700 | [diff] [blame] | 136 | chromeos::StreamPtr tls_stream_; // Must follow |raw_socket_|. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 137 | |
| 138 | // Read buffer for incoming message packets. |
| 139 | std::vector<char> read_socket_data_; |
| 140 | // Write buffer for outgoing message packets. |
| 141 | std::string write_socket_data_; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 142 | std::string queued_write_data_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 143 | |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 144 | // XMPP server name and port used for connection. |
| 145 | std::string host_; |
| 146 | uint16_t port_{0}; |
| 147 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 148 | chromeos::BackoffEntry backoff_entry_; |
| 149 | NotificationDelegate* delegate_{nullptr}; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 150 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 151 | XmppStreamParser stream_parser_{this}; |
| 152 | bool read_pending_{false}; |
| 153 | bool write_pending_{false}; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 154 | std::unique_ptr<IqStanzaHandler> iq_stanza_handler_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 155 | |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 156 | base::Timer ping_timer_{true, true}; |
| 157 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 158 | base::WeakPtrFactory<XmppChannel> weak_ptr_factory_{this}; |
| 159 | DISALLOW_COPY_AND_ASSIGN(XmppChannel); |
| 160 | }; |
| 161 | |
| 162 | } // namespace buffet |
| 163 | |
| 164 | #endif // BUFFET_NOTIFICATION_XMPP_CHANNEL_H_ |
| 165 | |