Home | History | Annotate | Download | only in hangout_services
      1 // Copyright 2013 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 chrome.runtime.onStartup.addListener(
      6     function() {
      7   chrome.alarms.create('hangout_services_fxpre', {'delayInMinutes': 2});
      8 });
      9 
     10 chrome.alarms.onAlarm.addListener(function(alarm) {
     11   if (!alarm || alarm.name != 'hangout_services_fxpre')
     12     return;
     13 
     14   var e = document.createElement('iframe');
     15   e.src = 'https://plus.google.com/hangouts/_/fxpre';
     16   document.body.appendChild(e);
     17 });
     18 
     19 chrome.runtime.onMessageExternal.addListener(
     20     function(message, sender, sendResponse) {
     21   function doSendResponse(value, error) {
     22     var errorMessage = error || chrome.extension.lastError;
     23     sendResponse({'value': value, 'error': errorMessage});
     24   }
     25 
     26   function getHost(url) {
     27     if (!url)
     28       return '';
     29     // Use the DOM to parse the URL. Since we don't add the anchor to
     30     // the page, this is the only reference to it and it will be
     31     // deleted once it's gone out of scope.
     32     var a = document.createElement('a');
     33     a.href = url;
     34     var origin = a.protocol + '//' + a.hostname;
     35     if (a.port != '')
     36       origin = origin + ':' + a.port;
     37     origin = origin + '/';
     38     return origin;
     39   }
     40 
     41   try {
     42     var method = message['method'];
     43     var origin = getHost(sender.url);
     44     if (method == 'chooseDesktopMedia') {
     45       var cancelId;
     46       function sendResponseWithCancelId(streamId) {
     47         var value = {'cancelId': cancelId,
     48                      'streamId': streamId};
     49         doSendResponse(value);
     50       }
     51       cancelId = chrome.desktopCapture.chooseDesktopMedia(
     52           ['screen', 'window'], sender.tab, sendResponseWithCancelId);
     53       return true;
     54     } else if (method == 'cancelChooseDesktopMedia') {
     55       var cancelId = message['cancelId'];
     56       chrome.desktopCapture.cancelChooseDesktopMedia(cancelId);
     57       doSendResponse();
     58       return false;
     59     } else if (method == 'cpu.getInfo') {
     60       chrome.system.cpu.getInfo(doSendResponse);
     61       return true;
     62     } else if (method == 'logging.setMetadata') {
     63       var metaData = message['metaData'];
     64       chrome.webrtcLoggingPrivate.setMetaData(
     65           sender.tab.id, origin, metaData, doSendResponse);
     66       return true;
     67     } else if (method == 'logging.start') {
     68       chrome.webrtcLoggingPrivate.start(sender.tab.id, origin, doSendResponse);
     69       return true;
     70     } else if (method == 'logging.uploadOnRenderClose') {
     71       chrome.webrtcLoggingPrivate.setUploadOnRenderClose(
     72           sender.tab.id, origin, true);
     73       doSendResponse();
     74       return false;
     75     } else if (method == 'logging.noUploadOnRenderClose') {
     76       chrome.webrtcLoggingPrivate.setUploadOnRenderClose(
     77           sender.tab.id, origin, false);
     78       doSendResponse();
     79       return false;
     80     } else if (method == 'logging.stop') {
     81       chrome.webrtcLoggingPrivate.stop(sender.tab.id, origin, doSendResponse);
     82       return true;
     83     } else if (method == 'logging.upload') {
     84       chrome.webrtcLoggingPrivate.upload(sender.tab.id, origin, doSendResponse);
     85       return true;
     86     } else if (method == 'logging.stopAndUpload') {
     87       chrome.webrtcLoggingPrivate.stop(
     88           sender.tab.id, origin, function() {
     89               chrome.webrtcLoggingPrivate.upload(
     90                   sender.tab.id, origin, doSendResponse);
     91           });
     92       return true;
     93     } else if (method == 'logging.discard') {
     94       chrome.webrtcLoggingPrivate.discard(
     95           sender.tab.id, origin, doSendResponse);
     96       return true;
     97     } else if (method == 'getSinks') {
     98       chrome.webrtcAudioPrivate.getSinks(doSendResponse);
     99       return true;
    100     } else if (method == 'getActiveSink') {
    101       chrome.webrtcAudioPrivate.getActiveSink(sender.tab.id, doSendResponse);
    102       return true;
    103     } else if (method == 'setActiveSink') {
    104       var sinkId = message['sinkId'];
    105       chrome.webrtcAudioPrivate.setActiveSink(
    106           sender.tab.id, sinkId, doSendResponse);
    107       return true;
    108     } else if (method == 'getAssociatedSink') {
    109       var sourceId = message['sourceId'];
    110       chrome.webrtcAudioPrivate.getAssociatedSink(
    111           origin, sourceId, doSendResponse);
    112       return true;
    113     } else if (method == 'isExtensionEnabled') {
    114       // This method is necessary because there may be more than one
    115       // version of this extension, under different extension IDs. By
    116       // first calling this method on the extension ID, the client can
    117       // check if it's loaded; if it's not, the extension system will
    118       // call the callback with no arguments and set
    119       // chrome.runtime.lastError.
    120       doSendResponse();
    121       return false;
    122     }
    123     throw new Error('Unknown method: ' + method);
    124   } catch (e) {
    125     doSendResponse(null, e.name + ': ' + e.message);
    126   }
    127 });
    128 
    129 // If Hangouts connects with a port named 'onSinksChangedListener', we
    130 // will register a listener and send it a message {'eventName':
    131 // 'onSinksChanged'} whenever the event fires.
    132 chrome.runtime.onConnectExternal.addListener(
    133     function(port) {
    134   if (port.name == 'onSinksChangedListener') {
    135     function clientListener() {
    136       port.postMessage({'eventName': 'onSinksChanged'});
    137     }
    138     chrome.webrtcAudioPrivate.onSinksChanged.addListener(clientListener);
    139 
    140     port.onDisconnect(function() {
    141         chrome.webrtcAudioPrivate.onSinksChanged.removeListener(
    142             clientListener);
    143       });
    144   }
    145 });
    146