summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/app_remoting/js/ar_background.js
blob: 4ee250768d2f5b94ed85a6213fa8b2ccb644cba0 (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
// 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.

/** @type {chrome.app.window.AppWindow} */
var mainWindow = null;

/**
 * The main window cannot delete its context menu entries on close because it
 * is being torn down at that point and doesn't have access to the necessary
 * APIs. Instead, it notifies the background page of the entries it creates,
 * and the background pages deletes them when the window is closed.
 *
 * @type {!Object}
 */
var contextMenuIds = {};

/** @param {chrome.app.runtime.LaunchData=} opt_launchData */
function createWindow(opt_launchData) {
  // If there is already a window, give it focus.
  if (mainWindow) {
    mainWindow.focus();
    return;
  }

  var typed_screen = /** @type {{availWidth: number, availHeight: number}} */
      (screen);

  var windowAttributes = {
    resizable: false,
    frame: remoting.platformIsMac() ? 'chrome' : 'none',
    bounds: {
      width: typed_screen.availWidth,
      height: typed_screen.availHeight,
      left: undefined,
      top: undefined
    }
  };

  function onClosed() {
    mainWindow = null;
    var ids = Object.keys(contextMenuIds);
    for (var i = 0; i < ids.length; ++i) {
      chrome.contextMenus.remove(ids[i]);
    }
    contextMenuIds = {};
  };

  /** @param {chrome.app.window.AppWindow} appWindow */
  function onCreate(appWindow) {
    // Set the global window.
    mainWindow = appWindow;

    // Clean up the windows sub-menu when the application quits.
    appWindow.onClosed.addListener(onClosed);
  };

  chrome.app.window.create('main.html', windowAttributes, onCreate);
};

/** @param {Event} event */
function onWindowMessage(event) {
  var method = /** @type {string} */ (event.data['method']);
  var id = /** @type {string} */ (event.data['id']);
  switch (method) {
    case 'addContextMenuId':
      contextMenuIds[id] = true;
      break;
    case 'removeContextMenuId':
      delete contextMenuIds[id];
      break;
  }
};

chrome.app.runtime.onLaunched.addListener(createWindow);
window.addEventListener('message', onWindowMessage, false);