tuple: update to make use of C++11
[chromium-blink-merge.git] / remoting / client / plugin / chromoting_instance.h
blob25246c1e8742780343789c9f5c7bd09eb9e173d2
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_
8 #include <string>
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/media_source_video_renderer.h"
25 #include "remoting/client/plugin/pepper_cursor_setter.h"
26 #include "remoting/client/plugin/pepper_input_handler.h"
27 #include "remoting/client/plugin/pepper_plugin_thread_delegate.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"
38 namespace base {
39 class DictionaryValue;
40 } // namespace base
42 namespace pp {
43 class InputEvent;
44 class Module;
45 class VarDictionary;
46 } // namespace pp
48 namespace jingle_glue {
49 class JingleThreadWrapper;
50 } // namespace jingle_glue
52 namespace webrtc {
53 class DesktopRegion;
54 class DesktopSize;
55 class DesktopVector;
56 } // namespace webrtc
58 namespace remoting {
60 class ChromotingClient;
61 class ChromotingStats;
62 class ClientContext;
63 class DelegatingSignalStrategy;
64 class FrameConsumer;
65 class FrameConsumerProxy;
66 class PepperAudioPlayer;
67 class PepperMouseLocker;
68 class TokenFetcherProxy;
69 class PepperView;
70 class RectangleUpdateDecoder;
71 class SignalStrategy;
72 class VideoRenderer;
74 struct ClientConfig;
76 class ChromotingInstance :
77 public ClientUserInterface,
78 public MediaSourceVideoRenderer::Delegate,
79 public protocol::ClipboardStub,
80 public protocol::CursorShapeStub,
81 public pp::Instance {
82 public:
83 // Plugin API version. This should be incremented whenever the API
84 // interface changes.
85 static const int kApiVersion = 7;
87 // Plugin API features. This allows orthogonal features to be supported
88 // without bumping the API version.
89 static const char kApiFeatures[];
91 // Capabilities supported by the plugin that should also be supported by the
92 // webapp to be enabled.
93 static const char kRequestedCapabilities[];
95 // Capabilities supported by the plugin that do not need to be supported by
96 // the webapp to be enabled.
97 static const char kSupportedCapabilities[];
99 // Backward-compatibility version used by for the messaging
100 // interface. Should be updated whenever we remove support for
101 // an older version of the API.
102 static const int kApiMinMessagingVersion = 5;
104 // Backward-compatibility version used by for the ScriptableObject
105 // interface. Should be updated whenever we remove support for
106 // an older version of the API.
107 static const int kApiMinScriptableVersion = 5;
109 explicit ChromotingInstance(PP_Instance instance);
110 ~ChromotingInstance() override;
112 // pp::Instance interface.
113 void DidChangeFocus(bool has_focus) override;
114 void DidChangeView(const pp::View& view) override;
115 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override;
116 void HandleMessage(const pp::Var& message) override;
117 bool HandleInputEvent(const pp::InputEvent& event) override;
119 // ClientUserInterface interface.
120 void OnConnectionState(protocol::ConnectionToHost::State state,
121 protocol::ErrorCode error) override;
122 void OnConnectionReady(bool ready) override;
123 void OnRouteChanged(const std::string& channel_name,
124 const protocol::TransportRoute& route) override;
125 void SetCapabilities(const std::string& capabilities) override;
126 void SetPairingResponse(
127 const protocol::PairingResponse& pairing_response) override;
128 void DeliverHostMessage(const protocol::ExtensionMessage& message) override;
129 protocol::ClipboardStub* GetClipboardStub() override;
130 protocol::CursorShapeStub* GetCursorShapeStub() override;
132 // protocol::ClipboardStub interface.
133 void InjectClipboardEvent(const protocol::ClipboardEvent& event) override;
135 // protocol::CursorShapeStub interface.
136 void SetCursorShape(const protocol::CursorShapeInfo& cursor_shape) override;
138 // Called by PepperView.
139 void SetDesktopSize(const webrtc::DesktopSize& size,
140 const webrtc::DesktopVector& dpi);
141 void SetDesktopShape(const webrtc::DesktopRegion& shape);
142 void OnFirstFrameReceived();
144 // Return statistics record by ChromotingClient.
145 // If no connection is currently active then NULL will be returned.
146 ChromotingStats* GetStats();
148 // Registers a global log message handler that redirects the log output to
149 // our plugin instance.
150 // This is called by the plugin's PPP_InitializeModule.
151 // Note that no logging will be processed unless a ChromotingInstance has been
152 // registered for logging (see RegisterLoggingInstance).
153 static void RegisterLogMessageHandler();
155 // Registers this instance so it processes messages sent by the global log
156 // message handler. This overwrites any previously registered instance.
157 void RegisterLoggingInstance();
159 // Unregisters this instance so that debug log messages will no longer be sent
160 // to it. If this instance is not the currently registered logging instance,
161 // then the currently registered instance will stay in effect.
162 void UnregisterLoggingInstance();
164 // A Log Message Handler that is called after each LOG message has been
165 // processed. This must be of type LogMessageHandlerFunction defined in
166 // base/logging.h.
167 static bool LogToUI(int severity, const char* file, int line,
168 size_t message_start, const std::string& str);
170 // Requests the webapp to fetch a third-party token.
171 void FetchThirdPartyToken(
172 const GURL& token_url,
173 const std::string& host_public_key,
174 const std::string& scope,
175 const base::WeakPtr<TokenFetcherProxy> pepper_token_fetcher);
177 private:
178 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
180 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
181 // Immediately calls |secret_fetched_callback| with |shared_secret|.
182 static void FetchSecretFromString(
183 const std::string& shared_secret,
184 bool pairing_supported,
185 const protocol::SecretFetchedCallback& secret_fetched_callback);
187 // Message handlers for messages that come from JavaScript. Called
188 // from HandleMessage().
189 void HandleConnect(const base::DictionaryValue& data);
190 void HandleDisconnect(const base::DictionaryValue& data);
191 void HandleOnIncomingIq(const base::DictionaryValue& data);
192 void HandleReleaseAllKeys(const base::DictionaryValue& data);
193 void HandleInjectKeyEvent(const base::DictionaryValue& data);
194 void HandleRemapKey(const base::DictionaryValue& data);
195 void HandleTrapKey(const base::DictionaryValue& data);
196 void HandleSendClipboardItem(const base::DictionaryValue& data);
197 void HandleNotifyClientResolution(const base::DictionaryValue& data);
198 void HandlePauseVideo(const base::DictionaryValue& data);
199 void HandleVideoControl(const base::DictionaryValue& data);
200 void HandlePauseAudio(const base::DictionaryValue& data);
201 void HandleOnPinFetched(const base::DictionaryValue& data);
202 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data);
203 void HandleRequestPairing(const base::DictionaryValue& data);
204 void HandleExtensionMessage(const base::DictionaryValue& data);
205 void HandleAllowMouseLockMessage();
206 void HandleEnableMediaSourceRendering();
207 void HandleSendMouseInputWhenUnfocused();
208 void HandleDelegateLargeCursors();
210 // Helper method to post messages to the webapp.
211 void PostChromotingMessage(const std::string& method,
212 const pp::VarDictionary& data);
214 // Same as above, but serializes messages to JSON before sending them. This
215 // method is used for backward compatibility with older version of the webapp
216 // that expect to received most messages formatted using JSON.
218 // TODO(sergeyu): When all current versions of the webapp support raw messages
219 // remove this method and use PostChromotingMessage() instead.
220 void PostLegacyJsonMessage(const std::string& method,
221 scoped_ptr<base::DictionaryValue> data);
223 // Posts trapped keys to the web-app to handle.
224 void SendTrappedKey(uint32 usb_keycode, bool pressed);
226 // Callback for DelegatingSignalStrategy.
227 void SendOutgoingIq(const std::string& iq);
229 void SendPerfStats();
231 void ProcessLogToUI(const std::string& message);
233 // Returns true if the hosting content has the chrome-extension:// scheme.
234 bool IsCallerAppOrExtension();
236 // Returns true if there is a ConnectionToHost and it is connected.
237 bool IsConnected();
239 // Used as the |FetchSecretCallback| for Me2Me connections.
240 // Uses the PIN request dialog in the webapp to obtain the shared secret.
241 void FetchSecretFromDialog(
242 bool pairing_supported,
243 const protocol::SecretFetchedCallback& secret_fetched_callback);
245 // MediaSourceVideoRenderer::Delegate implementation.
246 void OnMediaSourceSize(const webrtc::DesktopSize& size,
247 const webrtc::DesktopVector& dpi) override;
248 void OnMediaSourceShape(const webrtc::DesktopRegion& shape) override;
249 void OnMediaSourceReset(const std::string& format) override;
250 void OnMediaSourceData(uint8_t* buffer,
251 size_t buffer_size,
252 bool keyframe) override;
254 bool initialized_;
256 PepperPluginThreadDelegate plugin_thread_delegate_;
257 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
258 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
259 scoped_ptr<jingle_glue::JingleThreadWrapper> thread_wrapper_;
260 ClientContext context_;
261 scoped_ptr<VideoRenderer> video_renderer_;
262 scoped_ptr<PepperView> view_;
263 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
264 pp::View plugin_view_;
266 // Contains the most-recently-reported desktop shape, if any.
267 scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
269 scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
271 scoped_ptr<ChromotingClient> client_;
273 // Input pipeline components, in reverse order of distance from input source.
274 protocol::MouseInputFilter mouse_input_filter_;
275 protocol::InputEventTracker input_tracker_;
276 KeyEventMapper key_mapper_;
277 scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
278 PepperInputHandler input_handler_;
280 // Cursor shape handling components, in reverse order to that in which they
281 // process cursor shape events. Note that |mouse_locker_| appears in the
282 // cursor pipeline since it is triggered by receipt of an empty cursor.
283 PepperCursorSetter cursor_setter_;
284 scoped_ptr<PepperMouseLocker> mouse_locker_;
285 EmptyCursorFilter empty_cursor_filter_;
287 // Used to control text input settings, such as whether to show the IME.
288 pp::TextInputController text_input_controller_;
290 // PIN Fetcher.
291 bool use_async_pin_dialog_;
292 protocol::SecretFetchedCallback secret_fetched_callback_;
294 // Set to true if the webapp has requested to use MediaSource API for
295 // rendering. In that case all the encoded video will be passed to the
296 // webapp for decoding.
297 bool use_media_source_rendering_;
299 base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy_;
301 // Weak reference to this instance, used for global logging and task posting.
302 base::WeakPtrFactory<ChromotingInstance> weak_factory_;
304 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
307 } // namespace remoting
309 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_