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;
68 class ChromotingInstance :
69 public ClientUserInterface,
70 public protocol::ClipboardStub,
71 public protocol::CursorShapeStub,
74 // Plugin API version. This should be incremented whenever the API
76 static const int kApiVersion = 7;
78 // Plugin API features. This allows orthogonal features to be supported
79 // without bumping the API version.
80 static const char kApiFeatures[];
82 // Capabilities supported by the plugin that should also be supported by the
83 // webapp to be enabled.
84 static const char kRequestedCapabilities[];
86 // Capabilities supported by the plugin that do not need to be supported by
87 // the webapp to be enabled.
88 static const char kSupportedCapabilities[];
90 // Backward-compatibility version used by for the messaging
91 // interface. Should be updated whenever we remove support for
92 // an older version of the API.
93 static const int kApiMinMessagingVersion = 5;
95 // Backward-compatibility version used by for the ScriptableObject
96 // interface. Should be updated whenever we remove support for
97 // an older version of the API.
98 static const int kApiMinScriptableVersion = 5;
100 // Helper method to parse authentication_methods parameter.
101 static bool ParseAuthMethods(const std::string& auth_methods,
102 ClientConfig* config);
104 explicit ChromotingInstance(PP_Instance instance);
105 virtual ~ChromotingInstance();
107 // pp::Instance interface.
108 virtual void DidChangeFocus(bool has_focus) OVERRIDE;
109 virtual void DidChangeView(const pp::View& view) OVERRIDE;
110 virtual bool Init(uint32_t argc, const char* argn[],
111 const char* argv[]) OVERRIDE;
112 virtual void HandleMessage(const pp::Var& message) OVERRIDE;
113 virtual bool HandleInputEvent(const pp::InputEvent& event) OVERRIDE;
115 // ClientUserInterface interface.
116 virtual void OnConnectionState(protocol::ConnectionToHost::State state,
117 protocol::ErrorCode error) OVERRIDE;
118 virtual void OnConnectionReady(bool ready) OVERRIDE;
119 virtual void SetCapabilities(const std::string& capabilities) OVERRIDE;
120 virtual void SetPairingResponse(
121 const protocol::PairingResponse& pairing_response) OVERRIDE;
122 virtual void DeliverHostMessage(
123 const protocol::ExtensionMessage& message) OVERRIDE;
124 virtual protocol::ClipboardStub* GetClipboardStub() OVERRIDE;
125 virtual protocol::CursorShapeStub* GetCursorShapeStub() OVERRIDE;
126 virtual scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
127 GetTokenFetcher(const std::string& host_public_key) OVERRIDE;
129 // protocol::ClipboardStub interface.
130 virtual void InjectClipboardEvent(
131 const protocol::ClipboardEvent& event) OVERRIDE;
133 // protocol::CursorShapeStub interface.
134 virtual void SetCursorShape(
135 const protocol::CursorShapeInfo& cursor_shape) OVERRIDE;
137 // Called by PepperView.
138 void SetDesktopSize(const webrtc::DesktopSize& size,
139 const webrtc::DesktopVector& dpi);
140 void SetDesktopShape(const webrtc::DesktopRegion& shape);
141 void OnFirstFrameReceived();
143 // Return statistics record by ChromotingClient.
144 // If no connection is currently active then NULL will be returned.
145 ChromotingStats* GetStats();
147 // Registers a global log message handler that redirects the log output to
148 // our plugin instance.
149 // This is called by the plugin's PPP_InitializeModule.
150 // Note that no logging will be processed unless a ChromotingInstance has been
151 // registered for logging (see RegisterLoggingInstance).
152 static void RegisterLogMessageHandler();
154 // Registers this instance so it processes messages sent by the global log
155 // message handler. This overwrites any previously registered instance.
156 void RegisterLoggingInstance();
158 // Unregisters this instance so that debug log messages will no longer be sent
159 // to it. If this instance is not the currently registered logging instance,
160 // then the currently registered instance will stay in effect.
161 void UnregisterLoggingInstance();
163 // A Log Message Handler that is called after each LOG message has been
164 // processed. This must be of type LogMessageHandlerFunction defined in
166 static bool LogToUI(int severity, const char* file, int line,
167 size_t message_start, const std::string& str);
169 // Requests the webapp to fetch a third-party token.
170 void FetchThirdPartyToken(
171 const GURL& token_url,
172 const std::string& host_public_key,
173 const std::string& scope,
174 const base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher);
177 FRIEND_TEST_ALL_PREFIXES(ChromotingInstanceTest, TestCaseSetup);
179 // Used as the |FetchSecretCallback| for IT2Me (or Me2Me from old webapps).
180 // Immediately calls |secret_fetched_callback| with |shared_secret|.
181 static void FetchSecretFromString(
182 const std::string& shared_secret,
183 bool pairing_supported,
184 const protocol::SecretFetchedCallback& secret_fetched_callback);
186 // Message handlers for messages that come from JavaScript. Called
187 // from HandleMessage().
188 void HandleConnect(const base::DictionaryValue& data);
189 void HandleDisconnect(const base::DictionaryValue& data);
190 void HandleOnIncomingIq(const base::DictionaryValue& data);
191 void HandleReleaseAllKeys(const base::DictionaryValue& data);
192 void HandleInjectKeyEvent(const base::DictionaryValue& data);
193 void HandleRemapKey(const base::DictionaryValue& data);
194 void HandleTrapKey(const base::DictionaryValue& data);
195 void HandleSendClipboardItem(const base::DictionaryValue& data);
196 void HandleNotifyClientResolution(const base::DictionaryValue& data);
197 void HandlePauseVideo(const base::DictionaryValue& data);
198 void HandlePauseAudio(const base::DictionaryValue& data);
199 void HandleOnPinFetched(const base::DictionaryValue& data);
200 void HandleOnThirdPartyTokenFetched(const base::DictionaryValue& data);
201 void HandleRequestPairing(const base::DictionaryValue& data);
202 void HandleExtensionMessage(const base::DictionaryValue& data);
203 void HandleAllowMouseLockMessage();
205 // Helper method called from Connect() to connect with parsed config.
206 void ConnectWithConfig(const ClientConfig& config,
207 const std::string& local_jid);
209 // Helper method to post messages to the webapp.
210 void PostChromotingMessage(const std::string& method,
211 scoped_ptr<base::DictionaryValue> data);
213 // Posts trapped keys to the web-app to handle.
214 void SendTrappedKey(uint32 usb_keycode, bool pressed);
216 // Callback for DelegatingSignalStrategy.
217 void SendOutgoingIq(const std::string& iq);
219 void SendPerfStats();
221 void ProcessLogToUI(const std::string& message);
223 // Returns true if the hosting content has the chrome-extension:// scheme.
224 bool IsCallerAppOrExtension();
226 // Returns true if there is a ConnectionToHost and it is connected.
229 // Used as the |FetchSecretCallback| for Me2Me connections.
230 // Uses the PIN request dialog in the webapp to obtain the shared secret.
231 void FetchSecretFromDialog(
232 bool pairing_supported,
233 const protocol::SecretFetchedCallback& secret_fetched_callback);
237 PepperPluginThreadDelegate plugin_thread_delegate_;
238 scoped_refptr<PluginThreadTaskRunner> plugin_task_runner_;
239 ClientContext context_;
240 scoped_refptr<RectangleUpdateDecoder> rectangle_decoder_;
241 scoped_ptr<PepperView> view_;
242 scoped_ptr<base::WeakPtrFactory<FrameConsumer> > view_weak_factory_;
243 pp::View plugin_view_;
245 // Contains the most-recently-reported desktop shape, if any.
246 scoped_ptr<webrtc::DesktopRegion> desktop_shape_;
248 scoped_ptr<DelegatingSignalStrategy> signal_strategy_;
250 scoped_ptr<protocol::ConnectionToHost> host_connection_;
251 scoped_ptr<ChromotingClient> client_;
253 // Input pipeline components, in reverse order of distance from input source.
254 protocol::MouseInputFilter mouse_input_filter_;
255 protocol::InputEventTracker input_tracker_;
256 KeyEventMapper key_mapper_;
257 scoped_ptr<protocol::InputFilter> normalizing_input_filter_;
258 PepperInputHandler input_handler_;
261 bool use_async_pin_dialog_;
262 protocol::SecretFetchedCallback secret_fetched_callback_;
264 base::WeakPtr<PepperTokenFetcher> pepper_token_fetcher_;
266 // Weak reference to this instance, used for global logging and task posting.
267 base::WeakPtrFactory<ChromotingInstance> weak_factory_;
269 DISALLOW_COPY_AND_ASSIGN(ChromotingInstance);
272 } // namespace remoting
274 #endif // REMOTING_CLIENT_PLUGIN_CHROMOTING_INSTANCE_H_