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