| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Custom bindings for the app_window API. | 5 // Custom bindings for the app_window API. |
| 6 | 6 |
| 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 7 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 8 var sendRequest = require('sendRequest').sendRequest; | 8 var sendRequest = require('sendRequest').sendRequest; |
| 9 var appWindowNatives = requireNative('app_window'); | 9 var appWindowNatives = requireNative('app_window'); |
| 10 var forEach = require('utils').forEach; | 10 var forEach = require('utils').forEach; |
| 11 var GetView = appWindowNatives.GetView; | 11 var GetView = appWindowNatives.GetView; |
| 12 | 12 |
| 13 chromeHidden.registerCustomHook('app.window', function(bindingsAPI) { | 13 chromeHidden.registerCustomHook('app.window', function(bindingsAPI) { |
| 14 var apiFunctions = bindingsAPI.apiFunctions; | 14 var apiFunctions = bindingsAPI.apiFunctions; |
| 15 apiFunctions.setCustomCallback('create', function(name, request, result) { | 15 apiFunctions.setCustomCallback('create', |
| 16 var view = null; | 16 function(name, request, windowParams) { |
| 17 if (result.viewId) { | 17 if (!windowParams.viewId) { |
| 18 view = GetView(result.viewId, result.injectTitlebar); | 18 // Create failed? If given a callback, trigger it with an undefined |
| 19 // object. |
| 20 if (request.callback) { |
| 21 request.callback() |
| 22 delete request.callback; |
| 23 } |
| 24 return; |
| 19 } | 25 } |
| 26 |
| 27 var view = GetView(windowParams.viewId, windowParams.injectTitlebar); |
| 28 |
| 29 // Initialize appWindowData in the newly created JS context |
| 30 view.chrome.app.window.initializeAppWindow(windowParams); |
| 31 |
| 20 if (request.callback) { | 32 if (request.callback) { |
| 21 request.callback(view.chrome.app.window.current()); | 33 request.callback(view.chrome.app.window.current()); |
| 22 delete request.callback; | 34 delete request.callback; |
| 23 } | 35 } |
| 24 }) | |
| 25 var AppWindow = function() {}; | |
| 26 forEach(chromeHidden.internalAPIs.app.currentWindowInternal, function(fn) { | |
| 27 AppWindow.prototype[fn] = | |
| 28 chromeHidden.internalAPIs.app.currentWindowInternal[fn]; | |
| 29 }); | 36 }); |
| 30 AppWindow.prototype.moveTo = window.moveTo.bind(window); | |
| 31 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); | |
| 32 AppWindow.prototype.contentWindow = window; | |
| 33 | 37 |
| 34 apiFunctions.setHandleRequest('current', function() { | 38 apiFunctions.setHandleRequest('current', function() { |
| 35 return new AppWindow; | 39 if (!chromeHidden.currentAppWindow) { |
| 36 }) | 40 console.error('chrome.app.window.current() is null -- window not ' + |
| 41 'created with chrome.app.window.create()'); |
| 42 return null; |
| 43 } |
| 44 return chromeHidden.currentAppWindow; |
| 45 }); |
| 46 |
| 47 // This is an internal function, but needs to be bound with setHandleRequest |
| 48 // because it is called from a different JS context |
| 49 apiFunctions.setHandleRequest('initializeAppWindow', function(params) { |
| 50 var AppWindow = function() {}; |
| 51 forEach(chromeHidden.internalAPIs.app.currentWindowInternal, function(fn) { |
| 52 AppWindow.prototype[fn] = |
| 53 chromeHidden.internalAPIs.app.currentWindowInternal[fn]; |
| 54 }); |
| 55 AppWindow.prototype.moveTo = window.moveTo.bind(window); |
| 56 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); |
| 57 AppWindow.prototype.contentWindow = window; |
| 58 |
| 59 Object.defineProperty(AppWindow.prototype, 'id', {get: function() { |
| 60 return chromeHidden.appWindowData.id; |
| 61 }}); |
| 62 |
| 63 chromeHidden.appWindowData = { |
| 64 id: params.id || '' |
| 65 }; |
| 66 chromeHidden.currentAppWindow = new AppWindow; |
| 67 }); |
| 37 }); | 68 }); |
| OLD | NEW |