forked from mozilla-b2g/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_mixin.js
243 lines (218 loc) · 6.83 KB
/
browser_mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
'use strict';
(function(window) {
/**
* @mixin BrowserMixin
*/
window.BrowserMixin = {
reload: function bm_reload() {
if (this.browser.element) {
this.browser.element.reload();
}
},
/**
* A static timeout to make sure
* the next event don't happen too late.
*/
NEXTPAINT_TIMEOUT: 500,
/**
* Wait for a next paint event from mozbrowser iframe,
* The callback would be called in this.NEXTPAINT_TIMEOUT ms
* if the next paint event doesn't happen.
* The use case is for the moment just before we turn on
* the iframe visibility, so the TIMEOUT isn't too long.
*
* Note: for some reason we intend to use ensureFullRepaint now.
*
* @param {Function} callback The callback function to be invoked
* after we get next paint event.
*/
waitForNextPaint: function bm_waitForNextPaint(callback) {
if (!this.browser || !this.browser.element) {
if (callback) {
callback();
}
return;
}
var iframe = this.browser.element;
var nextPaintTimer;
var self = this;
var onNextPaint = function aw_onNextPaint() {
self.debug(' nextpainted.');
iframe.removeNextPaintListener(onNextPaint);
clearTimeout(nextPaintTimer);
callback();
};
nextPaintTimer = setTimeout(function ifNextPaintIsTooLate() {
self.debug(' nextpaint is timeouted.');
iframe.removeNextPaintListener(onNextPaint);
callback();
}, this.NEXTPAINT_TIMEOUT);
iframe.addNextPaintListener(onNextPaint);
},
/**
* get the screenshot of mozbrowser iframe.
* If it succeed, the blob would be stored in this._screenshotBlob.
* @param {Function} callback The callback function to be invoked
* after we get the screenshot.
*/
getScreenshot: function bm_getScreenshot(callback, width, height, timeout) {
if (!this.browser || !this.browser.element) {
if (callback) {
callback();
}
return;
}
// We don't need the screenshot of homescreen because:
// 1. Homescreen background is transparent,
// currently gecko only sends JPG to us.
// See bug 878003.
// 2. Homescreen screenshot isn't required by card view.
// Since getScreenshot takes additional memory usage,
// let's early return here.
var self = this;
var invoked = false;
var timer;
// First, let's check if we have a frontWindow, if so this is the one
// we will want a screenshot of!
if (this.frontWindow && this.frontWindow.isActive()) {
this.frontWindow.getScreenshot(callback, width, height, timeout);
return;
}
if (timeout) {
timer = window.setTimeout(function() {
if (invoked) {
return;
}
self.debug('getScreenshot timeout!');
invoked = true;
callback();
}, timeout);
}
var req = this.iframe.getScreenshot(
width || this.width || layoutManager.width,
height || this.height || layoutManager.height);
req.onsuccess = function gotScreenshotFromFrame(evt) {
var result = evt.target.result;
if (!width) {
// Refresh _screenshotBlob when no width/height is specified.
self._screenshotBlob = result;
}
self.debug('getScreenshot succeed!');
if (invoked)
return;
self.debug('get screenshot success!!!!');
invoked = true;
if (timer)
window.clearTimeout(timer);
if (callback)
callback(result);
};
req.onerror = function gotScreenshotFromFrameError(evt) {
self.debug('getScreenshot failed!');
if (invoked)
return;
invoked = true;
if (timer)
window.clearTimeout(timer);
if (callback)
callback();
};
},
focus: function bm_focus() {
if (this.browser && this.browser.element) {
this.browser.element.focus();
}
},
blur: function bm_blur() {
if (this.browser.element) {
this.browser.element.blur();
}
},
back: function bm_back() {
if (this.browser.element) {
this.browser.element.goBack();
}
},
forward: function bm_forward() {
if (this.browser.element) {
this.browser.element.goForward();
}
},
stop: function bm_stop() {
if (this.browser.element) {
this.browser.element.stop();
}
},
_setVisible: function bm__setVisible(visible) {
if (this.browser && this.browser.element &&
'setVisible' in this.browser.element) {
this.debug('setVisible on browser element:' + visible);
this.browser.element.setVisible(visible);
}
},
/**
* Set aria-hidden attribute on browser's element to handle its screen
* reader visibility.
* @type {Boolean} visible A flag indicating if the element should be screen
* reader visible.
*/
_setVisibleForScreenReader:
function bm__setVisibleForScreenReader(visible) {
if (this.browser && this.browser.element) {
this.debug('aria-hidden on browser element:' + !visible);
this.browser.element.setAttribute('aria-hidden', !visible);
}
},
/**
* Fire a DOM Request to detect the history has previous page or not.
* @param {Function} callback Called when DOM Request is done.
*/
canGoBack: function bm_cangoback(callback) {
var self = this;
if (this.browser.element) {
var r = this.browser.element.getCanGoBack();
r.onsuccess = function(evt) {
self._backable = evt.target.result;
if (callback)
callback(evt.target.result);
};
r.onerror = function(evt) {
if (callback)
callback();
};
} else {
if (callback)
callback();
}
},
/**
* Fire a DOM Request to detect the history has next page or not.
* @param {Function} callback Called when DOM Request is done.
*/
canGoForward: function bm_cangoforward(callback) {
var self = this;
if (this.browser.element) {
var r = this.browser.element.getCanGoForward();
r.onsuccess = function(evt) {
self._forwardable = evt.target.result;
if (callback)
callback(evt.target.result);
};
r.onerror = function(evt) {
if (callback)
callback();
};
} else {
if (callback)
callback();
}
},
/**
* Decide we are in out of process or not.
*/
isOOP: function bm_isOOP() {
return (this.browser_config && this.browser_config.oop);
}
};
AppWindow.addMixin(BrowserMixin);
}(this));