Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: ui/file_manager/video_player/js/cast/caster.js

Issue 1912493003: Video Player: Stop using Cast API shared module. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update an error message. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | ui/file_manager/video_player/js/video_player_metrics.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This hack prevents a bug on the cast extension. 5 // This hack prevents a bug on the cast extension.
6 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps. 6 // TODO(yoshiki): Remove this once the cast extension supports Chrome apps.
7 // Although localStorage in Chrome app is not supported, but it's used in the 7 // Although localStorage in Chrome app is not supported, but it's used in the
8 // cast extension. This line prevents an exception on using localStorage. 8 // cast extension. This line prevents an exception on using localStorage.
9 Object.defineProperty(window, 'localStorage', { 9 Object.defineProperty(window, 'localStorage', {
10 get: function() { return {}; } 10 get: function() { return {}; }
11 }); 11 });
12 12
13 /** 13 /**
14 * @type {string} 14 * @type {string}
15 * @const 15 * @const
16 */ 16 */
17 var APPLICATION_ID = '4CCB98DA'; 17 var APPLICATION_ID = '4CCB98DA';
18 18
19 util.addPageLoadHandler(function() { 19 util.addPageLoadHandler(function() {
20 initialize(); 20 initialize();
21 }.wrap()); 21 }.wrap());
22 22
23 /** 23 /**
24 * Starts initialization of cast-related feature. 24 * Starts initialization of cast-related feature.
25 */ 25 */
26 function initialize() { 26 function initialize() {
27 if (window.loadMockCastExtensionForTest) { 27 if (window.loadMockCastExtensionForTest) {
28 // If the test flag is set, the mock extension for test will be laoded by 28 // If the test flag is set, the mock extension for test will be laoded by
29 // the test script. Sets the handler to wait for loading. 29 // the test script. Sets the handler to wait for loading.
30 onLoadCastExtension(initializeApi); 30 onLoadCastSDK(initializeApi);
31 return; 31 return;
32 } 32 }
33 33
34 CastExtensionDiscoverer.findInstalledExtension(function(foundId) { 34 CastExtensionDiscoverer.findInstalledExtension(function(foundId) {
35 if (foundId) { 35 if (foundId) {
36 loadCastAPI(initializeApi); 36 loadCastSDK(foundId, initializeApi);
37 } else { 37 } else {
38 console.info('No Google Cast extension is installed.'); 38 console.info('No Google Cast extension is installed.');
39
40 metrics.recordCastAPIExtensionStatus(
41 metrics.CAST_API_EXTENSION_STATUS.SKIPPED);
42 } 39 }
43 }.wrap()); 40 }.wrap());
44 } 41 }
45 42
46 /** 43 /**
47 * Loads the cast API extention. If not install, the extension is installed 44 * Loads the Google Cast Sender SDK from the given cast extension.
48 * in background before load. The cast API will load the cast SDK automatically. 45 * The given callback is executes after the cast SDK is loaded.
49 * The given callback is executes after the cast SDK extension is initialized.
50 * 46 *
47 * @param {string} extensionId ID of the extension to be loaded.
51 * @param {function()} callback Callback (executed asynchronously). 48 * @param {function()} callback Callback (executed asynchronously).
52 * @param {boolean=} opt_secondTry Specify true if it's the second call after
53 * installation of Cast API extension.
54 */ 49 */
55 function loadCastAPI(callback, opt_secondTry) { 50 function loadCastSDK(extensionId, callback) {
56 var script = document.createElement('script'); 51 var script = document.createElement('script');
57 52
58 var onError = function() { 53 var onError = function() {
59 script.removeEventListener('error', onError); 54 script.removeEventListener('error', onError);
60 document.body.removeChild(script); 55 document.body.removeChild(script);
61 56 console.error('Google Cast extension load failed.');
62 if (opt_secondTry) {
63 metrics.recordCastAPIExtensionStatus(
64 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
65
66 // Shows error message and exits if it's the 2nd try.
67 console.error('Google Cast API extension load failed.');
68 return;
69 }
70
71 // Installs the Google Cast API extension and retry loading.
72 chrome.webstoreWidgetPrivate.installWebstoreItem(
73 'mafeflapfdfljijmlienjedomfjfmhpd',
74 true, // Don't use installation prompt.
75 function() {
76 if (chrome.runtime.lastError) {
77 metrics.recordCastAPIExtensionStatus(
78 metrics.CAST_API_EXTENSION_STATUS.INSTALLATION_FAILED);
79
80 console.error('Google Cast API extension installation error.',
81 chrome.runtime.lastError.message);
82 return;
83 }
84
85 console.info('Google Cast API extension installed.');
86
87 // Loads API again.
88 setTimeout(loadCastAPI.bind(null, callback, true), 0);
89 }.wrap());
90 }.wrap(); 57 }.wrap();
91 58
92 // Trys to load the cast API extention which is defined in manifest.json. 59 // Load the Cast Sender SDK provided by the given Cast extension.
93 script.src = '_modules/mafeflapfdfljijmlienjedomfjfmhpd/cast_sender.js'; 60 // Legacy Cast extension relies on the extension ID being set by bootstrap
61 // code, so set the ID here.
62 window.chrome['cast'] = window.chrome['cast'] || {};
63 window.chrome['cast']['extensionId'] = extensionId;
64 script.src = 'chrome-extension://' + extensionId + '/cast_sender.js';
94 script.addEventListener('error', onError); 65 script.addEventListener('error', onError);
95 script.addEventListener( 66 script.addEventListener('load', onLoadCastSDK.bind(null, callback));
96 'load', onLoadCastExtension.bind(null, callback, opt_secondTry));
97 document.body.appendChild(script); 67 document.body.appendChild(script);
98 } 68 }
99 69
100 /** 70 /**
101 * Loads the cast sdk extension. 71 * Handles load event of Cast SDK and make sure the Cast API is available.
102 * @param {function()} callback Callback (executed asynchronously). 72 * @param {function()} callback Callback which is called when the Caset Sender
103 * @param {boolean=} opt_installationOccured True if the extension is just 73 * API is ready for use.
104 * installed in this window. False or null if it's already installed.
105 */ 74 */
106 function onLoadCastExtension(callback, opt_installationOccured) { 75 function onLoadCastSDK(callback) {
107 var executeCallback = function() { 76 var executeCallback = function() {
108 if (opt_installationOccured) {
109 metrics.recordCastAPIExtensionStatus(
110 metrics.CAST_API_EXTENSION_STATUS.INSTALLED_AND_LOADED);
111 } else {
112 metrics.recordCastAPIExtensionStatus(
113 metrics.CAST_API_EXTENSION_STATUS.LOADED);
114 }
115
116 setTimeout(callback, 0); // Runs asynchronously. 77 setTimeout(callback, 0); // Runs asynchronously.
117 }; 78 };
118 79
119 if(!chrome.cast || !chrome.cast.isAvailable) { 80 if(!chrome.cast || !chrome.cast.isAvailable) {
120 var checkTimer = setTimeout(function() { 81 var checkTimer = setTimeout(function() {
121 console.error('Either "Google Cast API" or "Google Cast" extension ' + 82 console.error('Either "Google Cast API" or "Google Cast" extension ' +
122 'seems not to be installed?'); 83 'seems not to be installed?');
123
124 metrics.recordCastAPIExtensionStatus(
125 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
126 }.wrap(), 5000); 84 }.wrap(), 5000);
127 85
128 window['__onGCastApiAvailable'] = function(loaded, errorInfo) { 86 window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
129 clearTimeout(checkTimer); 87 clearTimeout(checkTimer);
130 88
131 if (loaded) { 89 if (loaded) {
132 executeCallback(); 90 executeCallback();
133 } else { 91 } else {
134 metrics.recordCastAPIExtensionStatus(
135 metrics.CAST_API_EXTENSION_STATUS.LOAD_FAILED);
136
137 console.error('Google Cast extension load failed.', errorInfo); 92 console.error('Google Cast extension load failed.', errorInfo);
138 } 93 }
139 }.wrap(); 94 }.wrap();
140 } else { 95 } else {
141 // Just executes the callback since the API is already loaded. 96 // Just executes the callback since the API is already loaded.
142 executeCallback(); 97 executeCallback();
143 } 98 }
144 } 99 }
145 100
146 /** 101 /**
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 metrics.recordNumberOfCastDevices(receivers.length); 141 metrics.recordNumberOfCastDevices(receivers.length);
187 player.setCastList(receivers); 142 player.setCastList(receivers);
188 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) { 143 } else if (availability == chrome.cast.ReceiverAvailability.UNAVAILABLE) {
189 metrics.recordNumberOfCastDevices(0); 144 metrics.recordNumberOfCastDevices(0);
190 player.setCastList([]); 145 player.setCastList([]);
191 } else { 146 } else {
192 console.error('Unexpected response in onReceiver.', arguments); 147 console.error('Unexpected response in onReceiver.', arguments);
193 player.setCastList([]); 148 player.setCastList([]);
194 } 149 }
195 } 150 }
OLDNEW
« no previous file with comments | « no previous file | ui/file_manager/video_player/js/video_player_metrics.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698