1// Copyright (c) 2012 The Chromium 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// The XmppSignalStrategy encapsulates all the logic to perform the signaling
6// STUN/ICE for jingle via a direct XMPP connection.
7//
8// This class is not threadsafe.
9
10#ifndef REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
11#define REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
12
13#include "remoting/jingle_glue/signal_strategy.h"
14
15#include <vector>
16
17#include "base/compiler_specific.h"
18#include "base/observer_list.h"
19#include "base/threading/non_thread_safe.h"
20#include "base/timer/timer.h"
21#include "third_party/libjingle/source/talk/base/sigslot.h"
22#include "third_party/libjingle/source/talk/xmpp/xmppclient.h"
23
24namespace net {
25class ClientSocketFactory;
26class URLRequestContextGetter;
27}  // namespace net
28
29namespace talk_base {
30class TaskRunner;
31}  // namespace talk_base
32
33namespace remoting {
34
35class JingleThread;
36
37class XmppSignalStrategy : public base::NonThreadSafe,
38                           public SignalStrategy,
39                           public buzz::XmppStanzaHandler,
40                           public sigslot::has_slots<> {
41 public:
42  // XMPP Server configuration for XmppSignalStrategy.
43  struct XmppServerConfig {
44    XmppServerConfig();
45    ~XmppServerConfig();
46
47    std::string host;
48    int port;
49    bool use_tls;
50
51    std::string username;
52    std::string auth_token;
53    std::string auth_service;
54  };
55
56  XmppSignalStrategy(
57      net::ClientSocketFactory* socket_factory,
58      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
59      const XmppServerConfig& xmpp_server_config);
60  virtual ~XmppSignalStrategy();
61
62  // SignalStrategy interface.
63  virtual void Connect() OVERRIDE;
64  virtual void Disconnect() OVERRIDE;
65  virtual State GetState() const OVERRIDE;
66  virtual Error GetError() const OVERRIDE;
67  virtual std::string GetLocalJid() const OVERRIDE;
68  virtual void AddListener(Listener* listener) OVERRIDE;
69  virtual void RemoveListener(Listener* listener) OVERRIDE;
70  virtual bool SendStanza(scoped_ptr<buzz::XmlElement> stanza) OVERRIDE;
71  virtual std::string GetNextId() OVERRIDE;
72
73  // buzz::XmppStanzaHandler interface.
74  virtual bool HandleStanza(const buzz::XmlElement* stanza) OVERRIDE;
75
76  // This method is used to update the auth info (for example when the OAuth
77  // access token is renewed). It is OK to call this even when we are in the
78  // CONNECTED state. It will be used on the next Connect() call.
79  void SetAuthInfo(const std::string& username,
80                   const std::string& auth_token,
81                   const std::string& auth_service);
82
83  // Use this method to override the default resource name used (optional).
84  // This will be used on the next Connect() call.
85  void SetResourceName(const std::string& resource_name);
86
87 private:
88  static buzz::PreXmppAuth* CreatePreXmppAuth(
89      const buzz::XmppClientSettings& settings);
90
91  void OnConnectionStateChanged(buzz::XmppEngine::State state);
92  void SetState(State new_state);
93
94  void SendKeepAlive();
95
96  net::ClientSocketFactory* socket_factory_;
97  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
98  std::string resource_name_;
99  scoped_ptr<talk_base::TaskRunner> task_runner_;
100  buzz::XmppClient* xmpp_client_;
101  XmppServerConfig xmpp_server_config_;
102
103  State state_;
104  Error error_;
105
106  ObserverList<Listener, true> listeners_;
107
108  base::RepeatingTimer<XmppSignalStrategy> keep_alive_timer_;
109
110  DISALLOW_COPY_AND_ASSIGN(XmppSignalStrategy);
111};
112
113}  // namespace remoting
114
115#endif  // REMOTING_JINGLE_GLUE_XMPP_SIGNAL_STRATEGY_H_
116