blob: 8db127e1b9bfa233bb7e226eaa3d26115e4b828a [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);
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 Vakulenko2684b512015-05-19 13:42:10 -070046 kTlsStarted,
47 kTlsCompleted,
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070048 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 Vakulenkobf71f702015-05-18 14:30:56 -070067 // 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 Vakulenko6e3c30e2015-05-21 17:39:25 -070074 void HandleMessageStanza(std::unique_ptr<XmlNode> stanza);
Alex Vakulenkobf71f702015-05-18 14:30:56 -070075 void RestartXmppStream();
76
Alex Vakulenko2684b512015-05-19 13:42:10 -070077 void StartTlsHandshake();
78 void OnTlsHandshakeComplete(chromeos::StreamPtr tls_stream);
79 void OnTlsError(const chromeos::Error* error);
80
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070081 void SendMessage(const std::string& message);
82 void WaitForMessage();
83
84 void OnConnected();
85 void OnMessageRead(size_t size);
86 void OnMessageSent();
Alex Vakulenkobf71f702015-05-18 14:30:56 -070087 void OnReadError(const chromeos::Error* error);
88 void OnWriteError(const chromeos::Error* error);
89 void Restart();
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070090
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 Vakulenko2684b512015-05-19 13:42:10 -070098 chromeos::StreamPtr tls_stream_;
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -070099
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 Vakulenkobf71f702015-05-18 14:30:56 -0700104 std::string queued_write_data_;
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700105
Alex Vakulenko2684b512015-05-19 13:42:10 -0700106 // XMPP server name and port used for connection.
107 std::string host_;
108 uint16_t port_{0};
109
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700110 chromeos::BackoffEntry backoff_entry_;
111 NotificationDelegate* delegate_{nullptr};
112 scoped_refptr<base::TaskRunner> task_runner_;
Alex Vakulenkobf71f702015-05-18 14:30:56 -0700113 XmppStreamParser stream_parser_{this};
114 bool read_pending_{false};
115 bool write_pending_{false};
Alex Vakulenkoeedf3be2015-05-13 17:52:02 -0700116
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