Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: chrome/browser/resources/chromeos/backloader/scripts/background.js

Issue 10832240: Added background page loader component extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 function Loader(pages) {
6 this.pages_ = pages;
7 this.pages_loaded_ = false;
8 this.loaded_count_ = false;
9 }
10
11 // Global instance.
12 Loader.instance_ = null;
13
14 // static.
15 Loader.getInstance = function() {
16 if (!Loader.instance_)
17 Loader.instance_ = new Loader(g_pages);
xiyuan 2012/08/16 16:55:38 Could you add a warning check to ensure g_pages is
zel 2012/08/16 17:17:22 Done.
18
19 return Loader.instance_;
20 };
21
22 Loader.prototype = {
23 // Alarm name.
24 ALARM_NAME: 'CrOSBkgLoaderAlarm',
25 // Initial delay.
26 DELAY_IN_MINUTES: 1,
27 // Periodic wakeup delay.
28 PERIOD_IN_MINUTES: 60,
29 // Delayed closing of the background page once when all iframes are loaded.
30 UNLOAD_DELAY_IN_MS: 10000,
31
32 // Loader start up. Kicks off alarm initialization if needed.
33 initialize: function() {
34 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this));
35 // Check if this alarm already exists, if not then create it.
36 chrome.alarms.get(this.ALARM_NAME, function(alarm) {
37 if (!alarm) {
38 chrome.alarms.create(this.ALARM_NAME,
39 {delayInMinutes: this.DELAY_IN_MINUTES,
40 periodInMinutes: this.PERIOD_IN_MINUTES});
41 window.close();
42 return;
43 }
44 }.bind(this));
45 },
46
47 onAlarm_: function(alarm) {
48 this.loadSubPages_();
49 },
50
51 onMessage_: function(event) {
52 var msg = event.data;
53 if (msg.method == 'validate') {
54 var msg = {
xiyuan 2012/08/16 16:55:38 nit: could you use a different var name here, e.g.
zel 2012/08/16 17:17:22 Done.
55 method: 'validationResults',
56 os: 'ChromeOS'
57 };
58 event.source.postMessage(msg, event.origin);
59 } else {
60 console.log('#### Loader.onMessage_: unknown message');
61 }
62 },
63
64 loadSubPages_: function() {
65 if (this.pages_loaded_)
66 return;
67
68 window.addEventListener('message', this.onMessage_.bind(this), false);
69 for (i = 0; i < this.pages_.length; i++) {
70 this.loadPage_(i, this.pages_[i]);
71 }
72 this.pages_loaded_ = true;
73 },
74
75 loadPage_: function(index, pageUrl) {
76 var iframe = document.createElement('iframe');
77 iframe.onload = function() {
78 this.loaded_count_++;
79 if (this.loaded_count_ < this.pages_.length)
80 return;
81
82 // Delay closing.
83 setInterval(function() {
84 window.close();
85 }.bind(this), this.UNLOAD_DELAY_IN_MS);
86 }.bind(this);
87 iframe.src = pageUrl;
88 iframe.name = 'frame_' + index;
89 document.body.appendChild(iframe);
90 }
91 };
92
93 Loader.getInstance().initialize();
xiyuan 2012/08/16 16:55:38 Could you wrap this into document.addEventListen
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698