blob: d83df480723643e464d04d19df949bf26beeae46 [file] [log] [blame]
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -07001// 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 Vakulenkobf71f702015-05-18 14:30:56 -07008#include <map>
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -07009#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 Vakulenkobf71f702015-05-18 14:30:56 -070021#include "buffet/notification/xmpp_stream_parser.h"
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070022
23namespace buffet {
24
Alex Vakulenkobf71f702015-05-18 14:30:56 -070025class XmppChannel : public NotificationChannel,
26 public XmppStreamParser::Delegate {
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070027 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 Vakulenko534a3122015-05-22 15:48:53 -070034 ~XmppChannel() override = default;
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070035
36 // Overrides from NotificationChannel.
37 std::string GetName() const override;
Alex Vakulenko6b028ae2015-05-29 09:38:59 -070038 bool IsConnected() const override;
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070039 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 Vakulenko2684b512015-05-19 13:42:10 -070047 kTlsStarted,
48 kTlsCompleted,
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070049 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 Vakulenkobf71f702015-05-18 14:30:56 -070068 // 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 Vakulenko6e3c30e2015-05-21 17:39:25 -070075 void HandleMessageStanza(std::unique_ptr<XmlNode> stanza);
Alex Vakulenkobf71f702015-05-18 14:30:56 -070076 void RestartXmppStream();
77
Alex Vakulenko2684b512015-05-19 13:42:10 -070078 void StartTlsHandshake();
79 void OnTlsHandshakeComplete(chromeos::StreamPtr tls_stream);
80 void OnTlsError(const chromeos::Error* error);
81
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070082 void SendMessage(const std::string& message);
83 void WaitForMessage();
84
85 void OnConnected();
86 void OnMessageRead(size_t size);
87 void OnMessageSent();
Alex Vakulenkobf71f702015-05-18 14:30:56 -070088 void OnReadError(const chromeos::Error* error);
89 void OnWriteError(const chromeos::Error* error);
90 void Restart();
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070091
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 Vakulenko26f557b2015-05-26 16:47:40 -070099 chromeos::StreamPtr tls_stream_; // Must follow |raw_socket_|.
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700100
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 Vakulenkobf71f702015-05-18 14:30:56 -0700105 std::string queued_write_data_;
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700106
Alex Vakulenko2684b512015-05-19 13:42:10 -0700107 // XMPP server name and port used for connection.
108 std::string host_;
109 uint16_t port_{0};
110
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700111 chromeos::BackoffEntry backoff_entry_;
112 NotificationDelegate* delegate_{nullptr};
113 scoped_refptr<base::TaskRunner> task_runner_;
Alex Vakulenkobf71f702015-05-18 14:30:56 -0700114 XmppStreamParser stream_parser_{this};
115 bool read_pending_{false};
116 bool write_pending_{false};
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700117
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