| 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 binding for the app_window API. | 5 // Custom binding for the app_window API. |
| 6 | 6 |
| 7 var appWindowNatives = requireNative('app_window_natives'); |
| 7 var Binding = require('binding').Binding; | 8 var Binding = require('binding').Binding; |
| 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 9 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 9 var chrome = requireNative('chrome').GetChrome(); | 10 var chrome = requireNative('chrome').GetChrome(); |
| 11 var Event = require('event_bindings').Event; |
| 12 var forEach = require('utils').forEach; |
| 10 var sendRequest = require('sendRequest').sendRequest; | 13 var sendRequest = require('sendRequest').sendRequest; |
| 11 var appWindowNatives = requireNative('app_window'); | |
| 12 var forEach = require('utils').forEach; | |
| 13 var GetView = appWindowNatives.GetView; | |
| 14 var OnContextReady = appWindowNatives.OnContextReady; | |
| 15 | 14 |
| 16 var appWindow = Binding.create('app.window'); | 15 var appWindow = Binding.create('app.window'); |
| 17 appWindow.registerCustomHook(function(bindingsAPI) { | 16 appWindow.registerCustomHook(function(bindingsAPI) { |
| 18 var apiFunctions = bindingsAPI.apiFunctions; | 17 var apiFunctions = bindingsAPI.apiFunctions; |
| 19 | 18 |
| 20 apiFunctions.setCustomCallback('create', | 19 apiFunctions.setCustomCallback('create', |
| 21 function(name, request, windowParams) { | 20 function(name, request, windowParams) { |
| 22 var view = null; | 21 var view = null; |
| 23 if (windowParams.viewId) | 22 if (windowParams.viewId) { |
| 24 view = GetView(windowParams.viewId, windowParams.injectTitlebar); | 23 view = appWindowNatives.GetView( |
| 24 windowParams.viewId, windowParams.injectTitlebar); |
| 25 } |
| 25 | 26 |
| 26 if (!view) { | 27 if (!view) { |
| 27 // No route to created window. If given a callback, trigger it with an | 28 // No route to created window. If given a callback, trigger it with an |
| 28 // undefined object. | 29 // undefined object. |
| 29 if (request.callback) { | 30 if (request.callback) { |
| 30 request.callback(); | 31 request.callback(); |
| 31 delete request.callback; | 32 delete request.callback; |
| 32 } | 33 } |
| 33 return; | 34 return; |
| 34 } | 35 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 47 view.chrome.app.window.initializeAppWindow(windowParams); | 48 view.chrome.app.window.initializeAppWindow(windowParams); |
| 48 | 49 |
| 49 var callback = request.callback; | 50 var callback = request.callback; |
| 50 if (callback) { | 51 if (callback) { |
| 51 delete request.callback; | 52 delete request.callback; |
| 52 if (!view) { | 53 if (!view) { |
| 53 callback(undefined); | 54 callback(undefined); |
| 54 return; | 55 return; |
| 55 } | 56 } |
| 56 | 57 |
| 57 var willCallback = OnContextReady(windowParams.viewId, function(success) { | 58 var willCallback = appWindowNatives.OnContextReady(windowParams.viewId, |
| 59 function(success) { |
| 58 if (success) { | 60 if (success) { |
| 59 callback(view.chrome.app.window.current()); | 61 callback(view.chrome.app.window.current()); |
| 60 } else { | 62 } else { |
| 61 callback(undefined); | 63 callback(undefined); |
| 62 } | 64 } |
| 63 }); | 65 }); |
| 64 if (!willCallback) { | 66 if (!willCallback) { |
| 65 callback(undefined); | 67 callback(undefined); |
| 66 } | 68 } |
| 67 } | 69 } |
| 68 }); | 70 }); |
| 69 | 71 |
| 70 apiFunctions.setHandleRequest('current', function() { | 72 apiFunctions.setHandleRequest('current', function() { |
| 71 if (!chromeHidden.currentAppWindow) { | 73 if (!chromeHidden.currentAppWindow) { |
| 72 console.error('chrome.app.window.current() is null -- window not ' + | 74 console.error('chrome.app.window.current() is null -- window not ' + |
| 73 'created with chrome.app.window.create()'); | 75 'created with chrome.app.window.create()'); |
| 74 return null; | 76 return null; |
| 75 } | 77 } |
| 76 return chromeHidden.currentAppWindow; | 78 return chromeHidden.currentAppWindow; |
| 77 }); | 79 }); |
| 78 | 80 |
| 79 chromeHidden.OnAppWindowClosed = function() { | |
| 80 if (!chromeHidden.currentAppWindow) | |
| 81 return; | |
| 82 chromeHidden.currentAppWindow.onClosed.dispatch(); | |
| 83 }; | |
| 84 | |
| 85 // This is an internal function, but needs to be bound with setHandleRequest | 81 // This is an internal function, but needs to be bound with setHandleRequest |
| 86 // because it is called from a different JS context. | 82 // because it is called from a different JS context. |
| 87 apiFunctions.setHandleRequest('initializeAppWindow', function(params) { | 83 apiFunctions.setHandleRequest('initializeAppWindow', function(params) { |
| 88 var currentWindowInternal = | 84 var currentWindowInternal = |
| 89 Binding.create('app.currentWindowInternal').generate(); | 85 Binding.create('app.currentWindowInternal').generate(); |
| 90 var AppWindow = function() {}; | 86 var AppWindow = function() {}; |
| 91 forEach(currentWindowInternal, function(fn) { | 87 forEach(currentWindowInternal, function(fn) { |
| 92 AppWindow.prototype[fn] = | 88 AppWindow.prototype[fn] = |
| 93 currentWindowInternal[fn]; | 89 currentWindowInternal[fn]; |
| 94 }); | 90 }); |
| 95 AppWindow.prototype.moveTo = window.moveTo.bind(window); | 91 AppWindow.prototype.moveTo = window.moveTo.bind(window); |
| 96 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); | 92 AppWindow.prototype.resizeTo = window.resizeTo.bind(window); |
| 97 AppWindow.prototype.contentWindow = window; | 93 AppWindow.prototype.contentWindow = window; |
| 98 AppWindow.prototype.onClosed = new chrome.Event; | 94 AppWindow.prototype.onClosed = new Event(); |
| 99 AppWindow.prototype.close = function() { | 95 AppWindow.prototype.close = function() { |
| 100 this.contentWindow.close(); | 96 this.contentWindow.close(); |
| 101 }; | 97 }; |
| 102 AppWindow.prototype.getBounds = function() { | 98 AppWindow.prototype.getBounds = function() { |
| 103 var bounds = chromeHidden.appWindowData.bounds; | 99 var bounds = chromeHidden.appWindowData.bounds; |
| 104 return { left: bounds.left, top: bounds.top, | 100 return { left: bounds.left, top: bounds.top, |
| 105 width: bounds.width, height: bounds.height }; | 101 width: bounds.width, height: bounds.height }; |
| 106 }; | 102 }; |
| 107 AppWindow.prototype.isFullscreen = function() { | 103 AppWindow.prototype.isFullscreen = function() { |
| 108 return chromeHidden.appWindowData.fullscreen; | 104 return chromeHidden.appWindowData.fullscreen; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 130 }); | 126 }); |
| 131 }); | 127 }); |
| 132 | 128 |
| 133 function boundsEqual(bounds1, bounds2) { | 129 function boundsEqual(bounds1, bounds2) { |
| 134 if (!bounds1 || !bounds2) | 130 if (!bounds1 || !bounds2) |
| 135 return false; | 131 return false; |
| 136 return (bounds1.left == bounds2.left && bounds1.top == bounds2.top && | 132 return (bounds1.left == bounds2.left && bounds1.top == bounds2.top && |
| 137 bounds1.width == bounds2.width && bounds1.height == bounds2.height); | 133 bounds1.width == bounds2.width && bounds1.height == bounds2.height); |
| 138 } | 134 } |
| 139 | 135 |
| 140 chromeHidden.updateAppWindowProperties = function(update) { | 136 function updateAppWindowProperties(update) { |
| 141 if (!chromeHidden.appWindowData) | 137 if (!chromeHidden.appWindowData) |
| 142 return; | 138 return; |
| 143 var oldData = chromeHidden.appWindowData; | 139 var oldData = chromeHidden.appWindowData; |
| 144 update.id = oldData.id; | 140 update.id = oldData.id; |
| 145 chromeHidden.appWindowData = update; | 141 chromeHidden.appWindowData = update; |
| 146 | 142 |
| 147 var currentWindow = chromeHidden.currentAppWindow; | 143 var currentWindow = chromeHidden.currentAppWindow; |
| 148 | 144 |
| 149 if (!boundsEqual(oldData.bounds, update.bounds)) | 145 if (!boundsEqual(oldData.bounds, update.bounds)) |
| 150 currentWindow["onBoundsChanged"].dispatch(); | 146 currentWindow["onBoundsChanged"].dispatch(); |
| 151 | 147 |
| 152 if (!oldData.fullscreen && update.fullscreen) | 148 if (!oldData.fullscreen && update.fullscreen) |
| 153 currentWindow["onFullscreened"].dispatch(); | 149 currentWindow["onFullscreened"].dispatch(); |
| 154 if (!oldData.minimized && update.minimized) | 150 if (!oldData.minimized && update.minimized) |
| 155 currentWindow["onMinimized"].dispatch(); | 151 currentWindow["onMinimized"].dispatch(); |
| 156 if (!oldData.maximized && update.maximized) | 152 if (!oldData.maximized && update.maximized) |
| 157 currentWindow["onMaximized"].dispatch(); | 153 currentWindow["onMaximized"].dispatch(); |
| 158 | 154 |
| 159 if ((oldData.fullscreen && !update.fullscreen) || | 155 if ((oldData.fullscreen && !update.fullscreen) || |
| 160 (oldData.minimized && !update.minimized) || | 156 (oldData.minimized && !update.minimized) || |
| 161 (oldData.maximized && !update.maximized)) | 157 (oldData.maximized && !update.maximized)) |
| 162 currentWindow["onRestored"].dispatch(); | 158 currentWindow["onRestored"].dispatch(); |
| 163 }; | 159 }; |
| 164 | 160 |
| 161 // Called by c/r/e/extension_helper.cc. |
| 162 function onAppWindowClosed() { |
| 163 if (!chromeHidden.currentAppWindow) |
| 164 return; |
| 165 chromeHidden.currentAppWindow.onClosed.dispatch(); |
| 166 } |
| 167 |
| 165 exports.binding = appWindow.generate(); | 168 exports.binding = appWindow.generate(); |
| 169 exports.onAppWindowClosed = onAppWindowClosed; |
| 170 exports.updateAppWindowProperties = updateAppWindowProperties; |
| OLD | NEW |