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

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

Issue 10855239: Wired background loader component extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 function Loader(pages) { 5 function Loader(pages) {
6 this.pages_ = pages; 6 this.pages_ = pages;
7 this.pages_loaded_ = false; 7 this.pagesLoaded_ = false;
8 this.loaded_count_ = false; 8 this.loadedCount_ = false;
9 } 9 }
10 10
11 // Global instance. 11 // Global instance.
12 Loader.instance_ = null; 12 Loader.instance_ = null;
13 13
14 // static. 14 // static.
15 Loader.getInstance = function() { 15 Loader.getInstance = function() {
16 if (!Loader.instance_) { 16 if (!Loader.instance_) {
17 if (!g_pages) 17 if (!g_pages)
18 console.log('Warning: Undefined g_pages.'); 18 console.log('Warning: Undefined g_pages.');
19 Loader.instance_ = new Loader(g_pages); 19 Loader.instance_ = new Loader(g_pages);
20 } 20 }
21 21
22 return Loader.instance_; 22 return Loader.instance_;
23 }; 23 };
24 24
25 Loader.prototype = { 25 Loader.prototype = {
26 // Alarm name. 26 // Alarm name.
27 ALARM_NAME: 'CrOSBkgLoaderAlarm', 27 ALARM_NAME: 'CrOSBkgLoaderAlarm',
28
28 // Initial delay. 29 // Initial delay.
29 DELAY_IN_MINUTES: 1, 30 DELAY_IN_MINUTES: 1,
31
30 // Periodic wakeup delay. 32 // Periodic wakeup delay.
31 PERIOD_IN_MINUTES: 60, 33 PERIOD_IN_MINUTES: 60,
34
32 // Delayed closing of the background page once when all iframes are loaded. 35 // Delayed closing of the background page once when all iframes are loaded.
33 UNLOAD_DELAY_IN_MS: 10000, 36 maxDelayMS_: 10000,
34 37
35 // Loader start up. Kicks off alarm initialization if needed. 38 // Loader start up. Kicks off alarm initialization if needed.
36 initialize: function() { 39 initialize: function() {
37 if (!g_pages || !g_pages.length) { 40 if (!g_pages || !g_pages.length) {
38 window.close(); 41 window.close();
39 return; 42 return;
40 } 43 }
41 44
42 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this)); 45 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this));
43 // Check if this alarm already exists, if not then create it. 46 // Check if this alarm already exists, if not then create it.
44 chrome.alarms.get(this.ALARM_NAME, function(alarm) { 47 chrome.alarms.get(this.ALARM_NAME, function(alarm) {
45 if (!alarm) { 48 if (!alarm) {
46 chrome.alarms.create(this.ALARM_NAME, 49 chrome.alarms.create(this.ALARM_NAME,
47 {delayInMinutes: this.DELAY_IN_MINUTES, 50 {delayInMinutes: this.DELAY_IN_MINUTES});
48 periodInMinutes: this.PERIOD_IN_MINUTES});
49 window.close(); 51 window.close();
50 return; 52 return;
51 } 53 }
52 }.bind(this)); 54 }.bind(this));
53 }, 55 },
54 56
55 onAlarm_: function(alarm) { 57 onAlarm_: function(alarm) {
56 this.loadSubPages_(); 58 if (alarm.name != this.ALARM_NAME)
59 return;
60
61 this.preparePages_();
57 }, 62 },
58 63
59 onMessage_: function(event) { 64 onMessage_: function(event) {
60 var msg = event.data; 65 var msg = event.data;
61 if (msg.method == 'validate') { 66 if (msg.method == 'validate') {
62 var reply_msg = { 67 var reply_msg = {
63 method: 'validationResults', 68 method: 'validationResults',
64 os: 'ChromeOS' 69 os: 'ChromeOS'
65 }; 70 };
66 event.source.postMessage(reply_msg, event.origin); 71 event.source.postMessage(reply_msg, event.origin);
67 } else { 72 } else {
68 console.log('#### Loader.onMessage_: unknown message'); 73 console.log('#### Loader.onMessage_: unknown message');
69 } 74 }
70 }, 75 },
71 76
72 loadSubPages_: function() { 77 // Find an extension in the |list| with matching extension |id|.
73 if (this.pages_loaded_) 78 getExtensionById_: function(list, id) {
79 for (var i = 0; i < list.length; i++) {
80 if (list[i].id == id)
81 return list[i];
82 }
83 return null;
84 },
85
86 preparePages_: function() {
87 if (this.pagesLoaded_)
74 return; 88 return;
75 89
76 window.addEventListener('message', this.onMessage_.bind(this), false); 90 window.addEventListener('message', this.onMessage_.bind(this), false);
77 for (i = 0; i < this.pages_.length; i++) { 91 chrome.management.getAll(function(list) {
78 this.loadPage_(i, this.pages_[i]); 92 // Get total count first.
79 } 93 var pagesToLoad = [];
80 this.pages_loaded_ = true; 94 for (var i = 0; i < this.pages_.length; i++) {
95 var page = this.pages_[i];
96 if (page.oneTime && page.initialized)
97 continue;
98
99 var extension = this.getExtensionById_(list, page.extensionId);
100 if (!extension || !extension.enabled) {
101 page.initialized = true;
102 continue;
103 }
104
105 page.initialized = true;
106 if (page.unloadDelayMS > this.maxDelayMS_)
107 this.maxDelayMS_ = page.unloadDelayMS;
108
109 pagesToLoad.push(page);
110 }
111 this.loadFrames_(pagesToLoad);
112 this.pagesLoaded_ = true;
113 }.bind(this));
81 }, 114 },
82 115
83 loadPage_: function(index, pageUrl) { 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);
120 }
121 },
122
123 incrementLoadCounter_: function() {
124 this.loadedCount_++;
125 if (this.loadedCount_ < this.load_target_)
126 return;
127
128 // Delay closing.
129 setInterval(function() {
130 window.close();
131 }.bind(this), this.maxDelayMS_);
132 },
133
134 loadLuncherFrame_: function(index, pageUrl) {
84 var iframe = document.createElement('iframe'); 135 var iframe = document.createElement('iframe');
85 iframe.onload = function() { 136 iframe.onload = function() {
86 this.loaded_count_++; 137 this.incrementLoadCounter_();
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); 138 }.bind(this);
95 iframe.src = pageUrl; 139 iframe.src = pageUrl;
96 iframe.name = 'frame_' + index; 140 iframe.name = 'frame_' + index;
97 document.body.appendChild(iframe); 141 document.body.appendChild(iframe);
98 } 142 }
99 }; 143 };
100 144
101 Loader.getInstance().initialize(); 145 Loader.getInstance().initialize();
OLDNEW
« no previous file with comments | « chrome/browser/resources/backloader/manifest.json ('k') | chrome/browser/resources/backloader/scripts/pages.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698