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. 4 5var shellNatives = requireNative('shell_natives'); 6var Binding = require('binding').Binding; 7var forEach = require('utils').forEach; 8var renderViewObserverNatives = requireNative('renderViewObserverNatives'); 9 10var currentAppWindow = null; 11 12var shell = Binding.create('shell'); 13shell.registerCustomHook(function(bindingsAPI) { 14 var apiFunctions = bindingsAPI.apiFunctions; 15 16 apiFunctions.setCustomCallback('createWindow', 17 function(name, request, windowParams) { 18 var view = null; 19 20 // When window creation fails, |windowParams| will be undefined. 21 if (windowParams && windowParams.viewId) { 22 view = shellNatives.GetView(windowParams.viewId); 23 } 24 25 if (!view) { 26 // No route to created window. If given a callback, trigger it with an 27 // undefined object. 28 if (request.callback) { 29 request.callback(undefined); 30 delete request.callback; 31 } 32 return; 33 } 34 35 // Initialize the app window in the newly created JS context 36 view.chrome.shell.initializeAppWindow(); 37 38 var callback = request.callback; 39 if (callback) { 40 delete request.callback; 41 42 var willCallback = 43 renderViewObserverNatives.OnDocumentElementCreated( 44 windowParams.viewId, 45 function(success) { 46 if (success) { 47 callback(view.chrome.shell.currentWindow()); 48 } else { 49 callback(undefined); 50 } 51 }); 52 if (!willCallback) { 53 callback(undefined); 54 } 55 } 56 }); 57 58 apiFunctions.setHandleRequest('currentWindow', function() { 59 if (!currentAppWindow) { 60 console.error( 61 'The JavaScript context calling chrome.shell.currentWindow() has' + 62 ' no associated AppWindow.'); 63 return null; 64 } 65 return currentAppWindow; 66 }); 67 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 70 // currentAppWindow. 71 apiFunctions.setHandleRequest('initializeAppWindow', function() { 72 var AppWindow = function() {}; 73 AppWindow.prototype.contentWindow = window; 74 currentAppWindow = new AppWindow; 75 }); 76}); 77 78exports.binding = shell.generate(); 79