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 // TODO(ajwong): We need to come up with a better description of the
      6 // responsibilities for each thread.
      7 
      8 #ifndef REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
      9 #define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
     10 
     11 #include <string>
     12 
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "ppapi/c/pp_instance.h"
     17 #include "ppapi/c/pp_rect.h"
     18 #include "ppapi/c/pp_resource.h"
     19 #include "ppapi/cpp/var.h"
     20 #include "third_party/skia/include/core/SkPoint.h"
     21 #include "third_party/skia/include/core/SkRegion.h"
     22 #include "third_party/skia/include/core/SkSize.h"
     23 
     24 // Windows defines 'PostMessage', so we have to undef it before we
     25 // include instance_private.h
     26 #if defined(PostMessage)
     27 #undef PostMessage
     28 #endif
     29 
     30 #include "ppapi/cpp/instance.h"
     31 #include "remoting/client/client_context.h"
     32 #include "remoting/client/client_user_interface.h"
     33 #include "remoting/client/key_event_mapper.h"
     34 #include "remoting/client/plugin/mac_key_event_processor.h"
     35 #include "remoting/client/plugin/pepper_input_handler.h"
     36 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
     37 #include "remoting/proto/event.pb.h"
     38 #include "remoting/protocol/client_stub.h"
     39 #include "remoting/protocol/clipboard_stub.h"
     40 #include "remoting/protocol/connection_to_host.h"
     41 #include "remoting/protocol/cursor_shape_stub.h"
     42 #include "remoting/protocol/input_event_tracker.h"
     43 #include "remoting/protocol/mouse_input_filter.h"
     44 #include "remoting/protocol/negotiating_client_authenticator.h"
     45 #include "remoting/protocol/third_party_client_authenticator.h"
     46 
     47 namespace base {
     48 class DictionaryValue;
     49 }  // namespace base
     50 
     51 namespace pp {
     52 class InputEvent;
     53 class Module;
     54 }  // namespace pp
     55 
     56 namespace remoting {
     57 
     58 class ChromotingClient;
     59 class ChromotingStats;
     60 class ClientContext;
     61 class FrameConsumerProxy;
     62 class PepperAudioPlayer;
     63 class PepperTokenFetcher;
     64 class PepperView;
     65 class PepperSignalStrategy;
     66 class RectangleUpdateDecoder;
     67 
     68 struct ClientConfig;
     69 
     70 class ChromotingInstance :
     71       public ClientUserInterface,
     72       public protocol::ClipboardStub,
     73       public protocol::CursorShapeStub,
     74       public pp::Instance {
     75  public:
     76   // Plugin API version. This should be incremented whenever the API
     77   // interface changes.
     78   static const int kApiVersion = 7;
     79 
     80   // Plugin API features. This allows orthogonal features to be supported
     81   // without bumping the API version.
     82   static const char kApiFeatures[];
     83 
     84   // Capabilities supported by the plugin that should also be supported by the
     85   // webapp to be enabled.
     86   static const char kRequestedCapabilities[];
     87 
     88   // Capabilities supported by the plugin that do not need to be supported by
     89   // the webapp to be enabled.
     90   static const char kSupportedCapabilities[];
     91 
     92   // Backward-compatibility version used by for the messaging
     93   // interface. Should be updated whenever we remove support for
     94   // an older version of the API.
     95   static const int kApiMinMessagingVersion = 5;
     96 
     97   // Backward-compatibility version used by for the ScriptableObject
     98   // interface. Should be updated whenever we remove support for
     99   // an older version of the API.
    100   static const int kApiMinScriptableVersion = 5;
    101 
    102   // Helper method to parse authentication_methods parameter.
    103   static bool ParseAuthMethods(const std::string& auth_methods,
    104                                ClientConfig* config);
    105 
    106   explicit ChromotingInstance(PP_Instance instance);
    107   virtual ~ChromotingInstance();
    108 
    109   // pp::Instance interface.
    110   virtual void DidChangeView(const pp::View& view) OVERRIDE;
    111   virtual bool Init(uint32_t argc, const char* argn[],
    112                     const char* argv[]) OVERRIDE;
    113   virtual void HandleMessage(const pp::Var& message) OVERRIDE;
    114   virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
    115 
    116   // ClientUserInterface interface.
    117   virtual void OnConnectionState(protocol::ConnectionToHost::State state,
    118                                  protocol::ErrorCode error) OVERRIDE;
    119   virtual void OnConnectionReady(bool ready) OVERRIDE;
    120   virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
    121   virtual void SetPairingResponse(
    122       const protocol::PairingResponse& pairing_response) OVERRIDE;
    123   virtual void DeliverHostMessage(
    124       const protocol::ExtensionMessage& message) OVERRIDE;
    125   virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
    126   virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
    127   virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
    128   GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
    129 
    130   // protocol::ClipboardStub interface.
    131   virtual void InjectClipboardEvent(
    132       const protocol::ClipboardEvent& event) OVERRIDE;
    133 
    134   // protocol::CursorShapeStub interface.
    135   virtual void SetCursorShape(
    136       const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
    137 
    138   // Called by PepperView.
    139   void SetDesktopSize(const SkISize& size, const SkIPoint& dpi);
    140   void SetDesktopShape(const SkRegion& shape);
    141   void OnFirstFrameReceived();
    142 
    143   // Return statistics record by ChromotingClient.
    144   // If no connection is currently active then NULL will be returned.
    145   ChromotingStats* GetStats();
    146 
    147   // Registers a global log message handler that redirects the log output to
    148   // our plugin instance.
    149   // This is called by the plugin's PPP_InitializeModule.
    150   // Note that no logging will be processed unless a ChromotingInstance has been
    151   // registered for logging (see RegisterLoggingInstance).
    152   static void RegisterLogMessageHandler();
    153 
    154   // Registers this instance so it processes messages sent by the global log
    155   // message handler. This overwrites any previously registered instance.
    156   void RegisterLoggingInstance();
    157 
    158   // Unregisters this instance so that debug log messages will no longer be sent
    159   // to it. If this instance is not the currently registered logging instance,
    160   // then the currently registered instance will stay in effect.
    161   void UnregisterLoggingInstance();
    162 
    163   // A Log Message Handler that is called after each LOG message has been
    164   // processed. This must be of type LogMessageHandlerFunction defined in
    165   // base/logging.h.
    166   static bool LogToUI(int severity, const char* file, int line,
    167                       size_t message_start, const std::string& str);
    168 
    169   // Requests the webapp to fetch a third-party token.
    170   void FetchThirdPartyToken(
    171       const GURL& token_url,
    172       const std::string& host_public_key,
    173       const std::string& scope,
    174       const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher);
    175 
    176  private:
    177   FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
    178 
    179   // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
    180   // Immediately calls |secret_fetched_callback| with |shared_secret|.
    181   static void FetchSecretFromString(
    182       const std::string& shared_secret,
    183       bool pairing_supported,
    184       const protocol::SecretFetchedCallback& secret_fetched_callback);
    185 
    186   // Message handlers for messages that come from JavaScript. Called
    187   // from HandleMessage().
    188   void Connect(const ClientConfig& config);
    189   void Disconnect();
    190   void OnIncomingIq(const std::string& iq);
    191   void ReleaseAllKeys();
    192   void InjectKeyEvent(const protocol::KeyEvent& event);
    193   void RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode);
    194   void TrapKey(uint32 usb_keycode, bool trap);
    195   void SendClipboardItem(const std::string& mime_type, const std::string& item);
    196   void NotifyClientResolution(int width, int height, int x_dpi, int y_dpi);
    197   void PauseVideo(bool pause);
    198   void PauseAudio(bool pause);
    199   void OnPinFetched(const std::string& pin);
    200   void OnThirdPartyTokenFetched(const std::string& token,
    201                                 const std::string& shared_secret);
    202   void RequestPairing(const std::string& client_name);
    203   void SendClientMessage(const std::string& type, const std::string& data);
    204 
    205   // Helper method to post messages to the webapp.
    206   void PostChromotingMessage(const std::string& method,
    207                              scoped_ptr<base::DictionaryValue> data);
    208 
    209   // Posts trapped keys to the web-app to handle.
    210   void SendTrappedKey(uint32 usb_keycode, bool pressed);
    211 
    212   // Callback for PepperSignalStrategy.
    213   void SendOutgoingIq(const std::string& iq);
    214 
    215   void SendPerfStats();
    216 
    217   void ProcessLogToUI(const std::string& message);
    218 
    219   // Returns true if the hosting content has the chrome-extension:// scheme.
    220   bool IsCallerAppOrExtension();
    221 
    222   // Returns true if there is a ConnectionToHost and it is connected.
    223   bool IsConnected();
    224 
    225   // Used as the |FetchSecretCallback| for Me2Me connections.
    226   // Uses the PIN request dialog in the webapp to obtain the shared secret.
    227   void FetchSecretFromDialog(
    228       bool pairing_supported,
    229       const protocol::SecretFetchedCallback& secret_fetched_callback);
    230 
    231   bool initialized_;
    232 
    233   PepperPluginThreadDelegate plugin_thread_delegate_;
    234   scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
    235   ClientContext context_;
    236   scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_;
    237   scoped_ptr<PepperView> view_;
    238   pp::View plugin_view_;
    239 
    240   // Contains the most-recently-reported desktop shape, if any.
    241   scoped_ptr<SkRegion> desktop_shape_;
    242 
    243   scoped_ptr<PepperSignalStrategy> signal_strategy_;
    244 
    245   scoped_ptr<protocol::ConnectionToHost> host_connection_;
    246   scoped_ptr<ChromotingClient> client_;
    247 
    248   // Input pipeline components, in reverse order of distance from input source.
    249   protocol::MouseInputFilter mouse_input_filter_;
    250   protocol::InputEventTracker input_tracker_;
    251 #if defined(OS_MACOSX)
    252   MacKeyEventProcessor mac_key_event_processor_;
    253 #endif
    254   KeyEventMapper key_mapper_;
    255   PepperInputHandler input_handler_;
    256 
    257   // PIN Fetcher.
    258   bool use_async_pin_dialog_;
    259   protocol::SecretFetchedCallback secret_fetched_callback_;
    260 
    261   base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_;
    262 
    263   // Weak reference to this instance, used for global logging and task posting.
    264   base::WeakPtrFactory<ChromotingInstance> weak_factory_;
    265 
    266   DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
    267 };
    268 
    269 }  // namespace remoting
    270 
    271 #endif  // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
    272