summaryrefslogtreecommitdiffstats
path: root/remoting/signaling/xmpp_signal_strategy.h
blob: 05eb079d9c31045b10c99de509d03a703fd93e52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// The XmppSignalStrategy encapsulates all the logic to perform the signaling
// STUN/ICE for jingle via a direct XMPP connection.
//
// This class is not threadsafe.

#ifndef REMOTING_SIGNALING_XMPP_SIGNAL_STRATEGY_H_
#define REMOTING_SIGNALING_XMPP_SIGNAL_STRATEGY_H_

#include "remoting/signaling/signal_strategy.h"

#include <vector>

#include "base/compiler_specific.h"
#include "base/observer_list.h"
#include "base/threading/non_thread_safe.h"
#include "base/timer/timer.h"
#include "third_party/libjingle/source/talk/xmpp/xmppclient.h"
#include "third_party/webrtc/base/sigslot.h"

namespace net {
class ClientSocketFactory;
class URLRequestContextGetter;
}  // namespace net

namespace rtc {
class TaskRunner;
}  // namespace rtc

namespace remoting {

class JingleThread;

class XmppSignalStrategy : public base::NonThreadSafe,
                           public SignalStrategy,
                           public buzz::XmppStanzaHandler,
                           public sigslot::has_slots<> {
 public:
  // XMPP Server configuration for XmppSignalStrategy.
  struct XmppServerConfig {
    XmppServerConfig();
    ~XmppServerConfig();

    std::string host;
    int port;
    bool use_tls;

    std::string username;
    std::string auth_token;
    std::string auth_service;
  };

  XmppSignalStrategy(
      net::ClientSocketFactory* socket_factory,
      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
      const XmppServerConfig& xmpp_server_config);
  virtual ~XmppSignalStrategy();

  // SignalStrategy interface.
  virtual void Connect() override;
  virtual void Disconnect() override;
  virtual State GetState() const override;
  virtual Error GetError() const override;
  virtual std::string GetLocalJid() const override;
  virtual void AddListener(Listener* listener) override;
  virtual void RemoveListener(Listener* listener) override;
  virtual bool SendStanza(scoped_ptr<buzz::XmlElement> stanza) override;
  virtual std::string GetNextId() override;

  // buzz::XmppStanzaHandler interface.
  virtual bool HandleStanza(const buzz::XmlElement* stanza) override;

  // This method is used to update the auth info (for example when the OAuth
  // access token is renewed). It is OK to call this even when we are in the
  // CONNECTED state. It will be used on the next Connect() call.
  void SetAuthInfo(const std::string& username,
                   const std::string& auth_token,
                   const std::string& auth_service);

  // Use this method to override the default resource name used (optional).
  // This will be used on the next Connect() call.
  void SetResourceName(const std::string& resource_name);

 private:
  static buzz::PreXmppAuth* CreatePreXmppAuth(
      const buzz::XmppClientSettings& settings);

  void OnConnectionStateChanged(buzz::XmppEngine::State state);
  void SetState(State new_state);

  void SendKeepAlive();

  net::ClientSocketFactory* socket_factory_;
  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
  std::string resource_name_;
  scoped_ptr<rtc::TaskRunner> task_runner_;
  buzz::XmppClient* xmpp_client_;
  XmppServerConfig xmpp_server_config_;

  State state_;
  Error error_;

  ObserverList<Listener, true> listeners_;

  base::RepeatingTimer<XmppSignalStrategy> keep_alive_timer_;

  DISALLOW_COPY_AND_ASSIGN(XmppSignalStrategy);
};

}  // namespace remoting

#endif  // REMOTING_SIGNALING_XMPP_SIGNAL_STRATEGY_H_