Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / extensions / pack_extension_overlay.js
blob05d23d1f2f9062f45b793b868a267d4e46d9f867
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', function() {
27         cr.dispatchSimpleEvent(overlay, 'cancelOverlay');
28       });
29       $('pack-extension-commit').addEventListener('click',
30           this.handleCommit_.bind(this));
31       $('browse-extension-dir').addEventListener('click',
32           this.handleBrowseExtensionDir_.bind(this));
33       $('browse-private-key').addEventListener('click',
34           this.handleBrowsePrivateKey_.bind(this));
35     },
37     /**
38      * Handles a click on the dismiss button.
39      * @param {Event} e The click event.
40      */
41     handleDismiss_: function(e) {
42       extensions.ExtensionSettings.showOverlay(null);
43     },
45     /**
46      * Handles a click on the pack button.
47      * @param {Event} e The click event.
48      */
49     handleCommit_: function(e) {
50       var extensionPath = $('extension-root-dir').value;
51       var privateKeyPath = $('extension-private-key').value;
52       chrome.developerPrivate.packDirectory(
53           extensionPath, privateKeyPath, 0, this.onPackResponse_.bind(this));
54     },
56     /**
57      * Utility function which asks the C++ to show a platform-specific file
58      * select dialog, and set the value property of |node| to the selected path.
59      * @param {chrome.developerPrivate.SelectType} selectType
60      *     The type of selection to use.
61      * @param {chrome.developerPrivate.FileType} fileType
62      *     The type of file to select.
63      * @param {HTMLInputElement} node The node to set the value of.
64      * @private
65      */
66     showFileDialog_: function(selectType, fileType, node) {
67       chrome.developerPrivate.choosePath(selectType, fileType, function(path) {
68         // Last error is set if the user canceled the dialog.
69         if (!chrome.runtime.lastError && path)
70           node.value = path;
71       });
72     },
74     /**
75      * Handles the showing of the extension directory browser.
76      * @param {Event} e Change event.
77      * @private
78      */
79     handleBrowseExtensionDir_: function(e) {
80       this.showFileDialog_(
81           chrome.developerPrivate.SelectType.FOLDER,
82           chrome.developerPrivate.FileType.LOAD,
83           /** @type {HTMLInputElement} */ ($('extension-root-dir')));
84     },
86     /**
87      * Handles the showing of the extension private key file.
88      * @param {Event} e Change event.
89      * @private
90      */
91     handleBrowsePrivateKey_: function(e) {
92       this.showFileDialog_(
93           chrome.developerPrivate.SelectType.FILE,
94           chrome.developerPrivate.FileType.PEM,
95           /** @type {HTMLInputElement} */ ($('extension-private-key')));
96     },
98     /**
99      * Handles a response from a packDirectory call.
100      * @param {PackDirectoryResponse} response The response of the pack call.
101      * @private
102      */
103     onPackResponse_: function(response) {
104       /** @type {string} */
105       var alertTitle;
106       /** @type {string} */
107       var alertOk;
108       /** @type {string} */
109       var alertCancel;
110       /** @type {function()} */
111       var alertOkCallback;
112       /** @type {function()} */
113       var alertCancelCallback;
115       var closeAlert = function() {
116         extensions.ExtensionSettings.showOverlay(null);
117       };
119       switch (response.status) {
120         case chrome.developerPrivate.PackStatus.SUCCESS:
121           alertTitle = loadTimeData.getString('packExtensionOverlay');
122           alertOk = loadTimeData.getString('ok');
123           alertOkCallback = closeAlert;
124           // No 'Cancel' option.
125           break;
126         case chrome.developerPrivate.PackStatus.WARNING:
127           alertTitle = loadTimeData.getString('packExtensionWarningTitle');
128           alertOk = loadTimeData.getString('packExtensionProceedAnyway');
129           alertCancel = loadTimeData.getString('cancel');
130           alertOkCallback = function() {
131             chrome.developerPrivate.packDirectory(
132                 response.item_path,
133                 response.pem_path,
134                 response.override_flags,
135                 this.onPackResponse_.bind(this));
136             closeAlert();
137           }.bind(this);
138           alertCancelCallback = closeAlert;
139           break;
140         case chrome.developerPrivate.PackStatus.ERROR:
141           alertTitle = loadTimeData.getString('packExtensionErrorTitle');
142           alertOk = loadTimeData.getString('ok');
143           alertOkCallback = function() {
144             extensions.ExtensionSettings.showOverlay(
145                 $('pack-extension-overlay'));
146           };
147           // No 'Cancel' option.
148           break;
149         default:
150           assertNotReached();
151           return;
152       }
154       alertOverlay.setValues(alertTitle,
155                              response.message,
156                              alertOk,
157                              alertCancel,
158                              alertOkCallback,
159                              alertCancelCallback);
160       extensions.ExtensionSettings.showOverlay($('alertOverlay'));
161     },
162   };
164   // Export
165   return {
166     PackExtensionOverlay: PackExtensionOverlay
167   };