summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/background/background.js
blob: 28c20105185d94023b38e20f430d55109a010f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** @suppress {duplicate} */
var remoting = remoting || {};

(function(){

/** @return {boolean} */
function isAppsV2() {
  var manifest = chrome.runtime.getManifest();
  if (manifest && manifest.app && manifest.app.background) {
    return true;
  }
  return false;
}

/** @param {remoting.AppLauncher} appLauncher */
function initializeAppV2(appLauncher) {
  /** @type {string} */
  var kNewWindowId = 'new-window';

  /** @param {OnClickData} info */
  function onContextMenu(info) {
    if (info.menuItemId == kNewWindowId) {
      appLauncher.launch();
    }
  }

  function initializeContextMenu() {
    chrome.contextMenus.create({
       id: kNewWindowId,
       contexts: ['launcher'],
       title: chrome.i18n.getMessage(/*i18n-content*/'NEW_WINDOW')
    });
    chrome.contextMenus.onClicked.addListener(onContextMenu);
  }

  initializeContextMenu();
  chrome.app.runtime.onLaunched.addListener(
      appLauncher.launch.bind(appLauncher)
  );
}

/**
 * The background service is responsible for listening to incoming connection
 * requests from Hangouts and the webapp.
 *
 * @param {remoting.AppLauncher} appLauncher
 */
function initializeBackgroundService(appLauncher) {
  function initializeIt2MeService() {
    /** @type {remoting.It2MeService} */
    remoting.it2meService = new remoting.It2MeService(appLauncher);
    remoting.it2meService.init();
  }

  chrome.runtime.onSuspend.addListener(function() {
    base.debug.assert(remoting.it2meService != null);
    remoting.it2meService.dispose();
    remoting.it2meService = null;
  });

  chrome.runtime.onSuspendCanceled.addListener(initializeIt2MeService);
  initializeIt2MeService();
}

function main() {
  /** @type {remoting.AppLauncher} */
  var appLauncher = new remoting.V1AppLauncher();
  if (isAppsV2()) {
    appLauncher = new remoting.V2AppLauncher();
    initializeAppV2(appLauncher);
  }
  initializeBackgroundService(appLauncher);
}

window.addEventListener('load', main, false);

}());