| OLD | NEW |
| (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 if (!g_pages) | |
| 18 console.log('Warning: Undefined g_pages.'); | |
| 19 Loader.instance_ = new Loader(g_pages); | |
| 20 } | |
| 21 | |
| 22 return Loader.instance_; | |
| 23 }; | |
| 24 | |
| 25 Loader.prototype = { | |
| 26 // Alarm name. | |
| 27 ALARM_NAME: 'CrOSBkgLoaderAlarm', | |
| 28 // Initial delay. | |
| 29 DELAY_IN_MINUTES: 1, | |
| 30 // Periodic wakeup delay. | |
| 31 PERIOD_IN_MINUTES: 60, | |
| 32 // Delayed closing of the background page once when all iframes are loaded. | |
| 33 UNLOAD_DELAY_IN_MS: 10000, | |
| 34 | |
| 35 // Loader start up. Kicks off alarm initialization if needed. | |
| 36 initialize: function() { | |
| 37 if (!g_pages || !g_pages.length) { | |
| 38 window.close(); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this)); | |
| 43 // Check if this alarm already exists, if not then create it. | |
| 44 chrome.alarms.get(this.ALARM_NAME, function(alarm) { | |
| 45 if (!alarm) { | |
| 46 chrome.alarms.create(this.ALARM_NAME, | |
| 47 {delayInMinutes: this.DELAY_IN_MINUTES, | |
| 48 periodInMinutes: this.PERIOD_IN_MINUTES}); | |
| 49 window.close(); | |
| 50 return; | |
| 51 } | |
| 52 }.bind(this)); | |
| 53 }, | |
| 54 | |
| 55 onAlarm_: function(alarm) { | |
| 56 this.loadSubPages_(); | |
| 57 }, | |
| 58 | |
| 59 onMessage_: function(event) { | |
| 60 var msg = event.data; | |
| 61 if (msg.method == 'validate') { | |
| 62 var reply_msg = { | |
| 63 method: 'validationResults', | |
| 64 os: 'ChromeOS' | |
| 65 }; | |
| 66 event.source.postMessage(reply_msg, event.origin); | |
| 67 } else { | |
| 68 console.log('#### Loader.onMessage_: unknown message'); | |
| 69 } | |
| 70 }, | |
| 71 | |
| 72 loadSubPages_: function() { | |
| 73 if (this.pages_loaded_) | |
| 74 return; | |
| 75 | |
| 76 window.addEventListener('message', this.onMessage_.bind(this), false); | |
| 77 for (i = 0; i < this.pages_.length; i++) { | |
| 78 this.loadPage_(i, this.pages_[i]); | |
| 79 } | |
| 80 this.pages_loaded_ = true; | |
| 81 }, | |
| 82 | |
| 83 loadPage_: function(index, pageUrl) { | |
| 84 var iframe = document.createElement('iframe'); | |
| 85 iframe.onload = function() { | |
| 86 this.loaded_count_++; | |
| 87 if (this.loaded_count_ < this.pages_.length) | |
| 88 return; | |
| 89 | |
| 90 // Delay closing. | |
| 91 setInterval(function() { | |
| 92 window.close(); | |
| 93 }.bind(this), this.UNLOAD_DELAY_IN_MS); | |
| 94 }.bind(this); | |
| 95 iframe.src = pageUrl; | |
| 96 iframe.name = 'frame_' + index; | |
| 97 document.body.appendChild(iframe); | |
| 98 } | |
| 99 }; | |
| 100 | |
| 101 Loader.getInstance().initialize(); | |
| OLD | NEW |