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