chromoting_instance.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 47namespace base { 48class DictionaryValue; 49} // namespace base 50 51namespace pp { 52class InputEvent; 53class Module; 54} // namespace pp 55 56namespace remoting { 57 58class ChromotingClient; 59class ChromotingStats; 60class ClientContext; 61class FrameConsumerProxy; 62class PepperAudioPlayer; 63class PepperTokenFetcher; 64class PepperView; 65class PepperSignalStrategy; 66class RectangleUpdateDecoder; 67 68struct ClientConfig; 69 70class ChromotingInstance : 71 public ClientUserInterface, 72 public protocol::ClipboardStub, 73 public protocol::CursorShapeStub, 74 public pp::Instance, 75 public base::SupportsWeakPtr<ChromotingInstance> { 76 public: 77 // Plugin API version. This should be incremented whenever the API 78 // interface changes. 79 static const int kApiVersion = 7; 80 81 // Plugin API features. This allows orthogonal features to be supported 82 // without bumping the API version. 83 static const char kApiFeatures[]; 84 85 // Capabilities supported by the plugin that should also be supported by the 86 // webapp to be enabled. 87 static const char kRequestedCapabilities[]; 88 89 // Capabilities supported by the plugin that do not need to be supported by 90 // the webapp to be enabled. 91 static const char kSupportedCapabilities[]; 92 93 // Backward-compatibility version used by for the messaging 94 // interface. Should be updated whenever we remove support for 95 // an older version of the API. 96 static const int kApiMinMessagingVersion = 5; 97 98 // Backward-compatibility version used by for the ScriptableObject 99 // interface. Should be updated whenever we remove support for 100 // an older version of the API. 101 static const int kApiMinScriptableVersion = 5; 102 103 // Helper method to parse authentication_methods parameter. 104 static bool ParseAuthMethods(const std::string& auth_methods, 105 ClientConfig* config); 106 107 explicit ChromotingInstance(PP_Instance instance); 108 virtual ~ChromotingInstance(); 109 110 // pp::Instance interface. 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 SetCapabilities(const std::string& capabilities) OVERRIDE; 122 virtual void SetPairingResponse( 123 const protocol::PairingResponse& pairing_response) 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 SkISize& size, const SkIPoint& dpi); 139 void SetDesktopShape(const SkRegion& shape); 140 void OnFirstFrameReceived(); 141 142 // Return statistics record by ChromotingClient. 143 // If no connection is currently active then NULL will be returned. 144 ChromotingStats* GetStats(); 145 146 // Registers a global log message handler that redirects the log output to 147 // our plugin instance. 148 // This is called by the plugin's PPP_InitializeModule. 149 // Note that no logging will be processed unless a ChromotingInstance has been 150 // registered for logging (see RegisterLoggingInstance). 151 static void RegisterLogMessageHandler(); 152 153 // Registers this instance so it processes messages sent by the global log 154 // message handler. This overwrites any previously registered instance. 155 void RegisterLoggingInstance(); 156 157 // Unregisters this instance so that debug log messages will no longer be sent 158 // to it. If this instance is not the currently registered logging instance, 159 // then the currently registered instance will stay in effect. 160 void UnregisterLoggingInstance(); 161 162 // A Log Message Handler that is called after each LOG message has been 163 // processed. This must be of type LogMessageHandlerFunction defined in 164 // base/logging.h. 165 static bool LogToUI(int severity, const char* file, int line, 166 size_t message_start, const std::string& str); 167 168 // Requests the webapp to fetch a third-party token. 169 void FetchThirdPartyToken( 170 const GURL& token_url, 171 const std::string& host_public_key, 172 const std::string& scope, 173 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher); 174 175 private: 176 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup); 177 178 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps). 179 // Immediately calls |secret_fetched_callback| with |shared_secret|. 180 static void FetchSecretFromString( 181 const std::string& shared_secret, 182 bool pairing_supported, 183 const protocol::SecretFetchedCallback& secret_fetched_callback); 184 185 // Message handlers for messages that come from JavaScript. Called 186 // from HandleMessage(). 187 void Connect(const ClientConfig& config); 188 void Disconnect(); 189 void OnIncomingIq(const std::string& iq); 190 void ReleaseAllKeys(); 191 void InjectKeyEvent(const protocol::KeyEvent& event); 192 void RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode); 193 void TrapKey(uint32 usb_keycode, bool trap); 194 void SendClipboardItem(const std::string& mime_type, const std::string& item); 195 void NotifyClientResolution(int width, int height, int x_dpi, int y_dpi); 196 void PauseVideo(bool pause); 197 void PauseAudio(bool pause); 198 void OnPinFetched(const std::string& pin); 199 void OnThirdPartyTokenFetched(const std::string& token, 200 const std::string& shared_secret); 201 void RequestPairing(const std::string& client_name); 202 203 // Helper method to post messages to the webapp. 204 void PostChromotingMessage(const std::string& method, 205 scoped_ptr<base::DictionaryValue> data); 206 207 // Posts trapped keys to the web-app to handle. 208 void SendTrappedKey(uint32 usb_keycode, bool pressed); 209 210 // Callback for PepperSignalStrategy. 211 void SendOutgoingIq(const std::string& iq); 212 213 void SendPerfStats(); 214 215 void ProcessLogToUI(const std::string& message); 216 217 // Returns true if the hosting content has the chrome-extension:// scheme. 218 bool IsCallerAppOrExtension(); 219 220 // Returns true if there is a ConnectionToHost and it is connected. 221 bool IsConnected(); 222 223 // Used as the |FetchSecretCallback| for Me2Me connections. 224 // Uses the PIN request dialog in the webapp to obtain the shared secret. 225 void FetchSecretFromDialog( 226 bool pairing_supported, 227 const protocol::SecretFetchedCallback& secret_fetched_callback); 228 229 bool initialized_; 230 231 PepperPluginThreadDelegate plugin_thread_delegate_; 232 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_; 233 ClientContext context_; 234 scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_; 235 scoped_ptr<PepperView> view_; 236 pp::View plugin_view_; 237 238 // Contains the most-recently-reported desktop shape, if any. 239 scoped_ptr<SkRegion> desktop_shape_; 240 241 scoped_ptr<PepperSignalStrategy> signal_strategy_; 242 243 scoped_ptr<protocol::ConnectionToHost> host_connection_; 244 scoped_ptr<ChromotingClient> client_; 245 246 // Input pipeline components, in reverse order of distance from input source. 247 protocol::MouseInputFilter mouse_input_filter_; 248 protocol::InputEventTracker input_tracker_; 249#if defined(OS_MACOSX) 250 MacKeyEventProcessor mac_key_event_processor_; 251#endif 252 KeyEventMapper key_mapper_; 253 PepperInputHandler input_handler_; 254 255 // PIN Fetcher. 256 bool use_async_pin_dialog_; 257 protocol::SecretFetchedCallback secret_fetched_callback_; 258 259 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_; 260 261 base::WeakPtrFactory<ChromotingInstance> weak_factory_; 262 263 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance); 264}; 265 266} // namespace remoting 267 268#endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_ 269