Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / host / plugin / host_script_object.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_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_
6 #define REMOTING_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_
7
8 #include <string>
9 #include <vector>
10
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"
21
22 namespace remoting {
23
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 {
29  public:
30   HostNPScriptObject(NPP plugin,
31                      NPObject* parent,
32                      scoped_refptr<AutoThreadTaskRunner> plugin_task_runner);
33   virtual ~HostNPScriptObject();
34
35   // Implementations used to implement the NPObject interface.
36   bool HasMethod(const std::string& method_name);
37   bool InvokeDefault(const NPVariant* args,
38                      uint32_t arg_count,
39                      NPVariant* result);
40   bool Invoke(const std::string& method_name,
41               const NPVariant* args,
42               uint32_t arg_count,
43               NPVariant* result);
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);
49
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);
54
55   void SetWindow(NPWindow* np_window);
56
57  private:
58   //////////////////////////////////////////////////////////
59   // Plugin methods for It2Me host.
60
61   // Start connection. args are:
62   //   string uid, string auth_token
63   // No result.
64   bool Connect(const NPVariant* args, uint32_t arg_count, NPVariant* result);
65
66   // Disconnect. No arguments or result.
67   bool Disconnect(const NPVariant* args, uint32_t arg_count, NPVariant* result);
68
69   // Localize strings. args are:
70   //   localize_func - a callback function which returns a localized string for
71   //   a given tag name.
72   // No result.
73   bool Localize(const NPVariant* args, uint32_t arg_count, NPVariant* result);
74
75   //////////////////////////////////////////////////////////
76   // Plugin methods for Me2Me host.
77
78   // Deletes all paired clients from the registry.
79   bool ClearPairedClients(const NPVariant* args,
80                           uint32_t arg_count,
81                           NPVariant* result);
82
83   // Deletes a paired client referenced by client id.
84   bool DeletePairedClient(const NPVariant* args,
85                           uint32_t arg_count,
86                           NPVariant* result);
87
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,
92                    uint32_t arg_count,
93                    NPVariant* result);
94
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,
102                   uint32_t arg_count,
103                   NPVariant* result);
104
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,
111                        uint32_t arg_count,
112                        NPVariant* result);
113
114   // Update host config for Me2Me. Args are:
115   //   string config
116   //   function(number) done_callback
117   bool UpdateDaemonConfig(const NPVariant* args,
118                           uint32_t arg_count,
119                           NPVariant* result);
120
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,
126                        uint32_t arg_count,
127                        NPVariant* result);
128
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,
133                         uint32_t arg_count,
134                         NPVariant* result);
135
136   // Retrieves the list of paired clients as a JSON-encoded string.
137   bool GetPairedClients(const NPVariant* args,
138                         uint32_t arg_count,
139                         NPVariant* result);
140
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,
145                             uint32_t arg_count,
146                             NPVariant* result);
147
148   // Download and install the host component.
149   //   function(number) done_callback
150   bool InstallHost(const NPVariant* args,
151                    uint32_t arg_count,
152                    NPVariant* result);
153
154   // Start the daemon process with the specified config. Args are:
155   //   string config
156   //   function(number) done_callback
157   bool StartDaemon(const NPVariant* args,
158                    uint32_t arg_count,
159                    NPVariant* result);
160
161   // Stop the daemon process. Args are:
162   //   function(number) done_callback
163   bool StopDaemon(const NPVariant* args, uint32_t arg_count, NPVariant* result);
164
165   //////////////////////////////////////////////////////////
166   // Implementation of It2MeHost::Observer methods.
167
168   // Notifies OnStateChanged handler of a state change.
169   virtual void OnStateChanged(It2MeHostState state) OVERRIDE;
170
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;
174
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;
178
179   // Stores the client user's name for the web-app to query.
180   virtual void OnClientAuthenticated(
181       const std::string& client_username) OVERRIDE;
182
183   // Used to generate localized strings to pass to the It2Me host core.
184   void LocalizeStrings(NPObject* localize_func);
185
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
189   // false on failure.
190   bool LocalizeString(NPObject* localize_func, const char* tag,
191                       base::string16* result);
192
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
196   // false on failure.
197   bool LocalizeStringWithSubstitution(NPObject* localize_func,
198                                       const char* tag,
199                                       const char* substitution,
200                                       base::string16* result);
201
202   //////////////////////////////////////////////////////////
203   // Helper methods for Me2Me host.
204
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);
213
214   // Callback handler for SetConfigAndStart(), Stop(), SetPin() and
215   // SetUsageStatsConsent() in DaemonController.
216   void InvokeAsyncResultCallback(scoped_ptr<ScopedRefNPObject> callback,
217                                  DaemonController::AsyncResult result);
218
219   // Callback handler for PairingRegistry methods that return a boolean
220   // success status.
221   void InvokeBooleanCallback(scoped_ptr<ScopedRefNPObject> callback,
222                              bool result);
223
224   // Callback handler for DaemonController::GetConfig().
225   void InvokeGetDaemonConfigCallback(scoped_ptr<ScopedRefNPObject> callback,
226                                      scoped_ptr<base::DictionaryValue> config);
227
228   // Callback handler for DaemonController::GetVersion().
229   void InvokeGetDaemonVersionCallback(scoped_ptr<ScopedRefNPObject> callback,
230                                       const std::string& version);
231
232   // Callback handler for GetPairedClients().
233   void InvokeGetPairedClientsCallback(
234       scoped_ptr<ScopedRefNPObject> callback,
235       scoped_ptr<base::ListValue> paired_clients);
236
237   // Callback handler for DaemonController::GetUsageStatsConsent().
238   void InvokeGetUsageStatsConsentCallback(
239       scoped_ptr<ScopedRefNPObject> callback,
240       const DaemonController::UsageStatsConsent& consent);
241
242   //////////////////////////////////////////////////////////
243   // Basic helper methods used for both It2Me and Me2me.
244
245   // Call LogDebugInfo handler if there is one.
246   // This must be called on the correct thread.
247   void LogDebugInfo(const std::string& message);
248
249   // Helper function for executing InvokeDefault on an NPObject, and ignoring
250   // the return value.
251   bool InvokeAndIgnoreResult(const ScopedRefNPObject& func,
252                              const NPVariant* args,
253                              uint32_t arg_count);
254
255   // Set an exception for the current call.
256   void SetException(const std::string& exception_string);
257
258   //////////////////////////////////////////////////////////
259   // Plugin state variables shared between It2Me and Me2Me.
260
261   NPP plugin_;
262   NPObject* parent_;
263   scoped_refptr<AutoThreadTaskRunner> plugin_task_runner_;
264   scoped_ptr<base::ThreadTaskRunnerHandle> plugin_task_runner_handle_;
265
266   // True if we're in the middle of handling a log message.
267   bool am_currently_logging_;
268
269   ScopedRefNPObject log_debug_info_func_;
270
271   //////////////////////////////////////////////////////////
272   // It2Me host state.
273
274   // Internal implementation of the It2Me host function.
275   scoped_ptr<ChromotingHostContext> host_context_;
276   scoped_refptr<It2MeHost> it2me_host_;
277
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_;
283
284   // IT2Me Talk server configuration used by |it2me_host_| to connect.
285   XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
286
287   // Chromoting Bot JID used by |it2me_host_| to register the host.
288   std::string directory_bot_jid_;
289
290   // Callbacks to notify in response to |it2me_host_| events.
291   ScopedRefNPObject on_nat_traversal_policy_changed_func_;
292   ScopedRefNPObject on_state_changed_func_;
293
294   //////////////////////////////////////////////////////////
295   // Me2Me host state.
296
297   // Platform-specific installation & configuration implementation.
298   scoped_refptr<DaemonController> daemon_controller_;
299
300   // TODO(sergeyu): Replace this thread with
301   // SequencedWorkerPool. Problem is that SequencedWorkerPool relies
302   // on MessageLoopProxy::current().
303   scoped_refptr<AutoThreadTaskRunner> worker_thread_;
304
305   // Used to load and update the paired clients for this host.
306   scoped_refptr<protocol::PairingRegistry> pairing_registry_;
307
308   //////////////////////////////////////////////////////////
309   // Plugin state used for both Ir2Me and Me2Me.
310
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_;
314
315   DISALLOW_COPY_AND_ASSIGN(HostNPScriptObject);
316 };
317
318 }  // namespace remoting
319
320 #endif  // REMOTING_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_