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/media_source_video_renderer.h"
24 #include "remoting/client/plugin/pepper_input_handler.h"
25 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
26 #include "remoting/proto/event.pb.h"
27 #include "remoting/protocol/client_stub.h"
28 #include "remoting/protocol/clipboard_stub.h"
29 #include "remoting/protocol/connection_to_host.h"
30 #include "remoting/protocol/cursor_shape_stub.h"
31 #include "remoting/protocol/input_event_tracker.h"
32 #include "remoting/protocol/mouse_input_filter.h"
33 #include "remoting/protocol/negotiating_client_authenticator.h"
34 #include "remoting/protocol/third_party_client_authenticator.h"
37 class DictionaryValue;
46 namespace jingle_glue {
47 class JingleThreadWrapper;
48 } // namespace jingle_glue
58 class ChromotingClient;
59 class ChromotingStats;
61 class DelegatingSignalStrategy;
63 class FrameConsumerProxy;
64 class PepperAudioPlayer;
65 class TokenFetcherProxy;
67 class RectangleUpdateDecoder;
73 class ChromotingInstance :
74 public ClientUserInterface,
75 public MediaSourceVideoRenderer::Delegate,
76 public protocol::ClipboardStub,
77 public protocol::CursorShapeStub,
80 // Plugin API version. This should be incremented whenever the API
82 static const int kApiVersion = 7;
84 // Plugin API features. This allows orthogonal features to be supported
85 // without bumping the API version.
86 static const char kApiFeatures[];
88 // Capabilities supported by the plugin that should also be supported by the
89 // webapp to be enabled.
90 static const char kRequestedCapabilities[];
92 // Capabilities supported by the plugin that do not need to be supported by
93 // the webapp to be enabled.
94 static const char kSupportedCapabilities[];
96 // Backward-compatibility version used by for the messaging
97 // interface. Should be updated whenever we remove support for
98 // an older version of the API.
99 static const int kApiMinMessagingVersion = 5;
101 // Backward-compatibility version used by for the ScriptableObject
102 // interface. Should be updated whenever we remove support for
103 // an older version of the API.
104 static const int kApiMinScriptableVersion = 5;
106 explicit ChromotingInstance(PP_Instance instance);
107 ~ChromotingInstance() override;
109 // pp::Instance interface.
110 void DidChangeFocus(bool has_focus) override;
111 void DidChangeView(const pp::View& view) override;
112 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override;
113 void HandleMessage(const pp::Var& message) override;
114 bool HandleInputEvent(const pp::InputEvent& event) override;
116 // ClientUserInterface interface.
117 void OnConnectionState(protocol::ConnectionToHost::State state,
118 protocol::ErrorCode error) override;
119 void OnConnectionReady(bool ready) override;
120 void OnRouteChanged(const std::string& channel_name,
121 const protocol::TransportRoute& route) override;
122 void SetCapabilities(const std::string& capabilities) override;
123 void SetPairingResponse(
124 const protocol::PairingResponse& pairing_response) override;
125 void DeliverHostMessage(const protocol::ExtensionMessage& message) override;
126 protocol::ClipboardStub* GetClipboardStub() override;
127 protocol::CursorShapeStub* GetCursorShapeStub() override;
129 // protocol::ClipboardStub interface.
130 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override;
132 // protocol::CursorShapeStub interface.
133 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override;
135 // Called by PepperView.
136 void SetDesktopSize(const webrtc::DesktopSize& size,
137 const webrtc::DesktopVector& dpi);
138 void SetDesktopShape(const webrtc::DesktopRegion& shape);
139 void OnFirstFrameReceived();
141 // Return statistics record by ChromotingClient.
142 // If no connection is currently active then NULL will be returned.
143 ChromotingStats* GetStats();
145 // Registers a global log message handler that redirects the log output to
146 // our plugin instance.
147 // This is called by the plugin's PPP_InitializeModule.
148 // Note that no logging will be processed unless a ChromotingInstance has been
149 // registered for logging (see RegisterLoggingInstance).
150 static void RegisterLogMessageHandler();
152 // Registers this instance so it processes messages sent by the global log
153 // message handler. This overwrites any previously registered instance.
154 void RegisterLoggingInstance();
156 // Unregisters this instance so that debug log messages will no longer be sent
157 // to it. If this instance is not the currently registered logging instance,
158 // then the currently registered instance will stay in effect.
159 void UnregisterLoggingInstance();
161 // A Log Message Handler that is called after each LOG message has been
162 // processed. This must be of type LogMessageHandlerFunction defined in
164 static bool LogToUI(int severity, const char* file, int line,
165 size_t message_start, const std::string& str);
167 // Requests the webapp to fetch a third-party token.
168 void FetchThirdPartyToken(
169 const GURL& token_url,
170 const std::string& host_public_key,
171 const std::string& scope,
172 const base::WeakPtr<TokenFetcherProxy> pepper_token_fetcher);
175 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
177 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
178 // Immediately calls |secret_fetched_callback| with |shared_secret|.
179 static void FetchSecretFromString(
180 const std::string& shared_secret,
181 bool pairing_supported,
182 const protocol::SecretFetchedCallback& secret_fetched_callback);
184 // Message handlers for messages that come from JavaScript. Called
185 // from HandleMessage().
186 void HandleConnect(const base::DictionaryValue& data);
187 void HandleDisconnect(const base::DictionaryValue& data);
188 void HandleOnIncomingIq(const base::DictionaryValue& data);
189 void HandleReleaseAllKeys(const base::DictionaryValue& data);
190 void HandleInjectKeyEvent(const base::DictionaryValue& data);
191 void HandleRemapKey(const base::DictionaryValue& data);
192 void HandleTrapKey(const base::DictionaryValue& data);
193 void HandleSendClipboardItem(const base::DictionaryValue& data);
194 void HandleNotifyClientResolution(const base::DictionaryValue& data);
195 void HandlePauseVideo(const base::DictionaryValue& data);
196 void HandleVideoControl(const base::DictionaryValue& data);
197 void HandlePauseAudio(const base::DictionaryValue& data);
198 void HandleOnPinFetched(const base::DictionaryValue& data);
199 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data);
200 void HandleRequestPairing(const base::DictionaryValue& data);
201 void HandleExtensionMessage(const base::DictionaryValue& data);
202 void HandleAllowMouseLockMessage();
203 void HandleEnableMediaSourceRendering();
204 void HandleSendMouseInputWhenUnfocused();
205 void HandleDelegateLargeCursors();
207 // Helper method to post messages to the webapp.
208 void PostChromotingMessage(const std::string& method,
209 const pp::VarDictionary& data);
211 // Same as above, but serializes messages to JSON before sending them. This
212 // method is used for backward compatibility with older version of the webapp
213 // that expect to received most messages formatted using JSON.
215 // TODO(sergeyu): When all current versions of the webapp support raw messages
216 // remove this method and use PostChromotingMessage() instead.
217 void PostLegacyJsonMessage(const std::string& method,
218 scoped_ptr<base::DictionaryValue> data);
220 // Posts trapped keys to the web-app to handle.
221 void SendTrappedKey(uint32 usb_keycode, bool pressed);
223 // Callback for DelegatingSignalStrategy.
224 void SendOutgoingIq(const std::string& iq);
226 void SendPerfStats();
228 void ProcessLogToUI(const std::string& message);
230 // Returns true if the hosting content has the chrome-extension:// scheme.
231 bool IsCallerAppOrExtension();
233 // Returns true if there is a ConnectionToHost and it is connected.
236 // Used as the |FetchSecretCallback| for Me2Me connections.
237 // Uses the PIN request dialog in the webapp to obtain the shared secret.
238 void FetchSecretFromDialog(
239 bool pairing_supported,
240 const protocol::SecretFetchedCallback& secret_fetched_callback);
242 // MediaSourceVideoRenderer::Delegate implementation.
243 void OnMediaSourceSize(const webrtc::DesktopSize& size,
244 const webrtc::DesktopVector& dpi) override;
245 void OnMediaSourceShape(const webrtc::DesktopRegion& shape) override;
246 void OnMediaSourceReset(const std::string& format) override;
247 void OnMediaSourceData(uint8_t* buffer,
249 bool keyframe) override;
253 PepperPluginThreadDelegate plugin_thread_delegate_;
254 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
255 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
256 scoped_ptr<jingle_glue::JingleThreadWrapper> thread_wrapper_;
257 ClientContext context_;
258 scoped_ptr<VideoRenderer> video_renderer_;
259 scoped_ptr<PepperView> view_;
260 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
261 pp::View plugin_view_;
263 // Contains the most-recently-reported desktop shape, if any.
264 scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
266 scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
268 scoped_ptr<ChromotingClient> client_;
270 // Input pipeline components, in reverse order of distance from input source.
271 protocol::MouseInputFilter mouse_input_filter_;
272 protocol::InputEventTracker input_tracker_;
273 KeyEventMapper key_mapper_;
274 scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
275 PepperInputHandler input_handler_;
277 // Used to control text input settings, such as whether to show the IME.
278 pp::TextInputController text_input_controller_;
281 bool use_async_pin_dialog_;
282 protocol::SecretFetchedCallback secret_fetched_callback_;
284 // Set to true if the webapp has requested to use MediaSource API for
285 // rendering. In that case all the encoded video will be passed to the
286 // webapp for decoding.
287 bool use_media_source_rendering_;
289 // Set to true if the web-app can handle large cursors. If false, then large
290 // cursors will be cropped to the maximum size supported by Pepper.
291 bool delegate_large_cursors_;
293 base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy_;
295 // Weak reference to this instance, used for global logging and task posting.
296 base::WeakPtrFactory<ChromotingInstance> weak_factory_;
298 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
301 } // namespace remoting
303 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_