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.
5 <include src="../uber/uber_utils.js">
6 <include src="extension_code.js">
7 <include src="extension_commands_overlay.js">
8 <include src="extension_error_overlay.js">
9 <include src="extension_focus_manager.js">
10 <include src="extension_list.js">
11 <include src="pack_extension_overlay.js">
12 <include src="extension_loader.js">
13 <include src="extension_options_overlay.js">
16 <include src="chromeos/kiosk_apps.js">
20 * The type of the extension data object. The definition is based on
21 * chrome/browser/ui/webui/extensions/extension_settings_handler.cc:
22 * ExtensionSettingsHandler::HandleRequestExtensionsData()
23 * @typedef {{developerMode: boolean,
25 * incognitoAvailable: boolean,
26 * loadUnpackedDisabled: boolean,
27 * profileIsSupervised: boolean,
28 * promoteAppsDevTools: boolean}}
30 var ExtensionDataResponse;
32 // Used for observing function of the backend datasource for this page by
34 var webuiResponded = false;
36 cr.define('extensions', function() {
37 var ExtensionsList = options.ExtensionsList;
39 // Implements the DragWrapper handler interface.
40 var dragWrapperHandler = {
42 shouldAcceptDrag: function(e) {
43 // We can't access filenames during the 'dragenter' event, so we have to
44 // wait until 'drop' to decide whether to do something with the file or
46 // See: http://www.w3.org/TR/2011/WD-html5-20110113/dnd.html#concept-dnd-p
47 return (e.dataTransfer.types &&
48 e.dataTransfer.types.indexOf('Files') > -1);
51 doDragEnter: function() {
52 chrome.send('startDrag');
53 ExtensionSettings.showOverlay(null);
54 ExtensionSettings.showOverlay($('drop-target-overlay'));
57 doDragLeave: function() {
58 ExtensionSettings.showOverlay(null);
59 chrome.send('stopDrag');
62 doDragOver: function(e) {
67 ExtensionSettings.showOverlay(null);
68 if (e.dataTransfer.files.length != 1)
72 // Files lack a check if they're a directory, but we can find out through
74 for (var i = 0; i < e.dataTransfer.items.length; ++i) {
75 if (e.dataTransfer.items[i].kind == 'file' &&
76 e.dataTransfer.items[i].webkitGetAsEntry().isDirectory) {
77 toSend = 'installDroppedDirectory';
81 // Only process files that look like extensions. Other files should
82 // navigate the browser normally.
84 /\.(crx|user\.js|zip)$/i.test(e.dataTransfer.files[0].name)) {
85 toSend = 'installDroppedFile';
96 * ExtensionSettings class
99 function ExtensionSettings() {}
101 cr.addSingletonGetter(ExtensionSettings);
103 ExtensionSettings.prototype = {
104 __proto__: HTMLDivElement.prototype,
107 * Whether or not to try to display the Apps Developer Tools promotion.
111 displayPromo_: false,
114 * Perform initial setup.
116 initialize: function() {
117 uber.onContentFrameLoaded();
118 cr.ui.FocusOutlineManager.forDocument(document);
119 measureCheckboxStrings();
122 uber.setTitle(loadTimeData.getString('extensionSettings'));
124 // This will request the data to show on the page and will get a response
125 // back in returnExtensionsData.
126 chrome.send('extensionSettingsRequestExtensionsData');
128 var extensionLoader = extensions.ExtensionLoader.getInstance();
130 $('toggle-dev-on').addEventListener('change',
131 this.handleToggleDevMode_.bind(this));
132 $('dev-controls').addEventListener('webkitTransitionEnd',
133 this.handleDevControlsTransitionEnd_.bind(this));
135 // Set up the three dev mode buttons (load unpacked, pack and update).
136 $('load-unpacked').addEventListener('click', function(e) {
137 extensionLoader.loadUnpacked();
139 $('pack-extension').addEventListener('click',
140 this.handlePackExtension_.bind(this));
141 $('update-extensions-now').addEventListener('click',
142 this.handleUpdateExtensionNow_.bind(this));
144 // Set up the close dialog for the apps developer tools promo.
145 $('apps-developer-tools-promo').querySelector('.close-button').
146 addEventListener('click', function(e) {
147 this.displayPromo_ = false;
148 this.updatePromoVisibility_();
149 chrome.send('extensionSettingsDismissADTPromo');
152 if (!loadTimeData.getBoolean('offStoreInstallEnabled')) {
153 this.dragWrapper_ = new cr.ui.DragWrapper(document.documentElement,
157 extensions.PackExtensionOverlay.getInstance().initializePage();
159 // Hook up the configure commands link to the overlay.
160 var link = document.querySelector('.extension-commands-config');
161 link.addEventListener('click',
162 this.handleExtensionCommandsConfig_.bind(this));
164 // Initialize the Commands overlay.
165 extensions.ExtensionCommandsOverlay.getInstance().initializePage();
167 extensions.ExtensionErrorOverlay.getInstance().initializePage(
168 extensions.ExtensionSettings.showOverlay);
170 extensions.ExtensionOptionsOverlay.getInstance().initializePage(
171 extensions.ExtensionSettings.showOverlay);
173 // Initialize the kiosk overlay.
175 var kioskOverlay = extensions.KioskAppsOverlay.getInstance();
176 kioskOverlay.initialize();
178 $('add-kiosk-app').addEventListener('click', function() {
179 ExtensionSettings.showOverlay($('kiosk-apps-page'));
180 kioskOverlay.didShowPage();
183 extensions.KioskDisableBailoutConfirm.getInstance().initialize();
186 cr.ui.overlay.setupOverlay($('drop-target-overlay'));
187 cr.ui.overlay.globalInitialization();
189 extensions.ExtensionFocusManager.getInstance().initialize();
191 var path = document.location.pathname;
192 if (path.length > 1) {
193 // Skip starting slash and remove trailing slash (if any).
194 var overlayName = path.slice(1).replace(/\/$/, '');
195 if (overlayName == 'configureCommands')
196 this.showExtensionCommandsConfigUi_();
201 * Updates the Chrome Apps and Extensions Developer Tools promotion's
205 updatePromoVisibility_: function() {
206 var extensionSettings = $('extension-settings');
207 var visible = extensionSettings.classList.contains('dev-mode') &&
210 var adtPromo = $('apps-developer-tools-promo');
211 var controls = adtPromo.querySelectorAll('a, button');
212 Array.prototype.forEach.call(controls, function(control) {
213 control[visible ? 'removeAttribute' : 'setAttribute']('tabindex', '-1');
216 adtPromo.setAttribute('aria-hidden', !visible);
217 extensionSettings.classList.toggle('adt-promo', visible);
221 * Handles the Pack Extension button.
222 * @param {Event} e Change event.
225 handlePackExtension_: function(e) {
226 ExtensionSettings.showOverlay($('pack-extension-overlay'));
227 chrome.send('metricsHandler:recordAction', ['Options_PackExtension']);
231 * Shows the Extension Commands configuration UI.
232 * @param {Event} e Change event.
235 showExtensionCommandsConfigUi_: function(e) {
236 ExtensionSettings.showOverlay($('extension-commands-overlay'));
237 chrome.send('metricsHandler:recordAction',
238 ['Options_ExtensionCommands']);
242 * Handles the Configure (Extension) Commands link.
243 * @param {Event} e Change event.
246 handleExtensionCommandsConfig_: function(e) {
247 this.showExtensionCommandsConfigUi_();
251 * Handles the Update Extension Now button.
252 * @param {Event} e Change event.
255 handleUpdateExtensionNow_: function(e) {
256 chrome.send('extensionSettingsAutoupdate');
260 * Handles the Toggle Dev Mode button.
261 * @param {Event} e Change event.
264 handleToggleDevMode_: function(e) {
265 if ($('toggle-dev-on').checked) {
266 $('dev-controls').hidden = false;
267 window.setTimeout(function() {
268 $('extension-settings').classList.add('dev-mode');
271 $('extension-settings').classList.remove('dev-mode');
273 window.setTimeout(this.updatePromoVisibility_.bind(this), 0);
275 chrome.send('extensionSettingsToggleDeveloperMode');
279 * Called when a transition has ended for #dev-controls.
280 * @param {Event} e webkitTransitionEnd event.
283 handleDevControlsTransitionEnd_: function(e) {
284 if (e.propertyName == 'height' &&
285 !$('extension-settings').classList.contains('dev-mode')) {
286 $('dev-controls').hidden = true;
292 * Called by the dom_ui_ to re-populate the page with data representing
293 * the current state of installed extensions.
294 * @param {ExtensionDataResponse} extensionsData
296 ExtensionSettings.returnExtensionsData = function(extensionsData) {
297 // We can get called many times in short order, thus we need to
298 // be careful to remove the 'finished loading' timeout.
299 if (this.loadingTimeout_)
300 window.clearTimeout(this.loadingTimeout_);
301 document.documentElement.classList.add('loading');
302 this.loadingTimeout_ = window.setTimeout(function() {
303 document.documentElement.classList.remove('loading');
306 webuiResponded = true;
308 if (extensionsData.extensions.length > 0) {
309 // Enforce order specified in the data or (if equal) then sort by
310 // extension name (case-insensitive) followed by their ID (in the case
311 // where extensions have the same name).
312 extensionsData.extensions.sort(function(a, b) {
313 function compare(x, y) {
314 return x < y ? -1 : (x > y ? 1 : 0);
316 return compare(a.order, b.order) ||
317 compare(a.name.toLowerCase(), b.name.toLowerCase()) ||
322 var pageDiv = $('extension-settings');
324 if (extensionsData.profileIsSupervised) {
325 pageDiv.classList.add('profile-is-supervised');
327 pageDiv.classList.remove('profile-is-supervised');
329 if (extensionsData.profileIsSupervised) {
330 pageDiv.classList.add('showing-banner');
331 $('toggle-dev-on').disabled = true;
334 pageDiv.classList.remove('showing-banner');
335 $('toggle-dev-on').disabled = false;
338 pageDiv.style.marginTop = marginTop + 'px';
340 if (extensionsData.developerMode) {
341 pageDiv.classList.add('dev-mode');
342 $('toggle-dev-on').checked = true;
343 $('dev-controls').hidden = false;
345 pageDiv.classList.remove('dev-mode');
346 $('toggle-dev-on').checked = false;
349 ExtensionSettings.getInstance().displayPromo_ =
350 extensionsData.promoteAppsDevTools;
351 ExtensionSettings.getInstance().updatePromoVisibility_();
353 $('load-unpacked').disabled = extensionsData.loadUnpackedDisabled;
355 ExtensionsList.prototype.data_ = extensionsData;
356 var extensionList = $('extension-settings-list');
357 ExtensionsList.decorate(extensionList);
360 // Indicate that warning |message| has occured for pack of |crx_path| and
361 // |pem_path| files. Ask if user wants override the warning. Send
362 // |overrideFlags| to repeated 'pack' call to accomplish the override.
363 ExtensionSettings.askToOverrideWarning =
364 function(message, crx_path, pem_path, overrideFlags) {
365 var closeAlert = function() {
366 ExtensionSettings.showOverlay(null);
369 alertOverlay.setValues(
370 loadTimeData.getString('packExtensionWarningTitle'),
372 loadTimeData.getString('packExtensionProceedAnyway'),
373 loadTimeData.getString('cancel'),
375 chrome.send('pack', [crx_path, pem_path, overrideFlags]);
379 ExtensionSettings.showOverlay($('alertOverlay'));
383 * Returns the current overlay or null if one does not exist.
384 * @return {Element} The overlay element.
386 ExtensionSettings.getCurrentOverlay = function() {
387 return document.querySelector('#overlay .page.showing');
391 * Sets the given overlay to show. This hides whatever overlay is currently
393 * @param {HTMLElement} node The overlay page to show. If falsey, all overlays
396 ExtensionSettings.showOverlay = function(node) {
397 var pageDiv = $('extension-settings');
399 pageDiv.style.width = window.getComputedStyle(pageDiv).width;
400 document.body.classList.add('no-scroll');
402 document.body.classList.remove('no-scroll');
403 pageDiv.style.width = '';
406 var currentlyShowingOverlay = ExtensionSettings.getCurrentOverlay();
407 if (currentlyShowingOverlay)
408 currentlyShowingOverlay.classList.remove('showing');
411 node.classList.add('showing');
413 var pages = document.querySelectorAll('.page');
414 for (var i = 0; i < pages.length; i++) {
415 pages[i].setAttribute('aria-hidden', node ? 'true' : 'false');
418 $('overlay').hidden = !node;
419 uber.invokeMethodOnParent(node ? 'beginInterceptingEvents' :
420 'stopInterceptingEvents');
424 * Utility function to find the width of various UI strings and synchronize
425 * the width of relevant spans. This is crucial for making sure the
426 * Enable/Enabled checkboxes align, as well as the Developer Mode checkbox.
428 function measureCheckboxStrings() {
430 var measuringDiv = $('font-measuring-div');
431 measuringDiv.textContent =
432 loadTimeData.getString('extensionSettingsEnabled');
433 measuringDiv.className = 'enabled-text';
434 var pxWidth = measuringDiv.clientWidth + trashWidth;
435 measuringDiv.textContent =
436 loadTimeData.getString('extensionSettingsEnable');
437 measuringDiv.className = 'enable-text';
438 pxWidth = Math.max(measuringDiv.clientWidth + trashWidth, pxWidth);
439 measuringDiv.textContent =
440 loadTimeData.getString('extensionSettingsDeveloperMode');
441 measuringDiv.className = '';
442 pxWidth = Math.max(measuringDiv.clientWidth, pxWidth);
444 var style = document.createElement('style');
445 style.type = 'text/css';
447 '.enable-checkbox-text {' +
448 ' min-width: ' + (pxWidth - trashWidth) + 'px;' +
450 '#dev-toggle span {' +
451 ' min-width: ' + pxWidth + 'px;' +
453 document.querySelector('head').appendChild(style);
458 ExtensionSettings: ExtensionSettings
462 window.addEventListener('load', function(e) {
463 extensions.ExtensionSettings.getInstance().initialize();