Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / client / plugin / chromoting_instance.h
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/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"
35
36 namespace base {
37 class DictionaryValue;
38 }  // namespace base
39
40 namespace pp {
41 class InputEvent;
42 class Module;
43 class VarDictionary;
44 }  // namespace pp
45
46 namespace jingle_glue {
47 class JingleThreadWrapper;
48 }  // namespace jingle_glue
49
50 namespace webrtc {
51 class DesktopRegion;
52 class DesktopSize;
53 class DesktopVector;
54 }  // namespace webrtc
55
56 namespace remoting {
57
58 class ChromotingClient;
59 class ChromotingStats;
60 class ClientContext;
61 class DelegatingSignalStrategy;
62 class FrameConsumer;
63 class FrameConsumerProxy;
64 class PepperAudioPlayer;
65 class TokenFetcherProxy;
66 class PepperView;
67 class RectangleUpdateDecoder;
68 class SignalStrategy;
69 class VideoRenderer;
70
71 struct ClientConfig;
72
73 class ChromotingInstance :
74       public ClientUserInterface,
75       public MediaSourceVideoRenderer::Delegate,
76       public protocol::ClipboardStub,
77       public protocol::CursorShapeStub,
78       public pp::Instance {
79  public:
80   // Plugin API version. This should be incremented whenever the API
81   // interface changes.
82   static const int kApiVersion = 7;
83
84   // Plugin API features. This allows orthogonal features to be supported
85   // without bumping the API version.
86   static const char kApiFeatures[];
87
88   // Capabilities supported by the plugin that should also be supported by the
89   // webapp to be enabled.
90   static const char kRequestedCapabilities[];
91
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[];
95
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;
100
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;
105
106   explicit ChromotingInstance(PP_Instance instance);
107   ~ChromotingInstance() override;
108
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;
115
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;
128
129   // protocol::ClipboardStub interface.
130   void InjectClipboardEvent(const protocol::ClipboardEvent& event) override;
131
132   // protocol::CursorShapeStub interface.
133   void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override;
134
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();
140
141   // Return statistics record by ChromotingClient.
142   // If no connection is currently active then NULL will be returned.
143   ChromotingStats* GetStats();
144
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();
151
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();
155
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();
160
161   // A Log Message Handler that is called after each LOG message has been
162   // processed. This must be of type LogMessageHandlerFunction defined in
163   // base/logging.h.
164   static bool LogToUI(int severity, const char* file, int line,
165                       size_t message_start, const std::string& str);
166
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);
173
174  private:
175   FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
176
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);
183
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();
206
207   // Helper method to post messages to the webapp.
208   void PostChromotingMessage(const std::string& method,
209                              const pp::VarDictionary& data);
210
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.
214   //
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);
219
220   // Posts trapped keys to the web-app to handle.
221   void SendTrappedKey(uint32 usb_keycode, bool pressed);
222
223   // Callback for DelegatingSignalStrategy.
224   void SendOutgoingIq(const std::string& iq);
225
226   void SendPerfStats();
227
228   void ProcessLogToUI(const std::string& message);
229
230   // Returns true if the hosting content has the chrome-extension:// scheme.
231   bool IsCallerAppOrExtension();
232
233   // Returns true if there is a ConnectionToHost and it is connected.
234   bool IsConnected();
235
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);
241
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,
248                          size_t buffer_size,
249                          bool keyframe) override;
250
251   bool initialized_;
252
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_;
262
263   // Contains the most-recently-reported desktop shape, if any.
264   scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
265
266   scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
267
268   scoped_ptr<ChromotingClient> client_;
269
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_;
276
277   // Used to control text input settings, such as whether to show the IME.
278   pp::TextInputController text_input_controller_;
279
280   // PIN Fetcher.
281   bool use_async_pin_dialog_;
282   protocol::SecretFetchedCallback secret_fetched_callback_;
283
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_;
288
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_;
292
293   base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy_;
294
295   // Weak reference to this instance, used for global logging and task posting.
296   base::WeakPtrFactory<ChromotingInstance> weak_factory_;
297
298   DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
299 };
300
301 }  // namespace remoting
302
303 #endif  // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_