Home | History | Annotate | Download | only in plugin
      1 // Copyright (c) 2012 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_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
      6 #define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/gtest_prod_util.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/thread_task_runner_handle.h"
     14 #include "ppapi/c/pp_instance.h"
     15 #include "ppapi/c/pp_rect.h"
     16 #include "ppapi/c/pp_resource.h"
     17 #include "ppapi/cpp/instance.h"
     18 #include "ppapi/cpp/text_input_controller.h"
     19 #include "ppapi/cpp/var.h"
     20 #include "remoting/client/client_context.h"
     21 #include "remoting/client/client_user_interface.h"
     22 #include "remoting/client/key_event_mapper.h"
     23 #include "remoting/client/plugin/media_source_video_renderer.h"
     24 #include "remoting/client/plugin/pepper_input_handler.h"
     25 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
     26 #include "remoting/proto/event.pb.h"
     27 #include "remoting/protocol/client_stub.h"
     28 #include "remoting/protocol/clipboard_stub.h"
     29 #include "remoting/protocol/connection_to_host.h"
     30 #include "remoting/protocol/cursor_shape_stub.h"
     31 #include "remoting/protocol/input_event_tracker.h"
     32 #include "remoting/protocol/mouse_input_filter.h"
     33 #include "remoting/protocol/negotiating_client_authenticator.h"
     34 #include "remoting/protocol/third_party_client_authenticator.h"
     35 
     36 namespace base {
     37 class DictionaryValue;
     38 }  // namespace base
     39 
     40 namespace pp {
     41 class InputEvent;
     42 class Module;
     43 class VarDictionary;
     44 }  // namespace pp
     45 
     46 namespace jingle_glue {
     47 class JingleThreadWrapper;
     48 }  // namespace jingle_glue
     49 
     50 namespace webrtc {
     51 class DesktopRegion;
     52 class DesktopSize;
     53 class DesktopVector;
     54 }  // namespace webrtc
     55 
     56 namespace remoting {
     57 
     58 class ChromotingClient;
     59 class ChromotingStats;
     60 class ClientContext;
     61 class DelegatingSignalStrategy;
     62 class FrameConsumer;
     63 class FrameConsumerProxy;
     64 class PepperAudioPlayer;
     65 class TokenFetcherProxy;
     66 class PepperView;
     67 class RectangleUpdateDecoder;
     68 class SignalStrategy;
     69 class VideoRenderer;
     70 
     71 struct ClientConfig;
     72 
     73 class ChromotingInstance :
     74       public ClientUserInterface,
     75       public MediaSourceVideoRenderer::Delegate,
     76       public protocol::ClipboardStub,
     77       public protocol::CursorShapeStub,
     78       public pp::Instance {
     79  public:
     80   // Plugin API version. This should be incremented whenever the API
     81   // interface changes.
     82   static const int kApiVersion = 7;
     83 
     84   // Plugin API features. This allows orthogonal features to be supported
     85   // without bumping the API version.
     86   static const char kApiFeatures[];
     87 
     88   // Capabilities supported by the plugin that should also be supported by the
     89   // webapp to be enabled.
     90   static const char kRequestedCapabilities[];
     91 
     92   // Capabilities supported by the plugin that do not need to be supported by
     93   // the webapp to be enabled.
     94   static const char kSupportedCapabilities[];
     95 
     96   // Backward-compatibility version used by for the messaging
     97   // interface. Should be updated whenever we remove support for
     98   // an older version of the API.
     99   static const int kApiMinMessagingVersion = 5;
    100 
    101   // Backward-compatibility version used by for the ScriptableObject
    102   // interface. Should be updated whenever we remove support for
    103   // an older version of the API.
    104   static const int kApiMinScriptableVersion = 5;
    105 
    106   explicit ChromotingInstance(PP_Instance instance);
    107   virtual ~ChromotingInstance();
    108 
    109   // pp::Instance interface.
    110   virtual void DidChangeFocus(bool has_focus) OVERRIDE;
    111   virtual void DidChangeView(const pp::View& view) OVERRIDE;
    112   virtual bool Init(uint32_t argc, const char* argn[],
    113                     const char* argv[]) OVERRIDE;
    114   virtual void HandleMessage(const pp::Var& message) OVERRIDE;
    115   virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
    116 
    117   // ClientUserInterface interface.
    118   virtual void OnConnectionState(protocol::ConnectionToHost::State state,
    119                                  protocol::ErrorCode error) OVERRIDE;
    120   virtual void OnConnectionReady(bool ready) OVERRIDE;
    121   virtual void OnRouteChanged(const std::string& channel_name,
    122                               const protocol::TransportRoute& route) OVERRIDE;
    123   virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
    124   virtual void SetPairingResponse(
    125       const protocol::PairingResponse& pairing_response) OVERRIDE;
    126   virtual void DeliverHostMessage(
    127       const protocol::ExtensionMessage& message) OVERRIDE;
    128   virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
    129   virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
    130 
    131   // protocol::ClipboardStub interface.
    132   virtual void InjectClipboardEvent(
    133       const protocol::ClipboardEvent& event) OVERRIDE;
    134 
    135   // protocol::CursorShapeStub interface.
    136   virtual void SetCursorShape(
    137       const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
    138 
    139   // Called by PepperView.
    140   void SetDesktopSize(const webrtc::DesktopSize& size,
    141                       const webrtc::DesktopVector& dpi);
    142   void SetDesktopShape(const webrtc::DesktopRegion& shape);
    143   void OnFirstFrameReceived();
    144 
    145   // Return statistics record by ChromotingClient.
    146   // If no connection is currently active then NULL will be returned.
    147   ChromotingStats* GetStats();
    148 
    149   // Registers a global log message handler that redirects the log output to
    150   // our plugin instance.
    151   // This is called by the plugin's PPP_InitializeModule.
    152   // Note that no logging will be processed unless a ChromotingInstance has been
    153   // registered for logging (see RegisterLoggingInstance).
    154   static void RegisterLogMessageHandler();
    155 
    156   // Registers this instance so it processes messages sent by the global log
    157   // message handler. This overwrites any previously registered instance.
    158   void RegisterLoggingInstance();
    159 
    160   // Unregisters this instance so that debug log messages will no longer be sent
    161   // to it. If this instance is not the currently registered logging instance,
    162   // then the currently registered instance will stay in effect.
    163   void UnregisterLoggingInstance();
    164 
    165   // A Log Message Handler that is called after each LOG message has been
    166   // processed. This must be of type LogMessageHandlerFunction defined in
    167   // base/logging.h.
    168   static bool LogToUI(int severity, const char* file, int line,
    169                       size_t message_start, const std::string& str);
    170 
    171   // Requests the webapp to fetch a third-party token.
    172   void FetchThirdPartyToken(
    173       const GURL& token_url,
    174       const std::string& host_public_key,
    175       const std::string& scope,
    176       const base::WeakPtr<TokenFetcherProxy> pepper_token_fetcher);
    177 
    178  private:
    179   FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
    180 
    181   // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
    182   // Immediately calls |secret_fetched_callback| with |shared_secret|.
    183   static void FetchSecretFromString(
    184       const std::string& shared_secret,
    185       bool pairing_supported,
    186       const protocol::SecretFetchedCallback& secret_fetched_callback);
    187 
    188   // Message handlers for messages that come from JavaScript. Called
    189   // from HandleMessage().
    190   void HandleConnect(const base::DictionaryValue& data);
    191   void HandleDisconnect(const base::DictionaryValue& data);
    192   void HandleOnIncomingIq(const base::DictionaryValue& data);
    193   void HandleReleaseAllKeys(const base::DictionaryValue& data);
    194   void HandleInjectKeyEvent(const base::DictionaryValue& data);
    195   void HandleRemapKey(const base::DictionaryValue& data);
    196   void HandleTrapKey(const base::DictionaryValue& data);
    197   void HandleSendClipboardItem(const base::DictionaryValue& data);
    198   void HandleNotifyClientResolution(const base::DictionaryValue& data);
    199   void HandlePauseVideo(const base::DictionaryValue& data);
    200   void HandleVideoControl(const base::DictionaryValue& data);
    201   void HandlePauseAudio(const base::DictionaryValue& data);
    202   void HandleOnPinFetched(const base::DictionaryValue& data);
    203   void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data);
    204   void HandleRequestPairing(const base::DictionaryValue& data);
    205   void HandleExtensionMessage(const base::DictionaryValue& data);
    206   void HandleAllowMouseLockMessage();
    207   void HandleEnableMediaSourceRendering();
    208   void HandleSendMouseInputWhenUnfocused();
    209   void HandleDelegateLargeCursors();
    210 
    211   // Helper method to post messages to the webapp.
    212   void PostChromotingMessage(const std::string& method,
    213                              const pp::VarDictionary& data);
    214 
    215   // Same as above, but serializes messages to JSON before sending them.  This
    216   // method is used for backward compatibility with older version of the webapp
    217   // that expect to received most messages formatted using JSON.
    218   //
    219   // TODO(sergeyu): When all current versions of the webapp support raw messages
    220   // remove this method and use PostChromotingMessage() instead.
    221   void PostLegacyJsonMessage(const std::string& method,
    222                        scoped_ptr<base::DictionaryValue> data);
    223 
    224   // Posts trapped keys to the web-app to handle.
    225   void SendTrappedKey(uint32 usb_keycode, bool pressed);
    226 
    227   // Callback for DelegatingSignalStrategy.
    228   void SendOutgoingIq(const std::string& iq);
    229 
    230   void SendPerfStats();
    231 
    232   void ProcessLogToUI(const std::string& message);
    233 
    234   // Returns true if the hosting content has the chrome-extension:// scheme.
    235   bool IsCallerAppOrExtension();
    236 
    237   // Returns true if there is a ConnectionToHost and it is connected.
    238   bool IsConnected();
    239 
    240   // Used as the |FetchSecretCallback| for Me2Me connections.
    241   // Uses the PIN request dialog in the webapp to obtain the shared secret.
    242   void FetchSecretFromDialog(
    243       bool pairing_supported,
    244       const protocol::SecretFetchedCallback& secret_fetched_callback);
    245 
    246   // MediaSourceVideoRenderer::Delegate implementation.
    247   virtual void OnMediaSourceSize(const webrtc::DesktopSize& size,
    248                                  const webrtc::DesktopVector& dpi) OVERRIDE;
    249   virtual void OnMediaSourceShape(const webrtc::DesktopRegion& shape) OVERRIDE;
    250   virtual void OnMediaSourceReset(const std::string& format) OVERRIDE;
    251   virtual void OnMediaSourceData(uint8_t* buffer, size_t buffer_size,
    252                                  bool keyframe) OVERRIDE;
    253 
    254   bool initialized_;
    255 
    256   PepperPluginThreadDelegate plugin_thread_delegate_;
    257   scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
    258   scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
    259   scoped_ptr<jingle_glue::JingleThreadWrapper> thread_wrapper_;
    260   ClientContext context_;
    261   scoped_ptr<VideoRenderer> video_renderer_;
    262   scoped_ptr<PepperView> view_;
    263   scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
    264   pp::View plugin_view_;
    265 
    266   // Contains the most-recently-reported desktop shape, if any.
    267   scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
    268 
    269   scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
    270 
    271   scoped_ptr<ChromotingClient> client_;
    272 
    273   // Input pipeline components, in reverse order of distance from input source.
    274   protocol::MouseInputFilter mouse_input_filter_;
    275   protocol::InputEventTracker input_tracker_;
    276   KeyEventMapper key_mapper_;
    277   scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
    278   PepperInputHandler input_handler_;
    279 
    280   // Used to control text input settings, such as whether to show the IME.
    281   pp::TextInputController text_input_controller_;
    282 
    283   // PIN Fetcher.
    284   bool use_async_pin_dialog_;
    285   protocol::SecretFetchedCallback secret_fetched_callback_;
    286 
    287   // Set to true if the webapp has requested to use MediaSource API for
    288   // rendering. In that case all the encoded video will be passed to the
    289   // webapp for decoding.
    290   bool use_media_source_rendering_;
    291 
    292   // Set to true if the web-app can handle large cursors. If false, then large
    293   // cursors will be cropped to the maximum size supported by Pepper.
    294   bool delegate_large_cursors_;
    295 
    296   base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy_;
    297 
    298   // Weak reference to this instance, used for global logging and task posting.
    299   base::WeakPtrFactory<ChromotingInstance> weak_factory_;
    300 
    301   DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
    302 };
    303 
    304 }  // namespace remoting
    305 
    306 #endif  // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
    307