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/empty_cursor_filter.h"
23 #include "remoting/client/key_event_mapper.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/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/performance_tracker.h"
38 #include "remoting/protocol/performance_tracker.h"
39 #include "remoting/protocol/third_party_client_authenticator.h"
42 class DictionaryValue
;
51 namespace jingle_glue
{
52 class JingleThreadWrapper
;
53 } // namespace jingle_glue
63 class ChromotingClient
;
65 class DelegatingSignalStrategy
;
66 class PepperAudioPlayer
;
67 class PepperMouseLocker
;
68 class TokenFetcherProxy
;
70 class ChromotingInstance
: public ClientUserInterface
,
71 public PepperVideoRenderer::EventHandler
,
72 public protocol::ClipboardStub
,
73 public protocol::CursorShapeStub
,
76 // Plugin API version. This should be incremented whenever the API
78 static const int kApiVersion
= 7;
80 // Plugin API features. This allows orthogonal features to be supported
81 // without bumping the API version.
82 static const char kApiFeatures
[];
84 // Capabilities supported by the plugin that should also be supported by the
85 // webapp to be enabled.
86 static const char kRequestedCapabilities
[];
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
[];
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;
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;
102 explicit ChromotingInstance(PP_Instance instance
);
103 ~ChromotingInstance() override
;
105 // pp::Instance interface.
106 void DidChangeFocus(bool has_focus
) override
;
107 void DidChangeView(const pp::View
& view
) override
;
108 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) override
;
109 void HandleMessage(const pp::Var
& message
) override
;
110 bool HandleInputEvent(const pp::InputEvent
& event
) override
;
112 // ClientUserInterface interface.
113 void OnConnectionState(protocol::ConnectionToHost::State state
,
114 protocol::ErrorCode error
) override
;
115 void OnConnectionReady(bool ready
) override
;
116 void OnRouteChanged(const std::string
& channel_name
,
117 const protocol::TransportRoute
& route
) override
;
118 void SetCapabilities(const std::string
& capabilities
) override
;
119 void SetPairingResponse(
120 const protocol::PairingResponse
& pairing_response
) override
;
121 void DeliverHostMessage(const protocol::ExtensionMessage
& message
) override
;
122 protocol::ClipboardStub
* GetClipboardStub() override
;
123 protocol::CursorShapeStub
* GetCursorShapeStub() override
;
125 // protocol::ClipboardStub interface.
126 void InjectClipboardEvent(const protocol::ClipboardEvent
& event
) override
;
128 // protocol::CursorShapeStub interface.
129 void SetCursorShape(const protocol::CursorShapeInfo
& cursor_shape
) override
;
131 // PepperVideoRenderer::EventHandler interface.
132 void OnVideoDecodeError() override
;
133 void OnVideoFirstFrameReceived() override
;
134 void OnVideoSize(const webrtc::DesktopSize
& size
,
135 const webrtc::DesktopVector
& dpi
) override
;
136 void OnVideoShape(const webrtc::DesktopRegion
* shape
) override
;
137 void OnVideoFrameDirtyRegion(
138 const webrtc::DesktopRegion
& dirty_region
) override
;
140 // Registers a global log message handler that redirects the log output to
141 // our plugin instance.
142 // This is called by the plugin's PPP_InitializeModule.
143 // Note that no logging will be processed unless a ChromotingInstance has been
144 // registered for logging (see RegisterLoggingInstance).
145 static void RegisterLogMessageHandler();
147 // Registers this instance so it processes messages sent by the global log
148 // message handler. This overwrites any previously registered instance.
149 void RegisterLoggingInstance();
151 // Unregisters this instance so that debug log messages will no longer be sent
152 // to it. If this instance is not the currently registered logging instance,
153 // then the currently registered instance will stay in effect.
154 void UnregisterLoggingInstance();
156 // A Log Message Handler that is called after each LOG message has been
157 // processed. This must be of type LogMessageHandlerFunction defined in
159 static bool LogToUI(int severity
, const char* file
, int line
,
160 size_t message_start
, const std::string
& str
);
162 // Requests the webapp to fetch a third-party token.
163 void FetchThirdPartyToken(
164 const GURL
& token_url
,
165 const std::string
& host_public_key
,
166 const std::string
& scope
,
167 const base::WeakPtr
<TokenFetcherProxy
> pepper_token_fetcher
);
169 // Updates the specified UMA enumeration histogram with the input value.
170 void UpdateUmaEnumHistogram(const std::string
& histogram_name
,
174 // Updates the specified UMA custom counts or custom times histogram with the
176 void UpdateUmaCustomHistogram(bool is_custom_counts_histogram
,
177 const std::string
& histogram_name
,
181 int histogram_buckets
);
184 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest
, TestCaseSetup
);
186 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
187 // Immediately calls |secret_fetched_callback| with |shared_secret|.
188 static void FetchSecretFromString(
189 const std::string
& shared_secret
,
190 bool pairing_supported
,
191 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
193 // Message handlers for messages that come from JavaScript. Called
194 // from HandleMessage().
195 void HandleConnect(const base::DictionaryValue
& data
);
196 void HandleDisconnect(const base::DictionaryValue
& data
);
197 void HandleOnIncomingIq(const base::DictionaryValue
& data
);
198 void HandleReleaseAllKeys(const base::DictionaryValue
& data
);
199 void HandleInjectKeyEvent(const base::DictionaryValue
& data
);
200 void HandleRemapKey(const base::DictionaryValue
& data
);
201 void HandleTrapKey(const base::DictionaryValue
& data
);
202 void HandleSendClipboardItem(const base::DictionaryValue
& data
);
203 void HandleNotifyClientResolution(const base::DictionaryValue
& data
);
204 void HandleVideoControl(const base::DictionaryValue
& data
);
205 void HandlePauseAudio(const base::DictionaryValue
& data
);
206 void HandleOnPinFetched(const base::DictionaryValue
& data
);
207 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue
& data
);
208 void HandleRequestPairing(const base::DictionaryValue
& data
);
209 void HandleExtensionMessage(const base::DictionaryValue
& data
);
210 void HandleAllowMouseLockMessage();
211 void HandleSendMouseInputWhenUnfocused();
212 void HandleDelegateLargeCursors();
213 void HandleEnableDebugRegion(const base::DictionaryValue
& data
);
214 void HandleEnableTouchEvents(const base::DictionaryValue
& data
);
218 // Helper method to post messages to the webapp.
219 void PostChromotingMessage(const std::string
& method
,
220 const pp::VarDictionary
& data
);
222 // Same as above, but serializes messages to JSON before sending them. This
223 // method is used for backward compatibility with older version of the webapp
224 // that expect to received most messages formatted using JSON.
226 // TODO(sergeyu): When all current versions of the webapp support raw messages
227 // remove this method and use PostChromotingMessage() instead.
228 void PostLegacyJsonMessage(const std::string
& method
,
229 scoped_ptr
<base::DictionaryValue
> data
);
231 // Posts trapped keys to the web-app to handle.
232 void SendTrappedKey(uint32 usb_keycode
, bool pressed
);
234 // Callback for DelegatingSignalStrategy.
235 void SendOutgoingIq(const std::string
& iq
);
237 void UpdatePerfStatsInUI();
239 // Returns true if there is a ConnectionToHost and it is connected.
242 // Used as the |FetchSecretCallback| for Me2Me connections.
243 // Uses the PIN request dialog in the webapp to obtain the shared secret.
244 void FetchSecretFromDialog(
245 bool pairing_supported
,
246 const protocol::SecretFetchedCallback
& secret_fetched_callback
);
250 PepperPluginThreadDelegate plugin_thread_delegate_
;
251 scoped_refptr
<PluginThreadTaskRunner
> plugin_task_runner_
;
252 scoped_ptr
<base::ThreadTaskRunnerHandle
> thread_task_runner_handle_
;
253 scoped_ptr
<jingle_glue::JingleThreadWrapper
> thread_wrapper_
;
254 ClientContext context_
;
255 protocol::PerformanceTracker perf_tracker_
;
256 scoped_ptr
<PepperVideoRenderer
> video_renderer_
;
257 pp::View plugin_view_
;
259 // Contains the most-recently-reported desktop shape, if any.
260 scoped_ptr
<webrtc::DesktopRegion
> desktop_shape_
;
262 scoped_ptr
<DelegatingSignalStrategy
> signal_strategy_
;
264 scoped_ptr
<ChromotingClient
> client_
;
266 // Input pipeline components, in reverse order of distance from input source.
267 protocol::MouseInputFilter mouse_input_filter_
;
268 protocol::InputEventTracker input_tracker_
;
269 TouchInputScaler touch_input_scaler_
;
270 KeyEventMapper key_mapper_
;
271 scoped_ptr
<protocol::InputFilter
> normalizing_input_filter_
;
272 PepperInputHandler input_handler_
;
274 // Cursor shape handling components, in reverse order to that in which they
275 // process cursor shape events. Note that |mouse_locker_| appears in the
276 // cursor pipeline since it is triggered by receipt of an empty cursor.
277 PepperCursorSetter cursor_setter_
;
278 scoped_ptr
<PepperMouseLocker
> mouse_locker_
;
279 EmptyCursorFilter empty_cursor_filter_
;
281 // Used to control text input settings, such as whether to show the IME.
282 pp::TextInputController text_input_controller_
;
285 bool use_async_pin_dialog_
;
286 protocol::SecretFetchedCallback secret_fetched_callback_
;
288 base::WeakPtr
<TokenFetcherProxy
> token_fetcher_proxy_
;
290 base::RepeatingTimer
<ChromotingInstance
> stats_update_timer_
;
292 base::TimeTicks connection_started_time
;
293 base::TimeTicks connection_authenticated_time_
;
294 base::TimeTicks connection_connected_time_
;
296 // Weak reference to this instance, used for global logging and task posting.
297 base::WeakPtrFactory
<ChromotingInstance
> weak_factory_
;
299 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance
);
302 } // namespace remoting
304 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_