Chromium Code Reviews| Index: chrome/browser/resources/chromeos/backloader/scripts/background.js |
| diff --git a/chrome/browser/resources/chromeos/backloader/scripts/background.js b/chrome/browser/resources/chromeos/backloader/scripts/background.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9022284416739a38771aa2ffc420c00d3ef977e9 |
| --- /dev/null |
| +++ b/chrome/browser/resources/chromeos/backloader/scripts/background.js |
| @@ -0,0 +1,93 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +function Loader(pages) { |
| + this.pages_ = pages; |
| + this.pages_loaded_ = false; |
| + this.loaded_count_ = false; |
| +} |
| + |
| +// Global instance. |
| +Loader.instance_ = null; |
| + |
| +// static. |
| +Loader.getInstance = function() { |
| + if (!Loader.instance_) |
| + 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.
|
| + |
| + return Loader.instance_; |
| +}; |
| + |
| +Loader.prototype = { |
| + // Alarm name. |
| + ALARM_NAME: 'CrOSBkgLoaderAlarm', |
| + // Initial delay. |
| + DELAY_IN_MINUTES: 1, |
| + // Periodic wakeup delay. |
| + PERIOD_IN_MINUTES: 60, |
| + // Delayed closing of the background page once when all iframes are loaded. |
| + UNLOAD_DELAY_IN_MS: 10000, |
| + |
| + // Loader start up. Kicks off alarm initialization if needed. |
| + initialize: function() { |
| + chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this)); |
| + // Check if this alarm already exists, if not then create it. |
| + chrome.alarms.get(this.ALARM_NAME, function(alarm) { |
| + if (!alarm) { |
| + chrome.alarms.create(this.ALARM_NAME, |
| + {delayInMinutes: this.DELAY_IN_MINUTES, |
| + periodInMinutes: this.PERIOD_IN_MINUTES}); |
| + window.close(); |
| + return; |
| + } |
| + }.bind(this)); |
| + }, |
| + |
| + onAlarm_: function(alarm) { |
| + this.loadSubPages_(); |
| + }, |
| + |
| + onMessage_: function(event) { |
| + var msg = event.data; |
| + if (msg.method == 'validate') { |
| + 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.
|
| + method: 'validationResults', |
| + os: 'ChromeOS' |
| + }; |
| + event.source.postMessage(msg, event.origin); |
| + } else { |
| + console.log('#### Loader.onMessage_: unknown message'); |
| + } |
| + }, |
| + |
| + loadSubPages_: function() { |
| + if (this.pages_loaded_) |
| + return; |
| + |
| + window.addEventListener('message', this.onMessage_.bind(this), false); |
| + for (i = 0; i < this.pages_.length; i++) { |
| + this.loadPage_(i, this.pages_[i]); |
| + } |
| + this.pages_loaded_ = true; |
| + }, |
| + |
| + loadPage_: function(index, pageUrl) { |
| + var iframe = document.createElement('iframe'); |
| + iframe.onload = function() { |
| + this.loaded_count_++; |
| + if (this.loaded_count_ < this.pages_.length) |
| + return; |
| + |
| + // Delay closing. |
| + setInterval(function() { |
| + window.close(); |
| + }.bind(this), this.UNLOAD_DELAY_IN_MS); |
| + }.bind(this); |
| + iframe.src = pageUrl; |
| + iframe.name = 'frame_' + index; |
| + document.body.appendChild(iframe); |
| + } |
| +}; |
| + |
| +Loader.getInstance().initialize(); |
|
xiyuan
2012/08/16 16:55:38
Could you wrap this into
document.addEventListen
|