-
Notifications
You must be signed in to change notification settings - Fork 3
/
presentation-api-shim.js
803 lines (720 loc) · 27.9 KB
/
presentation-api-shim.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/**
* @fileOverview Shim for the latest version of the Presentation API [1] that
* aims to support projecting to Chromecast devices, to attached devices (HDMI,
* Miracast, etc.) through the experimental Chromium build [2] and to a separate
* browser window as a fallback.
*
* Support for Chromecast devices is heavily restrained because Cast receiver
* applications need to be registered with Google before they may be used and
* this code needs to know about the mapping between the URL of the application
* and the application ID provided by Google upon registration.
*
* As such, applications that want to make use of the shim on Google Cast
* devices need first to issue a call to:
* navigator.presentation.registerCastApplication(appUrl, appId)
*
* Other restrictions:
* - the code uses Promises, underlying Web browser needs to support them
* - support for custom events is fairly limited. Only the "on" properties
* are supported to attach to events on exposed objects, no way to use
* "addEventListener" for the time being.
* - The Cast sender library [3] needs to be loaded before that code if one
* wants to support Chromecast devices.
* - the "onavailablechange" event is not implemented
* - the code probably does not properly handle cases where the receiver calls
* "session.close()". Not exactly sure what to do there.
*
* The code below is divided in 3 parts:
* a) the definition of the CastPresentationSession class that implements
* the PresentationSession interface on top of the Google Cast API
* b) the definition of the WindowPresentationSession class that implements
* the PresentationSession interface on top of window.open
* c) the actual definition of "navigator.presentation" and of the
* PresentationSession class that dispatches the request to either
* CastPresentationSession or WindowPresentationSession depending on
* the underlying context
* The first two parts could be moved to their own JS file, modules are not
* used here not to have to introduce dependencies to some module loader
* library.
*
* References:
* [1] http://webscreens.github.io/presentation-api/
* [2] http://webscreens.github.io/demo/#binaries
* [3] https://www.gstatic.com/cv/js/sender/v1/cast_sender.js
*/
(function () {
/**********************************************************************
Simple console logger to help with debugging. Caller may change logging
level by setting navigator.presentationLogLevel to one of "log", "info",
"warn", "error" or "none" (or null which also means "none").
Note this should be done before that shim is loaded!
**********************************************************************/
var log = function () {
var presentationLogLevel = navigator.presentationLogLevel || 'none';
if ((presentationLogLevel === 'none') || (arguments.length === 0)) {
return;
}
var level = arguments[0];
var params = null;
if ((level === 'log') ||
(level === 'info') ||
(level === 'warn') ||
(level === 'error')) {
// First parameter is the log level
params = Array.prototype.slice.call(arguments, 1);
}
else {
// No log level provided, assume "log"
level = 'log';
params = Array.prototype.slice.call(arguments);
}
if ((level === 'error') ||
((level === 'warn') &&
(presentationLogLevel !== 'error')) ||
((level === 'info') &&
(presentationLogLevel !== 'error') &&
(presentationLogLevel !== 'warn')) ||
((level === 'log') &&
(presentationLogLevel === 'log'))) {
console[level].apply(console, params);
}
};
/**********************************************************************
Exposes a CastPresentationSession class that implements the
PresentationSession interface on top of the Google Cast API and lets one
request display of Web content on available Google Cast devices.
The class exposes 2 static methods that return Promises:
1. "create": returns the Promise to get a new PresentationSession for the
requested Web content if possible.
2. "startReceiver": returns a Promise to get a PresentationSession if
the code is running within a Google Cast receiver application.
The class also exposes the "registerCastApplication" static function to
register the mapping between a receiver app URL and its Google Cast ID.
**********************************************************************/
var CastPresentationSession = (function () {
/**
* Whether the Cast API library is available or not.
* If it's not, the Promises returned by "create" and "startReceiver"
* will always end up being rejected.
*/
var castApiAvailable = false;
window['__onGCastApiAvailable'] = function (loaded, errorInfo) {
if (loaded) {
log('Google Cast API library is available and loaded');
castApiAvailable = true;
} else {
log('warn',
'Google Cast API library is available but could not be loaded',
errorInfo);
}
};
/**
* Whether the Cast API library has been initialized.
*
* That flag is used to support multiple calls to "requestSession". Once
* the Cast API library has been initialized, subsequent Cast session
* requests should directly call sessionRequest.
*/
var castApiInitialized = false;
/**
* Mapping table between receiver application URLs and Cast application IDs
*
* Ideally, there should not be any need to maintain such a mapping table
* but there is no way to have an arbitrary URL run on a Chromecast device.
*/
var castApplications = {};
/**
* Wraps an inner Google Cast session, expose useful properties and methods
* for that session to be used within a PresentationSession.
*
* @constructor
* @param {String} url Receiver application URL
* @param {chrome.cast.Session} session The running Cast session to wrap
* @implements {PresentationSession}
*/
var CastPresentationSession = function (url, session) {
this.url = url;
this.state = 'connected';
this.onstatechange = null;
this.onmessage = null;
this.session = session;
var that = this;
this.session.addUpdateListener(function (isAlive) {
that.state = isAlive ? 'connected' : 'disconnected';
log('received Cast session state update', 'isAlive=' + isAlive);
if (that.onstatechange) {
that.onstatechange(that.state);
}
});
var namespace = this.session.namespaces[0];
this.session.addMessageListener(namespace, function (namespace, message) {
log('received message from Cast receiver', message);
if (that.onmessage) {
that.onmessage(message);
}
});
};
/**
* Sends a message to the Google Cast device.
*
* @function
* @param {Object} message
*/
CastPresentationSession.prototype.postMessage = function (message) {
if (this.state !== 'connected') {
return;
}
log('post message to Cast receiver', message);
var namespace = this.session.namespaces[0];
this.session.sendMessage(namespace.name, message);
};
/**
* Close the Cast session
*
* @function
*/
CastPresentationSession.prototype.close = function () {
if (this.state !== 'connected') {
return;
}
log('close Cast session');
this.session.stop();
};
/**
* Registers the equivalence between the URL of a receiver application and
* its Google Cast app ID.
*
* @function
* @static
* @param {String} url URL of the receiver application
* @param {String} id The Cast application ID associated with that URL
*/
CastPresentationSession.registerCastApplication = function (url, id) {
castApplications[url] = id;
};
/**
* Creates a Google Cast session for the given presentation URL
*
* @function
* @static
* @param {String} url The URL of the receiver app to run
* @return {Promise} The promise to have a running Cast session
*/
CastPresentationSession.create = function (url) {
return new Promise(function (resolve, reject) {
if (!castApiAvailable) {
log('cannot create Cast session',
'Google Cast API library is not available');
reject();
return;
}
if (!castApplications[url]) {
log('cannot create Cast session',
'no receiver app known for url', url);
reject();
return;
}
var sessionCreated = false;
var applicationId = castApplications[url];
var sessionRequest = new chrome.cast.SessionRequest(applicationId);
var requestSession = function () {
log('request new Cast session for url', url);
chrome.cast.requestSession(function (session) {
log('got a new Cast session');
sessionCreated = true;
resolve(new CastPresentationSession(url, session));
}, function (error) {
if (sessionCreated) {
return;
}
if (error.code === 'cancel') {
log('info', 'user chose not to use Cast device');
}
else if (error.code === 'receiver_unavailable') {
log('info', 'no compatible Cast device found');
}
else {
log('error', 'could not create Cast session', error);
}
reject();
}, sessionRequest);
};
var apiConfig = new chrome.cast.ApiConfig(
sessionRequest,
function sessionListener(session) {
// Method called at most once after initialization if a running
// Cast session may be resumed
log('found existing Cast session, reusing');
sessionCreated = true;
resolve(new CastPresentationSession(url, session));
},
function receiverListener(available) {
// Method called whenever the number of Cast devices available in
// the local network changes. The method is called at least once
// after initialization. We're interested in that first call.
if (sessionCreated) {
return;
}
// Reject creation if there are no Google Cast devices that
// can handle the application.
if (available !== chrome.cast.ReceiverAvailability.AVAILABLE) {
log('cannot create Cast session',
'no Cast device available for url', url);
reject();
}
log('found at least one compatible Cast device');
requestSession();
});
if (castApiInitialized) {
// The Cast API library has already been initialized, call
// requestSession directly.
log('Google Cast API library already initialized',
'request new Cast session');
requestSession();
}
else {
// The Cast API library first needs to be initialized
log('initialize Google Cast API library for url', url);
chrome.cast.initialize(apiConfig, function () {
// Note actual session creation is handled by callback functions
// defined above
log('Google Cast API library initialized');
castApiInitialized = true;
}, function (err) {
log('error',
'Google Cast API library could not be initialized', err);
reject();
return;
});
}
});
};
/**
* Starts the Google Cast receiver code if needed, in other words if the
* code is running on a Google Cast device.
*
* @function
* @static
* @return {Promise} The promise to get a running PresentationSession if
* the code is running within a receiver app on a Google Cast device.
* The promise is rejected if the code is not running on such a device.
*/
CastPresentationSession.startReceiver = function () {
return new Promise(function (resolve, reject) {
// Detect whether the code is running on a Google Cast device. If it is,
// it means the code is used within a Receiver application and was
// launched as the result of a call to:
// navigator.presentation.requestSession
// NB: no better way to tell whether we're running on a Cast device
// for the time being, see:
// https://code.google.com/p/google-cast-sdk/issues/detail?id=157
var runningOnChromecast = !!window.navigator.userAgent.match(/CrKey/);
if (!runningOnChromecast) {
log('code is not running on a Google Cast device');
reject();
return;
}
// Start the Google Cast receiver
// Note the need to create the CastReceiverSession before the call to
// "start", as that class registers the namespace used for the
// communication channel.
log('code is running on a Google Cast device',
'start Google Cast receiver manager');
var castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
var session = new CastReceiverSession(castReceiverManager);
castReceiverManager.start();
castReceiverManager.onReady = function () {
log('Google Cast receiver manager started');
resolve(session);
};
// TODO: reject the Promise if the call to "start" fails (not sure
// which event to listen to though)
});
};
/**
* Presentation session that handles the case when the app is a receiver
* app running on a Google Cast Cast device.
*
* Note the external world does not need to know about the existence of
* that class. It merely needs to know it may receive a PresentationSession
* instance from the "startReceiver" method.
*
* @constructor
* @param {chrome.cast.CastReceiverManager} castReceiverManager The Cast
* receiver manager singleton
* @implements {PresentationSession}
*/
var CastReceiverSession = function (castReceiverManager) {
this.state = 'connected';
this.onmessage = null;
this.onstatechange = null;
this.castReceiverManager = castReceiverManager;
this.customMessageBus = castReceiverManager.getCastMessageBus(
'urn:x-cast:org.w3.webscreens.presentationapi.shim',
cast.receiver.CastMessageBus.MessageType.JSON);
var that = this;
this.customMessageBus.addEventListener('message', function (event) {
log('received message from Cast sender', event.data);
if (that.onmessage) {
that.onmessage(event.data);
}
});
};
/**
* Posts a message from a Cast receiver session to the sender.
*
* Note current implementation does not support multiple senders and
* will just broadcast the message to all connected senders.
*
* @function
* @param {Object} message Message to change
*/
CastReceiverSession.prototype.postMessage = function (message) {
if (this.state !== 'connected') {
return;
}
log('post message to Cast sender', message);
this.customMessageBus.broadcast(message);
};
/**
* Closes the receiver session
*
* TODO: this actually kills the application. Not really sure what the
* function should do instead: close the communication channel? That would
* leave the presentation session running on the Cast device without
* controller.
*
* @function
*/
CastReceiverSession.prototype.close = function () {
if (this.state !== 'connected') {
return;
}
log('stop Cast receiver manager');
this.state = 'disconnected';
this.castReceiverManager.stop();
};
// Expose the CastPresentationSession class to the external world
return CastPresentationSession;
})();
/**********************************************************************
Exposes an WindowPresentationSession class that implements the
PresentationSession interface on top of "window.open" and lets one
request display of Web content on second screens connected through a
video port or some wireless equivalent (e.g. Miracast, WiDi) provided
that the code runs in the appropriate modified version of Chromium
that adds support to the "presentation" parameter of "window.open".
If the code is not running in the appropriate build of Chromium, the
presentation session simply opens in a separate window on the same
screen.
The class exposes 2 static methods that return Promises:
1. "create": returns the Promise to get a new PresentationSession for
the requested Web content if possible.
2. "startReceiver": returns a Promise to get a PresentationSession if
the code is running in a receiver app. Note the Promise may never be
resolved in practice as the code will wait for the opener window to
send a "presentation" message.
**********************************************************************/
var WindowPresentationSession = (function () {
/**
* Represents a session to some attached screen, meaning a session to a
* secondary browsing context created and maintained by the current user
* agent
*
* @constructor
* @param {window} remoteWindow Pointer to the remote window
*/
var WindowPresentationSession = function (remoteWindow) {
this.state = 'connected';
this.onmessage = null;
this.onstatechange = null;
this.remoteWindow = remoteWindow;
var that = this;
window.addEventListener('message', function (event) {
if (event.source === remoteWindow) {
if (event.data === 'receivershutdown') {
log('received shut down message from presentation window',
'disconnect session');
that.state = 'disconnected';
if (that.onstatechange) {
that.onstatechange(that.state);
}
}
else {
log('received message from presentation window', event.data);
if (that.onmessage) {
that.onmessage(event.data);
}
}
}
}, false);
};
/**
* Sends a message to the attached screen
*
* @function
* @param {String} msg
*/
WindowPresentationSession.prototype.postMessage = function (msg) {
if (this.state !== 'connected') {
return;
}
log('post message to presentation window', msg);
this.remoteWindow.postMessage(msg, '*');
};
/**
* Closes the attached session
*
* @function
*/
WindowPresentationSession.prototype.close = function () {
if (this.state !== 'connected') {
return;
}
log('close presentation window');
this.remoteWindow.close();
this.state = 'disconnected';
if (this.onstatechange) {
this.onstatechange(this.state);
}
};
/**
* Create a presentation session for the first attached screen, falling
* back to a separate window if there are none.
*
* @function
* @param {String} url The URL to load in the presentation session
* @return {Promise} The promise to get a WindowPresentationSession instance
*/
WindowPresentationSession.create = function (url) {
return new Promise(function (resolve, reject) {
log('open presentation window');
var presentationWindow = window.open(url, '', 'presentation');
if (!presentationWindow) {
log('could not open presentation window');
reject();
return;
}
window.addEventListener('message', function (event) {
if ((event.source === presentationWindow) &&
(event.data === 'ispresentation')) {
log('received "is this a presentation session?" message ' +
'from presentation window');
log('post "presentation" message to presentation window');
presentationWindow.postMessage('presentation', '*');
}
else if ((event.source === presentationWindow) &&
(event.data === 'presentationready')) {
log('received "presentation ready" message ' +
'from presentation window');
resolve(new WindowPresentationSession(presentationWindow));
}
}, false);
});
};
/**
* Prepares a PresentationSession if the code is running within a
* receiver application.
*
* To determine whether that is the case, the code dispatches a
* "ispresentation" message to its opener window (if defined) and
* waits for a "presentation" ack from that opener window.
*
* @function
* @static
* @return {Promise} The promise to get a running PresentationSession if
* the code is running within a receiver app on a Google Cast device.
* The promise is rejected if the code is not running on such a device.
*/
WindowPresentationSession.startReceiver = function () {
return new Promise(function (resolve, reject) {
// No window opener? The code does not run a receiver app.
if (!window.opener) {
log('code is not running in a presentation window');
reject();
return;
}
var messageEventListener = function (event) {
if ((event.source === window.opener) &&
(event.data === 'presentation')) {
log('received "presentation" message from opener window');
log('code is running in a presentation window');
window.removeEventListener('message', messageEventListener);
resolve(new WindowPresentationSession(window.opener));
}
};
window.addEventListener('message', messageEventListener, false);
log('post "ispresentation" message to opener window ' +
'and wait for "presentation" message');
log('assume code is not running in a presentation window ' +
'in the meantime');
window.opener.postMessage('ispresentation', '*');
window.addEventListener('unload', function () {
log('presentation window is being closed');
if (window.opener) {
window.opener.postMessage('receivershutdown', '*');
}
}, false);
});
};
// Expose the WindowPresentationSession to the external world
return WindowPresentationSession;
})();
/**********************************************************************
Implements navigator.presentation, dispatching to
CastPresentationSession or WindowPresentationSession depending on
the underlying context.
**********************************************************************/
/**
* Implements a generic Presentation session that dispatches to either
* CastPresentationSession or WindowPresentationSession depending on
* the underlying context.
*
* @constructor
* @param {String} url URL of the application to project to a second screen
*/
var PresentationSession = function (url) {
this.url = url;
this.session = null;
this.state = 'disconnected';
this.onmessage = null;
this.onstatechange = null;
// Try with a Google Cast presentation session first,
// then with a window presentation session.
log('info', 'new presentation session requested for url', url);
log('try to create a Cast session');
var that = this;
CastPresentationSession.create(url)
.then(function (session) {
log('Cast presentation session created');
return session;
}, function () {
log('Cast session could not be created');
log('try to create a presentation window instead');
return WindowPresentationSession.create(url);
})
.then(function (session) {
log('info', 'presentation session created');
that.session = session;
that.state = session.state;
that.session.onmessage = function (message) {
if (that.onmessage) {
that.onmessage(message);
}
};
that.session.onstatechange = function (state) {
that.state = state;
if (that.onstatechange) {
that.onstatechange();
}
};
if (that.state === 'connected') {
if (that.onstatechange) {
that.onstatechange();
}
}
}, function () {
log('warn', 'could not create presentation session');
that.state = 'disconnected';
if (that.onstatechange) {
that.onstatechange();
}
});
};
/**
* Sends a message to the wrapped cast or window presentation session
*
* @function
* @param {*} message
*/
PresentationSession.prototype.postMessage = function (message) {
if (!this.session) {
log('Presentation session not available, cannot send message');
return;
}
if (this.state === 'disconnected') {
log('Presentation session is disconnected, cannot send message');
return;
}
this.session.postMessage(message);
};
/**
* Close the session
*/
PresentationSession.prototype.close = function () {
if (!this.session) {
return;
}
this.session.close();
this.session = null;
};
/**
* Implements the Presentation API
*
* TODO: the "onavailablechange" event is not yet implemented, mostly because
* it should depend on the URL of the application that will be presented.
*/
var Presentation = function () {
this.onavailablechange = null;
this.onpresent = null;
var that = this;
// Initializes presentation receiver bindings, dispatching the appropriate
// "present" event if needed and a final "presentationready" message to the
// sender (only useful for window presentations, Google Cast devices
// apparently only report that the connection is ready when all this code
// has run.
// 3 cases arise:
// 1. shim is running on a Google Cast device, and so running in a Google
// Cast receiver application. The event is fired.
// 2. shim is running in a window opened by some other window in response
// to a call to navigator.presentation.requestSession. The event is fired.
// 3. shim in running in regular Web app. No event fired.
window.addEventListener('load', function () {
log('info', 'check whether code is running in a presentation receiver app');
CastPresentationSession.startReceiver()
.then(function (session) {
return session;
}, function () {
return WindowPresentationSession.startReceiver();
})
.then(function (session) {
log('info', 'yes, code is running in a presentation receiver app');
if (that.onpresent) {
log('dispatch "present" message');
that.onpresent({
session: session
});
}
log('info', 'tell sender that presentation receiver app is ready');
session.postMessage('presentationready');
}, function () {
log('info', 'no, code is not running in a presentation receiver app');
});
}, false);
};
/**
* Requests display of web content on a connected screen
*
* @function
* @param {String} url The URL to display on a connected screen
*/
Presentation.prototype.requestSession = function (url) {
return new PresentationSession(url);
};
/**
* Non-standard function exposed so that this shim may know how to map
* a URL to be presented to a Cast receiver application on a Chromecast
* device
*
* @function
* @param {String} url URL of the receiver application
* @param {String} id The Cast application ID associated with that URL
*/
Presentation.prototype.registerCastApplication = function (url, id) {
CastPresentationSession.registerCastApplication(url, id);
};
// Expose the Presentation API to the navigator object
// (the called should immediately bind to the "present" event to
// detect execution in a receiver app)
navigator.presentation = new Presentation();
}());