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.
5 function Loader(pages) {
7 this.pagesLoaded_ = false;
8 this.loadedCount_ = false;
12 Loader.instance_ = null;
15 Loader.getInstance = function() {
16 if (!Loader.instance_) {
18 console.log('Warning: Undefined g_pages.');
19 Loader.instance_ = new Loader(g_pages);
22 return Loader.instance_;
27 ALARM_NAME: 'CrOSBkgLoaderAlarm',
32 // Periodic wakeup delay.
33 PERIOD_IN_MINUTES: 60,
35 // Delayed closing of the background page once when all iframes are loaded.
38 // Loader start up. Kicks off alarm initialization if needed.
39 initialize: function() {
40 if (!g_pages || !g_pages.length) {
45 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this));
46 // Check if this alarm already exists, if not then create it.
47 chrome.alarms.get(this.ALARM_NAME, function(alarm) {
49 chrome.alarms.create(this.ALARM_NAME,
50 {delayInMinutes: this.DELAY_IN_MINUTES});
57 onAlarm_: function(alarm) {
58 if (alarm.name != this.ALARM_NAME)
64 onMessage_: function(event) {
66 if (msg.method == 'validate') {
68 method: 'validationResults',
71 event.source.postMessage(replyMsg, event.origin);
73 console.log('#### Loader.onMessage_: unknown message');
77 // Find an extension in the |list| with matching extension |id|.
78 getExtensionById_: function(list, id) {
79 for (var i = 0; i < list.length; i++) {
86 preparePages_: function() {
87 if (this.pagesLoaded_)
90 window.addEventListener('message', this.onMessage_.bind(this), false);
91 chrome.management.getAll(function(list) {
92 // Get total count first.
94 for (var i = 0; i < this.pages_.length; i++) {
95 var page = this.pages_[i];
96 if (page.oneTime && page.initialized)
99 var extension = this.getExtensionById_(list, page.extensionId);
100 if (!extension || !extension.enabled) {
101 page.initialized = true;
105 page.initialized = true;
106 if (page.unloadDelayMS > this.maxDelayMS_)
107 this.maxDelayMS_ = page.unloadDelayMS;
109 pagesToLoad.push(page);
111 this.loadFrames_(pagesToLoad);
112 this.pagesLoaded_ = true;
116 loadFrames_: function(pages) {
117 this.load_target_ = pages.length;
118 for (var i = 0; i < pages.length; i++) {
119 this.loadLuncherFrame_(i, pages[i].pageUrl);
123 incrementLoadCounter_: function() {
125 if (this.loadedCount_ < this.load_target_)
129 setInterval(function() {
131 }.bind(this), this.maxDelayMS_);
134 loadLuncherFrame_: function(index, pageUrl) {
135 var iframe = document.createElement('iframe');
136 iframe.onload = function() {
137 this.incrementLoadCounter_();
139 iframe.src = pageUrl;
140 iframe.name = 'frame_' + index;
141 document.body.appendChild(iframe);
145 Loader.getInstance().initialize();