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/instance.h" 20 #include "ppapi/cpp/var.h" 21 #include "remoting/client/client_context.h" 22 #include "remoting/client/client_user_interface.h" 23 #include "remoting/client/key_event_mapper.h" 24 #include "remoting/client/plugin/normalizing_input_filter.h" 25 #include "remoting/client/plugin/pepper_input_handler.h" 26 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h" 27 #include "remoting/proto/event.pb.h" 28 #include "remoting/protocol/client_stub.h" 29 #include "remoting/protocol/clipboard_stub.h" 30 #include "remoting/protocol/connection_to_host.h" 31 #include "remoting/protocol/cursor_shape_stub.h" 32 #include "remoting/protocol/input_event_tracker.h" 33 #include "remoting/protocol/mouse_input_filter.h" 34 #include "remoting/protocol/negotiating_client_authenticator.h" 35 #include "remoting/protocol/third_party_client_authenticator.h" 36 37 namespace base { 38 class DictionaryValue; 39 } // namespace base 40 41 namespace pp { 42 class InputEvent; 43 class Module; 44 } // namespace pp 45 46 namespace webrtc { 47 class DesktopRegion; 48 class DesktopSize; 49 class DesktopVector; 50 } // namespace webrtc 51 52 namespace remoting { 53 54 class ChromotingClient; 55 class ChromotingStats; 56 class ClientContext; 57 class DelegatingSignalStrategy; 58 class FrameConsumer; 59 class FrameConsumerProxy; 60 class PepperAudioPlayer; 61 class PepperTokenFetcher; 62 class PepperView; 63 class RectangleUpdateDecoder; 64 class SignalStrategy; 65 66 struct ClientConfig; 67 68 class ChromotingInstance : 69 public ClientUserInterface, 70 public protocol::ClipboardStub, 71 public protocol::CursorShapeStub, 72 public pp::Instance { 73 public: 74 // Plugin API version. This should be incremented whenever the API 75 // interface changes. 76 static const int kApiVersion = 7; 77 78 // Plugin API features. This allows orthogonal features to be supported 79 // without bumping the API version. 80 static const char kApiFeatures[]; 81 82 // Capabilities supported by the plugin that should also be supported by the 83 // webapp to be enabled. 84 static const char kRequestedCapabilities[]; 85 86 // Capabilities supported by the plugin that do not need to be supported by 87 // the webapp to be enabled. 88 static const char kSupportedCapabilities[]; 89 90 // Backward-compatibility version used by for the messaging 91 // interface. Should be updated whenever we remove support for 92 // an older version of the API. 93 static const int kApiMinMessagingVersion = 5; 94 95 // Backward-compatibility version used by for the ScriptableObject 96 // interface. Should be updated whenever we remove support for 97 // an older version of the API. 98 static const int kApiMinScriptableVersion = 5; 99 100 // Helper method to parse authentication_methods parameter. 101 static bool ParseAuthMethods(const std::string& auth_methods, 102 ClientConfig* config); 103 104 explicit ChromotingInstance(PP_Instance instance); 105 virtual ~ChromotingInstance(); 106 107 // pp::Instance interface. 108 virtual void DidChangeFocus(bool has_focus) OVERRIDE; 109 virtual void DidChangeView(const pp::View& view) OVERRIDE; 110 virtual bool Init(uint32_t argc, const char* argn[], 111 const char* argv[]) OVERRIDE; 112 virtual void HandleMessage(const pp::Var& message) OVERRIDE; 113 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE; 114 115 // ClientUserInterface interface. 116 virtual void OnConnectionState(protocol::ConnectionToHost::State state, 117 protocol::ErrorCode error) OVERRIDE; 118 virtual void OnConnectionReady(bool ready) OVERRIDE; 119 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 120 virtual void SetPairingResponse( 121 const protocol::PairingResponse& pairing_response) OVERRIDE; 122 virtual void DeliverHostMessage( 123 const protocol::ExtensionMessage& message) OVERRIDE; 124 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; 125 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; 126 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> 127 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; 128 129 // protocol::ClipboardStub interface. 130 virtual void InjectClipboardEvent( 131 const protocol::ClipboardEvent& event) OVERRIDE; 132 133 // protocol::CursorShapeStub interface. 134 virtual void SetCursorShape( 135 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE; 136 137 // Called by PepperView. 138 void SetDesktopSize(const webrtc::DesktopSize& size, 139 const webrtc::DesktopVector& dpi); 140 void SetDesktopShape(const webrtc::DesktopRegion& 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 HandleConnect(const base::DictionaryValue& data); 189 void HandleDisconnect(const base::DictionaryValue& data); 190 void HandleOnIncomingIq(const base::DictionaryValue& data); 191 void HandleReleaseAllKeys(const base::DictionaryValue& data); 192 void HandleInjectKeyEvent(const base::DictionaryValue& data); 193 void HandleRemapKey(const base::DictionaryValue& data); 194 void HandleTrapKey(const base::DictionaryValue& data); 195 void HandleSendClipboardItem(const base::DictionaryValue& data); 196 void HandleNotifyClientResolution(const base::DictionaryValue& data); 197 void HandlePauseVideo(const base::DictionaryValue& data); 198 void HandlePauseAudio(const base::DictionaryValue& data); 199 void HandleOnPinFetched(const base::DictionaryValue& data); 200 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data); 201 void HandleRequestPairing(const base::DictionaryValue& data); 202 void HandleExtensionMessage(const base::DictionaryValue& data); 203 void HandleAllowMouseLockMessage(); 204 205 // Helper method called from Connect() to connect with parsed config. 206 void ConnectWithConfig(const ClientConfig& config, 207 const std::string& local_jid); 208 209 // Helper method to post messages to the webapp. 210 void PostChromotingMessage(const std::string& method, 211 scoped_ptr<base::DictionaryValue> data); 212 213 // Posts trapped keys to the web-app to handle. 214 void SendTrappedKey(uint32 usb_keycode, bool pressed); 215 216 // Callback for DelegatingSignalStrategy. 217 void SendOutgoingIq(const std::string& iq); 218 219 void SendPerfStats(); 220 221 void ProcessLogToUI(const std::string& message); 222 223 // Returns true if the hosting content has the chrome-extension:// scheme. 224 bool IsCallerAppOrExtension(); 225 226 // Returns true if there is a ConnectionToHost and it is connected. 227 bool IsConnected(); 228 229 // Used as the |FetchSecretCallback| for Me2Me connections. 230 // Uses the PIN request dialog in the webapp to obtain the shared secret. 231 void FetchSecretFromDialog( 232 bool pairing_supported, 233 const protocol::SecretFetchedCallback& secret_fetched_callback); 234 235 bool initialized_; 236 237 PepperPluginThreadDelegate plugin_thread_delegate_; 238 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_; 239 ClientContext context_; 240 scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_; 241 scoped_ptr<PepperView> view_; 242 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_; 243 pp::View plugin_view_; 244 245 // Contains the most-recently-reported desktop shape, if any. 246 scoped_ptr<webrtc::DesktopRegion> desktop_shape_; 247 248 scoped_ptr<DelegatingSignalStrategy> signal_strategy_; 249 250 scoped_ptr<protocol::ConnectionToHost> host_connection_; 251 scoped_ptr<ChromotingClient> client_; 252 253 // Input pipeline components, in reverse order of distance from input source. 254 protocol::MouseInputFilter mouse_input_filter_; 255 protocol::InputEventTracker input_tracker_; 256 KeyEventMapper key_mapper_; 257 scoped_ptr<protocol::InputFilter> normalizing_input_filter_; 258 PepperInputHandler input_handler_; 259 260 // PIN Fetcher. 261 bool use_async_pin_dialog_; 262 protocol::SecretFetchedCallback secret_fetched_callback_; 263 264 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_; 265 266 // Weak reference to this instance, used for global logging and task posting. 267 base::WeakPtrFactory<ChromotingInstance> weak_factory_; 268 269 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance); 270 }; 271 272 } // namespace remoting 273 274 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_ 275