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_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_
6 #define REMOTING_HOST_PLUGIN_HOST_SCRIPT_OBJECT_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 "base/time/time.h"
15 #include "remoting/base/auto_thread_task_runner.h"
16 #include "remoting/host/it2me/it2me_host.h"
17 #include "remoting/host/plugin/host_plugin_utils.h"
18 #include "remoting/host/setup/daemon_controller.h"
19 #include "remoting/jingle_glue/xmpp_signal_strategy.h"
20 #include "remoting/protocol/pairing_registry.h"
24 // NPAPI plugin implementation for remoting host script object.
25 // HostNPScriptObject creates threads that are required to run
26 // ChromotingHost and starts/stops the host on those threads. When
27 // destroyed it synchronously shuts down the host and all threads.
28 class HostNPScriptObject : public It2MeHost::Observer {
30 HostNPScriptObject(NPP plugin,
32 scoped_refptr<AutoThreadTaskRunner> plugin_task_runner);
33 virtual ~HostNPScriptObject();
35 // Implementations used to implement the NPObject interface.
36 bool HasMethod(const std::string& method_name);
37 bool InvokeDefault(const NPVariant* args,
40 bool Invoke(const std::string& method_name,
41 const NPVariant* args,
44 bool HasProperty(const std::string& property_name);
45 bool GetProperty(const std::string& property_name, NPVariant* result);
46 bool SetProperty(const std::string& property_name, const NPVariant* value);
47 bool RemoveProperty(const std::string& property_name);
48 bool Enumerate(std::vector<std::string>* values);
50 // Post LogDebugInfo to the correct proxy (and thus, on the correct thread).
51 // This should only be called by HostLogHandler. To log to the UI, use the
52 // standard HOST_LOG and it will be sent to this method.
53 void PostLogDebugInfo(const std::string& message);
55 void SetWindow(NPWindow* np_window);
58 //////////////////////////////////////////////////////////
59 // Plugin methods for It2Me host.
61 // Start connection. args are:
62 // string uid, string auth_token
64 bool Connect(const NPVariant* args, uint32_t arg_count, NPVariant* result);
66 // Disconnect. No arguments or result.
67 bool Disconnect(const NPVariant* args, uint32_t arg_count, NPVariant* result);
69 // Localize strings. args are:
70 // localize_func - a callback function which returns a localized string for
73 bool Localize(const NPVariant* args, uint32_t arg_count, NPVariant* result);
75 //////////////////////////////////////////////////////////
76 // Plugin methods for Me2Me host.
78 // Deletes all paired clients from the registry.
79 bool ClearPairedClients(const NPVariant* args,
83 // Deletes a paired client referenced by client id.
84 bool DeletePairedClient(const NPVariant* args,
88 // Fetches the host name, passing it to the supplied callback. Args are:
89 // function(string) callback
90 // Returns false if the parameters are invalid.
91 bool GetHostName(const NPVariant* args,
95 // Calculates PIN hash value to be stored in the config. Args are:
96 // string hostId Host ID.
97 // string pin The PIN.
98 // function(string) callback
99 // Passes the resulting hash value base64-encoded to the callback.
100 // Returns false if the parameters are invalid.
101 bool GetPinHash(const NPVariant* args,
105 // Generates new key pair to use for the host. The specified
106 // callback is called when when the key is generated. The key is
107 // returned in format understood by the host (PublicKeyInfo
108 // structure encoded with ASN.1 DER, and then BASE64). Args are:
109 // function(string) callback The callback to be called when done.
110 bool GenerateKeyPair(const NPVariant* args,
114 // Update host config for Me2Me. Args are:
116 // function(number) done_callback
117 bool UpdateDaemonConfig(const NPVariant* args,
121 // Loads daemon config. The first argument specifies the callback to be
122 // called once the config has been loaded. The config is passed as a JSON
123 // formatted string. Args are:
124 // function(string) callback
125 bool GetDaemonConfig(const NPVariant* args,
129 // Retrieves daemon version. The first argument specifies the callback to be
130 // called with the obtained version. The version is passed as a dotted
131 // version string, described in daemon_controller.h.
132 bool GetDaemonVersion(const NPVariant* args,
136 // Retrieves the list of paired clients as a JSON-encoded string.
137 bool GetPairedClients(const NPVariant* args,
141 // Retrieves the user's consent to report crash dumps. The first argument
142 // specifies the callback to be called with the recorder consent. Possible
143 // consent codes are defined in remoting/host/breakpad.h.
144 bool GetUsageStatsConsent(const NPVariant* args,
148 // Download and install the host component.
149 // function(number) done_callback
150 bool InstallHost(const NPVariant* args,
154 // Start the daemon process with the specified config. Args are:
156 // function(number) done_callback
157 bool StartDaemon(const NPVariant* args,
161 // Stop the daemon process. Args are:
162 // function(number) done_callback
163 bool StopDaemon(const NPVariant* args, uint32_t arg_count, NPVariant* result);
165 //////////////////////////////////////////////////////////
166 // Implementation of It2MeHost::Observer methods.
168 // Notifies OnStateChanged handler of a state change.
169 virtual void OnStateChanged(It2MeHostState state) OVERRIDE;
171 // If the web-app has registered a callback to be notified of changes to the
172 // NAT traversal policy, notify it.
173 virtual void OnNatPolicyChanged(bool nat_traversal_enabled) OVERRIDE;
175 // Stores the Access Code for the web-app to query.
176 virtual void OnStoreAccessCode(const std::string& access_code,
177 base::TimeDelta access_code_lifetime) OVERRIDE;
179 // Stores the client user's name for the web-app to query.
180 virtual void OnClientAuthenticated(
181 const std::string& client_username) OVERRIDE;
183 // Used to generate localized strings to pass to the It2Me host core.
184 void LocalizeStrings(NPObject* localize_func);
186 // Helper function for executing InvokeDefault on an NPObject that performs
187 // a string->string mapping without substitution. Stores the translation in
188 // |result| and returns true on success, or leaves it unchanged and returns
190 bool LocalizeString(NPObject* localize_func, const char* tag,
191 base::string16* result);
193 // Helper function for executing InvokeDefault on an NPObject that performs
194 // a string->string mapping with one substitution. Stores the translation in
195 // |result| and returns true on success, or leaves it unchanged and returns
197 bool LocalizeStringWithSubstitution(NPObject* localize_func,
199 const char* substitution,
200 base::string16* result);
202 //////////////////////////////////////////////////////////
203 // Helper methods for Me2Me host.
205 // Helpers for GenerateKeyPair().
206 static void DoGenerateKeyPair(
207 const scoped_refptr<AutoThreadTaskRunner>& plugin_task_runner,
208 const base::Callback<void (const std::string&,
209 const std::string&)>& callback);
210 void InvokeGenerateKeyPairCallback(scoped_ptr<ScopedRefNPObject> callback,
211 const std::string& private_key,
212 const std::string& public_key);
214 // Callback handler for SetConfigAndStart(), Stop(), SetPin() and
215 // SetUsageStatsConsent() in DaemonController.
216 void InvokeAsyncResultCallback(scoped_ptr<ScopedRefNPObject> callback,
217 DaemonController::AsyncResult result);
219 // Callback handler for PairingRegistry methods that return a boolean
221 void InvokeBooleanCallback(scoped_ptr<ScopedRefNPObject> callback,
224 // Callback handler for DaemonController::GetConfig().
225 void InvokeGetDaemonConfigCallback(scoped_ptr<ScopedRefNPObject> callback,
226 scoped_ptr<base::DictionaryValue> config);
228 // Callback handler for DaemonController::GetVersion().
229 void InvokeGetDaemonVersionCallback(scoped_ptr<ScopedRefNPObject> callback,
230 const std::string& version);
232 // Callback handler for GetPairedClients().
233 void InvokeGetPairedClientsCallback(
234 scoped_ptr<ScopedRefNPObject> callback,
235 scoped_ptr<base::ListValue> paired_clients);
237 // Callback handler for DaemonController::GetUsageStatsConsent().
238 void InvokeGetUsageStatsConsentCallback(
239 scoped_ptr<ScopedRefNPObject> callback,
240 const DaemonController::UsageStatsConsent& consent);
242 //////////////////////////////////////////////////////////
243 // Basic helper methods used for both It2Me and Me2me.
245 // Call LogDebugInfo handler if there is one.
246 // This must be called on the correct thread.
247 void LogDebugInfo(const std::string& message);
249 // Helper function for executing InvokeDefault on an NPObject, and ignoring
251 bool InvokeAndIgnoreResult(const ScopedRefNPObject& func,
252 const NPVariant* args,
255 // Set an exception for the current call.
256 void SetException(const std::string& exception_string);
258 //////////////////////////////////////////////////////////
259 // Plugin state variables shared between It2Me and Me2Me.
263 scoped_refptr<AutoThreadTaskRunner> plugin_task_runner_;
264 scoped_ptr<base::ThreadTaskRunnerHandle> plugin_task_runner_handle_;
266 // True if we're in the middle of handling a log message.
267 bool am_currently_logging_;
269 ScopedRefNPObject log_debug_info_func_;
271 //////////////////////////////////////////////////////////
274 // Internal implementation of the It2Me host function.
275 scoped_ptr<ChromotingHostContext> host_context_;
276 scoped_refptr<It2MeHost> it2me_host_;
278 // Cached, read-only copies of |it2me_host_| session state.
279 It2MeHostState state_;
280 std::string access_code_;
281 base::TimeDelta access_code_lifetime_;
282 std::string client_username_;
284 // IT2Me Talk server configuration used by |it2me_host_| to connect.
285 XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
287 // Chromoting Bot JID used by |it2me_host_| to register the host.
288 std::string directory_bot_jid_;
290 // Callbacks to notify in response to |it2me_host_| events.
291 ScopedRefNPObject on_nat_traversal_policy_changed_func_;
292 ScopedRefNPObject on_state_changed_func_;
294 //////////////////////////////////////////////////////////
297 // Platform-specific installation & configuration implementation.
298 scoped_refptr<DaemonController> daemon_controller_;
300 // TODO(sergeyu): Replace this thread with
301 // SequencedWorkerPool. Problem is that SequencedWorkerPool relies
302 // on MessageLoopProxy::current().
303 scoped_refptr<AutoThreadTaskRunner> worker_thread_;
305 // Used to load and update the paired clients for this host.
306 scoped_refptr<protocol::PairingRegistry> pairing_registry_;
308 //////////////////////////////////////////////////////////
309 // Plugin state used for both Ir2Me and Me2Me.
311 // Used to cancel pending tasks for this object when it is destroyed.
312 base::WeakPtr<HostNPScriptObject> weak_ptr_;
313 base::WeakPtrFactory<HostNPScriptObject> weak_factory_;
315 DISALLOW_COPY_AND_ASSIGN(HostNPScriptObject);
318 } // namespace remoting
320 #endif // REMOTING_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_