chromoting_instance.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 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 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 const protocol::SecretFetchedCallback& secret_fetched_callback); 179 180 // Message handlers for messages that come from JavaScript. Called 181 // from HandleMessage(). 182 void Connect(const ClientConfig& config); 183 void Disconnect(); 184 void OnIncomingIq(const std::string& iq); 185 void ReleaseAllKeys(); 186 void InjectKeyEvent(const protocol::KeyEvent& event); 187 void RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode); 188 void TrapKey(uint32 usb_keycode, bool trap); 189 void SendClipboardItem(const std::string& mime_type, const std::string& item); 190 void NotifyClientResolution(int width, int height, int x_dpi, int y_dpi); 191 void PauseVideo(bool pause); 192 void PauseAudio(bool pause); 193 void OnPinFetched(const std::string& pin); 194 void OnThirdPartyTokenFetched(const std::string& token, 195 const std::string& shared_secret); 196 197 // Helper method to post messages to the webapp. 198 void PostChromotingMessage(const std::string& method, 199 scoped_ptr<base::DictionaryValue> data); 200 201 // Posts trapped keys to the web-app to handle. 202 void SendTrappedKey(uint32 usb_keycode, bool pressed); 203 204 // Callback for PepperXmppProxy. 205 void SendOutgoingIq(const std::string& iq); 206 207 void SendPerfStats(); 208 209 void ProcessLogToUI(const std::string& message); 210 211 // Returns true if the hosting content has the chrome-extension:// scheme. 212 bool IsCallerAppOrExtension(); 213 214 // Returns true if there is a ConnectionToHost and it is connected. 215 bool IsConnected(); 216 217 // Used as the |FetchSecretCallback| for Me2Me connections. 218 // Uses the PIN request dialog in the webapp to obtain the shared secret. 219 void FetchSecretFromDialog( 220 const protocol::SecretFetchedCallback& secret_fetched_callback); 221 222 bool initialized_; 223 224 PepperPluginThreadDelegate plugin_thread_delegate_; 225 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_; 226 ClientContext context_; 227 scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_; 228 scoped_ptr<PepperView> view_; 229 pp::View plugin_view_; 230 231 scoped_ptr<protocol::ConnectionToHost> host_connection_; 232 scoped_ptr<ChromotingClient> client_; 233 234 // Input pipeline components, in reverse order of distance from input source. 235 protocol::MouseInputFilter mouse_input_filter_; 236 protocol::InputEventTracker input_tracker_; 237#if defined(OS_MACOSX) 238 MacKeyEventProcessor mac_key_event_processor_; 239#endif 240 KeyEventMapper key_mapper_; 241 PepperInputHandler input_handler_; 242 243 // XmppProxy is a refcounted interface used to perform thread-switching and 244 // detaching between objects whose lifetimes are controlled by pepper, and 245 // jingle_glue objects. This is used when if we start a sandboxed jingle 246 // connection. 247 scoped_refptr<PepperXmppProxy> xmpp_proxy_; 248 249 // PIN Fetcher. 250 bool use_async_pin_dialog_; 251 protocol::SecretFetchedCallback secret_fetched_callback_; 252 253 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_; 254 255 base::WeakPtrFactory<ChromotingInstance> weak_factory_; 256 257 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance); 258}; 259 260} // namespace remoting 261 262#endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_ 263