OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 // VPN Configuration identification. |
| 6 var configName = 'Mock configuration'; |
| 7 var configId; |
| 8 |
| 9 // Example configuration. |
| 10 var vpnParams = { |
| 11 'address': '127.0.0.1/32', |
| 12 'mtu': '1000', |
| 13 'exclusionList': ['127.0.0.1/32'], |
| 14 'inclusionList': ['0.0.0.0/0'], |
| 15 'dnsServers': ['8.8.8.8'], |
| 16 'reconnect': 'true' |
| 17 }; |
| 18 |
| 19 // Simple log to HTML |
| 20 function wlog(message) { |
| 21 var logEl = document.getElementById('log'); |
| 22 logEl.innerHTML += message + '<br />'; // Append to log. |
| 23 logEl.scrollTop = logEl.scrollHeight; // Scroll log to bottom. |
| 24 } |
| 25 |
| 26 // Create a VPN configuration using the createConfig method. |
| 27 // A VPN configuration is a persistent entry shown to the user in a native |
| 28 // Chrome OS UI. The user can select a VPN configuration from a list and |
| 29 // connect to it or disconnect from it. |
| 30 function create() { |
| 31 chrome.vpnProvider.createConfig(configName, function(id) { |
| 32 configId = id; |
| 33 wlog('JS: Created configuration with name=\'' + configName + '\'' + |
| 34 ' and id=\'' + configId + '\''); |
| 35 }); |
| 36 } |
| 37 |
| 38 // Bind connection to NaCl. |
| 39 function bind() { |
| 40 common.naclModule.postMessage({cmd: 'bind', name: configName, id: configId}); |
| 41 } |
| 42 |
| 43 function onSetParameters() { |
| 44 chrome.vpnProvider.setParameters(vpnParams, function() { |
| 45 wlog('JS: setParameters set!'); |
| 46 |
| 47 // Bind connection to NaCl. |
| 48 bind(); |
| 49 }); |
| 50 } |
| 51 |
| 52 function onBindSuccess() { |
| 53 // Notify the connection state as 'connected'. |
| 54 chrome.vpnProvider.notifyConnectionStateChanged('connected', function() { |
| 55 wlog('JS: notifyConnectionStateChanged connected!'); |
| 56 }); |
| 57 } |
| 58 |
| 59 // VpnProviders handlers. |
| 60 function onPlatformMessageListener(id, message, error) { |
| 61 wlog('JS: onPlatformMessage: id=\'' + id + '\' message=\'' + message + |
| 62 '\' error=\'' + error + '\''); |
| 63 |
| 64 if (message == 'connected') { |
| 65 wlog('JS: onPlatformMessage connected!'); |
| 66 |
| 67 // Notify NaCl module to connect to the VPN tunnel. |
| 68 common.naclModule.postMessage({cmd: 'connected'}); |
| 69 |
| 70 } else if (message == 'disconnected') { |
| 71 wlog('JS: onPlatformMessage disconnected!'); |
| 72 |
| 73 // Notify NaCl module to disconnect from the VPN tunnel. |
| 74 common.naclModule.postMessage({cmd: 'disconnected'}); |
| 75 } |
| 76 } |
| 77 |
| 78 // This function is called by common.js when a message is received from the |
| 79 // NaCl module. |
| 80 function handleMessage(message) { |
| 81 if (typeof message.data === 'string') { |
| 82 wlog(message.data); |
| 83 } else if (message.data['cmd'] == 'setParameters') { |
| 84 onSetParameters(); |
| 85 } else if (message.data['cmd'] == 'bindSuccess') { |
| 86 onBindSuccess(); |
| 87 } |
| 88 } |
| 89 |
| 90 // setupHandlers VpnProviders handlers. |
| 91 function setupHandlers() { |
| 92 // Add listeners to the events onPlatformMessage, onPacketReceived and |
| 93 // onConfigRemoved. |
| 94 chrome.vpnProvider.onPlatformMessage.addListener(onPlatformMessageListener); |
| 95 |
| 96 chrome.vpnProvider.onPacketReceived.addListener(function(data) { |
| 97 wlog('JS: onPacketReceived'); |
| 98 console.log('Unexpected event:vpnProvider.onPacketReceived ' + |
| 99 'called from JavaScript.'); |
| 100 }); |
| 101 |
| 102 chrome.vpnProvider.onConfigRemoved.addListener(function(id) { |
| 103 wlog('JS: onConfigRemoved: id=\'' + id + '\''); |
| 104 }); |
| 105 |
| 106 chrome.vpnProvider.onConfigCreated.addListener(function(id, name, data) { |
| 107 wlog('JS: onConfigCreated: id=\'' + id + '\' name=\'' + name + '\'' + |
| 108 'data=' + JSON.stringify(data)); |
| 109 }); |
| 110 |
| 111 chrome.vpnProvider.onUIEvent.addListener(function(event, id) { |
| 112 wlog('JS: onUIEvent: event=\'' + event + '\' id=\'' + id + '\''); |
| 113 }); |
| 114 } |
| 115 |
| 116 // This function is called by common.js when the NaCl module is |
| 117 // loaded. |
| 118 function moduleDidLoad() { |
| 119 // Once we load, hide the plugin. In this example, we don't display anything |
| 120 // in the plugin, so it is fine to hide it. |
| 121 common.hideModule(); |
| 122 |
| 123 if (chrome.vpnProvider === undefined) { |
| 124 wlog('JS: moduleDidLoad: chrome.vpnProvider undefined.'); |
| 125 console.log('JS: moduleDidLoad: chrome.vpnProvider undefined.'); |
| 126 return; |
| 127 } |
| 128 |
| 129 // Setup VpnProvider handlers. |
| 130 setupHandlers(); |
| 131 |
| 132 // All done, create the connection entry in the VPN UI. |
| 133 create(); |
| 134 } |
OLD | NEW |