OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include <time.h> |
| 6 #include <iostream> |
| 7 #include "ppapi/cpp/instance.h" |
| 8 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/cpp/var.h" |
| 10 #include "ppapi/cpp/var_dictionary.h" |
| 11 #include "ppapi/cpp/vpn_provider.h" |
| 12 #include "ppapi/utility/completion_callback_factory.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // The expected string sent by the browser. |
| 17 const char* const kHelloString = "hello"; |
| 18 const char* const kByeString = "bye"; |
| 19 // The string sent back to the browser upon receipt of a message |
| 20 // containing "hello". |
| 21 const char* const kHelloReplyString = "hello from NaCl"; |
| 22 const char* const kByeReplyString = "bye from NaCl"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 class HelloTutorialInstance : public pp::Instance { |
| 27 public: |
| 28 explicit HelloTutorialInstance(PP_Instance instance) |
| 29 : pp::Instance(instance), factory_(this) { |
| 30 vpn_ = new pp::VpnProvider(this); |
| 31 |
| 32 std::cerr << "VPN Provider NaCL: " |
| 33 << "HelloTutorialInstance::HelloTutorialInstance ()" << std::endl; |
| 34 |
| 35 // Initial Callaback registration |
| 36 vpn_->GetPlatformMessage(factory_.NewCallbackWithOutput( |
| 37 &HelloTutorialInstance::GetPlatformMessageCompletionCallback)); |
| 38 |
| 39 vpn_->GetPacket(factory_.NewCallbackWithOutput( |
| 40 &HelloTutorialInstance::GetPacketCompletionCallback)); |
| 41 |
| 42 vpn_->GetConfigMessage(factory_.NewCallbackWithOutput( |
| 43 &HelloTutorialInstance::GetConfigMessageCompletionCallback)); |
| 44 |
| 45 vpn_->GetUIMessage(factory_.NewCallbackWithOutput( |
| 46 &HelloTutorialInstance::GetUIMessageCompletionCallback)); |
| 47 } |
| 48 |
| 49 virtual ~HelloTutorialInstance() { |
| 50 std::cerr << "VPN Provider NaCL: " |
| 51 << "HelloTutorialInstance::~HelloTutorialInstance ()" |
| 52 << std::endl; |
| 53 delete vpn_; |
| 54 vpn_ = nullptr; |
| 55 } |
| 56 |
| 57 // Handles On Click messages from Javascript |
| 58 virtual void HandleMessage(const pp::Var& var_message) { |
| 59 // Ignore the message if it is not a string. |
| 60 if (!var_message.is_string()) |
| 61 return; |
| 62 |
| 63 // Get the string message and compare it to "hello". |
| 64 std::string message = var_message.AsString(); |
| 65 if (message == kHelloString) { |
| 66 config = "Adrian"; |
| 67 AppendServiceIDTimeStamp(config); |
| 68 vpn_->CreateConfig( |
| 69 config, factory_.NewCallbackWithOutput( |
| 70 &HelloTutorialInstance::CreateConfigCompletionCallback)); |
| 71 |
| 72 // If it matches, send our response back to JavaScript. |
| 73 pp::Var var_reply(kHelloReplyString); |
| 74 PostMessage(var_reply); |
| 75 } |
| 76 |
| 77 if (message == kByeString) { |
| 78 vpn_->DestroyConfig( |
| 79 config, factory_.NewCallback( |
| 80 &HelloTutorialInstance::DestroyConfigCompletionCallback)); |
| 81 |
| 82 // If it matches, send our response back to JavaScript. |
| 83 pp::Var var_reply(kByeReplyString); |
| 84 PostMessage(var_reply); |
| 85 } |
| 86 } |
| 87 |
| 88 void AppendServiceIDTimeStamp(std::string& service_id) { |
| 89 char time_string[20] = "YYYYMMDDhhmmss"; |
| 90 time_t t = time(NULL); |
| 91 strftime(time_string, sizeof(time_string), "%Y%m%d%H%M%S", localtime(&t)); |
| 92 time_string[sizeof(time_string) - 1] = '\0'; |
| 93 service_id.append(time_string); |
| 94 } |
| 95 |
| 96 void CreateConfigCompletionCallback(int32_t result, pp::Var id) { |
| 97 std::cerr << "VPN Provider NaCL: " |
| 98 << "HelloTutorialInstance::CreateConfigCompletionCallback (" |
| 99 << result << ", " << id.AsString() << ")" << std::endl; |
| 100 } |
| 101 |
| 102 void DestroyConfigCompletionCallback(int32_t result) { |
| 103 std::cerr << "VPN Provider NaCL: " |
| 104 << "HelloTutorialInstance::DestroyConfigCompletionCallback (" |
| 105 << result << ")" << std::endl; |
| 106 } |
| 107 void SendStateChangeNotificationCallback(int32_t result) { |
| 108 std::cerr << "VPN Provider NaCL: " |
| 109 << "HelloTutorialInstance::SendStateChangeNotificationCallback (" |
| 110 << result << ")" << std::endl; |
| 111 } |
| 112 |
| 113 void GetPlatformMessageCompletionCallback(int32_t result, |
| 114 const pp::Var& message) { |
| 115 std::cerr << "VPN Provider NaCL: " |
| 116 << "HelloTutorialInstance::GetPlatformMessageCompletionCallback (" |
| 117 << result << ")" << std::endl; |
| 118 |
| 119 pp::VarDictionary dict(message); |
| 120 std::cerr << "VPN Provider NaCL: " |
| 121 << "Received OnPlatformMessage (" |
| 122 << dict.Get(pp::Var("id")).AsString() << ", " |
| 123 << dict.Get(pp::Var("message")).AsInt() << ", " |
| 124 << dict.Get(pp::Var("error")).AsString() << ", " |
| 125 << ")" << std::endl; |
| 126 |
| 127 if (dict.Get(pp::Var("message")).AsInt() == |
| 128 PP_VPN_PROVIDER_PLATFORM_MESSAGE_CONNECTED) { |
| 129 std::vector<std::string> exclusionList; |
| 130 exclusionList.push_back(std::string("63.145.213.129/32")); |
| 131 exclusionList.push_back(std::string("63.145.212.0/24")); |
| 132 |
| 133 std::vector<std::string> inclusionList; |
| 134 inclusionList.push_back("0.0.0.0/0"); |
| 135 inclusionList.push_back("63.145.212.128/25"); |
| 136 |
| 137 std::vector<std::string> domainSearch; |
| 138 domainSearch.push_back("foo:bar"); |
| 139 |
| 140 std::vector<std::string> dnsServers; |
| 141 dnsServers.push_back("8.8.8.8"); |
| 142 |
| 143 std::cerr |
| 144 << "VPN Provider NaCL: SetParameters: " |
| 145 << vpn_->SetParameters( |
| 146 "10.10.10.10/24", "10.10.10.255", 1600, exclusionList, |
| 147 inclusionList, domainSearch, dnsServers, |
| 148 factory_.NewCallback( |
| 149 &HelloTutorialInstance::SetParametersCompletionCallback)) |
| 150 << std::endl; |
| 151 } |
| 152 |
| 153 // Re-register callback |
| 154 if (result == PP_OK) { |
| 155 vpn_->GetPlatformMessage(factory_.NewCallbackWithOutput( |
| 156 &HelloTutorialInstance::GetPlatformMessageCompletionCallback)); |
| 157 } |
| 158 } |
| 159 |
| 160 void SetParametersCompletionCallback(int32_t result) { |
| 161 std::cerr << "VPN Provider NaCL: " |
| 162 << "HelloTutorialInstance::SetParametersCompletionCallback (" |
| 163 << result << ")" << std::endl; |
| 164 |
| 165 vpn_->NotifyConnectionStateChanged( |
| 166 PP_VPN_PROVIDER_CONNECTION_STATE_CONNECTED, |
| 167 factory_.NewCallback( |
| 168 &HelloTutorialInstance::SendStateChangeNotificationCallback)); |
| 169 } |
| 170 |
| 171 void GetPacketCompletionCallback(int32_t result, const pp::Var packet) { |
| 172 std::cerr << "VPN Provider NaCL: " |
| 173 << "HelloTutorialInstance::GetPacketCompletionCallback (" |
| 174 << result << ")" << std::endl; |
| 175 |
| 176 // "Null Proxy" implementation |
| 177 vpn_->SendPacket(packet); |
| 178 |
| 179 // Re-register callback |
| 180 if (result == PP_OK) { |
| 181 vpn_->GetPacket(factory_.NewCallbackWithOutput( |
| 182 &HelloTutorialInstance::GetPacketCompletionCallback)); |
| 183 } |
| 184 } |
| 185 |
| 186 void GetConfigMessageCompletionCallback(int32_t result, |
| 187 const pp::Var& message) { |
| 188 std::cerr << "VPN Provider NaCL: " |
| 189 << "HelloTutorialInstance::GetConfigMessageCompletionCallback (" |
| 190 << result << ")" << std::endl; |
| 191 |
| 192 pp::VarDictionary dict(message); |
| 193 std::cerr << "VPN Provider NaCL: " |
| 194 << "Received OnConfigMessage (" |
| 195 << dict.Get(pp::Var("id")).AsString() << ", " |
| 196 << dict.Get(pp::Var("message")).AsInt() << ", " |
| 197 << dict.Get(pp::Var("name")).AsString() << ", " |
| 198 << dict.Get(pp::Var("data")).AsString() << ")" << std::endl; |
| 199 |
| 200 // Re-register callback |
| 201 if (result == PP_OK) { |
| 202 vpn_->GetConfigMessage(factory_.NewCallbackWithOutput( |
| 203 &HelloTutorialInstance::GetConfigMessageCompletionCallback)); |
| 204 } |
| 205 } |
| 206 |
| 207 void GetUIMessageCompletionCallback(int32_t result, const pp::Var& message) { |
| 208 std::cerr << "VPN Provider NaCL: " |
| 209 << "HelloTutorialInstance::GetUIMessageCompletionCallback (" |
| 210 << result << ")" << std::endl; |
| 211 |
| 212 pp::VarDictionary dict(message); |
| 213 std::cerr << "VPN Provider NaCL: " |
| 214 << "Received OnUIMessage (" |
| 215 << dict.Get(pp::Var("message")).AsInt() << ", " |
| 216 << dict.Get(pp::Var("id")).AsString() << ")" << std::endl; |
| 217 |
| 218 // Re-register callback |
| 219 if (result == PP_OK) { |
| 220 vpn_->GetUIMessage(factory_.NewCallbackWithOutput( |
| 221 &HelloTutorialInstance::GetUIMessageCompletionCallback)); |
| 222 } |
| 223 } |
| 224 |
| 225 std::string config; |
| 226 pp::VpnProvider* vpn_; |
| 227 pp::CompletionCallbackFactory<HelloTutorialInstance> factory_; |
| 228 }; |
| 229 |
| 230 class HelloTutorialModule : public pp::Module { |
| 231 public: |
| 232 HelloTutorialModule() : pp::Module() {} |
| 233 virtual ~HelloTutorialModule() {} |
| 234 |
| 235 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 236 return new HelloTutorialInstance(instance); |
| 237 } |
| 238 }; |
| 239 |
| 240 namespace pp { |
| 241 |
| 242 Module* CreateModule() { |
| 243 return new HelloTutorialModule(); |
| 244 } |
| 245 |
| 246 } // namespace pp |
OLD | NEW |