Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / file_manager / video_player / js / cast / cast_extension_discoverer.js
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
3 // found in the LICENSE file.
4
5 'use strict';
6
7 /**
8  * Discover the ID of installed cast extension.
9  * @constructor
10  */
11 function CastExtensionDiscoverer() {
12 }
13
14 /**
15  * Tentatice IDs to try.
16  * @type {Array.<string>}
17  * @const
18  */
19 CastExtensionDiscoverer.CAST_EXTENSION_IDS = [
20   'boadgeojelhgndaghljhdicfkmllpafd', // release
21   'dliochdbjfkdbacpmhlcpmleaejidimm', // beta
22   'hfaagokkkhdbgiakmmlclaapfelnkoah',
23   'fmfcbgogabcbclcofgocippekhfcmgfj',
24   'enhhojjnijigcajfphajepfemndkmdlo'
25 ];
26
27 /**
28  * @param {function(string)} callback Callback called with the extension ID. The
29  *     ID may be null if extension is not found.
30  */
31 CastExtensionDiscoverer.findInstalledExtension = function(callback) {
32   CastExtensionDiscoverer.findInstalledExtensionHelper_(0, callback);
33 };
34
35 /**
36  * @param {number} index Current index which is tried to check.
37  * @param {function(string)} callback Callback function which will be called
38  *     the extension is found.
39  * @private
40  */
41 CastExtensionDiscoverer.findInstalledExtensionHelper_ = function(index,
42     callback) {
43   if (index === CastExtensionDiscoverer.CAST_EXTENSION_IDS.length) {
44     // no extension found.
45     callback(null);
46     return;
47   }
48
49   CastExtensionDiscoverer.isExtensionInstalled_(
50       CastExtensionDiscoverer.CAST_EXTENSION_IDS[index],
51       function(installed) {
52         if (installed) {
53           callback(CastExtensionDiscoverer.CAST_EXTENSION_IDS[index]);
54         } else {
55           CastExtensionDiscoverer.findInstalledExtensionHelper_(index + 1,
56                                                                 callback);
57         }
58       });
59 };
60
61 /**
62  * The result will be notified on |callback|. True if installed, false not.
63  * @param {string} extensionId Id to be checked.
64  * @param {function(boolean)} callback Callback to notify the result.
65  * @private
66  */
67 CastExtensionDiscoverer.isExtensionInstalled_ =
68     function(extensionId, callback) {
69
70   var xhr = new XMLHttpRequest();
71   var url = 'chrome-extension://' + extensionId + '/cast_sender.js';
72   xhr.open('GET', url, true);
73   xhr.onerror = function() { callback(false); };
74   /** @param {*} event */
75   xhr.onreadystatechange = function(event) {
76     if (xhr.readyState == 4 && xhr.status === 200) {
77       // Cast extension found.
78       callback(true);
79     }
80   }.wrap(this);
81   xhr.send();
82 };