chromoting_instance.h revision 58537e28ecd584eab876aee8be7156509866d23a
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#include "third_party/skia/include/core/SkPoint.h" 37#include "third_party/skia/include/core/SkRegion.h" 38#include "third_party/skia/include/core/SkSize.h" 39 40namespace base { 41class DictionaryValue; 42} // namespace base 43 44namespace pp { 45class InputEvent; 46class Module; 47} // namespace pp 48 49namespace remoting { 50 51class ChromotingClient; 52class ChromotingStats; 53class ClientContext; 54class DelegatingSignalStrategy; 55class FrameConsumer; 56class FrameConsumerProxy; 57class PepperAudioPlayer; 58class PepperTokenFetcher; 59class PepperView; 60class RectangleUpdateDecoder; 61class SignalStrategy; 62 63struct ClientConfig; 64 65class ChromotingInstance : 66 public ClientUserInterface, 67 public protocol::ClipboardStub, 68 public protocol::CursorShapeStub, 69 public pp::Instance { 70 public: 71 // Plugin API version. This should be incremented whenever the API 72 // interface changes. 73 static const int kApiVersion = 7; 74 75 // Plugin API features. This allows orthogonal features to be supported 76 // without bumping the API version. 77 static const char kApiFeatures[]; 78 79 // Capabilities supported by the plugin that should also be supported by the 80 // webapp to be enabled. 81 static const char kRequestedCapabilities[]; 82 83 // Capabilities supported by the plugin that do not need to be supported by 84 // the webapp to be enabled. 85 static const char kSupportedCapabilities[]; 86 87 // Backward-compatibility version used by for the messaging 88 // interface. Should be updated whenever we remove support for 89 // an older version of the API. 90 static const int kApiMinMessagingVersion = 5; 91 92 // Backward-compatibility version used by for the ScriptableObject 93 // interface. Should be updated whenever we remove support for 94 // an older version of the API. 95 static const int kApiMinScriptableVersion = 5; 96 97 // Helper method to parse authentication_methods parameter. 98 static bool ParseAuthMethods(const std::string& auth_methods, 99 ClientConfig* config); 100 101 explicit ChromotingInstance(PP_Instance instance); 102 virtual ~ChromotingInstance(); 103 104 // pp::Instance interface. 105 virtual void DidChangeFocus(bool has_focus) OVERRIDE; 106 virtual void DidChangeView(const pp::View& view) OVERRIDE; 107 virtual bool Init(uint32_t argc, const char* argn[], 108 const char* argv[]) OVERRIDE; 109 virtual void HandleMessage(const pp::Var& message) OVERRIDE; 110 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE; 111 112 // ClientUserInterface interface. 113 virtual void OnConnectionState(protocol::ConnectionToHost::State state, 114 protocol::ErrorCode error) OVERRIDE; 115 virtual void OnConnectionReady(bool ready) OVERRIDE; 116 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 117 virtual void SetPairingResponse( 118 const protocol::PairingResponse& pairing_response) OVERRIDE; 119 virtual void DeliverHostMessage( 120 const protocol::ExtensionMessage& message) OVERRIDE; 121 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; 122 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; 123 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> 124 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; 125 126 // protocol::ClipboardStub interface. 127 virtual void InjectClipboardEvent( 128 const protocol::ClipboardEvent& event) OVERRIDE; 129 130 // protocol::CursorShapeStub interface. 131 virtual void SetCursorShape( 132 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE; 133 134 // Called by PepperView. 135 void SetDesktopSize(const SkISize& size, const SkIPoint& dpi); 136 void SetDesktopShape(const SkRegion& shape); 137 void OnFirstFrameReceived(); 138 139 // Return statistics record by ChromotingClient. 140 // If no connection is currently active then NULL will be returned. 141 ChromotingStats* GetStats(); 142 143 // Registers a global log message handler that redirects the log output to 144 // our plugin instance. 145 // This is called by the plugin's PPP_InitializeModule. 146 // Note that no logging will be processed unless a ChromotingInstance has been 147 // registered for logging (see RegisterLoggingInstance). 148 static void RegisterLogMessageHandler(); 149 150 // Registers this instance so it processes messages sent by the global log 151 // message handler. This overwrites any previously registered instance. 152 void RegisterLoggingInstance(); 153 154 // Unregisters this instance so that debug log messages will no longer be sent 155 // to it. If this instance is not the currently registered logging instance, 156 // then the currently registered instance will stay in effect. 157 void UnregisterLoggingInstance(); 158 159 // A Log Message Handler that is called after each LOG message has been 160 // processed. This must be of type LogMessageHandlerFunction defined in 161 // base/logging.h. 162 static bool LogToUI(int severity, const char* file, int line, 163 size_t message_start, const std::string& str); 164 165 // Requests the webapp to fetch a third-party token. 166 void FetchThirdPartyToken( 167 const GURL& token_url, 168 const std::string& host_public_key, 169 const std::string& scope, 170 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher); 171 172 private: 173 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup); 174 175 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps). 176 // Immediately calls |secret_fetched_callback| with |shared_secret|. 177 static void FetchSecretFromString( 178 const std::string& shared_secret, 179 bool pairing_supported, 180 const protocol::SecretFetchedCallback& secret_fetched_callback); 181 182 // Message handlers for messages that come from JavaScript. Called 183 // from HandleMessage(). 184 void HandleConnect(const base::DictionaryValue& data); 185 void HandleDisconnect(const base::DictionaryValue& data); 186 void HandleOnIncomingIq(const base::DictionaryValue& data); 187 void HandleReleaseAllKeys(const base::DictionaryValue& data); 188 void HandleInjectKeyEvent(const base::DictionaryValue& data); 189 void HandleRemapKey(const base::DictionaryValue& data); 190 void HandleTrapKey(const base::DictionaryValue& data); 191 void HandleSendClipboardItem(const base::DictionaryValue& data); 192 void HandleNotifyClientResolution(const base::DictionaryValue& data); 193 void HandlePauseVideo(const base::DictionaryValue& data); 194 void HandlePauseAudio(const base::DictionaryValue& data); 195 void HandleOnPinFetched(const base::DictionaryValue& data); 196 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data); 197 void HandleRequestPairing(const base::DictionaryValue& data); 198 void HandleExtensionMessage(const base::DictionaryValue& data); 199 void HandleAllowMouseLockMessage(); 200 201 // Helper method called from Connect() to connect with parsed config. 202 void ConnectWithConfig(const ClientConfig& config, 203 const std::string& local_jid); 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 DelegatingSignalStrategy. 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 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_; 239 pp::View plugin_view_; 240 241 // Contains the most-recently-reported desktop shape, if any. 242 scoped_ptr<SkRegion> desktop_shape_; 243 244 scoped_ptr<DelegatingSignalStrategy> signal_strategy_; 245 246 scoped_ptr<protocol::ConnectionToHost> host_connection_; 247 scoped_ptr<ChromotingClient> client_; 248 249 // Input pipeline components, in reverse order of distance from input source. 250 protocol::MouseInputFilter mouse_input_filter_; 251 protocol::InputEventTracker input_tracker_; 252 KeyEventMapper key_mapper_; 253 scoped_ptr<protocol::InputFilter> normalizing_input_filter_; 254 PepperInputHandler input_handler_; 255 256 // PIN Fetcher. 257 bool use_async_pin_dialog_; 258 protocol::SecretFetchedCallback secret_fetched_callback_; 259 260 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_; 261 262 // Weak reference to this instance, used for global logging and task posting. 263 base::WeakPtrFactory<ChromotingInstance> weak_factory_; 264 265 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance); 266}; 267 268} // namespace remoting 269 270#endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_ 271