Index: native_client_sdk/src/examples/api/vpn_provider/vpn_provider.cc |
diff --git a/native_client_sdk/src/examples/api/vpn_provider/vpn_provider.cc b/native_client_sdk/src/examples/api/vpn_provider/vpn_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..823e920473d4776daef8445fa7a1ec137c263376 |
--- /dev/null |
+++ b/native_client_sdk/src/examples/api/vpn_provider/vpn_provider.cc |
@@ -0,0 +1,141 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <queue> |
+ |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/cpp/var.h" |
+#include "ppapi/cpp/var_dictionary.h" |
+ |
+#include "vpn_provider_helper.h" |
+ |
+class VpnProviderInstance : public pp::Instance { |
+ public: |
+ explicit VpnProviderInstance(PP_Instance instance) |
+ : pp::Instance(instance), vpn_provider_helper_(this) { |
+ vpn_provider_helper_.Init(); |
+ } |
+ |
+ virtual ~VpnProviderInstance() {} |
+ |
+ // Handles On Click messages from Javascript |
emaxx
2016/07/18 13:46:58
The comment looks to be a bit confusing, as the me
adrian.belgun
2016/07/19 13:57:22
Done. Was a leftover from an older version.
|
+ virtual void HandleMessage(const pp::Var& message) { |
+ // Expecting only dictionary messages from JS. |
+ if (!message.is_dictionary()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Unexpected message. Not a dictionary."); |
+ return; |
+ } |
+ |
+ // Message type defined by 'cmd' key. |
+ pp::VarDictionary dict(message); |
+ pp::Var cmd = dict.Get("cmd"); |
+ if (cmd.is_undefined()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. No 'cmd' key."); |
+ return; |
+ } |
+ if (!cmd.is_string()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. Type for key 'cmd' is not string"); |
+ return; |
+ } |
+ |
+ std::string command = cmd.AsString(); |
emaxx
2016/07/18 13:46:58
nit: #include <string>
adrian.belgun
2016/07/19 13:57:22
Done.
|
+ if (cmd == "bind") { |
+ pp::Var name = dict.Get("name"); |
+ if (name.is_undefined()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. No 'name' key."); |
+ return; |
+ } |
+ if (!name.is_string()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. Type for key 'name' is not string"); |
+ return; |
+ } |
+ |
+ pp::Var id = dict.Get("id"); |
binji
2016/07/18 18:46:28
duplicated 3 times, probably worth making a functi
adrian.belgun
2016/07/19 13:57:22
Done.
|
+ if (id.is_undefined()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. No 'id' key."); |
+ return; |
+ } |
+ if (!id.is_string()) { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Malformed message. Type for key 'id' is not string"); |
+ return; |
+ } |
+ |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Bind request."); |
+ vpn_provider_helper_.Bind(name.AsString(), id.AsString()); |
+ return; |
+ } |
+ |
+ if (cmd == "connected") { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Connect request."); |
+ |
+ /* This is the place where the developer would establing the VPN |
+ * connection. The response would usually contain configuration details |
+ * for the tunnel obtained from the VPN implementation. |
+ * |
+ * Currently just signaling that is was executed succesfuly. |
+ */ |
+ |
+ pp::VarDictionary dict; |
+ dict.Set("cmd", "setParameters"); |
+ PostMessage(dict); |
+ return; |
+ } |
+ |
+ if (cmd == "disconnected") { |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Disconnect request."); |
+ |
+ /* This is the place where the developer would disconnect from the VPN |
+ * connection. |
+ */ |
+ |
+ return; |
+ } |
+ |
+ PostMessage( |
+ "NaCl: VpnProviderInstance::HandleMessage: " |
+ "Unexpected command."); |
+ } |
+ |
+ private: |
+ VpnProviderHelper vpn_provider_helper_; |
+}; |
+ |
+class VpnProviderModule : public pp::Module { |
+ public: |
+ VpnProviderModule() : pp::Module() {} |
+ virtual ~VpnProviderModule() {} |
+ |
+ virtual pp::Instance* CreateInstance(PP_Instance instance) { |
+ return new VpnProviderInstance(instance); |
+ } |
+}; |
+ |
+namespace pp { |
+ |
+Module* CreateModule() { |
+ return new VpnProviderModule(); |
+} |
+ |
+} // namespace pp |