/
webintents-server.js
231 lines (188 loc) · 6.14 KB
/
webintents-server.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var id;
var callbacks = {};
window.attachEventListener = function(obj, type, func, capture) {
if(!!obj.addEventListener) {
obj.addEventListener(type, func, capture);
}
else if(!!obj.attachEvent) {
obj.attachEvent("on" + type, func);
}
};
var Intents = new (function() {
this.getAllActions = function () {
var allActions = [];
for(var key in localStorage) {
var actions = JSON.parse(localStorage[key]);
if(!!actions.actions && actions.actions instanceof Array) {
allActions.push({ key: key, actions: actions.actions });
}
}
return allActions;
};
this.getActions = function(intent) {
if(!!intent == false) throw "No intent";
if(!!intent.action == false) throw "No action to resolve";
var actionData = localStorage[intent.action] || "{}";
var actions = JSON.parse(actionData);
if(actions instanceof Array) {
actions = { "actions" : actions };
}
else if (!!actions.actions == false) {
actions = { "actions" : [] };
}
var action;
var filteredActions = { "actions": [] };
// Find the actions that are of the correct type (or not). Does not handle *, yet
for(var i = 0; action = actions.actions[i]; i++) {
if(intent.type == action.type || !!intent.type == false) {
filteredActions.actions.push(action);
}
}
return filteredActions;
};
this.clearAll = function() {
localStorage.clear();
};
this.addAction = function(intent) {
if(!!intent == false) throw "No intent";
if(!!intent.action == false) throw "No action to resolve";
var actionData = localStorage[intent.action] || "{}";
var actions = JSON.parse(actionData);
if(actions instanceof Array) {
// Upgrade existing intents to new format.
actions = { "actions" : actions };
}
else if (!!actions.actions == false) {
actions = { "actions" : [] };
}
var action;
var found = false;
// Replace an existing action.
for(var i = 0; action = actions.actions[i]; i++) {
if(intent.action == action.action && intent.url == action.url && intent.type == action.type) {
actions.actions[i] = intent;
found = true;
break;
}
}
// Add a new action
if(found == false) {
actions.actions.push(intent);
}
localStorage[intent.action] = JSON.stringify(actions);
};
this.verify = function(intent) {
return true;
};
})();
if(window.parent.opener && window.parent.opener.Intents) {
verified = window.parent.opener.Intents.verify();
// The picker has said it is legit. (TODO), so close the window.
if(verified) window.parent.opener.close();
}
var MessageDispatcher = function() {
this.register = function(data, timestamp, e) {
Intents.addAction(data.intent);
};
this.startActivity = function(data, timestamp, e) {
var actions = Intents.getActions(data.intent);
var intentData = {
id: data.intent._id,
intent: data.intent,
timestamp: timestamp
};
localStorage[data.intent._id] = JSON.stringify(intentData);
IntentController.renderActions(actions, data.intent);
};
this.registerCallback = function(data, timestamp, e) {
callbacks[data.id] = {};
if(!!window.onstorage == false) {
// We are going to have to set up something that polls.
var timer = setInterval(function() {
var intentStr = localStorage.getItem(data.id);
if(!!intentStr) {
var intentObj = JSON.parse(intentStr);
if(intentObj.request == "sendResponse") {
window.postMessage(intentStr, "*");
clearInterval(timer);
}
}
},1000);
}
};
/*
* The service has sent a response, route it back to the correct frame.
*/
this.intentResponse = function(data, timestamp, e) {
var id = data.intent._id;
var intentData = {
"request": "sendResponse",
intent: data.intent,
timestamp: timestamp
};
localStorage[id] = JSON.stringify(intentData);
};
/*
* The correct frame has recieved the reposen, route it back to the parent app.
*/
this.sendResponse = function(data, timestamp, e) {
if(!!callbacks[data.intent._id] == false) {
return;
}
localStorage.removeItem(data.intent._id);
var message = JSON.stringify({ intent: data.intent, request: "response" });
window.parent.postMessage(
message,
"*"
);
};
};
var MessageHandler = function() {
var dispatcher = new MessageDispatcher();
this.handler = function(e) {
var data;
if(!!e.data) {
data = JSON.parse(e.data);
}
else {
var vals = localStorage[e.key];
try {
data = JSON.parse(vals);
} catch(ex) {
return;
}
}
if(data.origin && data.origin !== window.name ||
data.process && data.process == false) {
// If there is an intended origin, then enforce it, or the system says not to listen to the event.
return;
}
var timestamp = (new Date()).valueOf();
if(dispatcher[data.request])
dispatcher[data.request](data, timestamp, e);
};
}
var msgHandler = new MessageHandler();;
attachEventListener(window, "message", msgHandler.handler, false);
attachEventListener(window, "storage", msgHandler.handler, false);
attachEventListener(document, "storage", msgHandler.handler, false);
attachEventListener(window, "load", function() {
// Tell the app we are loaded.
var message = JSON.stringify({ request: "ready" });
window.parent.postMessage(message, "*");
}, false);
if(!!window.onstorage) {
// we don't have storage events, so lets poll.
}