15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef REMOTING_PROTOCOL_SESSION_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define REMOTING_PROTOCOL_SESSION_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "remoting/protocol/errors.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "remoting/protocol/session_config.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IPEndPoint;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace remoting {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace protocol {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ChannelFactory;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct TransportRoute;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generic interface for Chromotocol connection used by both client and host.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Provides access to the connection channels, but doesn't depend on the
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// protocol used for each channel.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Session {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum State {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Created, but not connecting yet.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INITIALIZING,
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Sent session-initiate, but haven't received session-accept.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CONNECTING,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Received session-initiate, but haven't sent session-accept.
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ACCEPTING,
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Session has been accepted and is pending authentication.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CONNECTED,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Session has been connected and authenticated.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AUTHENTICATED,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Session has been closed.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CLOSED,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Connection has failed.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    FAILED,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class EventHandler {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EventHandler() {}
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual ~EventHandler() {}
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called after session state has changed. It is safe to destroy
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // the session from within the handler if |state| is CLOSED or
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // FAILED.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnSessionStateChange(State state) = 0;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called whenever route for the channel specified with
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // |channel_name| changes. Session must not be destroyed by the
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // handler of this event.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnSessionRouteChange(const std::string& channel_name,
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const TransportRoute& route) = 0;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Called when ready state on one of the channels changes. See
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // comments in transport.h for explanation on what this state
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // means and how it can used.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    virtual void OnSessionChannelReady(const std::string& channel_name,
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       bool ready) {}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Session() {}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Session() {}
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set event handler for this session. |event_handler| must outlive
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this object.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetEventHandler(EventHandler* event_handler) = 0;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns error code for a failed session.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ErrorCode error() = 0;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // JID of the other side.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const std::string& jid() = 0;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Configuration of the protocol that was sent or received in the
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // session-initiate jingle message. Returned pointer is valid until
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // connection is closed.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const CandidateSessionConfig* candidate_config() = 0;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Protocol configuration. Can be called only after session has been accepted.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returned pointer is valid until connection is closed.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const SessionConfig& config() = 0;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set protocol configuration for an incoming session. Must be
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // called on the host before the connection is accepted, from
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ChromotocolServer::IncomingConnectionCallback.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void set_config(const SessionConfig& config) = 0;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetTransportChannelFactory() returns a factory that creates a new transport
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // channel for each logical channel. GetMultiplexedChannelFactory() channels
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // share a single underlying transport channel
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ChannelFactory* GetTransportChannelFactory() = 0;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ChannelFactory* GetMultiplexedChannelFactory() = 0;
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Closes connection. Callbacks are guaranteed not to be called
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // after this method returns. Must be called before the object is
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // destroyed, unless the state is set to FAILED or CLOSED.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close() = 0;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Session);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace protocol
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace remoting
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // REMOTING_PROTOCOL_SESSION_H_
121