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 // Start the daemon process with the specified config. Args are: 149 // string config 150 // function(number) done_callback 151 bool StartDaemon(const NPVariant* args, 152 uint32_t arg_count, 153 NPVariant* result); 154 155 // Stop the daemon process. Args are: 156 // function(number) done_callback 157 bool StopDaemon(const NPVariant* args, uint32_t arg_count, NPVariant* result); 158 159 ////////////////////////////////////////////////////////// 160 // Implementation of It2MeHost::Observer methods. 161 162 // Notifies OnStateChanged handler of a state change. 163 virtual void OnStateChanged(It2MeHostState state) OVERRIDE; 164 165 // If the web-app has registered a callback to be notified of changes to the 166 // NAT traversal policy, notify it. 167 virtual void OnNatPolicyChanged(bool nat_traversal_enabled) OVERRIDE; 168 169 // Stores the Access Code for the web-app to query. 170 virtual void OnStoreAccessCode(const std::string& access_code, 171 base::TimeDelta access_code_lifetime) OVERRIDE; 172 173 // Stores the client user's name for the web-app to query. 174 virtual void OnClientAuthenticated( 175 const std::string& client_username) OVERRIDE; 176 177 // Used to generate localized strings to pass to the It2Me host core. 178 void LocalizeStrings(NPObject* localize_func); 179 180 // Helper function for executing InvokeDefault on an NPObject that performs 181 // a string->string mapping without substitution. Stores the translation in 182 // |result| and returns true on success, or leaves it unchanged and returns 183 // false on failure. 184 bool LocalizeString(NPObject* localize_func, const char* tag, 185 base::string16* result); 186 187 // Helper function for executing InvokeDefault on an NPObject that performs 188 // a string->string mapping with one substitution. Stores the translation in 189 // |result| and returns true on success, or leaves it unchanged and returns 190 // false on failure. 191 bool LocalizeStringWithSubstitution(NPObject* localize_func, 192 const char* tag, 193 const char* substitution, 194 base::string16* result); 195 196 ////////////////////////////////////////////////////////// 197 // Helper methods for Me2Me host. 198 199 // Helpers for GenerateKeyPair(). 200 static void DoGenerateKeyPair( 201 const scoped_refptr<AutoThreadTaskRunner>& plugin_task_runner, 202 const base::Callback<void (const std::string&, 203 const std::string&)>& callback); 204 void InvokeGenerateKeyPairCallback(scoped_ptr<ScopedRefNPObject> callback, 205 const std::string& private_key, 206 const std::string& public_key); 207 208 // Callback handler for SetConfigAndStart(), Stop(), SetPin() and 209 // SetUsageStatsConsent() in DaemonController. 210 void InvokeAsyncResultCallback(scoped_ptr<ScopedRefNPObject> callback, 211 DaemonController::AsyncResult result); 212 213 // Callback handler for PairingRegistry methods that return a boolean 214 // success status. 215 void InvokeBooleanCallback(scoped_ptr<ScopedRefNPObject> callback, 216 bool result); 217 218 // Callback handler for DaemonController::GetConfig(). 219 void InvokeGetDaemonConfigCallback(scoped_ptr<ScopedRefNPObject> callback, 220 scoped_ptr<base::DictionaryValue> config); 221 222 // Callback handler for DaemonController::GetVersion(). 223 void InvokeGetDaemonVersionCallback(scoped_ptr<ScopedRefNPObject> callback, 224 const std::string& version); 225 226 // Callback handler for GetPairedClients(). 227 void InvokeGetPairedClientsCallback( 228 scoped_ptr<ScopedRefNPObject> callback, 229 scoped_ptr<base::ListValue> paired_clients); 230 231 // Callback handler for DaemonController::GetUsageStatsConsent(). 232 void InvokeGetUsageStatsConsentCallback( 233 scoped_ptr<ScopedRefNPObject> callback, 234 const DaemonController::UsageStatsConsent& consent); 235 236 ////////////////////////////////////////////////////////// 237 // Basic helper methods used for both It2Me and Me2me. 238 239 // Call LogDebugInfo handler if there is one. 240 // This must be called on the correct thread. 241 void LogDebugInfo(const std::string& message); 242 243 // Helper function for executing InvokeDefault on an NPObject, and ignoring 244 // the return value. 245 bool InvokeAndIgnoreResult(const ScopedRefNPObject& func, 246 const NPVariant* args, 247 uint32_t arg_count); 248 249 // Set an exception for the current call. 250 void SetException(const std::string& exception_string); 251 252 ////////////////////////////////////////////////////////// 253 // Plugin state variables shared between It2Me and Me2Me. 254 255 NPP plugin_; 256 NPObject* parent_; 257 scoped_refptr<AutoThreadTaskRunner> plugin_task_runner_; 258 scoped_ptr<base::ThreadTaskRunnerHandle> plugin_task_runner_handle_; 259 260 // True if we're in the middle of handling a log message. 261 bool am_currently_logging_; 262 263 ScopedRefNPObject log_debug_info_func_; 264 265 ////////////////////////////////////////////////////////// 266 // It2Me host state. 267 268 // Internal implementation of the It2Me host function. 269 scoped_ptr<ChromotingHostContext> host_context_; 270 scoped_refptr<It2MeHost> it2me_host_; 271 272 // Cached, read-only copies of |it2me_host_| session state. 273 It2MeHostState state_; 274 std::string access_code_; 275 base::TimeDelta access_code_lifetime_; 276 std::string client_username_; 277 278 // IT2Me Talk server configuration used by |it2me_host_| to connect. 279 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; 280 281 // Chromoting Bot JID used by |it2me_host_| to register the host. 282 std::string directory_bot_jid_; 283 284 // Callbacks to notify in response to |it2me_host_| events. 285 ScopedRefNPObject on_nat_traversal_policy_changed_func_; 286 ScopedRefNPObject on_state_changed_func_; 287 288 ////////////////////////////////////////////////////////// 289 // Me2Me host state. 290 291 // Platform-specific installation & configuration implementation. 292 scoped_refptr<DaemonController> daemon_controller_; 293 294 // TODO(sergeyu): Replace this thread with 295 // SequencedWorkerPool. Problem is that SequencedWorkerPool relies 296 // on MessageLoopProxy::current(). 297 scoped_refptr<AutoThreadTaskRunner> worker_thread_; 298 299 // Used to load and update the paired clients for this host. 300 scoped_refptr<protocol::PairingRegistry> pairing_registry_; 301 302 ////////////////////////////////////////////////////////// 303 // Plugin state used for both Ir2Me and Me2Me. 304 305 // Used to cancel pending tasks for this object when it is destroyed. 306 base::WeakPtr<HostNPScriptObject> weak_ptr_; 307 base::WeakPtrFactory<HostNPScriptObject> weak_factory_; 308 309 DISALLOW_COPY_AND_ASSIGN(HostNPScriptObject); 310 }; 311 312 } // namespace remoting 313 314 #endif // REMOTING_HOST_PLUGIN_HOST_SCRIPT_OBJECT_H_ 315