| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 var backgroundSetup = {} | 5 var backgroundSetup = {} |
| 6 | 6 |
| 7 // If all required activity information is available, then this will return an | 7 // If all required activity information is available, then this will return an |
| 8 // activity structure; otherwise, it will return null. This lets us | 8 // activity structure; otherwise, it will return null. This lets us |
| 9 // unconditionally assign to the 'activity' field in a JSON object. | 9 // unconditionally assign to the 'activity' field in a JSON object. |
| 10 tryCreateActivity_ = function(id, title, tabId) { | 10 tryCreateActivity_ = function(id, title, tabId) { |
| 11 if (id === undefined || title === undefined || tabId === undefined) | 11 if (id === undefined || title === undefined || tabId === undefined) |
| 12 return null; | 12 return null; |
| 13 | 13 |
| 14 return { | 14 return { |
| 15 'id': id, | 15 "id": id, |
| 16 'title': title, | 16 "title": title, |
| 17 'tabId': tabId | 17 "tabId": tabId |
| 18 }; | 18 }; |
| 19 } | 19 } |
| 20 | 20 |
| 21 var receiversActivities = []; | 21 var receiversActivities = []; |
| 22 var sendDevices = function() { | |
| 23 chrome.cast.devicesPrivate.updateDevices(receiversActivities); | |
| 24 } | |
| 25 chrome.cast.devicesPrivate.updateDevicesRequested.addListener(sendDevices); | |
| 26 | |
| 27 // Add a new receiver. |activityTitle| and |activityTabId| are optional | 22 // Add a new receiver. |activityTitle| and |activityTabId| are optional |
| 28 // parameters. | 23 // parameters. |
| 29 var addReceiver = function(id, receiverName, activityTitle, activityTabId) { | 24 addReceiver = function(id, receiverName, activityTitle, activityTabId) { |
| 30 receiversActivities.push({ | 25 receiversActivities.push({ |
| 31 'receiver': { | 26 "activity": tryCreateActivity_(id, activityTitle, activityTabId), |
| 32 'id': id, | 27 "receiver": { |
| 33 'name': receiverName | 28 "id": id, |
| 34 }, | 29 "name": receiverName |
| 35 'activity': tryCreateActivity_(id, activityTitle, activityTabId) | 30 } |
| 36 }); | 31 }); |
| 32 } |
| 33 // Required API method. |
| 34 getMirrorCapableReceiversAndActivities = function() { |
| 35 // For all of the API methods, we verify that |this| points to |
| 36 // backgroundSetup. In the actual extension, the API methods are |
| 37 // also free-standing but they are really class methods on backgroundSetup. |
| 38 if (this !== backgroundSetup) |
| 39 throw 'this !== backgroundSetup'; |
| 37 | 40 |
| 38 sendDevices(); | 41 return receiversActivities; |
| 39 } | 42 } |
| 40 | 43 |
| 44 var stopMirroringReason = ""; |
| 41 var stopMirroringCalled = false; | 45 var stopMirroringCalled = false; |
| 42 chrome.cast.devicesPrivate.stopCast.addListener(function(reason) { | 46 wasStopMirroringCalledWithUserStop = function() { |
| 43 if (reason !== 'user-stop') | 47 return stopMirroringCalled && stopMirroringReason == 'user-stop'; |
| 44 throw 'expected reason to be "user-stop"'; | 48 } |
| 49 // Required API method. |
| 50 stopMirroring = function(reason) { |
| 51 if (this !== backgroundSetup) |
| 52 throw 'this !== backgroundSetup'; |
| 53 |
| 54 stopMirroringReason = reason; |
| 55 stopMirroringCalled = true; |
| 45 | 56 |
| 46 var foundActivity = false; | 57 var foundActivity = false; |
| 47 for (item of receiversActivities) { | 58 for (item of receiversActivities) { |
| 48 if (item.activity != null) { | 59 if (item.activity != null) { |
| 49 item.activity = null; | 60 item.activity = null; |
| 50 foundActivity = true; | 61 foundActivity = true; |
| 51 } | 62 } |
| 52 } | 63 } |
| 53 if (foundActivity === false) | 64 if (foundActivity === false) |
| 54 throw 'stopMirroring called when there was nothing being mirrored' | 65 throw 'stopMirroring called when there was nothing being mirrored' |
| 66 } |
| 55 | 67 |
| 56 stopMirroringCalled = true; | 68 var launchTabId = 1; |
| 57 sendDevices(); | 69 var launchTabTitle = "Fake Cast"; |
| 58 }); | 70 var launchDesktopMirroringReceiverId = ""; |
| 71 getLaunchDesktopMirroringReceiverId = function() { |
| 72 return launchDesktopMirroringReceiverId; |
| 73 } |
| 74 // Required API method. |
| 75 launchDesktopMirroring = function(receiverId) { |
| 76 if (this !== backgroundSetup) |
| 77 throw 'this !== backgroundSetup'; |
| 59 | 78 |
| 60 | |
| 61 var launchDesktopMirroringReceiverId = ''; | |
| 62 chrome.cast.devicesPrivate.startCast.addListener(function(receiverId) { | |
| 63 launchDesktopMirroringReceiverId = receiverId; | 79 launchDesktopMirroringReceiverId = receiverId; |
| 64 | 80 |
| 65 var tabTitle = 'Tab Title'; | |
| 66 var tabId = 1; | |
| 67 | |
| 68 for (item of receiversActivities) { | 81 for (item of receiversActivities) { |
| 69 if (item.receiver.id == receiverId) { | 82 if (item.receiver.id == receiverId) { |
| 70 item.activity = tryCreateActivity_(receiverId, tabTitle, tabId); | 83 item.activity = |
| 84 tryCreateActivity_(receiverId, launchTabId, launchTabTitle); |
| 71 break; | 85 break; |
| 72 } | 86 } |
| 73 } | 87 } |
| 74 | 88 } |
| 75 sendDevices(); | |
| 76 }); | |
| OLD | NEW |