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