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/chromoting_stats.h"
21 #include "remoting/client/client_context.h"
22 #include "remoting/client/client_user_interface.h"
23 #include "remoting/client/empty_cursor_filter.h"
24 #include "remoting/client/key_event_mapper.h"
25 #include "remoting/client/plugin/pepper_cursor_setter.h"
26 #include "remoting/client/plugin/pepper_input_handler.h"
27 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
28 #include "remoting/client/plugin/pepper_video_renderer.h"
29 #include "remoting/client/touch_input_scaler.h"
30 #include "remoting/proto/event.pb.h"
31 #include "remoting/protocol/client_stub.h"
32 #include "remoting/protocol/clipboard_stub.h"
33 #include "remoting/protocol/connection_to_host.h"
34 #include "remoting/protocol/cursor_shape_stub.h"
35 #include "remoting/protocol/input_event_tracker.h"
36 #include "remoting/protocol/mouse_input_filter.h"
37 #include "remoting/protocol/negotiating_client_authenticator.h"
38 #include "remoting/protocol/third_party_client_authenticator.h"
41 class DictionaryValue
;
50 namespace jingle_glue
{
51 class JingleThreadWrapper
;
52 } // namespace jingle_glue
62 class ChromotingClient
;
64 class DelegatingSignalStrategy
;
65 class PepperAudioPlayer
;
66 class PepperMouseLocker
;
67 class TokenFetcherProxy
;
69 class ChromotingInstance
: public ClientUserInterface
,
70 public PepperVideoRenderer::EventHandler
,
71 public protocol::ClipboardStub
,
72 public protocol::CursorShapeStub
,
75 // Plugin API version. This should be incremented whenever the API
77 static const int kApiVersion
= 7;
79 // Plugin API features. This allows orthogonal features to be supported
80 // without bumping the API version.
81 static const char kApiFeatures
[];
83 // Capabilities supported by the plugin that should also be supported by the
84 // webapp to be enabled.
85 static const char kRequestedCapabilities
[];
87 // Capabilities supported by the plugin that do not need to be supported by
88 // the webapp to be enabled.
89 static const char kSupportedCapabilities
[];
91 // Backward-compatibility version used by for the messaging
92 // interface. Should be updated whenever we remove support for
93 // an older version of the API.
94 static const int kApiMinMessagingVersion
= 5;
96 // Backward-compatibility version used by for the ScriptableObject
97 // interface. Should be updated whenever we remove support for
98 // an older version of the API.
99 static const int kApiMinScriptableVersion
= 5;
101 explicit ChromotingInstance(PP_Instance instance
);
102 ~ChromotingInstance() override
;
104 // pp::Instance interface.
105 void DidChangeFocus(bool has_focus
) override
;
106 void DidChangeView(const pp::View
& view
) override
;
107 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) override
;
108 void HandleMessage(const pp::Var
& message
) override
;
109 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
111 // ClientUserInterface interface.
112 void OnConnectionState(protocol::ConnectionToHost::State state
,
113 protocol::ErrorCode error
) override
;
114 void OnConnectionReady(bool ready
) override
;
115 void OnRouteChanged(const std::string
& channel_name
,
116 const protocol::TransportRoute
& route
) override
;
117 void SetCapabilities(const std::string
& capabilities
) override
;
118 void SetPairingResponse(
119 const protocol::PairingResponse
& pairing_response
) override
;
120 void DeliverHostMessage(const protocol::ExtensionMessage
& message
) override
;
121 protocol::ClipboardStub
* GetClipboardStub() override
;
122 protocol::CursorShapeStub
* GetCursorShapeStub() override
;
124 // protocol::ClipboardStub interface.
125 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
) override
;
127 // protocol::CursorShapeStub interface.
128 void SetCursorShape(const protocol::CursorShapeInfo
& cursor_shape
) override
;
130 // PepperVideoRenderer::EventHandler interface.
131 void OnVideoDecodeError() override
;
132 void OnVideoFirstFrameReceived() override
;
133 void OnVideoSize(const webrtc::DesktopSize
& size
,
134 const webrtc::DesktopVector
& dpi
) override
;
135 void OnVideoShape(const webrtc::DesktopRegion
* shape
) override
;
136 void OnVideoFrameDirtyRegion(
137 const webrtc::DesktopRegion
& dirty_region
) override
;
139 // Registers a global log message handler that redirects the log output to
140 // our plugin instance.
141 // This is called by the plugin's PPP_InitializeModule.
142 // Note that no logging will be processed unless a ChromotingInstance has been
143 // registered for logging (see RegisterLoggingInstance).
144 static void RegisterLogMessageHandler();
146 // Registers this instance so it processes messages sent by the global log
147 // message handler. This overwrites any previously registered instance.
148 void RegisterLoggingInstance();
150 // Unregisters this instance so that debug log messages will no longer be sent
151 // to it. If this instance is not the currently registered logging instance,
152 // then the currently registered instance will stay in effect.
153 void UnregisterLoggingInstance();
155 // A Log Message Handler that is called after each LOG message has been
156 // processed. This must be of type LogMessageHandlerFunction defined in
158 static bool LogToUI(int severity
, const char* file
, int line
,
159 size_t message_start
, const std::string
& str
);
161 // Requests the webapp to fetch a third-party token.
162 void FetchThirdPartyToken(
163 const GURL
& token_url
,
164 const std::string
& host_public_key
,
165 const std::string
& scope
,
166 const base::WeakPtr
<TokenFetcherProxy
> pepper_token_fetcher
);
168 // Updates the specified UMA enumeration histogram with the input value.
169 void UpdateUmaEnumHistogram(const std::string
& histogram_name
,
173 // Updates the specified UMA custom counts or custom times histogram with the
175 void UpdateUmaCustomHistogram(bool is_custom_counts_histogram
,
176 const std::string
& histogram_name
,
180 int histogram_buckets
);
183 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest
, TestCaseSetup
);
185 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
186 // Immediately calls |secret_fetched_callback| with |shared_secret|.
187 static void FetchSecretFromString(
188 const std::string
& shared_secret
,
189 bool pairing_supported
,
190 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
192 // Message handlers for messages that come from JavaScript. Called
193 // from HandleMessage().
194 void HandleConnect(const base::DictionaryValue
& data
);
195 void HandleDisconnect(const base::DictionaryValue
& data
);
196 void HandleOnIncomingIq(const base::DictionaryValue
& data
);
197 void HandleReleaseAllKeys(const base::DictionaryValue
& data
);
198 void HandleInjectKeyEvent(const base::DictionaryValue
& data
);
199 void HandleRemapKey(const base::DictionaryValue
& data
);
200 void HandleTrapKey(const base::DictionaryValue
& data
);
201 void HandleSendClipboardItem(const base::DictionaryValue
& data
);
202 void HandleNotifyClientResolution(const base::DictionaryValue
& data
);
203 void HandleVideoControl(const base::DictionaryValue
& data
);
204 void HandlePauseAudio(const base::DictionaryValue
& data
);
205 void HandleOnPinFetched(const base::DictionaryValue
& data
);
206 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue
& data
);
207 void HandleRequestPairing(const base::DictionaryValue
& data
);
208 void HandleExtensionMessage(const base::DictionaryValue
& data
);
209 void HandleAllowMouseLockMessage();
210 void HandleSendMouseInputWhenUnfocused();
211 void HandleDelegateLargeCursors();
212 void HandleEnableDebugRegion(const base::DictionaryValue
& data
);
213 void HandleEnableTouchEvents(const base::DictionaryValue
& data
);
217 // Helper method to post messages to the webapp.
218 void PostChromotingMessage(const std::string
& method
,
219 const pp::VarDictionary
& data
);
221 // Same as above, but serializes messages to JSON before sending them. This
222 // method is used for backward compatibility with older version of the webapp
223 // that expect to received most messages formatted using JSON.
225 // TODO(sergeyu): When all current versions of the webapp support raw messages
226 // remove this method and use PostChromotingMessage() instead.
227 void PostLegacyJsonMessage(const std::string
& method
,
228 scoped_ptr
<base::DictionaryValue
> data
);
230 // Posts trapped keys to the web-app to handle.
231 void SendTrappedKey(uint32 usb_keycode
, bool pressed
);
233 // Callback for DelegatingSignalStrategy.
234 void SendOutgoingIq(const std::string
& iq
);
236 void SendPerfStats();
238 // Returns true if there is a ConnectionToHost and it is connected.
241 // Used as the |FetchSecretCallback| for Me2Me connections.
242 // Uses the PIN request dialog in the webapp to obtain the shared secret.
243 void FetchSecretFromDialog(
244 bool pairing_supported
,
245 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
249 PepperPluginThreadDelegate plugin_thread_delegate_
;
250 scoped_refptr
<PluginThreadTaskRunner
> plugin_task_runner_
;
251 scoped_ptr
<base::ThreadTaskRunnerHandle
> thread_task_runner_handle_
;
252 scoped_ptr
<jingle_glue::JingleThreadWrapper
> thread_wrapper_
;
253 ClientContext context_
;
254 scoped_ptr
<PepperVideoRenderer
> video_renderer_
;
255 pp::View plugin_view_
;
257 // Contains the most-recently-reported desktop shape, if any.
258 scoped_ptr
<webrtc::DesktopRegion
> desktop_shape_
;
260 scoped_ptr
<DelegatingSignalStrategy
> signal_strategy_
;
262 scoped_ptr
<ChromotingClient
> client_
;
264 // Input pipeline components, in reverse order of distance from input source.
265 protocol::MouseInputFilter mouse_input_filter_
;
266 protocol::InputEventTracker input_tracker_
;
267 TouchInputScaler touch_input_scaler_
;
268 KeyEventMapper key_mapper_
;
269 scoped_ptr
<protocol::InputFilter
> normalizing_input_filter_
;
270 PepperInputHandler input_handler_
;
272 // Cursor shape handling components, in reverse order to that in which they
273 // process cursor shape events. Note that |mouse_locker_| appears in the
274 // cursor pipeline since it is triggered by receipt of an empty cursor.
275 PepperCursorSetter cursor_setter_
;
276 scoped_ptr
<PepperMouseLocker
> mouse_locker_
;
277 EmptyCursorFilter empty_cursor_filter_
;
279 // Used to control text input settings, such as whether to show the IME.
280 pp::TextInputController text_input_controller_
;
283 bool use_async_pin_dialog_
;
284 protocol::SecretFetchedCallback secret_fetched_callback_
;
286 base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy_
;
288 // Weak reference to this instance, used for global logging and task posting.
289 base::WeakPtrFactory
<ChromotingInstance
> weak_factory_
;
291 base::TimeTicks connection_started_time
;
292 base::TimeTicks connection_authenticated_time_
;
293 base::TimeTicks connection_connected_time_
;
295 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance
);
298 } // namespace remoting
300 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_