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 /** 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(). 11 */ 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); 17 } 18 19 /** 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). 23 */ 24 function requestPluginsData() { 25 chrome.send('requestPluginsData'); 26 chrome.send('getShowDetails'); 27 } 28 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'; 35 36 document.body.className = show_details ? 'show-in-tmi-mode' : 'hide-tmi-mode'; 37 } 38 39 /** 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: 45 * { 46 * plugins: [ 47 * { 48 * name: 'Group Name', 49 * description: 'description', 50 * version: 'version', 51 * update_url: 'http://update/', 52 * critical: true, 53 * enabled: true, 54 * identifier: 'plugin-name', 55 * plugin_files: [ 56 * { 57 * path: '/blahblah/blahblah/MyCrappyPlugin.plugin', 58 * name: 'MyCrappyPlugin', 59 * version: '1.2.3', 60 * description: 'My crappy plugin', 61 * mimeTypes: [ 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' } 68 * ], 69 * enabledMode: 'enabledByUser' 70 * }, 71 * { 72 * path: '/tmp/MyFirst.plugin', 73 * name: 'MyFirstPlugin', 74 * version: '3.14r15926', 75 * description: 'My first plugin', 76 * mimeTypes: [ 77 * { description: 'New Guy Media', 78 * fileExtensions: ['mfp'], 79 * mimeType: 'application/x-my-first' } 80 * ], 81 * enabledMode: 'enabledByPolicy' 82 * }, 83 * { 84 * path: '/foobar/baz/YourGreatPlugin.plugin', 85 * name: 'YourGreatPlugin', 86 * version: '4.5', 87 * description: 'Your great plugin', 88 * mimeTypes: [ 89 * { description: 'Baz Stuff', 90 * fileExtensions: ['baz'], 91 * mimeType: 'application/x-your-baz' } 92 * ], 93 * enabledMode: 'disabledByUser' 94 * }, 95 * { 96 * path: '/foobiz/bar/HisGreatPlugin.plugin', 97 * name: 'HisGreatPlugin', 98 * version: '1.2', 99 * description: 'His great plugin', 100 * mimeTypes: [ 101 * { description: 'More baz Stuff', 102 * fileExtensions: ['bor'], 103 * mimeType: 'application/x-his-bor' } 104 * ], 105 * enabledMode: 'disabledByPolicy' 106 * } 107 * ] 108 * } 109 * ] 110 * } 111 */ 112 function returnPluginsData(pluginsData) { 113 var bodyContainer = $('body-container'); 114 var body = document.body; 115 116 // Set all page content to be visible so we can measure heights. 117 bodyContainer.style.visibility = 'hidden'; 118 body.className = ''; 119 var slidables = document.getElementsByClassName('show-in-tmi-mode'); 120 for (var i = 0; i < slidables.length; i++) 121 slidables[i].style.height = 'auto'; 122 123 renderTemplate(pluginsData); 124 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); 130 return false; 131 }; 132 } 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); 137 return false; 138 }; 139 } 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); 144 return false; 145 }; 146 } 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); 151 return false; 152 }; 153 } 154 var checkboxes = document.getElementsByClassName('always-allow'); 155 for (var i = 0; i < checkboxes.length; i++) { 156 checkboxes[i].onclick = function() { 157 handleSetPluginAlwaysAllowed(this); 158 }; 159 } 160 161 // Disable some controls for Guest in ChromeOS. 162 if (cr.isChromeOS) 163 uiAccountTweaks.UIAccountTweaks.applyGuestModeVisibility(document); 164 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'; 175 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'; 181 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'; 190 } 191 192 /** 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. 198 */ 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), 202 String(isGroup)]); 203 } 204 205 // Keeps track of whether details have been made visible (expanded) or not. 206 var tmiModeExpanded = false; 207 208 /* 209 * Toggles visibility of details. 210 */ 211 function toggleTmiMode() { 212 tmiModeExpanded = !tmiModeExpanded; 213 214 $('collapse').style.display = 215 tmiModeExpanded ? 'inline' : 'none'; 216 $('expand').style.display = 217 tmiModeExpanded ? 'none' : 'inline'; 218 219 document.body.className = 220 tmiModeExpanded ? 'show-tmi-mode' : 'hide-tmi-mode'; 221 222 chrome.send('saveShowDetailsToPrefs', [String(tmiModeExpanded)]); 223 } 224 225 function handleSetPluginAlwaysAllowed(el) { 226 chrome.send('setPluginAlwaysAllowed', [el.identifier, el.checked]); 227 } 228 229 /** 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. 233 */ 234 function shouldDisplayPluginVersion(plugin) { 235 return !!plugin.version && plugin.version != '0'; 236 } 237 238 /** 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. 242 */ 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; 251 } 252 253 /** 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. 257 */ 258 function isPluginEnabled(plugin) { 259 return plugin.enabledMode == 'enabledByUser' || 260 plugin.enabledMode == 'enabledByPolicy'; 261 } 262 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); 266 267 // Get data and have it displayed upon loading. 268 document.addEventListener('DOMContentLoaded', requestPluginsData); 269 270 // Add handlers to static HTML elements. 271 $('collapse').onclick = toggleTmiMode; 272 $('expand').onclick = toggleTmiMode; 273 $('details-link').onclick = toggleTmiMode; 274