summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_client.h
blob: cb7e2d3f849ac3aa523cbf4232b6dba0275e9862 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2011 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.

#ifndef REMOTING_JINGLE_GLUE_JINGLE_CLIENT_H_
#define REMOTING_JINGLE_GLUE_JINGLE_CLIENT_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "remoting/jingle_glue/iq_request.h"
#include "remoting/jingle_glue/xmpp_proxy.h"
#include "third_party/libjingle/source/talk/xmpp/xmppclient.h"

class MessageLoop;
class Task;

namespace talk_base {
class NetworkManager;
class PacketSocketFactory;
}  // namespace talk_base

namespace buzz {
class PreXmppAuth;
}  // namespace buzz

namespace cricket {
class HttpPortAllocator;
}  // namespace cricket

namespace cricket {
class BasicPortAllocator;
class SessionManager;
class TunnelSessionClient;
class SessionManagerTask;
class Session;
}  // namespace cricket

namespace remoting {

class JingleThread;
class PortAllocatorSessionFactory;
class XmppProxy;

// TODO(ajwong): The SignalStrategy stuff needs to be separated out to separate
// files.
class SignalStrategy {
 public:
  class StatusObserver {
   public:
    enum State {
      START,
      CONNECTING,
      CONNECTED,
      CLOSED,
    };

    // Called when state of the connection is changed.
    virtual void OnStateChange(State state) = 0;
    virtual void OnJidChange(const std::string& full_jid) = 0;
  };

  SignalStrategy() {}
  virtual ~SignalStrategy() {}
  virtual void Init(StatusObserver* observer) = 0;
  virtual void StartSession(cricket::SessionManager* session_manager) = 0;
  virtual void EndSession() = 0;
  virtual IqRequest* CreateIqRequest() = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(SignalStrategy);
};

class XmppSignalStrategy : public SignalStrategy, public sigslot::has_slots<> {
 public:
  XmppSignalStrategy(JingleThread* thread,
                     const std::string& username,
                     const std::string& auth_token,
                     const std::string& auth_token_service);
  virtual ~XmppSignalStrategy();

  virtual void Init(StatusObserver* observer);
  virtual void StartSession(cricket::SessionManager* session_manager);
  virtual void EndSession();
  virtual IqRequest* CreateIqRequest();

 private:
  friend class JingleClientTest;

  void OnConnectionStateChanged(buzz::XmppEngine::State state);
  static buzz::PreXmppAuth* CreatePreXmppAuth(
      const buzz::XmppClientSettings& settings);

  JingleThread* thread_;

  std::string username_;
  std::string auth_token_;
  std::string auth_token_service_;
  buzz::XmppClient* xmpp_client_;
  StatusObserver* observer_;

  DISALLOW_COPY_AND_ASSIGN(XmppSignalStrategy);
};

// TODO(hclam): Javascript implementation of this interface shouldn't be here.
class JavascriptSignalStrategy : public SignalStrategy {
 public:
  explicit JavascriptSignalStrategy(const std::string& your_jid);
  virtual ~JavascriptSignalStrategy();

  virtual void Init(StatusObserver* observer);
  virtual void StartSession(cricket::SessionManager* session_manager);
  virtual void EndSession();
  virtual void AttachXmppProxy(scoped_refptr<XmppProxy> xmpp_proxy);
  virtual JavascriptIqRequest* CreateIqRequest();

 private:
  std::string your_jid_;
  scoped_refptr<XmppProxy> xmpp_proxy_;
  JavascriptIqRegistry iq_registry_;
  scoped_ptr<SessionStartRequest> session_start_request_;

  DISALLOW_COPY_AND_ASSIGN(JavascriptSignalStrategy);
};

class JingleClient : public base::RefCountedThreadSafe<JingleClient>,
                     public SignalStrategy::StatusObserver {
 public:
  class Callback {
   public:
    virtual ~Callback() {}

    // Called when state of the connection is changed.
    virtual void OnStateChange(JingleClient* client, State state) = 0;
  };

  // Physical sockets are used if |network_manager| and
  // |socket_factory| are set to NULL. Otherwise ownership of these
  // objects is given to JingleClient.
  JingleClient(JingleThread* thread,
               SignalStrategy* signal_strategy,
               talk_base::NetworkManager* network_manager,
               talk_base::PacketSocketFactory* socket_factory,
               PortAllocatorSessionFactory* session_factory,
               Callback* callback);
  ~JingleClient();

  // Starts the XMPP connection initialization. Must be called only once.
  // |callback| specifies callback object for the client and must not be NULL.
  void Init();

  // Closes XMPP connection and stops the thread. Must be called before the
  // object is destroyed. If specified, |closed_task| is executed after the
  // connection is successfully closed.
  void Close();
  void Close(Task* closed_task);

  // Returns JID with resource ID. Empty string is returned if full JID is not
  // known yet, i.e. authentication hasn't finished.
  std::string GetFullJid();

  // Creates new IqRequest for this client. Ownership for of the created object
  // is transfered to the caller.
  IqRequest* CreateIqRequest();

  // The session manager used by this client. Must be called from the
  // jingle thread only. Returns NULL if the client is not active.
  cricket::SessionManager* session_manager();

  // Message loop used by this object to execute tasks.
  MessageLoop* message_loop();

  // SignalStrategy::StatusObserver implementation.
  virtual void OnStateChange(State state);
  virtual void OnJidChange(const std::string& full_jid);

 private:
  friend class JingleClientTest;

  void DoInitialize();
  void DoStartSession();
  void DoClose();

  // Updates current state of the connection. Must be called only in
  // the jingle thread.
  void UpdateState(State new_state);

  // Virtual for mocking in a unittest.
  void OnJingleInfo(const std::string& token,
                    const std::vector<std::string>& relay_hosts,
                    const std::vector<talk_base::SocketAddress>& stun_hosts);

  // This must be set to true to enable NAT traversal. STUN/Relay
  // servers are not used when NAT traversal is disabled, so P2P
  // connection will works only when both peers are on the same
  // network.
  bool enable_nat_traversing_;

  // JingleThread used for the connection. Set in the constructor.
  JingleThread* thread_;

  // Current state of the object.
  // Must be locked when accessing initialized_ or closed_.
  base::Lock state_lock_;
  State state_;
  bool initialized_;
  bool closed_;
  scoped_ptr<Task> closed_task_;

  // TODO(ajwong): HACK! remove this.  See DoStartSession() for details.
  bool initialized_finished_;

  // We need a separate lock for the jid since the |state_lock_| may be held
  // over a callback which can end up having a double lock.
  //
  // TODO(ajwong): Can we avoid holding the |state_lock_| over a callback and
  // remove this extra lock?
  base::Lock jid_lock_;
  std::string full_jid_;

  // Callback for this object. Callback must not be called if closed_ == true.
  Callback* callback_;

  SignalStrategy* signal_strategy_;
  scoped_ptr<talk_base::NetworkManager> network_manager_;
  scoped_ptr<talk_base::PacketSocketFactory> socket_factory_;
  scoped_ptr<cricket::HttpPortAllocator> port_allocator_;
  scoped_ptr<PortAllocatorSessionFactory> port_allocator_session_factory_;
  scoped_ptr<cricket::SessionManager> session_manager_;

  scoped_ptr<JingleInfoRequest> jingle_info_request_;

  DISALLOW_COPY_AND_ASSIGN(JingleClient);
};

}  // namespace remoting

#endif  // REMOTING_JINGLE_GLUE_JINGLE_CLIENT_H_