summaryrefslogtreecommitdiffstats
path: root/apps/shell/renderer/shell_custom_bindings.js
blob: 5fe9a9ae164e7a6607cc4be79ba4347580c5c1a2 (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
// 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.

var shellNatives = requireNative('shell_natives');
var Binding = require('binding').Binding;
var forEach = require('utils').forEach;
var renderViewObserverNatives = requireNative('renderViewObserverNatives');

var currentAppWindow = null;

var shell = Binding.create('shell');
shell.registerCustomHook(function(bindingsAPI) {
  var apiFunctions = bindingsAPI.apiFunctions;

  apiFunctions.setCustomCallback('createWindow',
                                 function(name, request, windowParams) {
    var view = null;

    // When window creation fails, |windowParams| will be undefined.
    if (windowParams && windowParams.viewId) {
      view = shellNatives.GetView(windowParams.viewId);
    }

    if (!view) {
      // No route to created window. If given a callback, trigger it with an
      // undefined object.
      if (request.callback) {
        request.callback(undefined);
        delete request.callback;
      }
      return;
    }

    // Initialize the app window in the newly created JS context
    view.chrome.shell.initializeAppWindow();

    var callback = request.callback;
    if (callback) {
      delete request.callback;

      var willCallback =
          renderViewObserverNatives.OnDocumentElementCreated(
              windowParams.viewId,
              function(success) {
                if (success) {
                  callback(view.chrome.shell.currentWindow());
                } else {
                  callback(undefined);
                }
              });
      if (!willCallback) {
        callback(undefined);
      }
    }
  });

  apiFunctions.setHandleRequest('currentWindow', function() {
    if (!currentAppWindow) {
      console.error(
          'The JavaScript context calling chrome.shell.currentWindow() has' +
          ' no associated AppWindow.');
      return null;
    }
    return currentAppWindow;
  });

  // This is an internal function, but needs to be bound into a closure
  // so the correct JS context is used for global variables such as
  // currentAppWindow.
  apiFunctions.setHandleRequest('initializeAppWindow', function() {
    var AppWindow = function() {};
    AppWindow.prototype.contentWindow = window;
    currentAppWindow = new AppWindow;
  });
});

exports.binding = shell.generate();