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 // TODO(ajwong): We need to come up with a better description of the
6 // responsibilities for each thread.
8 #ifndef REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
9 #define REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_
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/instance.h"
20 #include "ppapi/cpp/var.h"
21 #include "remoting/client/client_context.h"
22 #include "remoting/client/client_user_interface.h"
23 #include "remoting/client/key_event_mapper.h"
24 #include "remoting/client/plugin/normalizing_input_filter.h"
25 #include "remoting/client/plugin/pepper_input_handler.h"
26 #include "remoting/client/plugin/pepper_plugin_thread_delegate.h"
27 #include "remoting/proto/event.pb.h"
28 #include "remoting/protocol/client_stub.h"
29 #include "remoting/protocol/clipboard_stub.h"
30 #include "remoting/protocol/connection_to_host.h"
31 #include "remoting/protocol/cursor_shape_stub.h"
32 #include "remoting/protocol/input_event_tracker.h"
33 #include "remoting/protocol/mouse_input_filter.h"
34 #include "remoting/protocol/negotiating_client_authenticator.h"
35 #include "remoting/protocol/third_party_client_authenticator.h"
38 class DictionaryValue;
54 class ChromotingClient;
55 class ChromotingStats;
57 class DelegatingSignalStrategy;
59 class FrameConsumerProxy;
60 class PepperAudioPlayer;
61 class PepperTokenFetcher;
63 class RectangleUpdateDecoder;
69 class ChromotingInstance :
70 public ClientUserInterface,
71 public protocol::ClipboardStub,
72 public protocol::CursorShapeStub,
75 // Plugin API version. This should be incremented whenever the API
77 static const int kApiVersion = 7;
79 // Plugin API features. This allows orthogonal features to be supported
80 // without bumping the API version.
81 static const char kApiFeatures[];
83 // Capabilities supported by the plugin that should also be supported by the
84 // webapp to be enabled.
85 static const char kRequestedCapabilities[];
87 // Capabilities supported by the plugin that do not need to be supported by
88 // the webapp to be enabled.
89 static const char kSupportedCapabilities[];
91 // Backward-compatibility version used by for the messaging
92 // interface. Should be updated whenever we remove support for
93 // an older version of the API.
94 static const int kApiMinMessagingVersion = 5;
96 // Backward-compatibility version used by for the ScriptableObject
97 // interface. Should be updated whenever we remove support for
98 // an older version of the API.
99 static const int kApiMinScriptableVersion = 5;
101 // Helper method to parse authentication_methods parameter.
102 static bool ParseAuthMethods(const std::string& auth_methods,
103 ClientConfig* config);
105 explicit ChromotingInstance(PP_Instance instance);
106 virtual ~ChromotingInstance();
108 // pp::Instance interface.
109 virtual void DidChangeFocus(bool has_focus) OVERRIDE;
110 virtual void DidChangeView(const pp::View& view) OVERRIDE;
111 virtual bool Init(uint32_t argc, const char* argn[],
112 const char* argv[]) OVERRIDE;
113 virtual void HandleMessage(const pp::Var& message) OVERRIDE;
114 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
116 // ClientUserInterface interface.
117 virtual void OnConnectionState(protocol::ConnectionToHost::State state,
118 protocol::ErrorCode error) OVERRIDE;
119 virtual void OnConnectionReady(bool ready) OVERRIDE;
120 virtual void OnRouteChanged(const std::string& channel_name,
121 const protocol::TransportRoute& route) OVERRIDE;
122 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
123 virtual void SetPairingResponse(
124 const protocol::PairingResponse& pairing_response) OVERRIDE;
125 virtual void DeliverHostMessage(
126 const protocol::ExtensionMessage& message) OVERRIDE;
127 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
128 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
129 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
130 GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
132 // protocol::ClipboardStub interface.
133 virtual void InjectClipboardEvent(
134 const protocol::ClipboardEvent& event) OVERRIDE;
136 // protocol::CursorShapeStub interface.
137 virtual void SetCursorShape(
138 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
140 // Called by PepperView.
141 void SetDesktopSize(const webrtc::DesktopSize& size,
142 const webrtc::DesktopVector& dpi);
143 void SetDesktopShape(const webrtc::DesktopRegion& shape);
144 void OnFirstFrameReceived();
146 // Return statistics record by ChromotingClient.
147 // If no connection is currently active then NULL will be returned.
148 ChromotingStats* GetStats();
150 // Registers a global log message handler that redirects the log output to
151 // our plugin instance.
152 // This is called by the plugin's PPP_InitializeModule.
153 // Note that no logging will be processed unless a ChromotingInstance has been
154 // registered for logging (see RegisterLoggingInstance).
155 static void RegisterLogMessageHandler();
157 // Registers this instance so it processes messages sent by the global log
158 // message handler. This overwrites any previously registered instance.
159 void RegisterLoggingInstance();
161 // Unregisters this instance so that debug log messages will no longer be sent
162 // to it. If this instance is not the currently registered logging instance,
163 // then the currently registered instance will stay in effect.
164 void UnregisterLoggingInstance();
166 // A Log Message Handler that is called after each LOG message has been
167 // processed. This must be of type LogMessageHandlerFunction defined in
169 static bool LogToUI(int severity, const char* file, int line,
170 size_t message_start, const std::string& str);
172 // Requests the webapp to fetch a third-party token.
173 void FetchThirdPartyToken(
174 const GURL& token_url,
175 const std::string& host_public_key,
176 const std::string& scope,
177 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher);
180 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
182 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
183 // Immediately calls |secret_fetched_callback| with |shared_secret|.
184 static void FetchSecretFromString(
185 const std::string& shared_secret,
186 bool pairing_supported,
187 const protocol::SecretFetchedCallback& secret_fetched_callback);
189 // Message handlers for messages that come from JavaScript. Called
190 // from HandleMessage().
191 void HandleConnect(const base::DictionaryValue& data);
192 void HandleDisconnect(const base::DictionaryValue& data);
193 void HandleOnIncomingIq(const base::DictionaryValue& data);
194 void HandleReleaseAllKeys(const base::DictionaryValue& data);
195 void HandleInjectKeyEvent(const base::DictionaryValue& data);
196 void HandleRemapKey(const base::DictionaryValue& data);
197 void HandleTrapKey(const base::DictionaryValue& data);
198 void HandleSendClipboardItem(const base::DictionaryValue& data);
199 void HandleNotifyClientResolution(const base::DictionaryValue& data);
200 void HandlePauseVideo(const base::DictionaryValue& data);
201 void HandlePauseAudio(const base::DictionaryValue& data);
202 void HandleOnPinFetched(const base::DictionaryValue& data);
203 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data);
204 void HandleRequestPairing(const base::DictionaryValue& data);
205 void HandleExtensionMessage(const base::DictionaryValue& data);
206 void HandleAllowMouseLockMessage();
208 // Helper method called from Connect() to connect with parsed config.
209 void ConnectWithConfig(const ClientConfig& config,
210 const std::string& local_jid);
212 // Helper method to post messages to the webapp.
213 void PostChromotingMessage(const std::string& method,
214 scoped_ptr<base::DictionaryValue> data);
216 // Posts trapped keys to the web-app to handle.
217 void SendTrappedKey(uint32 usb_keycode, bool pressed);
219 // Callback for DelegatingSignalStrategy.
220 void SendOutgoingIq(const std::string& iq);
222 void SendPerfStats();
224 void ProcessLogToUI(const std::string& message);
226 // Returns true if the hosting content has the chrome-extension:// scheme.
227 bool IsCallerAppOrExtension();
229 // Returns true if there is a ConnectionToHost and it is connected.
232 // Used as the |FetchSecretCallback| for Me2Me connections.
233 // Uses the PIN request dialog in the webapp to obtain the shared secret.
234 void FetchSecretFromDialog(
235 bool pairing_supported,
236 const protocol::SecretFetchedCallback& secret_fetched_callback);
240 PepperPluginThreadDelegate plugin_thread_delegate_;
241 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
242 ClientContext context_;
243 scoped_ptr<VideoRenderer> video_renderer_;
244 scoped_ptr<PepperView> view_;
245 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
246 pp::View plugin_view_;
248 // Contains the most-recently-reported desktop shape, if any.
249 scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
251 scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
253 scoped_ptr<protocol::ConnectionToHost> host_connection_;
254 scoped_ptr<ChromotingClient> client_;
256 // Input pipeline components, in reverse order of distance from input source.
257 protocol::MouseInputFilter mouse_input_filter_;
258 protocol::InputEventTracker input_tracker_;
259 KeyEventMapper key_mapper_;
260 scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
261 PepperInputHandler input_handler_;
264 bool use_async_pin_dialog_;
265 protocol::SecretFetchedCallback secret_fetched_callback_;
267 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_;
269 // Weak reference to this instance, used for global logging and task posting.
270 base::WeakPtrFactory<ChromotingInstance> weak_factory_;
272 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
275 } // namespace remoting
277 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_