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"; | |
emaxx
2016/07/18 13:46:58
nit: Google JS style guide suggests to prefer the
adrian.belgun
2016/07/19 13:57:22
Done.
| |
7 var configId; | |
8 | |
9 // Example configuration. | |
10 vpnParams = { | |
emaxx
2016/07/18 13:46:58
nit: add "var".
adrian.belgun
2016/07/19 13:57:22
Done.
| |
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 configurations using the createConfig method. | |
emaxx
2016/07/18 13:46:58
nit: s/configurations/configuration/.
adrian.belgun
2016/07/19 13:57:22
Done.
| |
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("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( | |
45 vpnParams, | |
46 function() { | |
47 wlog("setParameters set!"); | |
48 | |
49 // Bind connection to NaCl. | |
50 bind(); | |
51 } | |
52 ); | |
emaxx
2016/07/18 13:46:58
nit: the closing bracket usually stays on the same
adrian.belgun
2016/07/19 13:57:22
Done.
| |
53 } | |
54 | |
55 function onBindSuccess() { | |
56 // Notify the connection state as "connected". | |
57 chrome.vpnProvider.notifyConnectionStateChanged( | |
58 "connected", | |
59 function() { | |
60 wlog("notifyConnectionStateChanged connected!"); | |
61 } | |
62 ); | |
63 } | |
64 | |
65 // VpnProviders handlers. | |
66 function onPlatformMessageListener(id, message, error) { | |
67 wlog("onPlatformMessage id='" + id + "' message='" + message + "' error='" + | |
68 error + "'"); | |
69 | |
70 if (message == "connected") { | |
71 wlog("onPlatformMessage connected!"); | |
72 | |
73 // Notify NaCl module to connect to the VPN tunnel. | |
74 common.naclModule.postMessage({cmd: 'connected'}); | |
emaxx
2016/07/18 13:46:58
nit: Please correct the indentation here and in li
adrian.belgun
2016/07/19 13:57:22
Done.
| |
75 | |
76 } else if (message == "disconnected") { | |
77 wlog("onPlatformMessage disconnected!"); | |
78 | |
79 // Notify NaCl module to disconnect from the VPN tunnel. | |
80 common.naclModule.postMessage({cmd: 'disconnected'}); | |
81 } | |
82 } | |
83 | |
84 // This function is called by common.js when a message is received from the | |
85 // NaCl module. | |
86 function handleMessage(message) { | |
87 if (typeof message.data === "string") { | |
88 wlog(message.data); | |
89 } else if (message.data['cmd'] == 'setParameters') { | |
90 onSetParameters(); | |
91 } else if (message.data['cmd'] == 'bindSuccess') { | |
92 onBindSuccess(); | |
93 } | |
94 } | |
95 | |
96 // setupHandlers VpnProviders handlers. | |
97 function setupHandlers() { | |
98 // Add listeners to the events onPlatformMessage, onPacketReceived and | |
99 // onConfigRemoved. | |
100 chrome.vpnProvider.onPlatformMessage.addListener(onPlatformMessageListener); | |
101 | |
102 chrome.vpnProvider.onPacketReceived.addListener(function(data) { | |
emaxx
2016/07/18 13:46:58
Is it correct that this JavaScript event should ne
adrian.belgun
2016/07/19 13:57:22
Yes. That's what I'm doing here. It's logging to t
| |
103 wlog("onPacketRecevied - JS"); | |
emaxx
2016/07/18 13:46:58
nit: s/onPacketRecevied/onPacketReceived/
adrian.belgun
2016/07/19 13:57:22
Done.
| |
104 }); | |
105 | |
106 chrome.vpnProvider.onConfigRemoved.addListener(function(id) { | |
107 wlog("onConfigRemoved id='" + id + "'"); | |
108 }); | |
109 | |
110 chrome.vpnProvider.onConfigCreated.addListener(function(id, name, data) { | |
111 wlog("onConfigCreated id='" + id + "' name='" + name + "'" + | |
112 " data='" + data + "'"); | |
emaxx
2016/07/18 13:46:58
nit: the data argument is not a string, so it shou
emaxx
2016/07/18 13:46:58
nit: please fix the indentation.
adrian.belgun
2016/07/19 13:57:22
Done.
adrian.belgun
2016/07/19 13:57:22
Acknowledged.
| |
113 }); | |
114 | |
115 chrome.vpnProvider.onUIEvent.addListener(function(event, id) { | |
116 wlog("onUIEvent event='" + event + "' id='" + id + "'"); | |
117 }); | |
118 } | |
119 | |
120 // This function is called by common.js when the NaCl module is | |
121 // loaded. | |
122 function moduleDidLoad() { | |
123 // Once we load, hide the plugin. In this example, we don't display anything | |
124 // in the plugin, so it is fine to hide it. | |
125 common.hideModule(); | |
126 | |
127 // Setup VpnProvider handlers. | |
128 setupHandlers(); | |
129 | |
130 // All done, create the connection entry in the VPN UI. | |
131 create(); | |
132 } | |
OLD | NEW |