Home | History | Annotate | Download | only in bridge
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_
      6 #define REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/message_loop/message_loop.h"
     14 #include "net/url_request/url_request_context_getter.h"
     15 #include "remoting/base/auto_thread.h"
     16 #include "remoting/client/chromoting_client.h"
     17 #include "remoting/client/client_config.h"
     18 #include "remoting/client/client_context.h"
     19 #include "remoting/client/client_user_interface.h"
     20 #include "remoting/client/frame_consumer_proxy.h"
     21 #include "remoting/client/software_video_renderer.h"
     22 
     23 #include "remoting/ios/bridge/frame_consumer_bridge.h"
     24 
     25 #include "remoting/jingle_glue/network_settings.h"
     26 #include "remoting/jingle_glue/xmpp_signal_strategy.h"
     27 #include "remoting/protocol/clipboard_stub.h"
     28 #include "remoting/protocol/connection_to_host.h"
     29 #include "remoting/protocol/cursor_shape_stub.h"
     30 
     31 namespace remoting {
     32 
     33 class ClientProxy;
     34 
     35 // ClientUserInterface that indirectly makes and receives OBJ_C calls from the
     36 // UI application
     37 class ClientInstance : public ClientUserInterface,
     38                        public protocol::ClipboardStub,
     39                        public protocol::CursorShapeStub,
     40                        public base::RefCountedThreadSafe<ClientInstance> {
     41  public:
     42   // Initiates a connection with the specified host. Call from the UI thread. To
     43   // connect with an unpaired host, pass in |pairing_id| and |pairing_secret| as
     44   // empty strings.
     45   ClientInstance(const base::WeakPtr<ClientProxy>& proxy,
     46                  const std::string& username,
     47                  const std::string& auth_token,
     48                  const std::string& host_jid,
     49                  const std::string& host_id,
     50                  const std::string& host_pubkey,
     51                  const std::string& pairing_id,
     52                  const std::string& pairing_secret);
     53 
     54   // Begins the connection process.  Should not be called again until after
     55   // |CleanUp|
     56   void Start();
     57 
     58   // Terminates the current connection (if it hasn't already failed) and cleans
     59   // up. Must be called before destruction can occur or a memory leak may occur.
     60   void Cleanup();
     61 
     62   // Notifies the user interface that the user needs to enter a PIN. The
     63   // current authentication attempt is put on hold until |callback| is invoked.
     64   // May be called on any thread.
     65   void FetchSecret(bool pairable,
     66                    const protocol::SecretFetchedCallback& callback);
     67 
     68   // Provides the user's PIN and resumes the host authentication attempt. Call
     69   // on the UI thread once the user has finished entering this PIN into the UI,
     70   // but only after the UI has been asked to provide a PIN (via FetchSecret()).
     71   void ProvideSecret(const std::string& pin, bool create_pair);
     72 
     73   // Moves the host's cursor to the specified coordinates, optionally with some
     74   // mouse button depressed. If |button| is BUTTON_UNDEFINED, no click is made.
     75   void PerformMouseAction(
     76       const webrtc::DesktopVector& position,
     77       const webrtc::DesktopVector& wheel_delta,
     78       int /* protocol::MouseEvent_MouseButton */ whichButton,
     79       bool button_down);
     80 
     81   // Sends the provided keyboard scan code to the host.
     82   void PerformKeyboardAction(int key_code, bool key_down);
     83 
     84   // ClientUserInterface implementation.
     85   virtual void OnConnectionState(protocol::ConnectionToHost::State state,
     86                                  protocol::ErrorCode error) OVERRIDE;
     87   virtual void OnConnectionReady(bool ready) OVERRIDE;
     88   virtual void OnRouteChanged(const std::string& channel_name,
     89                               const protocol::TransportRoute& route) OVERRIDE;
     90   virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
     91   virtual void SetPairingResponse(const protocol::PairingResponse& response)
     92       OVERRIDE;
     93   virtual void DeliverHostMessage(const protocol::ExtensionMessage& message)
     94       OVERRIDE;
     95   virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
     96   virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
     97   virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
     98       GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
     99 
    100   // CursorShapeStub implementation.
    101   virtual void InjectClipboardEvent(const protocol::ClipboardEvent& event)
    102       OVERRIDE;
    103 
    104   // ClipboardStub implementation.
    105   virtual void SetCursorShape(const protocol::CursorShapeInfo& shape) OVERRIDE;
    106 
    107  private:
    108   // This object is ref-counted, so it cleans itself up.
    109   virtual ~ClientInstance();
    110 
    111   void ConnectToHostOnNetworkThread(
    112       scoped_refptr<FrameConsumerProxy> consumer_proxy,
    113       const base::Closure& done);
    114   void DisconnectFromHostOnNetworkThread(const base::Closure& done);
    115 
    116   // Proxy to exchange messages between the
    117   // common Chromoting protocol and UI Application.
    118   base::WeakPtr<ClientProxy> proxyToClient_;
    119 
    120   // ID of the host we are connecting to.
    121   std::string host_id_;
    122 
    123   // This group of variables is to be used on the display thread.
    124   scoped_ptr<SoftwareVideoRenderer> video_renderer_;
    125   scoped_ptr<FrameConsumerBridge> view_;
    126 
    127   // This group of variables is to be used on the network thread.
    128   ClientConfig client_config_;
    129   scoped_ptr<ClientContext> client_context_;
    130   scoped_ptr<protocol::ConnectionToHost> connection_;
    131   scoped_ptr<ChromotingClient> client_;
    132   XmppSignalStrategy::XmppServerConfig xmpp_config_;
    133   scoped_ptr<XmppSignalStrategy> signaling_;  // Must outlive client_
    134 
    135   // Pass this the user's PIN once we have it. To be assigned and accessed on
    136   // the UI thread, but must be posted to the network thread to call it.
    137   protocol::SecretFetchedCallback pin_callback_;
    138 
    139   // Indicates whether to establish a new pairing with this host. This is
    140   // modified in ProvideSecret(), but thereafter to be used only from the
    141   // network thread. (This is safe because ProvideSecret() is invoked at most
    142   // once per run, and always before any reference to this flag.)
    143   bool create_pairing_;
    144 
    145   // Chromium code's connection to the OBJ_C message loop.  Once created the
    146   // MessageLoop will live for the life of the program.  An attempt was made to
    147   // create the primary message loop earlier in the programs life, but a
    148   // MessageLoop requires ARC to be disabled.
    149   base::MessageLoopForUI* ui_loop_;
    150 
    151   // References to native threads.
    152   scoped_refptr<AutoThreadTaskRunner> ui_task_runner_;
    153   scoped_refptr<AutoThreadTaskRunner> network_task_runner_;
    154 
    155   scoped_refptr<net::URLRequestContextGetter> url_requester_;
    156 
    157   friend class base::RefCountedThreadSafe<ClientInstance>;
    158 
    159   DISALLOW_COPY_AND_ASSIGN(ClientInstance);
    160 };
    161 
    162 }  // namespace remoting
    163 
    164 #endif  // REMOTING_IOS_BRIDGE_CLIENT_INSTANCE_H_
    165