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.
5 #ifndef REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
6 #define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "ppapi/c/pp_instance.h"
15 #include "ppapi/c/pp_rect.h"
16 #include "ppapi/c/pp_resource.h"
17 #include "ppapi/cpp/instance.h"
18 #include "ppapi/cpp/text_input_controller.h"
19 #include "ppapi/cpp/var.h"
20 #include "remoting/client/client_context.h"
21 #include "remoting/client/client_user_interface.h"
22 #include "remoting/client/key_event_mapper.h"
23 #include "remoting/client/plugin/empty_cursor_filter.h"
24 #include "remoting/client/plugin/pepper_cursor_setter.h"
25 #include "remoting/client/plugin/pepper_input_handler.h"
26 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
27 #include "remoting/client/plugin/pepper_video_renderer.h"
28 #include "remoting/client/plugin/touch_input_scaler.h"
29 #include "remoting/proto/event.pb.h"
30 #include "remoting/protocol/client_stub.h"
31 #include "remoting/protocol/clipboard_stub.h"
32 #include "remoting/protocol/connection_to_host.h"
33 #include "remoting/protocol/cursor_shape_stub.h"
34 #include "remoting/protocol/input_event_tracker.h"
35 #include "remoting/protocol/mouse_input_filter.h"
36 #include "remoting/protocol/negotiating_client_authenticator.h"
37 #include "remoting/protocol/third_party_client_authenticator.h"
40 class DictionaryValue
;
49 namespace jingle_glue
{
50 class JingleThreadWrapper
;
51 } // namespace jingle_glue
61 class ChromotingClient
;
63 class DelegatingSignalStrategy
;
64 class PepperAudioPlayer
;
65 class PepperMouseLocker
;
66 class TokenFetcherProxy
;
68 class ChromotingInstance
: public ClientUserInterface
,
69 public PepperVideoRenderer::EventHandler
,
70 public protocol::ClipboardStub
,
71 public protocol::CursorShapeStub
,
74 // Plugin API version. This should be incremented whenever the API
76 static const int kApiVersion
= 7;
78 // Plugin API features. This allows orthogonal features to be supported
79 // without bumping the API version.
80 static const char kApiFeatures
[];
82 // Capabilities supported by the plugin that should also be supported by the
83 // webapp to be enabled.
84 static const char kRequestedCapabilities
[];
86 // Capabilities supported by the plugin that do not need to be supported by
87 // the webapp to be enabled.
88 static const char kSupportedCapabilities
[];
90 // Backward-compatibility version used by for the messaging
91 // interface. Should be updated whenever we remove support for
92 // an older version of the API.
93 static const int kApiMinMessagingVersion
= 5;
95 // Backward-compatibility version used by for the ScriptableObject
96 // interface. Should be updated whenever we remove support for
97 // an older version of the API.
98 static const int kApiMinScriptableVersion
= 5;
100 explicit ChromotingInstance(PP_Instance instance
);
101 ~ChromotingInstance() override
;
103 // pp::Instance interface.
104 void DidChangeFocus(bool has_focus
) override
;
105 void DidChangeView(const pp::View
& view
) override
;
106 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) override
;
107 void HandleMessage(const pp::Var
& message
) override
;
108 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
110 // ClientUserInterface interface.
111 void OnConnectionState(protocol::ConnectionToHost::State state
,
112 protocol::ErrorCode error
) override
;
113 void OnConnectionReady(bool ready
) override
;
114 void OnRouteChanged(const std::string
& channel_name
,
115 const protocol::TransportRoute
& route
) override
;
116 void SetCapabilities(const std::string
& capabilities
) override
;
117 void SetPairingResponse(
118 const protocol::PairingResponse
& pairing_response
) override
;
119 void DeliverHostMessage(const protocol::ExtensionMessage
& message
) override
;
120 protocol::ClipboardStub
* GetClipboardStub() override
;
121 protocol::CursorShapeStub
* GetCursorShapeStub() override
;
123 // protocol::ClipboardStub interface.
124 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
) override
;
126 // protocol::CursorShapeStub interface.
127 void SetCursorShape(const protocol::CursorShapeInfo
& cursor_shape
) override
;
129 // PepperVideoRenderer::EventHandler interface.
130 void OnVideoDecodeError() override
;
131 void OnVideoFirstFrameReceived() override
;
132 void OnVideoSize(const webrtc::DesktopSize
& size
,
133 const webrtc::DesktopVector
& dpi
) override
;
134 void OnVideoShape(const webrtc::DesktopRegion
& shape
) override
;
135 void OnVideoFrameDirtyRegion(
136 const webrtc::DesktopRegion
& dirty_region
) override
;
138 // Registers a global log message handler that redirects the log output to
139 // our plugin instance.
140 // This is called by the plugin's PPP_InitializeModule.
141 // Note that no logging will be processed unless a ChromotingInstance has been
142 // registered for logging (see RegisterLoggingInstance).
143 static void RegisterLogMessageHandler();
145 // Registers this instance so it processes messages sent by the global log
146 // message handler. This overwrites any previously registered instance.
147 void RegisterLoggingInstance();
149 // Unregisters this instance so that debug log messages will no longer be sent
150 // to it. If this instance is not the currently registered logging instance,
151 // then the currently registered instance will stay in effect.
152 void UnregisterLoggingInstance();
154 // A Log Message Handler that is called after each LOG message has been
155 // processed. This must be of type LogMessageHandlerFunction defined in
157 static bool LogToUI(int severity
, const char* file
, int line
,
158 size_t message_start
, const std::string
& str
);
160 // Requests the webapp to fetch a third-party token.
161 void FetchThirdPartyToken(
162 const GURL
& token_url
,
163 const std::string
& host_public_key
,
164 const std::string
& scope
,
165 const base::WeakPtr
<TokenFetcherProxy
> pepper_token_fetcher
);
168 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest
, TestCaseSetup
);
170 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
171 // Immediately calls |secret_fetched_callback| with |shared_secret|.
172 static void FetchSecretFromString(
173 const std::string
& shared_secret
,
174 bool pairing_supported
,
175 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
177 // Message handlers for messages that come from JavaScript. Called
178 // from HandleMessage().
179 void HandleConnect(const base::DictionaryValue
& data
);
180 void HandleDisconnect(const base::DictionaryValue
& data
);
181 void HandleOnIncomingIq(const base::DictionaryValue
& data
);
182 void HandleReleaseAllKeys(const base::DictionaryValue
& data
);
183 void HandleInjectKeyEvent(const base::DictionaryValue
& data
);
184 void HandleRemapKey(const base::DictionaryValue
& data
);
185 void HandleTrapKey(const base::DictionaryValue
& data
);
186 void HandleSendClipboardItem(const base::DictionaryValue
& data
);
187 void HandleNotifyClientResolution(const base::DictionaryValue
& data
);
188 void HandlePauseVideo(const base::DictionaryValue
& data
);
189 void HandleVideoControl(const base::DictionaryValue
& data
);
190 void HandlePauseAudio(const base::DictionaryValue
& data
);
191 void HandleOnPinFetched(const base::DictionaryValue
& data
);
192 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue
& data
);
193 void HandleRequestPairing(const base::DictionaryValue
& data
);
194 void HandleExtensionMessage(const base::DictionaryValue
& data
);
195 void HandleAllowMouseLockMessage();
196 void HandleSendMouseInputWhenUnfocused();
197 void HandleDelegateLargeCursors();
198 void HandleEnableDebugRegion(const base::DictionaryValue
& data
);
199 void HandleEnableTouchEvents();
203 // Helper method to post messages to the webapp.
204 void PostChromotingMessage(const std::string
& method
,
205 const pp::VarDictionary
& data
);
207 // Same as above, but serializes messages to JSON before sending them. This
208 // method is used for backward compatibility with older version of the webapp
209 // that expect to received most messages formatted using JSON.
211 // TODO(sergeyu): When all current versions of the webapp support raw messages
212 // remove this method and use PostChromotingMessage() instead.
213 void PostLegacyJsonMessage(const std::string
& method
,
214 scoped_ptr
<base::DictionaryValue
> data
);
216 // Posts trapped keys to the web-app to handle.
217 void SendTrappedKey(uint32 usb_keycode
, bool pressed
);
219 // Callback for DelegatingSignalStrategy.
220 void SendOutgoingIq(const std::string
& iq
);
222 void SendPerfStats();
224 void ProcessLogToUI(const std::string
& message
);
226 // Returns true if the hosting content has the chrome-extension:// scheme.
227 bool IsCallerAppOrExtension();
229 // Returns true if there is a ConnectionToHost and it is connected.
232 // Used as the |FetchSecretCallback| for Me2Me connections.
233 // Uses the PIN request dialog in the webapp to obtain the shared secret.
234 void FetchSecretFromDialog(
235 bool pairing_supported
,
236 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
238 // Helper to log messages in the JS console in the webapp.
239 void LogToWebapp(const std::string
& message
);
243 PepperPluginThreadDelegate plugin_thread_delegate_
;
244 scoped_refptr
<PluginThreadTaskRunner
> plugin_task_runner_
;
245 scoped_ptr
<base::ThreadTaskRunnerHandle
> thread_task_runner_handle_
;
246 scoped_ptr
<jingle_glue::JingleThreadWrapper
> thread_wrapper_
;
247 ClientContext context_
;
248 scoped_ptr
<PepperVideoRenderer
> video_renderer_
;
249 pp::View plugin_view_
;
251 // Contains the most-recently-reported desktop shape, if any.
252 scoped_ptr
<webrtc::DesktopRegion
> desktop_shape_
;
254 scoped_ptr
<DelegatingSignalStrategy
> signal_strategy_
;
256 scoped_ptr
<ChromotingClient
> client_
;
258 // Input pipeline components, in reverse order of distance from input source.
259 protocol::MouseInputFilter mouse_input_filter_
;
260 protocol::InputEventTracker input_tracker_
;
261 TouchInputScaler touch_input_scaler_
;
262 KeyEventMapper key_mapper_
;
263 scoped_ptr
<protocol::InputFilter
> normalizing_input_filter_
;
264 PepperInputHandler input_handler_
;
266 // Cursor shape handling components, in reverse order to that in which they
267 // process cursor shape events. Note that |mouse_locker_| appears in the
268 // cursor pipeline since it is triggered by receipt of an empty cursor.
269 PepperCursorSetter cursor_setter_
;
270 scoped_ptr
<PepperMouseLocker
> mouse_locker_
;
271 EmptyCursorFilter empty_cursor_filter_
;
273 // Used to control text input settings, such as whether to show the IME.
274 pp::TextInputController text_input_controller_
;
277 bool use_async_pin_dialog_
;
278 protocol::SecretFetchedCallback secret_fetched_callback_
;
280 base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy_
;
282 // Weak reference to this instance, used for global logging and task posting.
283 base::WeakPtrFactory
<ChromotingInstance
> weak_factory_
;
285 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance
);
288 } // namespace remoting
290 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_