chromoting_instance.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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.
4
5#ifndef REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
6#define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
7
8#include <string>
9
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/var.h"
19#include "remoting/client/client_context.h"
20#include "remoting/client/client_user_interface.h"
21#include "remoting/client/key_event_mapper.h"
22#include "remoting/client/plugin/media_source_video_renderer.h"
23#include "remoting/client/plugin/pepper_input_handler.h"
24#include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
25#include "remoting/proto/event.pb.h"
26#include "remoting/protocol/client_stub.h"
27#include "remoting/protocol/clipboard_stub.h"
28#include "remoting/protocol/connection_to_host.h"
29#include "remoting/protocol/cursor_shape_stub.h"
30#include "remoting/protocol/input_event_tracker.h"
31#include "remoting/protocol/mouse_input_filter.h"
32#include "remoting/protocol/negotiating_client_authenticator.h"
33#include "remoting/protocol/third_party_client_authenticator.h"
34
35namespace base {
36class DictionaryValue;
37}  // namespace base
38
39namespace pp {
40class InputEvent;
41class Module;
42class VarDictionary;
43}  // namespace pp
44
45namespace jingle_glue {
46class JingleThreadWrapper;
47}  // namespace jingle_glue
48
49namespace webrtc {
50class DesktopRegion;
51class DesktopSize;
52class DesktopVector;
53}  // namespace webrtc
54
55namespace remoting {
56
57class ChromotingClient;
58class ChromotingStats;
59class ClientContext;
60class DelegatingSignalStrategy;
61class FrameConsumer;
62class FrameConsumerProxy;
63class PepperAudioPlayer;
64class PepperTokenFetcher;
65class PepperView;
66class RectangleUpdateDecoder;
67class SignalStrategy;
68class VideoRenderer;
69
70struct ClientConfig;
71
72class ChromotingInstance :
73      public ClientUserInterface,
74      public MediaSourceVideoRenderer::Delegate,
75      public protocol::ClipboardStub,
76      public protocol::CursorShapeStub,
77      public pp::Instance {
78 public:
79  // Plugin API version. This should be incremented whenever the API
80  // interface changes.
81  static const int kApiVersion = 7;
82
83  // Plugin API features. This allows orthogonal features to be supported
84  // without bumping the API version.
85  static const char kApiFeatures[];
86
87  // Capabilities supported by the plugin that should also be supported by the
88  // webapp to be enabled.
89  static const char kRequestedCapabilities[];
90
91  // Capabilities supported by the plugin that do not need to be supported by
92  // the webapp to be enabled.
93  static const char kSupportedCapabilities[];
94
95  // Backward-compatibility version used by for the messaging
96  // interface. Should be updated whenever we remove support for
97  // an older version of the API.
98  static const int kApiMinMessagingVersion = 5;
99
100  // Backward-compatibility version used by for the ScriptableObject
101  // interface. Should be updated whenever we remove support for
102  // an older version of the API.
103  static const int kApiMinScriptableVersion = 5;
104
105  // Helper method to parse authentication_methods parameter.
106  static bool ParseAuthMethods(const std::string& auth_methods,
107                               ClientConfig* config);
108
109  explicit ChromotingInstance(PP_Instance instance);
110  virtual ~ChromotingInstance();
111
112  // pp::Instance interface.
113  virtual void DidChangeFocus(bool has_focus) OVERRIDE;
114  virtual void DidChangeView(const pp::View& view) OVERRIDE;
115  virtual bool Init(uint32_t argc, const char* argn[],
116                    const char* argv[]) OVERRIDE;
117  virtual void HandleMessage(const pp::Var& message) OVERRIDE;
118  virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
119
120  // ClientUserInterface interface.
121  virtual void OnConnectionState(protocol::ConnectionToHost::State state,
122                                 protocol::ErrorCode error) OVERRIDE;
123  virtual void OnConnectionReady(bool ready) OVERRIDE;
124  virtual void OnRouteChanged(const std::string& channel_name,
125                              const protocol::TransportRoute& route) OVERRIDE;
126  virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
127  virtual void SetPairingResponse(
128      const protocol::PairingResponse& pairing_response) OVERRIDE;
129  virtual void DeliverHostMessage(
130      const protocol::ExtensionMessage& message) OVERRIDE;
131  virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
132  virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
133  virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
134  GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
135
136  // protocol::ClipboardStub interface.
137  virtual void InjectClipboardEvent(
138      const protocol::ClipboardEvent& event) OVERRIDE;
139
140  // protocol::CursorShapeStub interface.
141  virtual void SetCursorShape(
142      const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
143
144  // Called by PepperView.
145  void SetDesktopSize(const webrtc::DesktopSize& size,
146                      const webrtc::DesktopVector& dpi);
147  void SetDesktopShape(const webrtc::DesktopRegion& shape);
148  void OnFirstFrameReceived();
149
150  // Return statistics record by ChromotingClient.
151  // If no connection is currently active then NULL will be returned.
152  ChromotingStats* GetStats();
153
154  // Registers a global log message handler that redirects the log output to
155  // our plugin instance.
156  // This is called by the plugin's PPP_InitializeModule.
157  // Note that no logging will be processed unless a ChromotingInstance has been
158  // registered for logging (see RegisterLoggingInstance).
159  static void RegisterLogMessageHandler();
160
161  // Registers this instance so it processes messages sent by the global log
162  // message handler. This overwrites any previously registered instance.
163  void RegisterLoggingInstance();
164
165  // Unregisters this instance so that debug log messages will no longer be sent
166  // to it. If this instance is not the currently registered logging instance,
167  // then the currently registered instance will stay in effect.
168  void UnregisterLoggingInstance();
169
170  // A Log Message Handler that is called after each LOG message has been
171  // processed. This must be of type LogMessageHandlerFunction defined in
172  // base/logging.h.
173  static bool LogToUI(int severity, const char* file, int line,
174                      size_t message_start, const std::string& str);
175
176  // Requests the webapp to fetch a third-party token.
177  void FetchThirdPartyToken(
178      const GURL& token_url,
179      const std::string& host_public_key,
180      const std::string& scope,
181      const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher);
182
183 private:
184  FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
185
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);
192
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 HandlePauseVideo(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 HandleEnableMediaSourceRendering();
212  void HandleSendMouseInputWhenUnfocused();
213
214  // Helper method called from Connect() to connect with parsed config.
215  void ConnectWithConfig(const ClientConfig& config,
216                         const std::string& local_jid);
217
218  // Helper method to post messages to the webapp.
219  void PostChromotingMessage(const std::string& method,
220                             const pp::VarDictionary& data);
221
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.
225  //
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);
230
231  // Posts trapped keys to the web-app to handle.
232  void SendTrappedKey(uint32 usb_keycode, bool pressed);
233
234  // Callback for DelegatingSignalStrategy.
235  void SendOutgoingIq(const std::string& iq);
236
237  void SendPerfStats();
238
239  void ProcessLogToUI(const std::string& message);
240
241  // Returns true if the hosting content has the chrome-extension:// scheme.
242  bool IsCallerAppOrExtension();
243
244  // Returns true if there is a ConnectionToHost and it is connected.
245  bool IsConnected();
246
247  // Used as the |FetchSecretCallback| for Me2Me connections.
248  // Uses the PIN request dialog in the webapp to obtain the shared secret.
249  void FetchSecretFromDialog(
250      bool pairing_supported,
251      const protocol::SecretFetchedCallback& secret_fetched_callback);
252
253  // MediaSourceVideoRenderer::Delegate implementation.
254  virtual void OnMediaSourceSize(const webrtc::DesktopSize& size,
255                                 const webrtc::DesktopVector& dpi) OVERRIDE;
256  virtual void OnMediaSourceShape(const webrtc::DesktopRegion& shape) OVERRIDE;
257  virtual void OnMediaSourceReset(const std::string& format) OVERRIDE;
258  virtual void OnMediaSourceData(uint8_t* buffer, size_t buffer_size) OVERRIDE;
259
260  bool initialized_;
261
262  PepperPluginThreadDelegate plugin_thread_delegate_;
263  scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
264  scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
265  scoped_ptr<jingle_glue::JingleThreadWrapper> thread_wrapper_;
266  ClientContext context_;
267  scoped_ptr<VideoRenderer> video_renderer_;
268  scoped_ptr<PepperView> view_;
269  scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
270  pp::View plugin_view_;
271
272  // Contains the most-recently-reported desktop shape, if any.
273  scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
274
275  scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
276
277  scoped_ptr<protocol::ConnectionToHost> host_connection_;
278  scoped_ptr<ChromotingClient> client_;
279
280  // Input pipeline components, in reverse order of distance from input source.
281  protocol::MouseInputFilter mouse_input_filter_;
282  protocol::InputEventTracker input_tracker_;
283  KeyEventMapper key_mapper_;
284  scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
285  PepperInputHandler input_handler_;
286
287  // PIN Fetcher.
288  bool use_async_pin_dialog_;
289  protocol::SecretFetchedCallback secret_fetched_callback_;
290
291  // Set to true if the webapp has requested to use MediaSource API for
292  // rendering. In that case all the encoded video will be passed to the
293  // webapp for decoding.
294  bool use_media_source_rendering_;
295
296  base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_;
297
298  // Weak reference to this instance, used for global logging and task posting.
299  base::WeakPtrFactory<ChromotingInstance> weak_factory_;
300
301  DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
302};
303
304}  // namespace remoting
305
306#endif  // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
307