1 // Copyright 2014 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.
5 var shellNatives = requireNative('shell_natives');
6 var Binding = require('binding').Binding;
7 var forEach = require('utils').forEach;
8 var renderViewObserverNatives = requireNative('renderViewObserverNatives');
10 var currentAppWindow = null;
12 var shell = Binding.create('shell');
13 shell.registerCustomHook(function(bindingsAPI) {
14 var apiFunctions = bindingsAPI.apiFunctions;
16 apiFunctions.setCustomCallback('createWindow',
17 function(name, request, windowParams) {
20 // When window creation fails, |windowParams| will be undefined.
21 if (windowParams && windowParams.viewId) {
22 view = shellNatives.GetView(windowParams.viewId);
26 // No route to created window. If given a callback, trigger it with an
28 if (request.callback) {
29 request.callback(undefined);
30 delete request.callback;
35 // Initialize the app window in the newly created JS context
36 view.chrome.shell.initializeAppWindow();
38 var callback = request.callback;
40 delete request.callback;
43 renderViewObserverNatives.OnDocumentElementCreated(
47 callback(view.chrome.shell.currentWindow());
58 apiFunctions.setHandleRequest('currentWindow', function() {
59 if (!currentAppWindow) {
61 'The JavaScript context calling chrome.shell.currentWindow() has' +
62 ' no associated AppWindow.');
65 return currentAppWindow;
68 // This is an internal function, but needs to be bound into a closure
69 // so the correct JS context is used for global variables such as
71 apiFunctions.setHandleRequest('initializeAppWindow', function() {
72 var AppWindow = function() {};
73 AppWindow.prototype.contentWindow = window;
74 currentAppWindow = new AppWindow;
78 exports.binding = shell.generate();