summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_client.h
blob: d0b140b52c7b7612b67ad80dd0722bde14f7e851 (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
// 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 <vector>

#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "remoting/jingle_glue/iq_request.h"
#include "remoting/jingle_glue/signal_strategy.h"
#include "remoting/jingle_glue/xmpp_proxy.h"

class MessageLoop;
class Task;

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

namespace cricket {
class HttpPortAllocator;
}  // namespace cricket

namespace cricket {
class BasicPortAllocator;
class SessionManager;
}  // namespace cricket

namespace remoting {

class JingleInfoRequest;
class PortAllocatorSessionFactory;

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(MessageLoop* message_loop,
               SignalStrategy* signal_strategy,
               talk_base::NetworkManager* network_manager,
               talk_base::PacketSocketFactory* socket_factory,
               PortAllocatorSessionFactory* session_factory,
               Callback* callback);
  virtual ~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. |closed_task| is executed after
  // the connection is successfully closed.
  void Close(const base::Closure& 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(const base::Closure& closed_task);

  // 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_;

  MessageLoop* message_loop_;

  // 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_