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

Unified 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 side-by-side diff with in-line comments
Download patch
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..27f3489ebcef8176a1686a3e627b7ec314c5e34a
--- /dev/null
+++ b/chrome/browser/resources/chromeos/backloader/scripts/background.js
@@ -0,0 +1,85 @@
+// 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;
+// Alarm name.
+Loader.ALARM_NAME = 'BkgLoaderAlarm';
+// Initial delay.
+Loader.DELAY_IN_MINUTES = 1;
+// Periodic wakeup delay.
+Loader.PERIOD_IN_MINUTES = 60;
+// Delayed closing of the background page once when all iframes are loaded.
+Loader.UNLOAD_DELAY_IN_MS = 10000;
+
+// static.
+Loader.getInstance = function() {
+ if (!Loader.instance_)
+ Loader.instance_ = new Loader(g_pages);
+
+ return Loader.instance_;
+};
+
+// Alarm event handler.
+Loader.onAlarm = function(alarm) {
+ Loader.getInstance().onAlarm_(alarm);
+};
+
+// Loader start up. Kicks off alarm initialization if needed.
+Loader.prototype.start = function() {
+ console.log('Starting background loader');
+ chrome.alarms.onAlarm.addListener(Loader.onAlarm);
+ // Check if this alarm already exists, if not then create it.
+ chrome.alarms.get(Loader.ALARM_NAME, function(alarm) {
+ if (!alarm) {
+ console.log('Creating alarm!');
+ chrome.alarms.create(Loader.ALARM_NAME,
+ {delayInMinutes: Loader.DELAY_IN_MINUTES,
+ periodInMinutes: Loader.PERIOD_IN_MINUTES});
+ window.close();
+ return;
+ }
+ console.log('Has alarm already!');
+ }.bind(this));
+};
+
+Loader.prototype.onAlarm_ = function(alarm) {
+ console.log('Got alarm ' + alarm.name);
+ if (this.pages_loaded_) {
+ console.log('Pages already loaded!');
+ return;
+ }
+ for (i = 0; i < this.pages_.length; i++) {
+ this.loadPage_(i, this.pages_[i]);
+ }
+ this.pages_loaded_ = true;
+};
+
+Loader.prototype.loadPage_ = function(index, pageUrl) {
+ var iframe = document.createElement('iframe');
+ iframe.onload = function() {
+ this.loaded_count_++;
+ console.log('Total ' + this.loaded_count_ + ' pages loaded.');
+ if (this.loaded_count_ < this.pages_.length)
+ return;
+
+ console.log('All pages loaded!');
+ // Delay closing.
+ setInterval(function() {
+ console.log('Closing background page.');
+ window.close();
+ }.bind(this), Loader.UNLOAD_DELAY_IN_MS);
+ }.bind(this);
+ iframe.src = pageUrl;
+ iframe.name = 'frame' + index;
+ document.body.appendChild(iframe);
+};
+
+Loader.getInstance().start();

Powered by Google App Engine
This is Rietveld 408576698