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..bcf786ee3ec9bafc5285c45fab27e6e9ffb6816d |
--- /dev/null |
+++ b/native_client_sdk/src/examples/api/vpn_provider/vpn_provider.cc |
@@ -0,0 +1,246 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
binji
2016/02/24 19:23:09
2016, remove (c)
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <time.h> |
binji
2016/02/24 19:23:09
nit: sort alphabetically, and separate libc includ
|
+#include <iostream> |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/cpp/var.h" |
+#include "ppapi/cpp/var_dictionary.h" |
+#include "ppapi/cpp/vpn_provider.h" |
+#include "ppapi/utility/completion_callback_factory.h" |
+ |
+namespace { |
+ |
+// The expected string sent by the browser. |
+const char* const kHelloString = "hello"; |
+const char* const kByeString = "bye"; |
+// The string sent back to the browser upon receipt of a message |
+// containing "hello". |
+const char* const kHelloReplyString = "hello from NaCl"; |
+const char* const kByeReplyString = "bye from NaCl"; |
+ |
+} // namespace |
+ |
+class HelloTutorialInstance : public pp::Instance { |
+ public: |
+ explicit HelloTutorialInstance(PP_Instance instance) |
+ : pp::Instance(instance), factory_(this) { |
+ vpn_ = new pp::VpnProvider(this); |
+ |
+ std::cerr << "VPN Provider NaCL: " |
binji
2016/02/24 19:23:09
nit: NaCl (lowercase l) here and elsewhere
|
+ << "HelloTutorialInstance::HelloTutorialInstance ()" << std::endl; |
+ |
+ // Initial Callaback registration |
binji
2016/02/24 19:23:09
sp: callback
|
+ vpn_->GetPlatformMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetPlatformMessageCompletionCallback)); |
binji
2016/02/24 19:23:09
These names are a bit verbose, maybe just "OnGetPl
|
+ |
+ vpn_->GetPacket(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetPacketCompletionCallback)); |
+ |
+ vpn_->GetConfigMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetConfigMessageCompletionCallback)); |
+ |
+ vpn_->GetUIMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetUIMessageCompletionCallback)); |
+ } |
+ |
+ virtual ~HelloTutorialInstance() { |
+ std::cerr << "VPN Provider NaCL: " |
binji
2016/02/24 19:23:09
It's nicer if these messages are sent to the page.
|
+ << "HelloTutorialInstance::~HelloTutorialInstance ()" |
+ << std::endl; |
+ delete vpn_; |
+ vpn_ = nullptr; |
binji
2016/02/24 19:23:09
prefer NULL, one of our compilers is quite old (~2
|
+ } |
+ |
+ // Handles On Click messages from Javascript |
+ virtual void HandleMessage(const pp::Var& var_message) { |
+ // Ignore the message if it is not a string. |
+ if (!var_message.is_string()) |
+ return; |
+ |
+ // Get the string message and compare it to "hello". |
+ std::string message = var_message.AsString(); |
+ if (message == kHelloString) { |
+ config = "Adrian"; |
+ AppendServiceIDTimeStamp(config); |
+ vpn_->CreateConfig( |
+ config, factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::CreateConfigCompletionCallback)); |
+ |
+ // If it matches, send our response back to JavaScript. |
+ pp::Var var_reply(kHelloReplyString); |
+ PostMessage(var_reply); |
+ } |
+ |
+ if (message == kByeString) { |
+ vpn_->DestroyConfig( |
+ config, factory_.NewCallback( |
+ &HelloTutorialInstance::DestroyConfigCompletionCallback)); |
+ |
+ // If it matches, send our response back to JavaScript. |
+ pp::Var var_reply(kByeReplyString); |
+ PostMessage(var_reply); |
+ } |
+ } |
+ |
+ void AppendServiceIDTimeStamp(std::string& service_id) { |
+ char time_string[20] = "YYYYMMDDhhmmss"; |
+ time_t t = time(NULL); |
+ strftime(time_string, sizeof(time_string), "%Y%m%d%H%M%S", localtime(&t)); |
+ time_string[sizeof(time_string) - 1] = '\0'; |
+ service_id.append(time_string); |
+ } |
+ |
+ void CreateConfigCompletionCallback(int32_t result, pp::Var id) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::CreateConfigCompletionCallback (" |
+ << result << ", " << id.AsString() << ")" << std::endl; |
+ } |
+ |
+ void DestroyConfigCompletionCallback(int32_t result) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::DestroyConfigCompletionCallback (" |
+ << result << ")" << std::endl; |
+ } |
+ void SendStateChangeNotificationCallback(int32_t result) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::SendStateChangeNotificationCallback (" |
+ << result << ")" << std::endl; |
+ } |
+ |
+ void GetPlatformMessageCompletionCallback(int32_t result, |
+ const pp::Var& message) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::GetPlatformMessageCompletionCallback (" |
+ << result << ")" << std::endl; |
+ |
+ pp::VarDictionary dict(message); |
+ std::cerr << "VPN Provider NaCL: " |
+ << "Received OnPlatformMessage (" |
+ << dict.Get(pp::Var("id")).AsString() << ", " |
binji
2016/02/24 19:23:09
you can just write dict.Get("id").AsString(), ther
|
+ << dict.Get(pp::Var("message")).AsInt() << ", " |
+ << dict.Get(pp::Var("error")).AsString() << ", " |
+ << ")" << std::endl; |
+ |
+ if (dict.Get(pp::Var("message")).AsInt() == |
+ PP_VPN_PROVIDER_PLATFORM_MESSAGE_CONNECTED) { |
+ std::vector<std::string> exclusionList; |
+ exclusionList.push_back(std::string("63.145.213.129/32")); |
binji
2016/02/24 19:23:09
same here, use implicit conversion to std::string
|
+ exclusionList.push_back(std::string("63.145.212.0/24")); |
+ |
+ std::vector<std::string> inclusionList; |
+ inclusionList.push_back("0.0.0.0/0"); |
+ inclusionList.push_back("63.145.212.128/25"); |
binji
2016/02/24 19:23:09
what do these IP addresses point to? Is this usabl
|
+ |
+ std::vector<std::string> domainSearch; |
+ domainSearch.push_back("foo:bar"); |
+ |
+ std::vector<std::string> dnsServers; |
+ dnsServers.push_back("8.8.8.8"); |
+ |
+ std::cerr |
+ << "VPN Provider NaCL: SetParameters: " |
+ << vpn_->SetParameters( |
+ "10.10.10.10/24", "10.10.10.255", 1600, exclusionList, |
+ inclusionList, domainSearch, dnsServers, |
+ factory_.NewCallback( |
+ &HelloTutorialInstance::SetParametersCompletionCallback)) |
+ << std::endl; |
+ } |
+ |
+ // Re-register callback |
+ if (result == PP_OK) { |
+ vpn_->GetPlatformMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetPlatformMessageCompletionCallback)); |
+ } |
+ } |
+ |
+ void SetParametersCompletionCallback(int32_t result) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::SetParametersCompletionCallback (" |
+ << result << ")" << std::endl; |
+ |
+ vpn_->NotifyConnectionStateChanged( |
+ PP_VPN_PROVIDER_CONNECTION_STATE_CONNECTED, |
+ factory_.NewCallback( |
+ &HelloTutorialInstance::SendStateChangeNotificationCallback)); |
+ } |
+ |
+ void GetPacketCompletionCallback(int32_t result, const pp::Var packet) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::GetPacketCompletionCallback (" |
+ << result << ")" << std::endl; |
+ |
+ // "Null Proxy" implementation |
+ vpn_->SendPacket(packet); |
+ |
+ // Re-register callback |
+ if (result == PP_OK) { |
+ vpn_->GetPacket(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetPacketCompletionCallback)); |
+ } |
+ } |
+ |
+ void GetConfigMessageCompletionCallback(int32_t result, |
+ const pp::Var& message) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::GetConfigMessageCompletionCallback (" |
+ << result << ")" << std::endl; |
+ |
+ pp::VarDictionary dict(message); |
+ std::cerr << "VPN Provider NaCL: " |
+ << "Received OnConfigMessage (" |
+ << dict.Get(pp::Var("id")).AsString() << ", " |
+ << dict.Get(pp::Var("message")).AsInt() << ", " |
+ << dict.Get(pp::Var("name")).AsString() << ", " |
+ << dict.Get(pp::Var("data")).AsString() << ")" << std::endl; |
+ |
+ // Re-register callback |
+ if (result == PP_OK) { |
+ vpn_->GetConfigMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetConfigMessageCompletionCallback)); |
+ } |
+ } |
+ |
+ void GetUIMessageCompletionCallback(int32_t result, const pp::Var& message) { |
+ std::cerr << "VPN Provider NaCL: " |
+ << "HelloTutorialInstance::GetUIMessageCompletionCallback (" |
+ << result << ")" << std::endl; |
+ |
+ pp::VarDictionary dict(message); |
+ std::cerr << "VPN Provider NaCL: " |
+ << "Received OnUIMessage (" |
+ << dict.Get(pp::Var("message")).AsInt() << ", " |
+ << dict.Get(pp::Var("id")).AsString() << ")" << std::endl; |
+ |
+ // Re-register callback |
+ if (result == PP_OK) { |
+ vpn_->GetUIMessage(factory_.NewCallbackWithOutput( |
+ &HelloTutorialInstance::GetUIMessageCompletionCallback)); |
+ } |
+ } |
+ |
+ std::string config; |
+ pp::VpnProvider* vpn_; |
+ pp::CompletionCallbackFactory<HelloTutorialInstance> factory_; |
+}; |
+ |
+class HelloTutorialModule : public pp::Module { |
+ public: |
+ HelloTutorialModule() : pp::Module() {} |
+ virtual ~HelloTutorialModule() {} |
+ |
+ virtual pp::Instance* CreateInstance(PP_Instance instance) { |
+ return new HelloTutorialInstance(instance); |
+ } |
+}; |
+ |
+namespace pp { |
+ |
+Module* CreateModule() { |
+ return new HelloTutorialModule(); |
Sam Clegg
2016/02/24 18:16:28
Rename the module and instance to match the name o
|
+} |
+ |
+} // namespace pp |