Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 /** | |
| 6 * Defines the cast.ExtensionApi class. This class inherits cast.Api and must | |
| 7 * be decraled after the parent is loaded. So we introduce this method to | |
| 8 * decrale it with delay. | |
| 9 */ | |
| 10 function loadCastExtensionApi() { | |
| 11 if (!cast) { | |
| 12 console.error('"cast" namespace is not defined.'); | |
| 13 return; | |
| 14 } | |
| 15 | |
| 16 /** | |
| 17 * @constructor | |
| 18 * @param {string} castExtensionId Extension ID of cast extension. | |
| 19 * @extends {cast.Api} | |
| 20 */ | |
| 21 cast.ExtensionApi = function(castExtensionId) { | |
| 22 this.castExtensionId_ = castExtensionId; | |
| 23 cast.Client.clientId_ = chrome.runtime.id; | |
| 24 | |
| 25 cast.Api.call(this); | |
| 26 }; | |
| 27 | |
| 28 cast.ExtensionApi.prototype.__proto__ = cast.Api.prototype; | |
| 29 | |
| 30 /** | |
| 31 * @override | |
| 32 */ | |
| 33 cast.ExtensionApi.prototype.init = function() { | |
| 34 chrome.runtime.onMessageExternal.addListener( | |
| 35 this.onMessageExternal_.bind(this)); | |
| 36 this.sendRequest(cast.AppRequestType.REGISTER_CLIENT, {}); | |
| 37 cast.isAvailable = true; | |
| 38 }; | |
| 39 | |
| 40 /** | |
| 41 * @override | |
| 42 */ | |
| 43 cast.ExtensionApi.prototype.onDisconnect = function() {}; | |
| 44 | |
| 45 /** | |
| 46 * @override | |
| 47 */ | |
| 48 cast.ExtensionApi.prototype.sendRequest = function(requestType, request) { | |
| 49 if (!this.castExtensionId_) { | |
| 50 return null; | |
|
fukino
2014/07/11 04:35:51
nit: Chromium coding style says "Do not use braces
| |
| 51 } | |
| 52 | |
| 53 var appRequest = new cast.Message(cast.Client.getClientId(), | |
| 54 cast.NAME, cast.Client.getNextSeq(), requestType, request); | |
| 55 | |
| 56 // Use response callback for message ACK to detect extension unload. | |
| 57 var responseCallback = function() { | |
| 58 if (chrome.runtime.lastError) { | |
| 59 // Unregister the cast extension. | |
| 60 this.castExtensionId_ = ''; | |
| 61 cast.isAvailable = false; | |
| 62 this.onDisconnect(); | |
| 63 } | |
| 64 }.bind(this); | |
| 65 | |
| 66 chrome.runtime.sendMessage(this.castExtensionId_, | |
| 67 appRequest, | |
| 68 responseCallback); | |
| 69 return appRequest; | |
| 70 }; | |
| 71 | |
| 72 /** | |
| 73 * @param {string} message | |
| 74 * @param {function(boolean)} sender | |
| 75 * @param {function(boolean)} sendResponse | |
| 76 * @private | |
| 77 */ | |
| 78 cast.ExtensionApi.prototype.onMessageExternal_ = function(message, | |
| 79 sender, sendResponse) { | |
| 80 if (!this.castExtensionId_ || sender.id != this.castExtensionId_) { | |
| 81 return; | |
|
fukino
2014/07/11 04:35:50
ditto
| |
| 82 } | |
| 83 | |
| 84 if (!message) { | |
| 85 // No content. | |
| 86 return; | |
|
fukino
2014/07/11 04:35:50
ditto
| |
| 87 } | |
| 88 | |
| 89 this.processCastMessage(/** @type {cast.Message} */(message)); | |
| 90 }; | |
| 91 } | |
| OLD | NEW |