chromoting_instance.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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// TODO(ajwong): We need to come up with a better description of the
6// responsibilities for each thread.
7
8#ifndef REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
9#define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
10
11#include <string>
12
13#include "base/gtest_prod_util.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "ppapi/c/pp_instance.h"
17#include "ppapi/c/pp_rect.h"
18#include "ppapi/c/pp_resource.h"
19#include "ppapi/cpp/var.h"
20#include "third_party/skia/include/core/SkPoint.h"
21#include "third_party/skia/include/core/SkSize.h"
22
23// Windows defines 'PostMessage', so we have to undef it before we
24// include instance_private.h
25#if defined(PostMessage)
26#undef PostMessage
27#endif
28
29#include "ppapi/cpp/instance.h"
30#include "remoting/client/client_context.h"
31#include "remoting/client/client_user_interface.h"
32#include "remoting/client/key_event_mapper.h"
33#include "remoting/client/plugin/mac_key_event_processor.h"
34#include "remoting/client/plugin/pepper_input_handler.h"
35#include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
36#include "remoting/proto/event.pb.h"
37#include "remoting/protocol/client_stub.h"
38#include "remoting/protocol/clipboard_stub.h"
39#include "remoting/protocol/connection_to_host.h"
40#include "remoting/protocol/cursor_shape_stub.h"
41#include "remoting/protocol/input_event_tracker.h"
42#include "remoting/protocol/mouse_input_filter.h"
43#include "remoting/protocol/negotiating_client_authenticator.h"
44#include "remoting/protocol/third_party_client_authenticator.h"
45
46namespace base {
47class DictionaryValue;
48}  // namespace base
49
50namespace pp {
51class InputEvent;
52class Module;
53}  // namespace pp
54
55namespace remoting {
56
57class ChromotingClient;
58class ChromotingStats;
59class ClientContext;
60class FrameConsumerProxy;
61class PepperAudioPlayer;
62class PepperTokenFetcher;
63class PepperView;
64class PepperXmppProxy;
65class RectangleUpdateDecoder;
66
67struct ClientConfig;
68
69class ChromotingInstance :
70      public ClientUserInterface,
71      public protocol::ClipboardStub,
72      public protocol::CursorShapeStub,
73      public pp::Instance,
74      public base::SupportsWeakPtr<ChromotingInstance> {
75 public:
76  // Plugin API version. This should be incremented whenever the API
77  // interface changes.
78  static const int kApiVersion = 7;
79
80  // Plugin API features. This allows orthogonal features to be supported
81  // without bumping the API version.
82  static const char kApiFeatures[];
83
84  // Capabilities supported by the plugin that should also be supported by the
85  // webapp to be enabled.
86  static const char kRequestedCapabilities[];
87
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[];
91
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;
96
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;
101
102  // Helper method to parse authentication_methods parameter.
103  static bool ParseAuthMethods(const std::string& auth_methods,
104                               ClientConfig* config);
105
106  explicit ChromotingInstance(PP_Instance instance);
107  virtual ~ChromotingInstance();
108
109  // pp::Instance interface.
110  virtual void DidChangeView(const pp::View& view) OVERRIDE;
111  virtual void DidChangeFocus(bool has_focus) OVERRIDE;
112  virtual bool Init(uint32_t argc, const char* argn[],
113                    const char* argv[]) OVERRIDE;
114  virtual void HandleMessage(const pp::Var& message) OVERRIDE;
115  virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
116
117  // ClientUserInterface interface.
118  virtual void OnConnectionState(protocol::ConnectionToHost::State state,
119                                 protocol::ErrorCode error) OVERRIDE;
120  virtual void OnConnectionReady(bool ready) OVERRIDE;
121  virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
122  virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
123  virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
124  virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
125  GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
126
127  // protocol::ClipboardStub interface.
128  virtual void InjectClipboardEvent(
129      const protocol::ClipboardEvent& event) OVERRIDE;
130
131  // protocol::CursorShapeStub interface.
132  virtual void SetCursorShape(
133      const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
134
135  // Called by PepperView.
136  void SetDesktopSize(const SkISize& size, const SkIPoint& dpi);
137  void OnFirstFrameReceived();
138
139  // Return statistics record by ChromotingClient.
140  // If no connection is currently active then NULL will be returned.
141  ChromotingStats* GetStats();
142
143  // Registers a global log message handler that redirects the log output to
144  // our plugin instance.
145  // This is called by the plugin's PPP_InitializeModule.
146  // Note that no logging will be processed unless a ChromotingInstance has been
147  // registered for logging (see RegisterLoggingInstance).
148  static void RegisterLogMessageHandler();
149
150  // Registers this instance so it processes messages sent by the global log
151  // message handler. This overwrites any previously registered instance.
152  void RegisterLoggingInstance();
153
154  // Unregisters this instance so that debug log messages will no longer be sent
155  // to it. If this instance is not the currently registered logging instance,
156  // then the currently registered instance will stay in effect.
157  void UnregisterLoggingInstance();
158
159  // A Log Message Handler that is called after each LOG message has been
160  // processed. This must be of type LogMessageHandlerFunction defined in
161  // base/logging.h.
162  static bool LogToUI(int severity, const char* file, int line,
163                      size_t message_start, const std::string& str);
164
165  // Requests the webapp to fetch a third-party token.
166  void FetchThirdPartyToken(
167      const GURL& token_url,
168      const std::string& host_public_key,
169      const std::string& scope,
170      const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher);
171
172 private:
173  FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
174
175  // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
176  // Immediately calls |secret_fetched_callback| with |shared_secret|.
177  static void FetchSecretFromString(
178      const std::string& shared_secret,
179      bool pairing_supported,
180      const protocol::SecretFetchedCallback& secret_fetched_callback);
181
182  // Message handlers for messages that come from JavaScript. Called
183  // from HandleMessage().
184  void Connect(const ClientConfig& config);
185  void Disconnect();
186  void OnIncomingIq(const std::string& iq);
187  void ReleaseAllKeys();
188  void InjectKeyEvent(const protocol::KeyEvent& event);
189  void RemapKey(uint32 in_usb_keycode, uint32 out_usb_keycode);
190  void TrapKey(uint32 usb_keycode, bool trap);
191  void SendClipboardItem(const std::string& mime_type, const std::string& item);
192  void NotifyClientResolution(int width, int height, int x_dpi, int y_dpi);
193  void PauseVideo(bool pause);
194  void PauseAudio(bool pause);
195  void OnPinFetched(const std::string& pin);
196  void OnThirdPartyTokenFetched(const std::string& token,
197                                const std::string& shared_secret);
198
199  // Helper method to post messages to the webapp.
200  void PostChromotingMessage(const std::string& method,
201                             scoped_ptr<base::DictionaryValue> data);
202
203  // Posts trapped keys to the web-app to handle.
204  void SendTrappedKey(uint32 usb_keycode, bool pressed);
205
206  // Callback for PepperXmppProxy.
207  void SendOutgoingIq(const std::string& iq);
208
209  void SendPerfStats();
210
211  void ProcessLogToUI(const std::string& message);
212
213  // Returns true if the hosting content has the chrome-extension:// scheme.
214  bool IsCallerAppOrExtension();
215
216  // Returns true if there is a ConnectionToHost and it is connected.
217  bool IsConnected();
218
219  // Used as the |FetchSecretCallback| for Me2Me connections.
220  // Uses the PIN request dialog in the webapp to obtain the shared secret.
221  void FetchSecretFromDialog(
222      bool pairing_supported,
223      const protocol::SecretFetchedCallback& secret_fetched_callback);
224
225  bool initialized_;
226
227  PepperPluginThreadDelegate plugin_thread_delegate_;
228  scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
229  ClientContext context_;
230  scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_;
231  scoped_ptr<PepperView> view_;
232  pp::View plugin_view_;
233
234  scoped_ptr<protocol::ConnectionToHost> host_connection_;
235  scoped_ptr<ChromotingClient> client_;
236
237  // Input pipeline components, in reverse order of distance from input source.
238  protocol::MouseInputFilter mouse_input_filter_;
239  protocol::InputEventTracker input_tracker_;
240#if defined(OS_MACOSX)
241  MacKeyEventProcessor mac_key_event_processor_;
242#endif
243  KeyEventMapper key_mapper_;
244  PepperInputHandler input_handler_;
245
246  // XmppProxy is a refcounted interface used to perform thread-switching and
247  // detaching between objects whose lifetimes are controlled by pepper, and
248  // jingle_glue objects. This is used when if we start a sandboxed jingle
249  // connection.
250  scoped_refptr<PepperXmppProxy> xmpp_proxy_;
251
252  // PIN Fetcher.
253  bool use_async_pin_dialog_;
254  protocol::SecretFetchedCallback secret_fetched_callback_;
255
256  base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_;
257
258  base::WeakPtrFactory<ChromotingInstance> weak_factory_;
259
260  DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
261};
262
263}  // namespace remoting
264
265#endif  // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
266