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

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.pages_loaded_ = false;
8 this.loaded_count_ = false; 8 this.loaded_count_ = false;
9 } 9 }
10 10
(...skipping 11 matching lines...) Expand all
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 // Initial delay. 28 // Initial delay.
29 DELAY_IN_MINUTES: 1, 29 DELAY_IN_MINUTES: 1,
30 // Periodic wakeup delay. 30 // Periodic wakeup delay.
31 PERIOD_IN_MINUTES: 60, 31 PERIOD_IN_MINUTES: 60,
32 // Delayed closing of the background page once when all iframes are loaded. 32 // Delayed closing of the background page once when all iframes are loaded.
James Hawkins 2012/09/11 18:49:22 Optional nit: Blank line above this to separate it
zel 2012/09/11 19:59:40 Done.
33 UNLOAD_DELAY_IN_MS: 10000, 33 max_delay_ms_: 10000,
James Hawkins 2012/09/11 18:49:22 nit: JS variable names use javascriptCase, e.g. ma
zel 2012/09/11 19:59:40 Done.
34 34
35 // Loader start up. Kicks off alarm initialization if needed. 35 // Loader start up. Kicks off alarm initialization if needed.
36 initialize: function() { 36 initialize: function() {
37 if (!g_pages || !g_pages.length) { 37 if (!g_pages || !g_pages.length) {
38 window.close(); 38 window.close();
39 return; 39 return;
40 } 40 }
41 41
42 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this)); 42 chrome.alarms.onAlarm.addListener(this.onAlarm_.bind(this));
43 // Check if this alarm already exists, if not then create it. 43 // Check if this alarm already exists, if not then create it.
44 chrome.alarms.get(this.ALARM_NAME, function(alarm) { 44 chrome.alarms.get(this.ALARM_NAME, function(alarm) {
45 if (!alarm) { 45 if (!alarm) {
46 chrome.alarms.create(this.ALARM_NAME, 46 chrome.alarms.create(this.ALARM_NAME,
47 {delayInMinutes: this.DELAY_IN_MINUTES, 47 {delayInMinutes: this.DELAY_IN_MINUTES});
48 periodInMinutes: this.PERIOD_IN_MINUTES});
49 window.close(); 48 window.close();
50 return; 49 return;
51 } 50 }
52 }.bind(this)); 51 }.bind(this));
53 }, 52 },
54 53
55 onAlarm_: function(alarm) { 54 onAlarm_: function(alarm) {
56 this.loadSubPages_(); 55 if (alarm.name != this.ALARM_NAME)
56 return;
57
58 this.preparePages_();
57 }, 59 },
58 60
59 onMessage_: function(event) { 61 onMessage_: function(event) {
60 var msg = event.data; 62 var msg = event.data;
61 if (msg.method == 'validate') { 63 if (msg.method == 'validate') {
62 var reply_msg = { 64 var reply_msg = {
63 method: 'validationResults', 65 method: 'validationResults',
64 os: 'ChromeOS' 66 os: 'ChromeOS'
65 }; 67 };
66 event.source.postMessage(reply_msg, event.origin); 68 event.source.postMessage(reply_msg, event.origin);
67 } else { 69 } else {
68 console.log('#### Loader.onMessage_: unknown message'); 70 console.log('#### Loader.onMessage_: unknown message');
69 } 71 }
70 }, 72 },
71 73
72 loadSubPages_: function() { 74 getExtensionById_: function(list, id) {
James Hawkins 2012/09/11 18:49:22 nit: Document method and params. Here and elsewhe
zel 2012/09/11 19:59:40 Done.
75 for (var i = 0; i < list.length; i++) {
76 if (list[i].id == id) {
James Hawkins 2012/09/11 18:49:22 nit: No braces for single-line blocks.
zel 2012/09/11 19:59:40 Done.
77 return list[i];
78 }
79 }
80 return null;
81 },
82
83 preparePages_: function() {
73 if (this.pages_loaded_) 84 if (this.pages_loaded_)
74 return; 85 return;
75 86
76 window.addEventListener('message', this.onMessage_.bind(this), false); 87 window.addEventListener('message', this.onMessage_.bind(this), false);
77 for (i = 0; i < this.pages_.length; i++) { 88 chrome.management.getAll(function(list) {
78 this.loadPage_(i, this.pages_[i]); 89 // Get total count first.
79 } 90 var pages_to_load = [];
James Hawkins 2012/09/11 18:49:22 nit: pagesToLoad.
zel 2012/09/11 19:59:40 Done.
80 this.pages_loaded_ = true; 91 for (var i = 0; i < this.pages_.length; i++) {
92 var page = this.pages_[i];
93 if (page.one_time && page.initialized)
94 continue;
95
96 var extension = this.getExtensionById_(list, page.extension_id);
97 if (!extension || !extension.enabled) {
98 page.initialized = true;
99 continue;
100 }
101
102 page.initialized = true;
103 if (page.unload_delay_ms > this.max_delay_ms_)
104 this.max_delay_ms_ = page.unload_delay_ms;
105
106 pages_to_load.push(page);
107 }
108 this.loadFrames_(pages_to_load);
109 this.pages_loaded_ = true;
110 }.bind(this));
81 }, 111 },
82 112
83 loadPage_: function(index, pageUrl) { 113 loadFrames_: function(pages) {
114 this.load_target_ = pages.length;
115 for (var i = 0; i < pages.length; i++) {
116 this.loadLuncherFrame_(i, pages[i].page_url);
117 }
118 },
119
120 incrementLoadCounter_: function() {
121 this.loaded_count_++;
122 if (this.loaded_count_ < this.load_target_)
123 return;
124
125 // Delay closing.
126 setInterval(function() {
127 window.close();
128 }.bind(this), this.max_delay_ms_);
129 },
130
131 loadLuncherFrame_: function(index, page_url) {
James Hawkins 2012/09/11 18:49:22 nit: pageUrl
zel 2012/09/11 19:59:40 Done.
84 var iframe = document.createElement('iframe'); 132 var iframe = document.createElement('iframe');
85 iframe.onload = function() { 133 iframe.onload = function() {
86 this.loaded_count_++; 134 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); 135 }.bind(this);
95 iframe.src = pageUrl; 136 iframe.src = page_url;
96 iframe.name = 'frame_' + index; 137 iframe.name = 'frame_' + index;
97 document.body.appendChild(iframe); 138 document.body.appendChild(iframe);
98 } 139 }
99 }; 140 };
100 141
101 Loader.getInstance().initialize(); 142 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