Added unit test for DevTools' ephemeral port support.
[chromium-blink-merge.git] / apps / shell / renderer / shell_custom_bindings.js
blob5fe9a9ae164e7a6607cc4be79ba4347580c5c1a2
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) {
18 var view = null;
20 // When window creation fails, |windowParams| will be undefined.
21 if (windowParams && windowParams.viewId) {
22 view = shellNatives.GetView(windowParams.viewId);
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;
32 return;
35 // Initialize the app window in the newly created JS context
36 view.chrome.shell.initializeAppWindow();
38 var callback = request.callback;
39 if (callback) {
40 delete request.callback;
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);
51 });
52 if (!willCallback) {
53 callback(undefined);
56 });
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;
65 return currentAppWindow;
66 });
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 });
78 exports.binding = shell.generate();