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.
6 * Takes the |pluginsData| input argument which represents data about the
7 * currently installed/running plugins and populates the html jstemplate with
8 * that data. It expects an object structure like the above.
9 * @param {Object} pluginsData Detailed info about installed plugins. Same
10 * expected format as returnPluginsData().
12 function renderTemplate(pluginsData) {
13 // This is the javascript code that processes the template:
14 var input = new JsEvalContext(pluginsData);
15 var output = $('pluginTemplate');
16 jstProcess(input, output);
20 * Asks the C++ PluginsDOMHandler to get details about the installed plugins and
21 * return detailed data about the configuration. The PluginsDOMHandler should
22 * reply to returnPluginsData() (below).
24 function requestPluginsData() {
25 chrome.send('requestPluginsData');
26 chrome.send('getShowDetails');
29 function loadShowDetailsFromPrefs(show_details) {
30 tmiModeExpanded = show_details;
31 $('collapse').style.display =
32 show_details ? 'inline' : 'none';
33 $('expand').style.display =
34 show_details ? 'none' : 'inline';
36 document.body.className = show_details ? 'show-in-tmi-mode' : 'hide-tmi-mode';
40 * Called by the web_ui_ to re-populate the page with data representing the
41 * current state of installed plugins.
42 * @param {Object} pluginsData Detailed info about installed plugins. The
43 * template expects each plugin's format to match the following structure to
44 * correctly populate the page:
49 * description: 'description',
51 * update_url: 'http://update/',
54 * identifier: 'plugin-name',
57 * path: '/blahblah/blahblah/MyCrappyPlugin.plugin',
58 * name: 'MyCrappyPlugin',
60 * description: 'My crappy plugin',
62 * { description: 'Foo Media',
63 * fileExtensions: ['foo'],
64 * mimeType: 'application/x-my-foo' },
65 * { description: 'Bar Stuff',
66 * fileExtensions: ['bar', 'baz'],
67 * mimeType: 'application/my-bar' }
69 * enabledMode: 'enabledByUser'
72 * path: '/tmp/MyFirst.plugin',
73 * name: 'MyFirstPlugin',
74 * version: '3.14r15926',
75 * description: 'My first plugin',
77 * { description: 'New Guy Media',
78 * fileExtensions: ['mfp'],
79 * mimeType: 'application/x-my-first' }
81 * enabledMode: 'enabledByPolicy'
84 * path: '/foobar/baz/YourGreatPlugin.plugin',
85 * name: 'YourGreatPlugin',
87 * description: 'Your great plugin',
89 * { description: 'Baz Stuff',
90 * fileExtensions: ['baz'],
91 * mimeType: 'application/x-your-baz' }
93 * enabledMode: 'disabledByUser'
96 * path: '/foobiz/bar/HisGreatPlugin.plugin',
97 * name: 'HisGreatPlugin',
99 * description: 'His great plugin',
101 * { description: 'More baz Stuff',
102 * fileExtensions: ['bor'],
103 * mimeType: 'application/x-his-bor' }
105 * enabledMode: 'disabledByPolicy'
112 function returnPluginsData(pluginsData) {
113 var bodyContainer = $('body-container');
114 var body = document.body;
116 // Set all page content to be visible so we can measure heights.
117 bodyContainer.style.visibility = 'hidden';
119 var slidables = document.getElementsByClassName('show-in-tmi-mode');
120 for (var i = 0; i < slidables.length; i++)
121 slidables[i].style.height = 'auto';
123 renderTemplate(pluginsData);
125 // Add handlers to dynamically created HTML elements.
126 var links = document.getElementsByClassName('disable-plugin-link');
127 for (var i = 0; i < links.length; i++) {
128 links[i].onclick = function() {
129 handleEnablePlugin(this, false, false);
133 links = document.getElementsByClassName('enable-plugin-link');
134 for (var i = 0; i < links.length; i++) {
135 links[i].onclick = function() {
136 handleEnablePlugin(this, true, false);
140 links = document.getElementsByClassName('disable-group-link');
141 for (var i = 0; i < links.length; i++) {
142 links[i].onclick = function() {
143 handleEnablePlugin(this, false, true);
147 links = document.getElementsByClassName('enable-group-link');
148 for (var i = 0; i < links.length; i++) {
149 links[i].onclick = function() {
150 handleEnablePlugin(this, true, true);
154 var checkboxes = document.getElementsByClassName('always-allow');
155 for (var i = 0; i < checkboxes.length; i++) {
156 checkboxes[i].onclick = function() {
157 handleSetPluginAlwaysAllowed(this);
161 // Disable some controls for Guest in ChromeOS.
163 uiAccountTweaks.UIAccountTweaks.applyGuestModeVisibility(document);
165 // Make sure the left column (with "Description:", "Location:", etc.) is the
166 // same size for all plugins.
167 var labels = document.getElementsByClassName('plugin-details-label');
168 var maxLabelWidth = 0;
169 for (var i = 0; i < labels.length; i++)
170 labels[i].style.width = 'auto';
171 for (var i = 0; i < labels.length; i++)
172 maxLabelWidth = Math.max(maxLabelWidth, labels[i].offsetWidth);
173 for (var i = 0; i < labels.length; i++)
174 labels[i].style.width = maxLabelWidth + 'px';
176 // Explicitly set the height for each element that wants to be "slid" in and
177 // out when the tmiModeExpanded is toggled.
178 var slidables = document.getElementsByClassName('show-in-tmi-mode');
179 for (var i = 0; i < slidables.length; i++)
180 slidables[i].style.height = slidables[i].offsetHeight + 'px';
182 // Reset visibility of page based on the current tmi mode.
183 $('collapse').style.display =
184 tmiModeExpanded ? 'inline' : 'none';
185 $('expand').style.display =
186 tmiModeExpanded ? 'none' : 'inline';
187 bodyContainer.style.visibility = 'visible';
188 body.className = tmiModeExpanded ?
189 'show-tmi-mode-initial' : 'hide-tmi-mode-initial';
193 * Handles a 'enable' or 'disable' button getting clicked.
194 * @param {HTMLElement} node The HTML element for the plugin being changed.
195 * @param {boolean} enable Whether to enable or disable the plugin.
196 * @param {boolean} isGroup True if we're enabling/disabling a plugin group,
197 * rather than a single plugin.
199 function handleEnablePlugin(node, enable, isGroup) {
200 // Tell the C++ PluginsDOMHandler to enable/disable the plugin.
201 chrome.send('enablePlugin', [String(node.path), String(enable),
205 // Keeps track of whether details have been made visible (expanded) or not.
206 var tmiModeExpanded = false;
209 * Toggles visibility of details.
211 function toggleTmiMode() {
212 tmiModeExpanded = !tmiModeExpanded;
214 $('collapse').style.display =
215 tmiModeExpanded ? 'inline' : 'none';
216 $('expand').style.display =
217 tmiModeExpanded ? 'none' : 'inline';
219 document.body.className =
220 tmiModeExpanded ? 'show-tmi-mode' : 'hide-tmi-mode';
222 chrome.send('saveShowDetailsToPrefs', [String(tmiModeExpanded)]);
225 function handleSetPluginAlwaysAllowed(el) {
226 chrome.send('setPluginAlwaysAllowed', [el.identifier, el.checked]);
230 * @param {Object} plugin An object containing the information about a plugin.
231 * See returnPluginsData() for the format of this object.
232 * @return {boolean} Whether the plugin's version should be displayed.
234 function shouldDisplayPluginVersion(plugin) {
235 return !!plugin.version && plugin.version != '0';
239 * @param {Object} plugin An object containing the information about a plugin.
240 * See returnPluginsData() for the format of this object.
241 * @return {boolean} Whether the plugin's description should be displayed.
243 function shouldDisplayPluginDescription(plugin) {
244 // Only display the description if it's not blank and if it's not just the
245 // name, version, or combination thereof.
246 return plugin.description &&
247 plugin.description != plugin.name &&
248 plugin.description != plugin.version &&
249 plugin.description != 'Version ' + plugin.version &&
250 plugin.description != plugin.name + ' ' + plugin.version;
254 * @param {Object} plugin An object containing the information about a plugin.
255 * See returnPluginsData() for the format of this object.
256 * @return {boolean} Whether the plugin is enabled.
258 function isPluginEnabled(plugin) {
259 return plugin.enabledMode == 'enabledByUser' ||
260 plugin.enabledMode == 'enabledByPolicy';
263 // Unfortunately, we don't have notifications for plugin (list) status changes
264 // (yet), so in the meanwhile just update regularly.
265 setInterval(requestPluginsData, 30000);
267 // Get data and have it displayed upon loading.
268 document.addEventListener('DOMContentLoaded', requestPluginsData);
270 // Add handlers to static HTML elements.
271 $('collapse').onclick = toggleTmiMode;
272 $('expand').onclick = toggleTmiMode;
273 $('details-link').onclick = toggleTmiMode;