| 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 object |
| 19 if (request.callback) { |
| 20 request.callback() |
| 21 delete request.callback; |
| 22 } |
| 23 return; |
| 19 } | 24 } |
| 25 |
| 26 var view = GetView(windowParams.viewId, windowParams.injectTitlebar); |
| 27 |
| 28 // Call makeAppWindow in the newly created JS context |
| 29 var appWindow = view.chrome.app.window.makeAppWindow(windowParams); |
| 30 |
| 20 if (request.callback) { | 31 if (request.callback) { |
| 21 request.callback(view.chrome.app.window.current()); | 32 request.callback(appWindow); |
| 22 delete request.callback; | 33 delete request.callback; |
| 23 } | 34 } |
| 24 }) | |
| 25 var AppWindow = function() {}; | |
| 26 forEach(chromeHidden.internalAPIs.app.currentWindowInternal, function(fn) { | |
| 27 AppWindow.prototype[fn] = | |
| 28 chromeHidden.internalAPIs.app.currentWindowInternal[fn]; | |
| 29 }); | 35 }); |
| 30 AppWindow.prototype.moveTo = window.moveTo.bind(window); | 36 |
| 31 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); | |
| 32 AppWindow.prototype.contentWindow = window; | |
| 33 // So as not to break apps that use .dom. http://crbug.com/147668 | |
| 34 // TODO(jeremya): remove this once M23 has branched. | |
| 35 AppWindow.prototype.dom = window; | |
| 36 apiFunctions.setHandleRequest('current', function() { | 37 apiFunctions.setHandleRequest('current', function() { |
| 37 return new AppWindow; | 38 if (!chromeHidden.currentAppWindow) { |
| 38 }) | 39 console.error('chrome.app.window.current() is null -- window not ' + |
| 40 'created with chrome.app.window.create()'); |
| 41 return null; |
| 42 } |
| 43 return chromeHidden.currentAppWindow; |
| 44 }); |
| 45 |
| 46 // This is an internal function, but needs to be bound with setHandleRequest |
| 47 // because it is called from a different JS context |
| 48 apiFunctions.setHandleRequest('makeAppWindow', function(params) { |
| 49 var AppWindow = function() {}; |
| 50 forEach(chromeHidden.internalAPIs.app.currentWindowInternal, function(fn) { |
| 51 AppWindow.prototype[fn] = |
| 52 chromeHidden.internalAPIs.app.currentWindowInternal[fn]; |
| 53 }); |
| 54 AppWindow.prototype.moveTo = window.moveTo.bind(window); |
| 55 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); |
| 56 AppWindow.prototype.contentWindow = window; |
| 57 // So as not to break apps that use .dom. http://crbug.com/147668 |
| 58 // TODO(jeremya): remove this once M23 has branched. |
| 59 AppWindow.prototype.dom = window; |
| 60 |
| 61 Object.defineProperty(AppWindow.prototype, 'id', {get: function() { |
| 62 return chromeHidden.appWindowCache.id; |
| 63 }}); |
| 64 |
| 65 chromeHidden.appWindowCache = { |
| 66 id: params.id ? params.id : '' |
| 67 }; |
| 68 chromeHidden.currentAppWindow = new AppWindow; |
| 69 return chromeHidden.currentAppWindow; |
| 70 }); |
| 39 }); | 71 }); |
| OLD | NEW |