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> |
| 16 | #include <base/task_runner.h> |
| 17 | #include <chromeos/backoff_entry.h> |
| 18 | #include <chromeos/streams/stream.h> |
| 19 | |
| 20 | #include "buffet/notification/notification_channel.h" |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 21 | #include "buffet/notification/xmpp_stream_parser.h" |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 22 | |
| 23 | namespace buffet { |
| 24 | |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 25 | class XmppChannel : public NotificationChannel, |
| 26 | public XmppStreamParser::Delegate { |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 27 | public: |
| 28 | // |account| is the robot account for buffet and |access_token| |
| 29 | // it the OAuth token. Note that the OAuth token expires fairly frequently |
| 30 | // so you will need to reset the XmppClient every time this happens. |
| 31 | XmppChannel(const std::string& account, |
| 32 | const std::string& access_token, |
| 33 | const scoped_refptr<base::TaskRunner>& task_runner); |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 34 | ~XmppChannel() override = default; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 35 | |
| 36 | // Overrides from NotificationChannel. |
| 37 | std::string GetName() const override; |
Alex Vakulenko | 6b028ae | 2015-05-29 09:38:59 -0700 | [diff] [blame] | 38 | bool IsConnected() const override; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 39 | void AddChannelParameters(base::DictionaryValue* channel_json) override; |
| 40 | void Start(NotificationDelegate* delegate) override; |
| 41 | void Stop() override; |
| 42 | |
| 43 | // Internal states for the XMPP stream. |
| 44 | enum class XmppState { |
| 45 | kNotStarted, |
| 46 | kStarted, |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 47 | kTlsStarted, |
| 48 | kTlsCompleted, |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 49 | kAuthenticationStarted, |
| 50 | kAuthenticationFailed, |
| 51 | kStreamRestartedPostAuthentication, |
| 52 | kBindSent, |
| 53 | kSessionStarted, |
| 54 | kSubscribeStarted, |
| 55 | kSubscribed, |
| 56 | }; |
| 57 | |
| 58 | protected: |
| 59 | virtual void Connect(const std::string& host, uint16_t port, |
| 60 | const base::Closure& callback); |
| 61 | |
| 62 | XmppState state_{XmppState::kNotStarted}; |
| 63 | |
| 64 | // The connection socket stream to the XMPP server. |
| 65 | chromeos::Stream* stream_{nullptr}; |
| 66 | |
| 67 | private: |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 68 | // Overrides from XmppStreamParser::Delegate. |
| 69 | void OnStreamStart(const std::string& node_name, |
| 70 | std::map<std::string, std::string> attributes) override; |
| 71 | void OnStreamEnd(const std::string& node_name) override; |
| 72 | void OnStanza(std::unique_ptr<XmlNode> stanza) override; |
| 73 | |
| 74 | void HandleStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | 6e3c30e | 2015-05-21 17:39:25 -0700 | [diff] [blame] | 75 | void HandleMessageStanza(std::unique_ptr<XmlNode> stanza); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 76 | void RestartXmppStream(); |
| 77 | |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 78 | void StartTlsHandshake(); |
| 79 | void OnTlsHandshakeComplete(chromeos::StreamPtr tls_stream); |
| 80 | void OnTlsError(const chromeos::Error* error); |
| 81 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 82 | void SendMessage(const std::string& message); |
| 83 | void WaitForMessage(); |
| 84 | |
| 85 | void OnConnected(); |
| 86 | void OnMessageRead(size_t size); |
| 87 | void OnMessageSent(); |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 88 | void OnReadError(const chromeos::Error* error); |
| 89 | void OnWriteError(const chromeos::Error* error); |
| 90 | void Restart(); |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 91 | |
| 92 | // Robot account name for the device. |
| 93 | std::string account_; |
| 94 | |
| 95 | // OAuth access token for the account. Expires fairly frequently. |
| 96 | std::string access_token_; |
| 97 | |
| 98 | chromeos::StreamPtr raw_socket_; |
Alex Vakulenko | 26f557b | 2015-05-26 16:47:40 -0700 | [diff] [blame] | 99 | chromeos::StreamPtr tls_stream_; // Must follow |raw_socket_|. |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 100 | |
| 101 | // Read buffer for incoming message packets. |
| 102 | std::vector<char> read_socket_data_; |
| 103 | // Write buffer for outgoing message packets. |
| 104 | std::string write_socket_data_; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 105 | std::string queued_write_data_; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 106 | |
Alex Vakulenko | 2684b51 | 2015-05-19 13:42:10 -0700 | [diff] [blame] | 107 | // XMPP server name and port used for connection. |
| 108 | std::string host_; |
| 109 | uint16_t port_{0}; |
| 110 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 111 | chromeos::BackoffEntry backoff_entry_; |
| 112 | NotificationDelegate* delegate_{nullptr}; |
| 113 | scoped_refptr<base::TaskRunner> task_runner_; |
Alex Vakulenko | bf71f70 | 2015-05-18 14:30:56 -0700 | [diff] [blame] | 114 | XmppStreamParser stream_parser_{this}; |
| 115 | bool read_pending_{false}; |
| 116 | bool write_pending_{false}; |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 117 | |
| 118 | base::WeakPtrFactory<XmppChannel> weak_ptr_factory_{this}; |
| 119 | DISALLOW_COPY_AND_ASSIGN(XmppChannel); |
| 120 | }; |
| 121 | |
| 122 | } // namespace buffet |
| 123 | |
| 124 | #endif // BUFFET_NOTIFICATION_XMPP_CHANNEL_H_ |
| 125 | |