-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-api.js
More file actions
97 lines (81 loc) · 3.27 KB
/
browser-api.js
File metadata and controls
97 lines (81 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var app = require('electron').app;
var BrowserWindow = require('electron').BrowserWindow;
var ipc = require('electron').ipcMain;
var EventEmitter = require('events');
var process = require('process');
process.env.GOOGLE_API_KEY = 'AIzaSyDWUqBQqohHRt5QtGTSHdW2CtZN9AcAGBA';
var browserWindow;
function initBrowserWindow()
{
browserWindow = new BrowserWindow({ show: false });
browserWindow.loadURL('file://' + __dirname + '/browser-api.html');
}
app.on('ready', function()
{
exports.GeoLocation = function GeoLocation()
{
if(!browserWindow)
initBrowserWindow();
this.idCounter = 0;
this.idMapping = [];
this.successMapping = [];
this.errorMapping = [];
};
exports.GeoLocation.prototype = new EventEmitter();
exports.GeoLocation.prototype.getCurrentPosition = function getCurrentPosition(callback, errorCallback, options)
{
browserWindow.webContents.executeJavaScript('geolocation("getCurrentPosition", ' + JSON.stringify(options) + ');');
ipc.once('got-current-position', function(event, position) { callback(position); });
ipc.once('error-getting-current-position', function(event, error) { errorCallback(error); });
};
exports.GeoLocation.prototype.watchPosition = function getCurrentPosition(callback, errorCallback, options)
{
var id = this.idCounter++;
browserWindow.webContents.executeJavaScript('geolocation("watchPosition", ' + JSON.stringify(options) + ');', false, this.idCallback.bind(this, id));
ipc.on('got-current-position', this.successMapping[id] = function(event, position) { callback(position); });
ipc.on('error-getting-current-position', this.errorMapping[id] = function(event, error) { errorCallback(error); });
return id;
};
exports.GeoLocation.prototype.idCallback = function idCallback(ourID, remoteID)
{
this.idMapping[ourID] = remoteID;
if(this.idMapping[ourID] == -1) //'poisoned' id, see clearWatch
this.clearWatch(ourID);
};
exports.GeoLocation.prototype.clearWatch = function clearWatch(id)
{
if(this.idMapping[id])
{
browserWindow.webContents.executeJavaScript('navigator.geolocation.clearWatch(' + JSON.stringify(id) + ');');
delete this.idMapping[id];
ipc.removeListener('got-current-position', this.successMapping[id]);
delete this.successMapping[id];
ipc.removeListener('error-getting-current-position', this.errorMapping[id]);
delete this.errorMapping[id];
}
//maybe we do not have the remote ID yet, 'poison' this id, so the idCallback will call us again once we get it
//but don't allow the user to clear future watches in advance
else if(id < this.idCounter)
{
this.idMapping[id] = -1;
}
};
exports.OnlineStatus = function OnlineStatus()
{
if(!browserWindow)
initBrowserWindow();
this.reEmit = this.reEmit.bind(this);
ipc.on('online-status-changed', this.reEmit);
};
exports.OnlineStatus.prototype = new EventEmitter();
exports.OnlineStatus.prototype.reEmit = function reEmit(event, status)
{
this.emit('online-status-changed', status);
};
exports.OnlineStatus.prototype.destroy = function destroy()
{
this.emit('will-destroy', this);
ipc.removeListener('online-status-changed', reEmit);
this.removeAllListeners();
};
});