- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / extensions / pack_extension_overlay.js
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 cr.define('extensions', function() {
6   /**
7    * PackExtensionOverlay class
8    * Encapsulated handling of the 'Pack Extension' overlay page.
9    * @constructor
10    */
11   function PackExtensionOverlay() {
12   }
13
14   cr.addSingletonGetter(PackExtensionOverlay);
15
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));
25
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     },
35
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     },
43
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.send('pack', [extensionPath, privateKeyPath, 0]);
52     },
53
54     /**
55      * Utility function which asks the C++ to show a platform-specific file
56      * select dialog, and fire |callback| with the |filePath| that resulted.
57      * |selectType| can be either 'file' or 'folder'. |operation| can be 'load'
58      * or 'pem' which are signals to the C++ to do some operation-specific
59      * configuration.
60      * @private
61      */
62     showFileDialog_: function(selectType, operation, callback) {
63       handleFilePathSelected = function(filePath) {
64         callback(filePath);
65         handleFilePathSelected = function() {};
66       };
67
68       chrome.send('packExtensionSelectFilePath', [selectType, operation]);
69     },
70
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_('folder', 'load', function(filePath) {
78         $('extension-root-dir').value = filePath;
79       });
80     },
81
82     /**
83      * Handles the showing of the extension private key file.
84      * @param {Event} e Change event.
85      * @private
86      */
87     handleBrowsePrivateKey_: function(e) {
88       this.showFileDialog_('file', 'pem', function(filePath) {
89         $('extension-private-key').value = filePath;
90       });
91     },
92   };
93
94   /**
95    * Wrap up the pack process by showing the success |message| and closing
96    * the overlay.
97    * @param {string} message The message to show to the user.
98    */
99   PackExtensionOverlay.showSuccessMessage = function(message) {
100     alertOverlay.setValues(
101         loadTimeData.getString('packExtensionOverlay'),
102         message,
103         loadTimeData.getString('ok'),
104         '',
105         function() {
106           extensions.ExtensionSettings.showOverlay(null);
107         },
108         null);
109     extensions.ExtensionSettings.showOverlay($('alertOverlay'));
110   };
111
112   /**
113    * Post an alert overlay showing |message|, and upon acknowledgement, close
114    * the alert overlay and return to showing the PackExtensionOverlay.
115    */
116   PackExtensionOverlay.showError = function(message) {
117     alertOverlay.setValues(
118         loadTimeData.getString('packExtensionErrorTitle'),
119         message,
120         loadTimeData.getString('ok'),
121         '',
122         function() {
123           extensions.ExtensionSettings.showOverlay($('pack-extension-overlay'));
124         },
125         null);
126     extensions.ExtensionSettings.showOverlay($('alertOverlay'));
127   };
128
129   // Export
130   return {
131     PackExtensionOverlay: PackExtensionOverlay
132   };
133 });