summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/pepper_session.h
blob: 861ff8df32404969167c383c64056de2e56a7981 (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
// 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_PROTOCOL_PEPPER_SESSION_H_
#define REMOTING_PROTOCOL_PEPPER_SESSION_H_

#include <map>
#include <string>

#include "base/memory/ref_counted.h"
#include "base/timer.h"
#include "crypto/rsa_private_key.h"
#include "net/base/completion_callback.h"
#include "remoting/protocol/jingle_messages.h"
#include "remoting/protocol/session.h"
#include "remoting/protocol/session_config.h"

namespace net {
class CertVerifier;
class ClientSocketFactory;
class Socket;
class StreamSocket;
class X509Certificate;
}  // namespace net

namespace pp {
class Instance;
}  // namespace pp

namespace remoting {

class IqRequest;

namespace protocol {

class PepperChannel;
class PepperSessionManager;
class SocketWrapper;

// Implements the protocol::Session interface using the Pepper P2P
// Transport API. Created by PepperSessionManager for incoming and
// outgoing connections.
class PepperSession : public Session {
 public:
  virtual ~PepperSession();

  // Session interface.
  virtual void SetStateChangeCallback(
      const StateChangeCallback& callback) OVERRIDE;
  virtual Error error() OVERRIDE;
  virtual void CreateStreamChannel(
      const std::string& name,
      const StreamChannelCallback& callback) OVERRIDE;
  virtual void CreateDatagramChannel(
      const std::string& name,
      const DatagramChannelCallback& callback) OVERRIDE;
  virtual net::Socket* control_channel() OVERRIDE;
  virtual net::Socket* event_channel() OVERRIDE;
  virtual const std::string& jid() OVERRIDE;
  virtual const CandidateSessionConfig* candidate_config() OVERRIDE;
  virtual const SessionConfig& config() OVERRIDE;
  virtual void set_config(const SessionConfig& config) OVERRIDE;
  virtual const std::string& initiator_token() OVERRIDE;
  virtual void set_initiator_token(const std::string& initiator_token) OVERRIDE;
  virtual const std::string& receiver_token() OVERRIDE;
  virtual void set_receiver_token(const std::string& receiver_token) OVERRIDE;
  virtual void set_shared_secret(const std::string& secret) OVERRIDE;
  virtual const std::string& shared_secret() OVERRIDE;
  virtual void Close() OVERRIDE;

 private:
  friend class PepperSessionManager;
  friend class PepperStreamChannel;

  typedef std::map<std::string, PepperChannel*> ChannelsMap;

  PepperSession(PepperSessionManager* session_manager);

  // Start cs connection by sending session-initiate message.
  void StartConnection(const std::string& peer_jid,
                       const std::string& peer_public_key,
                       const std::string& client_token,
                       CandidateSessionConfig* config,
                       const StateChangeCallback& state_change_callback);

  // Handler for session-initiate response.
  void OnSessionInitiateResponse(const buzz::XmlElement* response);

  // Called when an error occurs. Sets |error_| and closes the session.
  void OnError(Error error);

  // Called by PepperSessionManager on incoming |message|. Must fill
  // in |reply|.
  void OnIncomingMessage(const JingleMessage& message,
                         JingleMessageReply* reply);

  // Message handlers for incoming messages.
  void OnAccept(const JingleMessage& message, JingleMessageReply* reply);
  void OnTerminate(const JingleMessage& message, JingleMessageReply* reply);
  void ProcessTransportInfo(const JingleMessage& message);

  // Called from OnAccept() to initialize session config.
  bool InitializeConfigFromDescription(const ContentDescription* description);

  // Called by PepperChannel.
  void AddLocalCandidate(const cricket::Candidate& candidate);
  void OnDeleteChannel(PepperChannel* channel);

  void SendTransportInfo();
  void OnTransportInfoResponse(const buzz::XmlElement* response);

  // Helper methods to create event and control channels.
  // TODO(sergeyu): Remove these methods.
  void CreateChannels();
  void OnChannelConnected(scoped_ptr<net::Socket>* socket_container,
                          net::StreamSocket* socket);

  // Close all the channels and terminate the session.
  void CloseInternal(bool failed);

  // Sets |state_| to |new_state| and calls state change callback.
  void SetState(State new_state);

  PepperSessionManager* session_manager_;
  std::string peer_jid_;
  std::string peer_public_key_;
  scoped_ptr<CandidateSessionConfig> candidate_config_;
  StateChangeCallback state_change_callback_;

  std::string session_id_;
  State state_;
  Error error_;

  std::string remote_cert_;
  SessionConfig config_;

  std::string shared_secret_;
  std::string initiator_token_;
  std::string receiver_token_;

  scoped_ptr<IqRequest> initiate_request_;
  scoped_ptr<IqRequest> transport_info_request_;

  ChannelsMap channels_;

  scoped_ptr<net::Socket> control_channel_socket_;
  scoped_ptr<net::Socket> event_channel_socket_;

  base::OneShotTimer<PepperSession> transport_infos_timer_;
  std::list<cricket::Candidate> pending_candidates_;

  DISALLOW_COPY_AND_ASSIGN(PepperSession);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_PEPPER_SESSION_H_