OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // This hack prevents a bug on the cast extension. | 7 // This hack prevents a bug on the cast extension. |
8 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. | 8 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. |
9 // Although localStorage in Chrome app is not supported, but it's used in the | 9 // Although localStorage in Chrome app is not supported, but it's used in the |
10 // cast extension. This line prevents an exception on using localStorage. | 10 // cast extension. This line prevents an exception on using localStorage. |
11 window.__defineGetter__('localStorage', function() { return {}; }); | 11 window.__defineGetter__('localStorage', function() { return {}; }); |
12 | 12 |
13 // THIS IS A TEST APP. | 13 // THIS IS A TEST APP. |
14 // TODO(yoshiki): Fix this before launch. | 14 // TODO(yoshiki): Fix this before launch. |
15 var APPLICATION_ID = '214CC863'; | 15 var APPLICATION_ID = '214CC863'; |
16 | 16 |
17 util.addPageLoadHandler(function() { | 17 util.addPageLoadHandler(function() { |
18 initialize(); | 18 initialize(); |
19 }.wrap()); | 19 }.wrap()); |
20 | 20 |
21 /** | 21 /** |
22 * Starts initialization of cast-related feature. | 22 * Starts initialization of cast-related feature. |
23 */ | 23 */ |
24 function initialize() { | 24 function initialize() { |
| 25 if (window.loadMockCastExtensionForTest) { |
| 26 // If the test flag is set, the mock extension for test will be laoded by |
| 27 // the test script. Sets the handler to wait for loading. |
| 28 onLoadCastExtension(initializeApi); |
| 29 return; |
| 30 } |
| 31 |
25 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { | 32 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { |
26 if (foundId) | 33 if (foundId) |
27 loadCastAPI(initializeApi); | 34 loadCastAPI(initializeApi); |
28 else | 35 else |
29 console.info('No Google Cast extension is installed.'); | 36 console.info('No Google Cast extension is installed.'); |
30 }.wrap()); | 37 }.wrap()); |
31 } | 38 } |
32 | 39 |
33 /** | 40 /** |
34 * Executes the given callback after the cast extension is initialized. | 41 * Loads the cast API extention. If not install, the extension is installed |
| 42 * in background before load. The cast API will load the cast SDK automatically. |
| 43 * The given callback is executes after the cast SDK extension is initialized. |
| 44 * |
35 * @param {function} callback Callback (executed asynchronously). | 45 * @param {function} callback Callback (executed asynchronously). |
36 * @param {boolean=} opt_secondTry Spericy try if it's second call after | 46 * @param {boolean=} opt_secondTry Spericy try if it's second call after |
37 * installation of Cast API extension. | 47 * installation of Cast API extension. |
38 */ | 48 */ |
39 function loadCastAPI(callback, opt_secondTry) { | 49 function loadCastAPI(callback, opt_secondTry) { |
40 var script = document.createElement('script'); | 50 var script = document.createElement('script'); |
41 | 51 |
42 var onError = function() { | 52 var onError = function() { |
43 script.removeEventListener('error', onError); | 53 script.removeEventListener('error', onError); |
44 document.body.removeChild(script); | 54 document.body.removeChild(script); |
(...skipping 14 matching lines...) Expand all Loading... |
59 chrome.runtime.lastError.message); | 69 chrome.runtime.lastError.message); |
60 return; | 70 return; |
61 } | 71 } |
62 | 72 |
63 console.info('Google Cast API extension installed.'); | 73 console.info('Google Cast API extension installed.'); |
64 // Loads API again. | 74 // Loads API again. |
65 setTimeout(loadCastAPI.bind(null, callback, true)); | 75 setTimeout(loadCastAPI.bind(null, callback, true)); |
66 }.wrap()); | 76 }.wrap()); |
67 }.wrap(); | 77 }.wrap(); |
68 | 78 |
69 var onLoad = function() { | 79 // Trys to load the cast API extention which is defined in manifest.json. |
70 if(!chrome.cast || !chrome.cast.isAvailable) { | |
71 var checkTimer = setTimeout(function() { | |
72 console.error('Either "Google Cast API" or "Google Cast" extension ' + | |
73 'seems not to be installed?'); | |
74 }.wrap(), 5000); | |
75 | |
76 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { | |
77 clearTimeout(checkTimer); | |
78 | |
79 if (loaded) | |
80 callback(); | |
81 else | |
82 console.error('Google Cast extension load failed.', errorInfo); | |
83 }.wrap(); | |
84 } else { | |
85 setTimeout(callback); // Runs asynchronously. | |
86 } | |
87 }.wrap(); | |
88 | |
89 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; | 80 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; |
90 script.addEventListener('error', onError); | 81 script.addEventListener('error', onError); |
91 script.addEventListener('load', onLoad); | 82 script.addEventListener('load', onLoadCastExtension.bind(null, callback)); |
92 document.body.appendChild(script); | 83 document.body.appendChild(script); |
93 } | 84 } |
94 | 85 |
95 /** | 86 /** |
| 87 * Loads the cast sdk extension. |
| 88 * @param {function()} callback Callback (executed asynchronously). |
| 89 */ |
| 90 function onLoadCastExtension(callback) { |
| 91 if(!chrome.cast || !chrome.cast.isAvailable) { |
| 92 var checkTimer = setTimeout(function() { |
| 93 console.error('Either "Google Cast API" or "Google Cast" extension ' + |
| 94 'seems not to be installed?'); |
| 95 }.wrap(), 5000); |
| 96 |
| 97 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { |
| 98 clearTimeout(checkTimer); |
| 99 |
| 100 if (loaded) |
| 101 callback(); |
| 102 else |
| 103 console.error('Google Cast extension load failed.', errorInfo); |
| 104 }.wrap(); |
| 105 } else { |
| 106 setTimeout(callback); // Runs asynchronously. |
| 107 } |
| 108 }; |
| 109 |
| 110 /** |
96 * Initialize Cast API. | 111 * Initialize Cast API. |
97 */ | 112 */ |
98 function initializeApi() { | 113 function initializeApi() { |
99 var onSession = function() { | 114 var onSession = function() { |
100 // TODO(yoshiki): Implement this. | 115 // TODO(yoshiki): Implement this. |
101 }; | 116 }; |
102 | 117 |
103 var onInitSuccess = function() { | 118 var onInitSuccess = function() { |
104 // TODO(yoshiki): Implement this. | 119 // TODO(yoshiki): Implement this. |
105 }; | 120 }; |
(...skipping 24 matching lines...) Expand all Loading... |
130 } | 145 } |
131 | 146 |
132 player.setCastList(receivers); | 147 player.setCastList(receivers); |
133 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { | 148 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { |
134 player.setCastList([]); | 149 player.setCastList([]); |
135 } else { | 150 } else { |
136 console.error('Unexpected response in onReceiver.', arguments); | 151 console.error('Unexpected response in onReceiver.', arguments); |
137 player.setCastList([]); | 152 player.setCastList([]); |
138 } | 153 } |
139 } | 154 } |
OLD | NEW |