This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
window_manager.js
1152 lines (961 loc) · 36 KB
/
window_manager.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
//
// This file calls getElementById without waiting for an onload event, so it
// must have a defer attribute or be included at the end of the <body>.
//
// This module is responsible for launching apps and for allowing
// the user to switch among apps and kill apps. Specifically, it handles:
// launching apps,
// killing apps
// keeping track of the set of running apps (which we call tasks here)
// keeping track of which task is displayed (the foreground task)
// changing the foreground task
// hiding all apps to display the homescreen
// displaying the app switcher to allow the user to switch and kill apps
// performing appropriate transition animations between:
// the homescreen and an app
// the homescreen and the switcher
// an app and the homescreen
// the switcher and the homescreen
// the switcher and the current foreground task
// the switcher and a different task
// Handling Home key events to switch to the homescreen and the switcher
//
// The public API of the module is small. It defines an WindowManager object
// with these methods:
//
// launch(origin): switch to the specified app
// kill(origin, callback): stop specified app
// reload(origin): reload the given app
// getDisplayedApp(origin): return the origin of the currently displayed app
// setOrientationForApp(origin): set the phone to orientation to a given app
// getAppFrame(origin): returns the iframe element for the specified origin
// which is assumed to be running. This is only currently used
// for tests and chrome stuff: see the end of the file
// getNumberOfRunningApps(): returns the numbers of running apps.
// setAppSize(origin): set/reset the size of the given app to it's origin
// state, only used by keyboard manager to restore the app window.
// XXX: should be removed.
// setDisplayedApp(origin): set displayed app.
// XXX: should be removed.
//
// This module does not (at least not currently) have anything to do
// with the homescreen. It simply assumes that if it hides all running
// apps the homescreen will show
//
// TODO
// It would be nice eventually to centralize much of the homescreen
// event handling code in a single place. When or if we do that, then
// this module will just expose methods for managing the list of apps
// and app visibility but will leave all the event handling to another module.
//
'use strict';
var WindowManager = (function() {
// Holds the origin of the home screen, which should be the first
// app we launch through web activity during boot
var homescreen = null;
var homescreenURL = '';
var homescreenManifestURL = '';
// Screenshot in sprite -- to use, or not to use,
// that's the question.
var useScreenshotInSprite = true;
// keep the reference of inline activity frame here
var inlineActivityFrame = null;
// Some document elements we use
var loadingIcon = document.getElementById('statusbar-loading');
var windows = document.getElementById('windows');
var dialogOverlay = document.getElementById('dialog-overlay');
var screenElement = document.getElementById('screen');
var banner = document.getElementById('system-banner');
var bannerContainer = banner.firstElementChild;
//
// The set of running apps.
// This is a map from app origin to an object like this:
// {
// name: the app's name
// manifest: the app's manifest object
// frame: the iframe element that the app is displayed in
// launchTime: last time when app gets active
// }
//
var runningApps = {};
var numRunningApps = 0; // appendFrame() and removeFrame() maintain this count
var nextAppId = 0; // to give each app's iframe a unique id attribute
// The origin of the currently displayed app, or null if there isn't one
var displayedApp = null;
// Public function. Return the origin of the currently displayed app
// or null if there is none.
function getDisplayedApp() {
return displayedApp || null;
}
// Make the specified app the displayed app.
// Public function. Pass null to make the homescreen visible
function launch(origin) {
// If it is already being displayed, do nothing
if (displayedApp === origin)
return;
// If the app is already running (or there is no app), just display it
if (!origin || isRunning(origin))
setDisplayedApp(origin);
// launch() can be called from outside the card switcher
// hiding it if needed
if (CardsView.cardSwitcherIsShown())
CardsView.cardTaskSwitcher();
}
function isRunning(origin) {
return runningApps.hasOwnProperty(origin);
}
function getAppFrame(origin) {
if (isRunning(origin))
return runningApps[origin].frame;
else
return null;
}
// Set the size of the app's iframe to match the size of the screen.
// We have to call this on resize events (which happen when the
// phone orientation is changed). And also when an app is launched
// and each time an app is brought to the front, since the
// orientation could have changed since it was last displayed
function setAppSize(origin) {
var app = runningApps[origin];
if (!app)
return;
var frame = app.frame;
var manifest = app.manifest;
var cssWidth = window.innerWidth + 'px';
var cssHeight = window.innerHeight - StatusBar.height + 'px';
if (app.manifest.fullscreen)
cssHeight = window.innerHeight + 'px';
frame.style.width =
dialogOverlay.style.width = cssWidth;
frame.style.height =
dialogOverlay.style.height = cssHeight;
setInlineActivityFrameSize();
}
// Copy the dimension of the currently displayed app
function setInlineActivityFrameSize() {
if (!inlineActivityFrame)
return;
var app = runningApps[displayedApp];
var appFrame = app.frame;
var frame = inlineActivityFrame;
frame.style.width = appFrame.style.width;
frame.style.height = appFrame.style.height;
}
var openFrame = null;
var closeFrame = null;
var openCallback = null;
var closeCallback = null;
// Create a window sprite element to perform windows open/close
// animations.
var sprite = document.createElement('div');
sprite.id = 'windowSprite';
sprite.dataset.zIndexLevel = 'window-sprite';
screenElement.appendChild(sprite);
sprite.appendChild(document.createElement('div'));
// This event handler is triggered when the transition ends.
// We're going to do two transitions, so it gets called twice.
sprite.addEventListener('transitionend', function spriteTransition(e) {
var prop = e.propertyName;
switch (sprite.className) {
case 'opening':
// transitionend will be called twice since we touched two properties.
// Only responsive to the property that takes the longest to transit
if (prop !== 'transform')
return;
openFrame.classList.add('active');
windows.classList.add('active');
// If frame is still unpainted to this point, we will have to pause
// the transition and wait for the mozbrowserfirstpaint event.
if ('unpainted' in openFrame.dataset) {
openFrame.addEventListener(
'mozbrowserfirstpaint', function continueSpriteTransition() {
openFrame.removeEventListener(
'mozbrowserfirstpaint', continueSpriteTransition);
// Run getAppScreenshotFromFrame() to ensure all CSS backgrounds
// of the apps are loaded.
getAppScreenshotFromFrame(displayedApp,
function screenshotTaken() {
sprite.className = 'opened';
});
});
return;
}
sprite.className = 'opened';
break;
case 'opened':
openFrame.setVisible(true);
openFrame.focus();
// Dispatch an 'appopen' event.
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('appopen', true, false, { origin: displayedApp });
openFrame.dispatchEvent(evt);
setTimeout(openCallback);
sprite.style.background = '';
sprite.className = '';
break;
case 'closing':
closeFrame.classList.remove('active');
windows.classList.remove('active');
screenElement.classList.remove('fullscreen-app');
sprite.className = 'closed';
break;
case 'closed':
// transitionend will be called twice since we touched two properties.
// Only responsive to the property that takes the longest to transit
if (prop !== 'transform')
return;
setTimeout(closeCallback);
sprite.style.background = '';
sprite.className = '';
break;
}
});
// On-disk database for window manager.
// It's only for app screenshots right now.
var database = null;
var DB_SCREENSHOT_OBJSTORE = 'screenshots';
(function openDatabase() {
var DB_VERSION = 1;
var DB_NAME = 'window_manager';
var req = window.indexedDB.open(DB_NAME, DB_VERSION);
req.onerror = function() {
console.error('Window Manager: opening database failed.');
};
req.onupgradeneeded = function databaseUpgradeneeded() {
database = req.result;
if (database.objectStoreNames.contains(DB_SCREENSHOT_OBJSTORE))
database.deleteObjectStore(DB_SCREENSHOT_OBJSTORE);
var store = database.createObjectStore(
DB_SCREENSHOT_OBJSTORE, { keyPath: 'origin' });
};
req.onsuccess = function databaseSuccess() {
database = req.result;
};
})();
function putAppScreenshotToDatabase(origin, data) {
if (!database)
return;
var txn = database.transaction(DB_SCREENSHOT_OBJSTORE, 'readwrite');
txn.onerror = function() {
console.warn(
'Window Manager: transaction error while trying to save screenshot.');
};
var store = txn.objectStore(DB_SCREENSHOT_OBJSTORE);
var req = store.put({
origin: origin,
screenshot: data
});
req.onerror = function(evt) {
console.warn(
'Window Manager: put error while trying to save screenshot.');
};
}
function getAppScreenshotFromDatabase(origin, callback) {
if (!database) {
console.warn(
'Window Manager: Neither database nor app frame is ' +
'ready for getting screenshot.');
callback();
return;
}
var req = database.transaction(DB_SCREENSHOT_OBJSTORE)
.objectStore(DB_SCREENSHOT_OBJSTORE).get(origin);
req.onsuccess = function() {
if (!req.result) {
console.log('Window Manager: No screenshot in database. ' +
'This is expected from a fresh installed app.');
callback();
return;
}
callback(req.result.screenshot, true);
}
req.onerror = function(evt) {
console.warn('Window Manager: get screenshot from database failed.');
callback();
};
}
function deleteAppScreenshotFromDatabase(origin) {
var txn = database.transaction(DB_SCREENSHOT_OBJSTORE);
var store = txn.objectStore(DB_SCREENSHOT_OBJSTORE);
store.delete(origin);
}
function getAppScreenshotFromFrame(origin, callback) {
var app = runningApps[origin];
if (!app || !app.frame) {
callback();
return;
}
var req = app.frame.getScreenshot();
// Workaround https://bugzilla.mozilla.org/show_bug.cgi?id=787519
var isTimeout = false;
var timer = setTimeout(function getScreenshotTimeout() {
console.warn('Window Manager: getScreenshot timeout.');
isTimeout = true;
callback();
}, 500);
req.onsuccess = function(evt) {
if (isTimeout)
return;
clearTimeout(timer);
var result = evt.target.result;
callback(result, false);
};
req.onerror = function(evt) {
if (isTimeout)
return;
clearTimeout(timer);
console.warn('Window Manager: getScreenshot failed.');
callback();
};
}
// Meta method for get the screenshot from the app frame,
// and save it to database.
function saveAppScreenshot(origin, callback) {
getAppScreenshotFromFrame(origin, function gotScreenshot(screenshot) {
if (callback)
callback(screenshot);
if (!screenshot)
return;
putAppScreenshotToDatabase(origin, screenshot);
});
}
// Meta method for getting app screenshot from database, or
// get it from the app frame.
function getAppScreenshot(origin, callback) {
if (!callback)
return;
var app = runningApps[origin];
// If the frame is just being append and app content is just being loaded,
// let's get the screenshot from the database instead.
if (!app || 'unpainted' in app.frame.dataset) {
getAppScreenshotFromDatabase(origin, callback);
return;
}
getAppScreenshotFromFrame(origin, function(screenshot, isCached) {
if (!screenshot) {
getAppScreenshotFromDatabase(origin, callback);
return;
}
callback(screenshot, isCached);
});
}
function afterPaint(callback) {
window.addEventListener('MozAfterPaint', function afterPainted() {
window.removeEventListener('MozAfterPaint', afterPainted);
setTimeout(callback);
});
}
// Perform an "open" animation for the app's iframe
function openWindow(origin, callback) {
var app = runningApps[origin];
openFrame = app.frame;
openCallback = callback || function() {};
if (origin === homescreen) {
openCallback();
windows.classList.add('active');
openFrame.classList.add('homescreen');
openFrame.setVisible(true);
openFrame.focus();
} else {
if (app.manifest.fullscreen)
screenElement.classList.add('fullscreen-app');
// Get the screenshot of the app and put it on the sprite
// before starting the transition
sprite.className = 'before-open';
getAppScreenshot(origin, function(screenshot, isCached) {
sprite.dataset.mask = isCached;
if (!screenshot || !useScreenshotInSprite) {
sprite.dataset.mask = false;
sprite.className = 'opening';
return;
}
sprite.style.background = '#fff url(' + screenshot + ')';
// Make sure Gecko paint the sprite first
afterPaint(function() {
// Start the transition
sprite.className = 'opening';
});
});
}
}
function closeWindow(origin, callback) {
var app = runningApps[origin];
closeFrame = app.frame;
closeCallback = callback || function() {};
// Send a synthentic 'appwillclose' event.
// The keyboard uses this and the appclose event to know when to close
// See https://github.com/andreasgal/gaia/issues/832
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('appwillclose', true, false, { origin: origin });
closeFrame.dispatchEvent(evt);
// Take keyboard focus away from the closing window
closeFrame.blur();
closeFrame.setVisible(false);
// Get the screenshot of the app and put it on the sprite
// before starting the transition
sprite.className = 'before-close';
getAppScreenshot(origin, function(screenshot, isCached) {
sprite.dataset.mask = isCached;
if (!screenshot || !useScreenshotInSprite) {
sprite.dataset.mask = false;
sprite.className = 'closing';
return;
}
sprite.style.background = '#fff url(' + screenshot + ')';
// Make sure Gecko paint the sprite first
afterPaint(function() {
// Start the transition
sprite.className = 'closing';
});
});
}
// Ensure the homescreen is loaded and return its frame. Restarts
// the homescreen app if it was killed in the background.
function ensureHomescreen() {
if (!isRunning(homescreen)) {
var app = Applications.getByManifestURL(homescreenManifestURL);
appendFrame(homescreen, homescreenURL,
app.manifest.name, app.manifest, app.manifestURL);
setAppSize(homescreen);
openWindow(homescreen, null);
}
return runningApps[homescreen].frame;
}
// Switch to a different app
function setDisplayedApp(origin, callback) {
var currentApp = displayedApp, newApp = origin || homescreen;
// Returns the frame reference of the home screen app.
// Restarts the homescreen app if it was killed in the background.
var homescreenFrame = ensureHomescreen();
// Discard any existing activity
stopInlineActivity();
// Case 1: the app is already displayed
if (currentApp && currentApp == newApp) {
// Just run the callback right away
if (callback)
callback();
}
// Case 2: null->homescreen || homescreen->app
else if ((!currentApp && newApp == homescreen) ||
(currentApp == homescreen && newApp)) {
if (!currentApp)
homescreenFrame.setVisible(true);
setAppSize(newApp);
openWindow(newApp, function windowOpened() {
// Move the homescreen into the background only
// after the transition completes, since it's
// visible during the transition.
if (currentApp)
homescreenFrame.setVisible(false);
if (callback)
callback();
});
}
// Case 3: app->homescreen
else if (currentApp && currentApp != homescreen && newApp == homescreen) {
// Animate the window close. Ensure the homescreen is in the
// foreground since it will be shown during the animation.
homescreenFrame.setVisible(true);
setAppSize(newApp);
closeWindow(currentApp, callback);
}
// Case 4: app-to-app transition
else {
setAppSize(newApp);
closeWindow(currentApp, function closeWindow() {
openWindow(newApp, callback);
});
}
// Set homescreen as active,
// to control the z-index between homescreen & keyboard iframe
if ((newApp == homescreen) && homescreenFrame) {
homescreenFrame.classList.add('active');
} else {
homescreenFrame.classList.remove('active');
}
// Lock orientation as needed
if (newApp == null) { // going to the homescreen, so force portrait
screen.mozLockOrientation('portrait-primary');
} else {
setOrientationForApp(newApp);
}
// Record the time when app was launched,
// need this to display apps in proper order on CardsView.
// We would also need this to determined the freshness of the frame
// for making screenshots.
if (newApp)
runningApps[newApp].launchTime = Date.now();
// Set displayedApp to the new value
displayedApp = origin;
// Update the loading icon since the displayedApp is changed
updateLoadingIcon();
// If the app has a attention screen open, displaying it
AttentionScreen.showForOrigin(origin);
}
function setOrientationForApp(origin) {
if (origin == null) { // homescreen
screen.mozLockOrientation('portrait-primary');
return;
}
var app = runningApps[origin];
if (!app)
return;
var manifest = app.manifest;
if (manifest.orientation) {
var rv = screen.mozLockOrientation(manifest.orientation);
if (rv === false) {
console.warn('screen.mozLockOrientation() returned false for',
origin, 'orientation', manifest.orientation);
}
}
else { // If no orientation was requested, then let it rotate
screen.mozUnlockOrientation();
}
}
var isOutOfProcessDisabled = false;
SettingsListener.observe('debug.oop.disabled', false, function(value) {
isOutOfProcessDisabled = value;
});
function createFrame(origin, url, name, manifest, manifestURL) {
var frame = document.createElement('iframe');
frame.setAttribute('mozallowfullscreen', 'true');
frame.className = 'appWindow';
frame.dataset.frameOrigin = origin;
frame.src = url;
// Note that we don't set the frame size here. That will happen
// when we display the app in setDisplayedApp()
// Most apps currently need to be hosted in a special 'mozbrowser' iframe.
// They also need to be marked as 'mozapp' to be recognized as apps by the
// platform.
frame.setAttribute('mozbrowser', 'true');
frame.setAttribute('mozapp', manifestURL);
// These apps currently have bugs preventing them from being
// run out of process. All other apps will be run OOP.
//
var outOfProcessBlackList = [
// Bugs that are shared among multiple apps are listed here.
// Bugs that affect only specific apps should be listed under
// the apps themselves.
//
// Keyboard always shows up alpha when app using keyboard is run OOP
// https://bugzilla.mozilla.org/show_bug.cgi?id=776118
// Keyboard doesn't show up correctly when app run OOP
// https://github.com/mozilla-b2g/gaia/issues/2656
'Browser',
// Requires nested content processes (bug 761935)
'Cost Control',
// Cross-process SMS (bug 775997)
'E-Mail',
// SSL/TLS support can only happen in the main process although
// the TCP support without security will accidentally work OOP
// (bug 770778)
'Image Uploader',
// Cannot upload files when OOP
// bug 783878
// /!\ Also remove it from outOfProcessBlackList of background_service.js
// Once this app goes OOP. (can be done by reverting a commit)
'Messages'
// Crashes when launched OOP (bug 775997)
];
if (!isOutOfProcessDisabled &&
outOfProcessBlackList.indexOf(name) === -1) {
// FIXME: content shouldn't control this directly
frame.setAttribute('remote', 'true');
console.info('%%%%% Launching', name, 'as remote (OOP)');
} else {
console.info('%%%%% Launching', name, 'as local');
}
return frame;
}
function appendFrame(origin, url, name, manifest, manifestURL) {
// Create the <iframe mozbrowser mozapp> that hosts the app
var frame = createFrame(origin, url, name, manifest, manifestURL);
frame.id = 'appframe' + nextAppId++;
frame.dataset.frameType = 'window';
// frames are began unpainted. This dataset value will pause the
// opening sprite transition so users will not see whitish screen.
frame.dataset.unpainted = true;
frame.addEventListener('mozbrowserfirstpaint', function painted() {
frame.removeEventListener('mozbrowserfirstpaint', painted);
delete frame.dataset.unpainted;
// Save the screenshot when we got mozbrowserfirstpaint event,
// regardless of the sprite transition state.
// setTimeout() here ensures that we get the screenshot with content.
setTimeout(function() {
saveAppScreenshot(origin);
});
});
// Add the iframe to the document
windows.appendChild(frame);
// And map the app origin to the info we need for the app
runningApps[origin] = {
name: name,
manifest: manifest,
frame: frame,
launchTime: 0
};
numRunningApps++;
}
function startInlineActivity(origin, url, name, manifest, manifestURL) {
// Create the <iframe mozbrowser mozapp> that hosts the app
var frame = createFrame(origin, url, name, manifest, manifestURL);
frame.classList.add('inlineActivity');
frame.dataset.frameType = 'inline-activity';
// Start the transition only until the frame is painted
frame.addEventListener('mozbrowserfirstpaint', function painted() {
frame.removeEventListener('mozbrowserfirstpaint', painted);
frame.classList.add('active');
screenElement.classList.add('inline-activity');
});
// Discard any existing activity
stopInlineActivity();
// Save the reference
inlineActivityFrame = frame;
// Set the size
setInlineActivityFrameSize();
// Add the iframe to the document
windows.appendChild(frame);
}
function removeFrame(origin) {
var app = runningApps[origin];
if (app.frame)
windows.removeChild(app.frame);
delete runningApps[origin];
numRunningApps--;
}
function stopInlineActivity() {
if (!inlineActivityFrame)
return;
var frame = inlineActivityFrame;
inlineActivityFrame = null;
frame.classList.remove('active');
screenElement.classList.remove('inline-activity');
frame.addEventListener('transitionend', function frameTransitionend() {
frame.removeEventListener('transitionend', frameTransitionend);
windows.removeChild(frame);
});
}
// Start running the specified app.
// In order to have a nice smooth open animation,
// we don't actually set the iframe src property until
// the animation has completed.
function start(origin) {
if (isRunning(origin))
return;
var app = Applications.getByOrigin(origin);
// TODO: is the startPoint argument implemented?
// and is it passed back to us in the webapps-launch method?
// If so, we could use that to pass a query string or fragmentid
// to append to the apps' URL.
app.launch();
}
// There are two types of mozChromeEvent we need to handle
// in order to launch the app for Gecko
window.addEventListener('mozChromeEvent', function(e) {
var manifestURL = e.detail.manifestURL;
if (!manifestURL)
return;
var app = Applications.getByManifestURL(manifestURL);
if (!app)
return;
var name = app.manifest.name;
var origin = app.origin;
// Check if it's a virtual app from a entry point.
// If so, change the app name and origin to the
// entry point.
var entryPoints = app.manifest.entry_points;
if (entryPoints) {
var givenPath = e.detail.url.substr(origin.length);
// Workaround here until the bug (to be filed) is fixed
// Basicly, gecko is sending the URL without launch_path sometimes
for (var ep in entryPoints) {
var currentEp = entryPoints[ep];
var path = givenPath;
if (path.indexOf('?') != -1) {
path = path.substr(0, path.indexOf('?'));
}
//Remove the origin and / to find if if the url is the entry point
if (path.indexOf('/' + ep) == 0 &&
(currentEp.launch_path == path)) {
origin = origin + currentEp.launch_path;
name = currentEp.name;
}
}
}
switch (e.detail.type) {
// mozApps API is asking us to launch the app
// We will launch it in foreground
case 'webapps-launch':
if (!isRunning(origin)) {
appendFrame(origin, e.detail.url,
name, app.manifest, app.manifestURL);
}
setDisplayedApp(origin, null, 'window');
break;
// System Message Handler API is asking us to open the specific URL
// that handles the pending system message.
// We will launch it in background if it's not handling an activity.
case 'open-app':
if (e.detail.isActivity && e.detail.target.disposition == 'inline') {
// Inline activities behaves more like a dialog,
// let's deal them here.
startInlineActivity(origin, e.detail.url,
name, app.manifest, app.manifestURL);
return;
}
if (isRunning(origin)) {
// If the app is in foreground, it's too risky to change it's
// URL. We'll ignore this request.
if (displayedApp !== origin) {
var frame = getAppFrame(origin);
// If the app is opened and it is loaded to the correct page,
// then there is nothing to do.
if (frame.src !== e.detail.url) {
// Rewrite the URL of the app frame to the requested URL.
// XXX: We could ended opening URls not for the app frame
// in the app frame. But we don't care.
frame.src = e.detail.url;
}
}
} else if (origin !== homescreen) {
// XXX: We could ended opening URls not for the app frame
// in the app frame. But we don't care.
appendFrame(origin, e.detail.url,
name, app.manifest, app.manifestURL);
} else {
ensureHomescreen();
}
// If nothing is opened yet, consider the first application opened
// as the homescreen.
if (!homescreen) {
homescreen = origin;
// Save the entry manifest URL and launch URL so that we can restart
// the homescreen later, if necessary.
homescreenURL = e.detail.url;
homescreenManifestURL = manifestURL;
return;
}
// We will only bring web activity handling apps to the foreground
if (!e.detail.isActivity)
return;
// XXX: the correct way would be for UtilityTray to close itself
// when there is a appwillopen/appopen event.
UtilityTray.hide();
setDisplayedApp(origin);
break;
}
});
// If the application tried to close themselves by calling window.close()
// we will handle that here.
// XXX: currently broken, see
// https://bugzilla.mozilla.org/show_bug.cgi?id=789392
window.addEventListener('mozbrowserclose', function(e) {
if (!'frameType' in e.target.dataset)
return;
switch (e.target.dataset.frameType) {
case 'window':
kill(e.target.dataset.frameOrigin);
break;
case 'inline-activity':
stopInlineActivity();
break;
}
});
// If there is a new application coming in, we should switch to
// home screen so the user would see the icon pops up.
window.addEventListener('applicationinstall', function(e) {
setDisplayedApp(homescreen);
});
// Deal with application uninstall event
// if the application is being uninstalled, we ensure it stop running here.
window.addEventListener('applicationuninstall', function(e) {
kill(e.detail.application.origin);
deleteAppScreenshotFromDatabase(e.detail.application.origin);
});
function showCrashBanner(manifestURL) {
var app = Applications.getByManifestURL(manifestURL);
var _ = navigator.mozL10n.get;
banner.addEventListener('animationend', function animationend() {
banner.removeEventListener('animationend', animationend);
banner.classList.remove('visible');
});
banner.classList.add('visible');
bannerContainer.textContent = _('foreground-app-crash-notification',
{ name: app.manifest.name });
}
// Deal with crashed apps
window.addEventListener('mozbrowsererror', function(e) {
if (!'frameType' in e.target.dataset)
return;
var origin = e.target.dataset.frameOrigin;
var manifestURL = e.target.getAttribute('mozapp');
if (e.target.dataset.frameType == 'inline-activity') {
stopInlineActivity();
showCrashBanner(manifestURL);
return;
}
if (e.target.dataset.frameType !== 'window')
return;
/*
detail.type = error (Server Not Found case)
is handled in Modal Dialog
*/
if (e.detail.type !== 'fatal')
return;
// If the crashing app is currently displayed, we will present
// the user with a banner notification.
if (displayedApp == origin)
showCrashBanner(manifestURL);
// If the crashing app is the home screen app and it is the displaying app
// we will need to relaunch it right away.
// Alternatively, if home screen is not the displaying app,
// we will not relaunch it until the foreground app is closed.
// (to be dealt in setDisplayedApp(), not here)
if (displayedApp == homescreen) {
kill(origin, ensureHomescreen);
return;
}
// Actually remove the frame, and trigger the closing transition
// if the app is currently displaying
kill(origin);
});
// Stop running the app with the specified origin
function kill(origin, callback) {
if (!isRunning(origin))
return;