Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_NOTIFICATION_XMPP_CHANNEL_H_ |
| 6 | #define LIBWEAVE_SRC_NOTIFICATION_XMPP_CHANNEL_H_ |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 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> |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 16 | #include <weave/stream.h> |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 17 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/backoff_entry.h" |
| 19 | #include "src/notification/notification_channel.h" |
| 20 | #include "src/notification/xmpp_iq_stanza_handler.h" |
| 21 | #include "src/notification/xmpp_stream_parser.h" |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 22 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 23 | namespace weave { |
| 24 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 25 | namespace provider { |
| 26 | class Network; |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 27 | class TaskRunner; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 28 | } |
Vitaly Buka | 387b4f4 | 2015-07-30 23:55:13 -0700 | [diff] [blame] | 29 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 30 | // Simple interface to abstract XmppChannel's SendMessage() method. |
| 31 | class XmppChannelInterface { |
| 32 | public: |
| 33 | virtual void SendMessage(const std::string& message) = 0; |
| 34 | |
| 35 | protected: |
Vitaly Buka | 3bfb13d | 2015-11-24 14:46:13 -0800 | [diff] [blame] | 36 | virtual ~XmppChannelInterface() {} |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 39 | class XmppChannel : public NotificationChannel, |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 40 | public XmppStreamParser::Delegate, |
| 41 | public XmppChannelInterface { |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 42 | public: |
| 43 | // |account| is the robot account for buffet and |access_token| |
| 44 | // it the OAuth token. Note that the OAuth token expires fairly frequently |
| 45 | // so you will need to reset the XmppClient every time this happens. |
| 46 | XmppChannel(const std::string& account, |
| 47 | const std::string& access_token, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 48 | provider::TaskRunner* task_runner, |
| 49 | provider::Network* network); |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 50 | ~XmppChannel() override = default; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 51 | |
| 52 | // Overrides from NotificationChannel. |
| 53 | std::string GetName() const override; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 54 | bool IsConnected() const override; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 55 | void AddChannelParameters(base::DictionaryValue* channel_json) override; |
| 56 | void Start(NotificationDelegate* delegate) override; |
| 57 | void Stop() override; |
| 58 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 59 | const std::string& jid() const { return jid_; } |
| 60 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 61 | // Internal states for the XMPP stream. |
| 62 | enum class XmppState { |
| 63 | kNotStarted, |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 64 | kConnecting, |
| 65 | kConnected, |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 66 | kAuthenticationStarted, |
| 67 | kAuthenticationFailed, |
| 68 | kStreamRestartedPostAuthentication, |
| 69 | kBindSent, |
| 70 | kSessionStarted, |
| 71 | kSubscribeStarted, |
| 72 | kSubscribed, |
| 73 | }; |
| 74 | |
| 75 | protected: |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 76 | // These methods are internal helpers that can be overloaded by unit tests |
| 77 | // to help provide unit-test-specific functionality. |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 78 | virtual void SchedulePing(base::TimeDelta interval, base::TimeDelta timeout); |
| 79 | void ScheduleRegularPing(); |
| 80 | void ScheduleFastPing(); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 81 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 82 | private: |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 83 | friend class IqStanzaHandler; |
Vitaly Buka | a4b3983 | 2015-09-09 02:11:03 -0700 | [diff] [blame] | 84 | friend class FakeXmppChannel; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 85 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 86 | // Overrides from XmppStreamParser::Delegate. |
| 87 | void OnStreamStart(const std::string& node_name, |
| 88 | std::map<std::string, std::string> attributes) override; |
| 89 | void OnStreamEnd(const std::string& node_name) override; |
| 90 | void OnStanza(std::unique_ptr<XmlNode> stanza) override; |
| 91 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 92 | // Overrides from XmppChannelInterface. |
| 93 | void SendMessage(const std::string& message) override; |
| 94 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 95 | void HandleStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 96 | void HandleMessageStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 97 | void RestartXmppStream(); |
| 98 | |
Vitaly Buka | a4b3983 | 2015-09-09 02:11:03 -0700 | [diff] [blame] | 99 | void CreateSslSocket(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 100 | void OnSslSocketReady(std::unique_ptr<Stream> stream, ErrorPtr error); |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 101 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 102 | void WaitForMessage(); |
| 103 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 104 | void OnMessageRead(size_t size, ErrorPtr error); |
| 105 | void OnMessageSent(ErrorPtr error); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 106 | void Restart(); |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 107 | void CloseStream(); |
| 108 | |
| 109 | // XMPP connection state machine's state handlers. |
| 110 | void OnBindCompleted(std::unique_ptr<XmlNode> reply); |
| 111 | void OnSessionEstablished(std::unique_ptr<XmlNode> reply); |
| 112 | void OnSubscribed(std::unique_ptr<XmlNode> reply); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 113 | |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 114 | // Sends a ping request to the server to check if the connection is still |
| 115 | // valid. |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 116 | void PingServer(base::TimeDelta timeout); |
| 117 | void OnPingResponse(base::Time sent_time, std::unique_ptr<XmlNode> reply); |
| 118 | void OnPingTimeout(base::Time sent_time); |
| 119 | |
Vitaly Buka | bf6840a | 2015-09-21 13:38:56 -0700 | [diff] [blame] | 120 | void OnConnectivityChanged(); |
Alex Vakulenko | 5cd7997 | 2015-06-01 13:21:18 -0700 | [diff] [blame] | 121 | |
Vitaly Buka | a4b3983 | 2015-09-09 02:11:03 -0700 | [diff] [blame] | 122 | XmppState state_{XmppState::kNotStarted}; |
| 123 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 124 | // Robot account name for the device. |
| 125 | std::string account_; |
| 126 | |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 127 | // Full JID of this device. |
| 128 | std::string jid_; |
| 129 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 130 | // OAuth access token for the account. Expires fairly frequently. |
| 131 | std::string access_token_; |
| 132 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 133 | provider::Network* network_{nullptr}; |
Vitaly Buka | a4b3983 | 2015-09-09 02:11:03 -0700 | [diff] [blame] | 134 | std::unique_ptr<Stream> stream_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 135 | |
| 136 | // Read buffer for incoming message packets. |
| 137 | std::vector<char> read_socket_data_; |
| 138 | // Write buffer for outgoing message packets. |
| 139 | std::string write_socket_data_; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 140 | std::string queued_write_data_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 141 | |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 142 | // XMPP server name and port used for connection. |
| 143 | std::string host_; |
| 144 | uint16_t port_{0}; |
| 145 | |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 146 | BackoffEntry backoff_entry_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 147 | NotificationDelegate* delegate_{nullptr}; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 148 | provider::TaskRunner* task_runner_{nullptr}; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 149 | XmppStreamParser stream_parser_{this}; |
| 150 | bool read_pending_{false}; |
| 151 | bool write_pending_{false}; |
Alex Vakulenko | dea76b2 | 2015-06-01 13:18:06 -0700 | [diff] [blame] | 152 | std::unique_ptr<IqStanzaHandler> iq_stanza_handler_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 153 | |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 154 | base::WeakPtrFactory<XmppChannel> ping_ptr_factory_{this}; |
| 155 | base::WeakPtrFactory<XmppChannel> task_ptr_factory_{this}; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 156 | base::WeakPtrFactory<XmppChannel> weak_ptr_factory_{this}; |
| 157 | DISALLOW_COPY_AND_ASSIGN(XmppChannel); |
| 158 | }; |
| 159 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 160 | } // namespace weave |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 161 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 162 | #endif // LIBWEAVE_SRC_NOTIFICATION_XMPP_CHANNEL_H_ |