| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // This module implements a wrapper for a guestview that manages its | 5 // This module implements a wrapper for a guestview that manages its |
| 6 // creation, attaching, and destruction. | 6 // creation, attaching, and destruction. |
| 7 | 7 |
| 8 var EventBindings = require('event_bindings'); |
| 8 var GuestViewInternal = | 9 var GuestViewInternal = |
| 9 require('binding').Binding.create('guestViewInternal').generate(); | 10 require('binding').Binding.create('guestViewInternal').generate(); |
| 10 var GuestViewInternalNatives = requireNative('guest_view_internal'); | 11 var GuestViewInternalNatives = requireNative('guest_view_internal'); |
| 11 | 12 |
| 13 // Events. |
| 14 var CreateEvent = function(name) { |
| 15 var eventOpts = {supportsListeners: true, supportsFilters: true}; |
| 16 return new EventBindings.Event(name, undefined, eventOpts); |
| 17 }; |
| 18 var ResizeEvent = CreateEvent('guestViewInternal.onResize'); |
| 19 |
| 12 // Possible states. | 20 // Possible states. |
| 13 var GUEST_STATE_ATTACHED = 2; | 21 var GUEST_STATE_ATTACHED = 2; |
| 14 var GUEST_STATE_CREATED = 1; | 22 var GUEST_STATE_CREATED = 1; |
| 15 var GUEST_STATE_START = 0; | 23 var GUEST_STATE_START = 0; |
| 16 | 24 |
| 17 // Error messages. | 25 // Error messages. |
| 18 var ERROR_MSG_ALREADY_ATTACHED = 'The guest has already been attached.'; | 26 var ERROR_MSG_ALREADY_ATTACHED = 'The guest has already been attached.'; |
| 19 var ERROR_MSG_ALREADY_CREATED = 'The guest has already been created.'; | 27 var ERROR_MSG_ALREADY_CREATED = 'The guest has already been created.'; |
| 20 var ERROR_MSG_INVALID_STATE = 'The guest is in an invalid state.'; | 28 var ERROR_MSG_INVALID_STATE = 'The guest is in an invalid state.'; |
| 21 var ERROR_MSG_NOT_ATTACHED = 'The guest is not attached.'; | 29 var ERROR_MSG_NOT_ATTACHED = 'The guest is not attached.'; |
| 22 var ERROR_MSG_NOT_CREATED = 'The guest has not been created.'; | 30 var ERROR_MSG_NOT_CREATED = 'The guest has not been created.'; |
| 23 | 31 |
| 32 // Properties. |
| 33 var PROPERTY_ON_RESIZE = 'onResize'; |
| 34 |
| 24 // Contains and hides the internal implementation details of |GuestView|, | 35 // Contains and hides the internal implementation details of |GuestView|, |
| 25 // including maintaining its state and enforcing the proper usage of its API | 36 // including maintaining its state and enforcing the proper usage of its API |
| 26 // fucntions. | 37 // fucntions. |
| 27 | 38 function GuestViewImpl(guestView, viewType, guestInstanceId) { |
| 28 function GuestViewImpl(viewType, guestInstanceId) { | |
| 29 if (guestInstanceId) { | 39 if (guestInstanceId) { |
| 30 this.id = guestInstanceId; | 40 this.id = guestInstanceId; |
| 31 this.state = GUEST_STATE_CREATED; | 41 this.state = GUEST_STATE_CREATED; |
| 32 } else { | 42 } else { |
| 33 this.id = 0; | 43 this.id = 0; |
| 34 this.state = GUEST_STATE_START; | 44 this.state = GUEST_STATE_START; |
| 35 } | 45 } |
| 36 this.actionQueue = []; | 46 this.actionQueue = []; |
| 37 this.contentWindow = null; | 47 this.contentWindow = null; |
| 48 this.guestView = guestView; |
| 38 this.pendingAction = null; | 49 this.pendingAction = null; |
| 39 this.viewType = viewType; | 50 this.viewType = viewType; |
| 40 this.internalInstanceId = 0; | 51 this.internalInstanceId = 0; |
| 52 |
| 53 this.setupOnResize(); |
| 41 } | 54 } |
| 42 | 55 |
| 56 // Sets up the onResize property on the GuestView. |
| 57 GuestViewImpl.prototype.setupOnResize = function() { |
| 58 Object.defineProperty(this.guestView, PROPERTY_ON_RESIZE, { |
| 59 get: function() { |
| 60 return this[PROPERTY_ON_RESIZE]; |
| 61 }.bind(this), |
| 62 set: function(value) { |
| 63 this[PROPERTY_ON_RESIZE] = value; |
| 64 }.bind(this), |
| 65 enumerable: true |
| 66 }); |
| 67 |
| 68 this.callOnResize = function(e) { |
| 69 if (!this[PROPERTY_ON_RESIZE]) { |
| 70 return; |
| 71 } |
| 72 this[PROPERTY_ON_RESIZE](e.oldWidth, e.oldHeight, e.newWidth, e.newHeight); |
| 73 }.bind(this); |
| 74 }; |
| 75 |
| 43 // Callback wrapper that is used to call the callback of the pending action (if | 76 // Callback wrapper that is used to call the callback of the pending action (if |
| 44 // one exists), and then performs the next action in the queue. | 77 // one exists), and then performs the next action in the queue. |
| 45 GuestViewImpl.prototype.handleCallback = function(callback) { | 78 GuestViewImpl.prototype.handleCallback = function(callback) { |
| 46 if (callback) { | 79 if (callback) { |
| 47 callback(); | 80 callback(); |
| 48 } | 81 } |
| 49 this.pendingAction = null; | 82 this.pendingAction = null; |
| 50 this.performNextAction(); | 83 this.performNextAction(); |
| 51 }; | 84 }; |
| 52 | 85 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // createGuest() callback, handle potential creation failure, and advance the | 188 // createGuest() callback, handle potential creation failure, and advance the |
| 156 // queue. | 189 // queue. |
| 157 var callbackWrapper = function(callback, guestInstanceId) { | 190 var callbackWrapper = function(callback, guestInstanceId) { |
| 158 this.id = guestInstanceId; | 191 this.id = guestInstanceId; |
| 159 | 192 |
| 160 // Check if creation failed. | 193 // Check if creation failed. |
| 161 if (this.id === 0) { | 194 if (this.id === 0) { |
| 162 this.state = GUEST_STATE_START; | 195 this.state = GUEST_STATE_START; |
| 163 } | 196 } |
| 164 | 197 |
| 198 ResizeEvent.addListener(this.callOnResize, {instanceId: this.id}); |
| 165 this.handleCallback(callback); | 199 this.handleCallback(callback); |
| 166 }; | 200 }; |
| 167 | 201 |
| 168 GuestViewInternal.createGuest(this.viewType, | 202 GuestViewInternal.createGuest(this.viewType, |
| 169 createParams, | 203 createParams, |
| 170 callbackWrapper.bind(this, callback)); | 204 callbackWrapper.bind(this, callback)); |
| 171 | 205 |
| 172 this.state = GUEST_STATE_CREATED; | 206 this.state = GUEST_STATE_CREATED; |
| 173 }; | 207 }; |
| 174 | 208 |
| 175 // Internal implementation of destroy(). | 209 // Internal implementation of destroy(). |
| 176 GuestViewImpl.prototype.destroyImpl = function(callback) { | 210 GuestViewImpl.prototype.destroyImpl = function(callback) { |
| 177 // Check the current state. | 211 // Check the current state. |
| 178 if (!this.checkState('destroy')) { | 212 if (!this.checkState('destroy')) { |
| 179 this.handleCallback(callback); | 213 this.handleCallback(callback); |
| 180 return; | 214 return; |
| 181 } | 215 } |
| 182 | 216 |
| 183 if (this.state == GUEST_STATE_START) { | 217 if (this.state == GUEST_STATE_START) { |
| 184 // destroy() does nothing in this case. | 218 // destroy() does nothing in this case. |
| 185 this.handleCallback(callback); | 219 this.handleCallback(callback); |
| 186 return; | 220 return; |
| 187 } | 221 } |
| 188 | 222 |
| 189 GuestViewInternal.destroyGuest(this.id, | 223 GuestViewInternal.destroyGuest(this.id, |
| 190 this.handleCallback.bind(this, callback)); | 224 this.handleCallback.bind(this, callback)); |
| 191 | 225 |
| 226 // Reset the state of the destroyed guest; |
| 192 this.contentWindow = null; | 227 this.contentWindow = null; |
| 193 this.id = 0; | 228 this.id = 0; |
| 194 this.internalInstanceId = 0; | 229 this.internalInstanceId = 0; |
| 195 this.state = GUEST_STATE_START; | 230 this.state = GUEST_STATE_START; |
| 231 if (ResizeEvent.hasListener(this.callOnResize)) { |
| 232 ResizeEvent.removeListener(this.callOnResize); |
| 233 } |
| 196 }; | 234 }; |
| 197 | 235 |
| 198 // Internal implementation of detach(). | 236 // Internal implementation of detach(). |
| 199 GuestViewImpl.prototype.detachImpl = function(callback) { | 237 GuestViewImpl.prototype.detachImpl = function(callback) { |
| 200 // Check the current state. | 238 // Check the current state. |
| 201 if (!this.checkState('detach')) { | 239 if (!this.checkState('detach')) { |
| 202 this.handleCallback(callback); | 240 this.handleCallback(callback); |
| 203 return; | 241 return; |
| 204 } | 242 } |
| 205 | 243 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 221 } | 259 } |
| 222 | 260 |
| 223 GuestViewInternal.setAutoSize(this.id, autoSizeParams, | 261 GuestViewInternal.setAutoSize(this.id, autoSizeParams, |
| 224 this.handleCallback.bind(this, callback)); | 262 this.handleCallback.bind(this, callback)); |
| 225 }; | 263 }; |
| 226 | 264 |
| 227 // The exposed interface to a guestview. Exposes in its API the functions | 265 // The exposed interface to a guestview. Exposes in its API the functions |
| 228 // attach(), create(), destroy(), and getId(). All other implementation details | 266 // attach(), create(), destroy(), and getId(). All other implementation details |
| 229 // are hidden. | 267 // are hidden. |
| 230 function GuestView(viewType, guestInstanceId) { | 268 function GuestView(viewType, guestInstanceId) { |
| 231 privates(this).internal = new GuestViewImpl(viewType, guestInstanceId); | 269 privates(this).internal = new GuestViewImpl(this, viewType, guestInstanceId); |
| 232 } | 270 } |
| 233 | 271 |
| 234 // Attaches the guestview to the container with ID |internalInstanceId|. | 272 // Attaches the guestview to the container with ID |internalInstanceId|. |
| 235 GuestView.prototype.attach = function( | 273 GuestView.prototype.attach = function( |
| 236 internalInstanceId, viewInstanceId, attachParams, callback) { | 274 internalInstanceId, viewInstanceId, attachParams, callback) { |
| 237 var internal = privates(this).internal; | 275 var internal = privates(this).internal; |
| 238 internal.actionQueue.push(internal.attachImpl.bind( | 276 internal.actionQueue.push(internal.attachImpl.bind( |
| 239 internal, internalInstanceId, viewInstanceId, attachParams, callback)); | 277 internal, internalInstanceId, viewInstanceId, attachParams, callback)); |
| 240 internal.performNextAction(); | 278 internal.performNextAction(); |
| 241 }; | 279 }; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 }; | 317 }; |
| 280 | 318 |
| 281 // Returns the ID for this guestview. | 319 // Returns the ID for this guestview. |
| 282 GuestView.prototype.getId = function() { | 320 GuestView.prototype.getId = function() { |
| 283 var internal = privates(this).internal; | 321 var internal = privates(this).internal; |
| 284 return internal.id; | 322 return internal.id; |
| 285 }; | 323 }; |
| 286 | 324 |
| 287 // Exports | 325 // Exports |
| 288 exports.GuestView = GuestView; | 326 exports.GuestView = GuestView; |
| OLD | NEW |