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 CreateEvent = require('guestViewEvents').CreateEvent; |
8 var EventBindings = require('event_bindings'); | 9 var EventBindings = require('event_bindings'); |
9 var GuestViewInternal = | 10 var GuestViewInternal = |
10 require('binding').Binding.create('guestViewInternal').generate(); | 11 require('binding').Binding.create('guestViewInternal').generate(); |
11 var GuestViewInternalNatives = requireNative('guest_view_internal'); | 12 var GuestViewInternalNatives = requireNative('guest_view_internal'); |
12 | 13 |
13 // Events. | 14 // 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'); | 15 var ResizeEvent = CreateEvent('guestViewInternal.onResize'); |
19 | 16 |
20 // Possible states. | 17 // Possible states. |
21 var GUEST_STATE_ATTACHED = 2; | 18 var GUEST_STATE_ATTACHED = 2; |
22 var GUEST_STATE_CREATED = 1; | 19 var GUEST_STATE_CREATED = 1; |
23 var GUEST_STATE_START = 0; | 20 var GUEST_STATE_START = 0; |
24 | 21 |
25 // Error messages. | 22 // Error messages. |
26 var ERROR_MSG_ALREADY_ATTACHED = 'The guest has already been attached.'; | 23 var ERROR_MSG_ALREADY_ATTACHED = 'The guest has already been attached.'; |
27 var ERROR_MSG_ALREADY_CREATED = 'The guest has already been created.'; | 24 var ERROR_MSG_ALREADY_CREATED = 'The guest has already been created.'; |
28 var ERROR_MSG_INVALID_STATE = 'The guest is in an invalid state.'; | 25 var ERROR_MSG_INVALID_STATE = 'The guest is in an invalid state.'; |
29 var ERROR_MSG_NOT_ATTACHED = 'The guest is not attached.'; | 26 var ERROR_MSG_NOT_ATTACHED = 'The guest is not attached.'; |
30 var ERROR_MSG_NOT_CREATED = 'The guest has not been created.'; | 27 var ERROR_MSG_NOT_CREATED = 'The guest has not been created.'; |
31 | 28 |
32 // Properties. | 29 // Properties. |
33 var PROPERTY_ON_RESIZE = 'onResize'; | 30 var PROPERTY_ON_RESIZE = 'onresize'; |
34 | 31 |
35 // Contains and hides the internal implementation details of |GuestView|, | 32 // Contains and hides the internal implementation details of |GuestView|, |
36 // including maintaining its state and enforcing the proper usage of its API | 33 // including maintaining its state and enforcing the proper usage of its API |
37 // fucntions. | 34 // fucntions. |
38 function GuestViewImpl(guestView, viewType, guestInstanceId) { | 35 function GuestViewImpl(guestView, viewType, guestInstanceId) { |
39 if (guestInstanceId) { | 36 if (guestInstanceId) { |
40 this.id = guestInstanceId; | 37 this.id = guestInstanceId; |
41 this.state = GUEST_STATE_CREATED; | 38 this.state = GUEST_STATE_CREATED; |
42 } else { | 39 } else { |
43 this.id = 0; | 40 this.id = 0; |
44 this.state = GUEST_STATE_START; | 41 this.state = GUEST_STATE_START; |
45 } | 42 } |
46 this.actionQueue = []; | 43 this.actionQueue = []; |
47 this.contentWindow = null; | 44 this.contentWindow = null; |
48 this.guestView = guestView; | 45 this.guestView = guestView; |
49 this.pendingAction = null; | 46 this.pendingAction = null; |
50 this.viewType = viewType; | 47 this.viewType = viewType; |
51 this.internalInstanceId = 0; | 48 this.internalInstanceId = 0; |
52 | 49 |
53 this.setupOnResize(); | 50 this.setupOnResize(); |
54 } | 51 } |
55 | 52 |
56 // Sets up the onResize property on the GuestView. | 53 // Sets up the onResize property on the GuestView. |
57 GuestViewImpl.prototype.setupOnResize = function() { | 54 GuestViewImpl.prototype.setupOnResize = function() { |
58 Object.defineProperty(this.guestView, PROPERTY_ON_RESIZE, { | 55 $Object.defineProperty(this.guestView, PROPERTY_ON_RESIZE, { |
59 get: function() { | 56 get: function() { |
60 return this[PROPERTY_ON_RESIZE]; | 57 return this[PROPERTY_ON_RESIZE]; |
61 }.bind(this), | 58 }.bind(this), |
62 set: function(value) { | 59 set: function(value) { |
63 this[PROPERTY_ON_RESIZE] = value; | 60 this[PROPERTY_ON_RESIZE] = value; |
64 }.bind(this), | 61 }.bind(this), |
65 enumerable: true | 62 enumerable: true |
66 }); | 63 }); |
67 | 64 |
68 this.callOnResize = function(e) { | 65 this.callOnResize = function(e) { |
69 if (!this[PROPERTY_ON_RESIZE]) { | 66 if (!this[PROPERTY_ON_RESIZE]) { |
70 return; | 67 return; |
71 } | 68 } |
72 this[PROPERTY_ON_RESIZE](e.oldWidth, e.oldHeight, e.newWidth, e.newHeight); | 69 this[PROPERTY_ON_RESIZE](e); |
73 }.bind(this); | 70 }.bind(this); |
74 }; | 71 }; |
75 | 72 |
76 // Callback wrapper that is used to call the callback of the pending action (if | 73 // Callback wrapper that is used to call the callback of the pending action (if |
77 // one exists), and then performs the next action in the queue. | 74 // one exists), and then performs the next action in the queue. |
78 GuestViewImpl.prototype.handleCallback = function(callback) { | 75 GuestViewImpl.prototype.handleCallback = function(callback) { |
79 if (callback) { | 76 if (callback) { |
80 callback(); | 77 callback(); |
81 } | 78 } |
82 this.pendingAction = null; | 79 this.pendingAction = null; |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 }; | 319 }; |
323 | 320 |
324 // Returns the ID for this guestview. | 321 // Returns the ID for this guestview. |
325 GuestView.prototype.getId = function() { | 322 GuestView.prototype.getId = function() { |
326 var internal = privates(this).internal; | 323 var internal = privates(this).internal; |
327 return internal.id; | 324 return internal.id; |
328 }; | 325 }; |
329 | 326 |
330 // Exports | 327 // Exports |
331 exports.GuestView = GuestView; | 328 exports.GuestView = GuestView; |
OLD | NEW |