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)// The purpose of SessionManager is to facilitate creation of chromotocol 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sessions. Both host and client use it to establish chromotocol 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sessions. JingleChromotocolServer implements this inteface using 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// libjingle. 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OUTGOING SESSIONS 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Connect() must be used to create new session to a remote host. The 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returned session is initially in INITIALIZING state. Later state is 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// changed to CONNECTED if the session is accepted by the host or 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CLOSED if the session is rejected. 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// INCOMING SESSIONS 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The IncomingSessionCallback is called when a client attempts to connect. 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The callback function decides whether the session should be accepted or 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// rejected. 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// AUTHENTICATION 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Implementations of the Session and SessionManager interfaces 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// delegate authentication to an Authenticator implementation. For 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// incoming connections authenticators are created using an 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// AuthenticatorFactory set via the set_authenticator_factory() 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// method. For outgoing sessions authenticator must be passed to the 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Connect() method. The Session's state changes to AUTHENTICATED once 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// authentication succeeds. 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SESSION OWNERSHIP AND SHUTDOWN 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The SessionManager must not be closed or destroyed before all sessions 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created by that SessionManager are destroyed. Caller owns Sessions 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// created by a SessionManager (except rejected 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// sessions). The SignalStrategy must outlive the SessionManager. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PROTOCOL VERSION NEGOTIATION 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When client connects to a host it sends a session-initiate stanza with list 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of supported configurations for each channel. If the host decides to accept 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// session, then it selects configuration that is supported by both sides 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and then replies with the session-accept stanza that contans selected 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// configuration. The configuration specified in the session-accept is used 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for the session. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The CandidateSessionConfig class represents list of configurations 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// supported by an endpoint. The |candidate_config| argument in the Connect() 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// specifies configuration supported on the client side. When the host receives 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// session-initiate stanza, the IncomingSessionCallback is called. The 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// configuration sent in the session-intiate staza is available via 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ChromotocolConnnection::candidate_config(). If an incoming session is 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// being accepted then the IncomingSessionCallback callback function must 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// select session configuration and then set it with Session::set_config(). 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef REMOTING_PROTOCOL_SESSION_MANAGER_H_ 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define REMOTING_PROTOCOL_SESSION_MANAGER_H_ 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h" 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/non_thread_safe.h" 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "remoting/protocol/session.h" 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace remoting { 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SignalStrategy; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace protocol { 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Authenticator; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AuthenticatorFactory; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Generic interface for Chromoting session manager. 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(sergeyu): Split this into two separate interfaces: one for the 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// client side and one for the host side. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SessionManager : public base::NonThreadSafe { 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SessionManager() {} 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~SessionManager() {} 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) enum IncomingSessionResponse { 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Accept the session. 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ACCEPT, 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reject the session due to incompatible session configuration. 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) INCOMPATIBLE, 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reject the session because the host is currently disabled due 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to previous login attempts. 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) OVERLOAD, 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Reject the session because the client is not allowed to connect 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to the host. 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DECLINE, 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) class Listener { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Listener() {} 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when the session manager is ready to create outgoing 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // sessions. May be called from Init() or after Init() 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // returns. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnSessionManagerReady() = 0; 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when a new session is received. If the host decides to 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // accept the session it should set the |response| to 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ACCEPT. Otherwise it should set it to DECLINE, or 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // INCOMPATIBLE. INCOMPATIBLE indicates that the session has 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // incompatible configuration, and cannot be accepted. If the 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // callback accepts the |session| then it must also set 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // configuration for the |session| using Session::set_config(). 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The callback must take ownership of the |session| if it ACCEPTs it. 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnIncomingSession(Session* session, 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IncomingSessionResponse* response) = 0; 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~Listener() {} 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) }; 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initializes the session client. Caller retains ownership of the 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |signal_strategy| and |listener|. 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Init(SignalStrategy* signal_strategy, 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Listener* listener) = 0; 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Tries to create a session to the host |jid|. Must be called only 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // after initialization has finished successfully, i.e. after 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Listener::OnInitialized() has been called. 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |host_jid| is the full jid of the host to connect to. 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |authenticator| is a client authenticator for the session. 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |config| contains the session configurations that the client supports. 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual scoped_ptr<Session> Connect( 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& host_jid, 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<Authenticator> authenticator, 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<CandidateSessionConfig> config) = 0; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Close session manager. Can be called only after all corresponding 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // sessions are destroyed. No callbacks are called after this method 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // returns. 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Close() = 0; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set authenticator factory that should be used to authenticate 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // incoming connection. No connections will be accepted if 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // authenticator factory isn't set. Must not be called more than 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // once per SessionManager because it may not be safe to delete 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // factory before all authenticators it created are deleted. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void set_authenticator_factory( 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<AuthenticatorFactory> authenticator_factory) = 0; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(SessionManager); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace protocol 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace remoting 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // REMOTING_PROTOCOL_SESSION_MANAGER_H_ 159