chromoting_instance.h revision 868fa2fe829687343ffae624259930155e16dbd8
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/SkSize.h" 22 23// Windows defines 'PostMessage', so we have to undef it before we 24// include instance_private.h 25#if defined(PostMessage) 26#undef PostMessage 27#endif 28 29#include "ppapi/cpp/instance.h" 30#include "remoting/client/client_context.h" 31#include "remoting/client/client_user_interface.h" 32#include "remoting/client/key_event_mapper.h" 33#include "remoting/client/plugin/mac_key_event_processor.h" 34#include "remoting/client/plugin/pepper_input_handler.h" 35#include "remoting/client/plugin/pepper_plugin_thread_delegate.h" 36#include "remoting/proto/event.pb.h" 37#include "remoting/protocol/client_stub.h" 38#include "remoting/protocol/clipboard_stub.h" 39#include "remoting/protocol/connection_to_host.h" 40#include "remoting/protocol/cursor_shape_stub.h" 41#include "remoting/protocol/input_event_tracker.h" 42#include "remoting/protocol/mouse_input_filter.h" 43#include "remoting/protocol/negotiating_client_authenticator.h" 44#include "remoting/protocol/third_party_client_authenticator.h" 45 46namespace base { 47class DictionaryValue; 48} // namespace base 49 50namespace pp { 51class InputEvent; 52class Module; 53} // namespace pp 54 55namespace remoting { 56 57class ChromotingClient; 58class ChromotingStats; 59class ClientContext; 60class FrameConsumerProxy; 61class PepperAudioPlayer; 62class PepperTokenFetcher; 63class PepperView; 64class PepperXmppProxy; 65class RectangleUpdateDecoder; 66 67struct ClientConfig; 68 69class ChromotingInstance : 70 public ClientUserInterface, 71 public protocol::ClipboardStub, 72 public protocol::CursorShapeStub, 73 public pp::Instance, 74 public base::SupportsWeakPtr<ChromotingInstance> { 75 public: 76 // Plugin API version. This should be incremented whenever the API 77 // interface changes. 78 static const int kApiVersion = 7; 79 80 // Plugin API features. This allows orthogonal features to be supported 81 // without bumping the API version. 82 static const char kApiFeatures[]; 83 84 // Capabilities supported by the plugin that should also be supported by the 85 // webapp to be enabled. 86 static const char kRequestedCapabilities[]; 87 88 // Capabilities supported by the plugin that do not need to be supported by 89 // the webapp to be enabled. 90 static const char kSupportedCapabilities[]; 91 92 // Backward-compatibility version used by for the messaging 93 // interface. Should be updated whenever we remove support for 94 // an older version of the API. 95 static const int kApiMinMessagingVersion = 5; 96 97 // Backward-compatibility version used by for the ScriptableObject 98 // interface. Should be updated whenever we remove support for 99 // an older version of the API. 100 static const int kApiMinScriptableVersion = 5; 101 102 // Helper method to parse authentication_methods parameter. 103 static bool ParseAuthMethods(const std::string& auth_methods, 104 ClientConfig* config); 105 106 explicit ChromotingInstance(PP_Instance instance); 107 virtual ~ChromotingInstance(); 108 109 // pp::Instance interface. 110 virtual void DidChangeView(const pp::View& view) OVERRIDE; 111 virtual bool Init(uint32_t argc, const char* argn[], 112 const char* argv[]) OVERRIDE; 113 virtual void HandleMessage(const pp::Var& message) OVERRIDE; 114 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE; 115 116 // ClientUserInterface interface. 117 virtual void OnConnectionState(protocol::ConnectionToHost::State state, 118 protocol::ErrorCode error) OVERRIDE; 119 virtual void OnConnectionReady(bool ready) OVERRIDE; 120 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE; 121 virtual void SetPairingResponse( 122 const protocol::PairingResponse& pairing_response) OVERRIDE; 123 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE; 124 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE; 125 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> 126 GetTokenFetcher(const std::string& host_public_key) OVERRIDE; 127 128 // protocol::ClipboardStub interface. 129 virtual void InjectClipboardEvent( 130 const protocol::ClipboardEvent& event) OVERRIDE; 131 132 // protocol::CursorShapeStub interface. 133 virtual void SetCursorShape( 134 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE; 135 136 // Called by PepperView. 137 void SetDesktopSize(const SkISize& size, const SkIPoint& dpi); 138 void OnFirstFrameReceived(); 139 140 // Return statistics record by ChromotingClient. 141 // If no connection is currently active then NULL will be returned. 142 ChromotingStats* GetStats(); 143 144 // Registers a global log message handler that redirects the log output to 145 // our plugin instance. 146 // This is called by the plugin's PPP_InitializeModule. 147 // Note that no logging will be processed unless a ChromotingInstance has been 148 // registered for logging (see RegisterLoggingInstance). 149 static void RegisterLogMessageHandler(); 150 151 // Registers this instance so it processes messages sent by the global log 152 // message handler. This overwrites any previously registered instance. 153 void RegisterLoggingInstance(); 154 155 // Unregisters this instance so that debug log messages will no longer be sent 156 // to it. If this instance is not the currently registered logging instance, 157 // then the currently registered instance will stay in effect. 158 void UnregisterLoggingInstance(); 159 160 // A Log Message Handler that is called after each LOG message has been 161 // processed. This must be of type LogMessageHandlerFunction defined in 162 // base/logging.h. 163 static bool LogToUI(int severity, const char* file, int line, 164 size_t message_start, const std::string& str); 165 166 // Requests the webapp to fetch a third-party token. 167 void FetchThirdPartyToken( 168 const GURL& token_url, 169 const std::string& host_public_key, 170 const std::string& scope, 171 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher); 172 173 private: 174 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup); 175 176 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps). 177 // Immediately calls |secret_fetched_callback| with |shared_secret|. 178 static void FetchSecretFromString( 179 const std::string& shared_secret, 180 bool pairing_supported, 181 const protocol::SecretFetchedCallback& secret_fetched_callback); 182 183 // Message handlers for messages that come from JavaScript. Called 184 // from HandleMessage(). 185 void Connect(const ClientConfig& config); 186 void Disconnect(); 187 void OnIncomingIq(const std::string& iq); 188 void ReleaseAllKeys(); 189 void InjectKeyEvent(const protocol::KeyEvent& event); 190 void RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode); 191 void TrapKey(uint32 usb_keycode, bool trap); 192 void SendClipboardItem(const std::string& mime_type, const std::string& item); 193 void NotifyClientResolution(int width, int height, int x_dpi, int y_dpi); 194 void PauseVideo(bool pause); 195 void PauseAudio(bool pause); 196 void OnPinFetched(const std::string& pin); 197 void OnThirdPartyTokenFetched(const std::string& token, 198 const std::string& shared_secret); 199 void RequestPairing(const std::string& client_name); 200 201 // Helper method to post messages to the webapp. 202 void PostChromotingMessage(const std::string& method, 203 scoped_ptr<base::DictionaryValue> data); 204 205 // Posts trapped keys to the web-app to handle. 206 void SendTrappedKey(uint32 usb_keycode, bool pressed); 207 208 // Callback for PepperXmppProxy. 209 void SendOutgoingIq(const std::string& iq); 210 211 void SendPerfStats(); 212 213 void ProcessLogToUI(const std::string& message); 214 215 // Returns true if the hosting content has the chrome-extension:// scheme. 216 bool IsCallerAppOrExtension(); 217 218 // Returns true if there is a ConnectionToHost and it is connected. 219 bool IsConnected(); 220 221 // Used as the |FetchSecretCallback| for Me2Me connections. 222 // Uses the PIN request dialog in the webapp to obtain the shared secret. 223 void FetchSecretFromDialog( 224 bool pairing_supported, 225 const protocol::SecretFetchedCallback& secret_fetched_callback); 226 227 bool initialized_; 228 229 PepperPluginThreadDelegate plugin_thread_delegate_; 230 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_; 231 ClientContext context_; 232 scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_; 233 scoped_ptr<PepperView> view_; 234 pp::View plugin_view_; 235 236 scoped_ptr<protocol::ConnectionToHost> host_connection_; 237 scoped_ptr<ChromotingClient> client_; 238 239 // Input pipeline components, in reverse order of distance from input source. 240 protocol::MouseInputFilter mouse_input_filter_; 241 protocol::InputEventTracker input_tracker_; 242#if defined(OS_MACOSX) 243 MacKeyEventProcessor mac_key_event_processor_; 244#endif 245 KeyEventMapper key_mapper_; 246 PepperInputHandler input_handler_; 247 248 // XmppProxy is a refcounted interface used to perform thread-switching and 249 // detaching between objects whose lifetimes are controlled by pepper, and 250 // jingle_glue objects. This is used when if we start a sandboxed jingle 251 // connection. 252 scoped_refptr<PepperXmppProxy> xmpp_proxy_; 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 base::WeakPtrFactory<ChromotingInstance> weak_factory_; 261 262 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance); 263}; 264 265} // namespace remoting 266 267#endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_ 268