ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / resources / extensions / pack_extension_overlay.js
blob8cb7725d026d04bac48c85a440f1c90607b272f0
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 cr.define('extensions', function() {
6   /**
7    * PackExtensionOverlay class
8    * Encapsulated handling of the 'Pack Extension' overlay page.
9    * @constructor
10    */
11   function PackExtensionOverlay() {
12   }
14   cr.addSingletonGetter(PackExtensionOverlay);
16   PackExtensionOverlay.prototype = {
17     /**
18      * Initialize the page.
19      */
20     initializePage: function() {
21       var overlay = $('overlay');
22       cr.ui.overlay.setupOverlay(overlay);
23       cr.ui.overlay.globalInitialization();
24       overlay.addEventListener('cancelOverlay', this.handleDismiss_.bind(this));
26       $('pack-extension-dismiss').addEventListener('click',
27           this.handleDismiss_.bind(this));
28       $('pack-extension-commit').addEventListener('click',
29           this.handleCommit_.bind(this));
30       $('browse-extension-dir').addEventListener('click',
31           this.handleBrowseExtensionDir_.bind(this));
32       $('browse-private-key').addEventListener('click',
33           this.handleBrowsePrivateKey_.bind(this));
34     },
36     /**
37      * Handles a click on the dismiss button.
38      * @param {Event} e The click event.
39      */
40     handleDismiss_: function(e) {
41       extensions.ExtensionSettings.showOverlay(null);
42     },
44     /**
45      * Handles a click on the pack button.
46      * @param {Event} e The click event.
47      */
48     handleCommit_: function(e) {
49       var extensionPath = $('extension-root-dir').value;
50       var privateKeyPath = $('extension-private-key').value;
51       chrome.developerPrivate.packDirectory(
52           extensionPath, privateKeyPath, 0, this.onPackResponse_.bind(this));
53     },
55     /**
56      * Utility function which asks the C++ to show a platform-specific file
57      * select dialog, and set the value property of |node| to the selected path.
58      * @param {SelectType} selectType The type of selection to use.
59      * @param {FileType} fileType The type of file to select.
60      * @param {HTMLInputElement} node The node to set the value of.
61      * @private
62      */
63     showFileDialog_: function(selectType, fileType, node) {
64       chrome.developerPrivate.choosePath(selectType, fileType, function(path) {
65         // Last error is set if the user canceled the dialog.
66         if (!chrome.runtime.lastError && path)
67           node.value = path;
68       });
69     },
71     /**
72      * Handles the showing of the extension directory browser.
73      * @param {Event} e Change event.
74      * @private
75      */
76     handleBrowseExtensionDir_: function(e) {
77       this.showFileDialog_(
78           'FOLDER',
79           'LOAD',
80           /** @type {HTMLInputElement} */ ($('extension-root-dir')));
81     },
83     /**
84      * Handles the showing of the extension private key file.
85      * @param {Event} e Change event.
86      * @private
87      */
88     handleBrowsePrivateKey_: function(e) {
89       this.showFileDialog_(
90           'FILE',
91           'PEM',
92           /** @type {HTMLInputElement} */ ($('extension-private-key')));
93     },
95     /**
96      * Handles a response from a packDirectory call.
97      * @param {PackDirectoryResponse} response The response of the pack call.
98      * @private
99      */
100     onPackResponse_: function(response) {
101       /** @type {string} */
102       var alertTitle;
103       /** @type {string} */
104       var alertOk;
105       /** @type {string} */
106       var alertCancel;
107       /** @type {function()} */
108       var alertOkCallback;
109       /** @type {function()} */
110       var alertCancelCallback;
112       var closeAlert = function() {
113         extensions.ExtensionSettings.showOverlay(null);
114       };
116       // TODO(devlin): Once we expose enums on extension APIs, we should use
117       // those objects, instead of a string.
118       switch (response.status) {
119         case 'SUCCESS':
120           alertTitle = loadTimeData.getString('packExtensionOverlay');
121           alertOk = loadTimeData.getString('ok');
122           alertOkCallback = closeAlert;
123           // No 'Cancel' option.
124           break;
125         case 'WARNING':
126           alertTitle = loadTimeData.getString('packExtensionWarningTitle');
127           alertOk = loadTimeData.getString('packExtensionProceedAnyway');
128           alertCancel = loadTimeData.getString('cancel');
129           alertOkCallback = function() {
130             chrome.developerPrivate.packDirectory(
131                 response.item_path,
132                 response.pem_path,
133                 response.override_flags,
134                 this.onPackResponse_.bind(this));
135             closeAlert();
136           }.bind(this);
137           alertCancelCallback = closeAlert;
138           break;
139         case 'ERROR':
140           alertTitle = loadTimeData.getString('packExtensionErrorTitle');
141           alertOk = loadTimeData.getString('ok');
142           alertOkCallback = function() {
143             extensions.ExtensionSettings.showOverlay(
144                 $('pack-extension-overlay'));
145           };
146           // No 'Cancel' option.
147           break;
148         default:
149           assertNotReached();
150           return;
151       }
153       alertOverlay.setValues(alertTitle,
154                              response.message,
155                              alertOk,
156                              alertCancel,
157                              alertOkCallback,
158                              alertCancelCallback);
159       extensions.ExtensionSettings.showOverlay($('alertOverlay'));
160     },
161   };
163   // Export
164   return {
165     PackExtensionOverlay: PackExtensionOverlay
166   };