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