chromoting_instance.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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 DidChangeView(const pp::View& view) OVERRIDE; 106 virtual bool Init(uint32_t argc, const char* argn[], 107 const char* argv[]) OVERRIDE; 108 virtual void HandleMessage(const pp::Var& message) OVERRIDE; 109 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE; 110 111 // ClientUserInterface interface. 112 virtual void OnConnectionState(protocol::ConnectionToHost::State state, 113 protocol::ErrorCode error) OVERRIDE; 114 virtual void OnConnectionReady(bool ready) OVERRIDE; 115 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 116 virtual void SetPairingResponse( 117 const protocol::PairingResponse& pairing_response) OVERRIDE; 118 virtual void DeliverHostMessage( 119 const protocol::ExtensionMessage& message) OVERRIDE; 120 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; 121 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; 122 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> 123 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; 124 125 // protocol::ClipboardStub interface. 126 virtual void InjectClipboardEvent( 127 const protocol::ClipboardEvent& event) OVERRIDE; 128 129 // protocol::CursorShapeStub interface. 130 virtual void SetCursorShape( 131 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE; 132 133 // Called by PepperView. 134 void SetDesktopSize(const SkISize& size, const SkIPoint& dpi); 135 void SetDesktopShape(const SkRegion& shape); 136 void OnFirstFrameReceived(); 137 138 // Return statistics record by ChromotingClient. 139 // If no connection is currently active then NULL will be returned. 140 ChromotingStats* GetStats(); 141 142 // Registers a global log message handler that redirects the log output to 143 // our plugin instance. 144 // This is called by the plugin's PPP_InitializeModule. 145 // Note that no logging will be processed unless a ChromotingInstance has been 146 // registered for logging (see RegisterLoggingInstance). 147 static void RegisterLogMessageHandler(); 148 149 // Registers this instance so it processes messages sent by the global log 150 // message handler. This overwrites any previously registered instance. 151 void RegisterLoggingInstance(); 152 153 // Unregisters this instance so that debug log messages will no longer be sent 154 // to it. If this instance is not the currently registered logging instance, 155 // then the currently registered instance will stay in effect. 156 void UnregisterLoggingInstance(); 157 158 // A Log Message Handler that is called after each LOG message has been 159 // processed. This must be of type LogMessageHandlerFunction defined in 160 // base/logging.h. 161 static bool LogToUI(int severity, const char* file, int line, 162 size_t message_start, const std::string& str); 163 164 // Requests the webapp to fetch a third-party token. 165 void FetchThirdPartyToken( 166 const GURL& token_url, 167 const std::string& host_public_key, 168 const std::string& scope, 169 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher); 170 171 private: 172 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup); 173 174 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps). 175 // Immediately calls |secret_fetched_callback| with |shared_secret|. 176 static void FetchSecretFromString( 177 const std::string& shared_secret, 178 bool pairing_supported, 179 const protocol::SecretFetchedCallback& secret_fetched_callback); 180 181 // Message handlers for messages that come from JavaScript. Called 182 // from HandleMessage(). 183 void HandleConnect(const base::DictionaryValue& data); 184 void HandleDisconnect(const base::DictionaryValue& data); 185 void HandleOnIncomingIq(const base::DictionaryValue& data); 186 void HandleReleaseAllKeys(const base::DictionaryValue& data); 187 void HandleInjectKeyEvent(const base::DictionaryValue& data); 188 void HandleRemapKey(const base::DictionaryValue& data); 189 void HandleTrapKey(const base::DictionaryValue& data); 190 void HandleSendClipboardItem(const base::DictionaryValue& data); 191 void HandleNotifyClientResolution(const base::DictionaryValue& data); 192 void HandlePauseVideo(const base::DictionaryValue& data); 193 void HandlePauseAudio(const base::DictionaryValue& data); 194 void HandleOnPinFetched(const base::DictionaryValue& data); 195 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data); 196 void HandleRequestPairing(const base::DictionaryValue& data); 197 void HandleExtensionMessage(const base::DictionaryValue& data); 198 199 // Helper method called from Connect() to connect with parsed config. 200 void ConnectWithConfig(const ClientConfig& config, 201 const std::string& local_jid); 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 DelegatingSignalStrategy. 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 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_; 237 pp::View plugin_view_; 238 239 // Contains the most-recently-reported desktop shape, if any. 240 scoped_ptr<SkRegion> desktop_shape_; 241 242 scoped_ptr<DelegatingSignalStrategy> signal_strategy_; 243 244 scoped_ptr<protocol::ConnectionToHost> host_connection_; 245 scoped_ptr<ChromotingClient> client_; 246 247 // Input pipeline components, in reverse order of distance from input source. 248 protocol::MouseInputFilter mouse_input_filter_; 249 protocol::InputEventTracker input_tracker_; 250 KeyEventMapper key_mapper_; 251 scoped_ptr<protocol::InputFilter> normalizing_input_filter_; 252 PepperInputHandler input_handler_; 253 254 // PIN Fetcher. 255 bool use_async_pin_dialog_; 256 protocol::SecretFetchedCallback secret_fetched_callback_; 257 258 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_; 259 260 // Weak reference to this instance, used for global logging and task posting. 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