Update all remaining Files app's private APIs to work on entries.
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / file_manager_private_custom_bindings.js
blob281cddc39327fbfc5c4ebd0caee38b487a0361ef
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 // Custom binding for the fileManagerPrivate API.
7 // Bindings
8 var binding = require('binding').Binding.create('fileManagerPrivate');
9 var eventBindings = require('event_bindings');
11 // Natives
12 var fileManagerPrivateNatives = requireNative('file_manager_private');
13 var fileBrowserHandlerNatives = requireNative('file_browser_handler');
15 // Internals
16 var fileManagerPrivateInternal =
17     require('binding').Binding.create('fileManagerPrivateInternal').generate();
19 // Shorthands
20 var GetFileSystem = fileManagerPrivateNatives.GetFileSystem;
21 var GetExternalFileEntry = fileBrowserHandlerNatives.GetExternalFileEntry;
23 binding.registerCustomHook(function(bindingsAPI) {
24   var apiFunctions = bindingsAPI.apiFunctions;
26   apiFunctions.setCustomCallback('searchDrive',
27       function(name, request, callback, response) {
28     if (response && !response.error && response.entries) {
29       response.entries = response.entries.map(function(entry) {
30         return GetExternalFileEntry(entry);
31       });
32     }
34     // So |callback| doesn't break if response is not defined.
35     if (!response)
36       response = {};
38     if (callback)
39       callback(response.entries, response.nextFeed);
40   });
42   apiFunctions.setCustomCallback('searchDriveMetadata',
43       function(name, request, callback, response) {
44     if (response && !response.error) {
45       for (var i = 0; i < response.length; i++) {
46         response[i].entry =
47             GetExternalFileEntry(response[i].entry);
48       }
49     }
51     // So |callback| doesn't break if response is not defined.
52     if (!response)
53       response = {};
55     if (callback)
56       callback(response);
57   });
59   apiFunctions.setHandleRequest('resolveIsolatedEntries',
60                                 function(entries, callback) {
61     var urls = entries.map(function(entry) {
62       return fileBrowserHandlerNatives.GetEntryURL(entry);
63     });
64     fileManagerPrivateInternal.resolveIsolatedEntries(urls, function(
65         entryDescriptions) {
66       callback(entryDescriptions.map(function(description) {
67         return GetExternalFileEntry(description);
68       }));
69     });
70   });
72   apiFunctions.setHandleRequest('getEntryProperties',
73                                 function(entries, names, callback) {
74     var urls = entries.map(function(entry) {
75       return fileBrowserHandlerNatives.GetEntryURL(entry);
76     });
77     fileManagerPrivateInternal.getEntryProperties(urls, names, callback);
78   });
80   apiFunctions.setHandleRequest('addFileWatch', function(entry, callback) {
81     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
82     fileManagerPrivateInternal.addFileWatch(url, callback);
83   });
85   apiFunctions.setHandleRequest('removeFileWatch', function(entry, callback) {
86     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
87     fileManagerPrivateInternal.removeFileWatch(url, callback);
88   });
90   apiFunctions.setHandleRequest('getEntryActions', function(entry, callback) {
91     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
92     fileManagerPrivateInternal.getEntryActions(url, callback);
93   });
95   apiFunctions.setHandleRequest('executeEntryAction', function(
96         entry, actionId, callback) {
97     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
98     fileManagerPrivateInternal.executeEntryAction(url, actionId, callback);
99   });
101   apiFunctions.setHandleRequest('computeChecksum', function(entry, callback) {
102     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
103     fileManagerPrivateInternal.computeChecksum(url, callback);
104   });
106   apiFunctions.setHandleRequest('getMimeType', function(entry, callback) {
107     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
108     fileManagerPrivateInternal.getMimeType(url, callback);
109   });
111   apiFunctions.setHandleRequest('pinDriveFile', function(entry, pin, callback) {
112     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
113     fileManagerPrivateInternal.pinDriveFile(url, pin, callback);
114   });
116   apiFunctions.setHandleRequest('executeTask',
117       function(taskId, entries, callback) {
118         var urls = entries.map(function(entry) {
119           return fileBrowserHandlerNatives.GetEntryURL(entry);
120         });
121         fileManagerPrivateInternal.executeTask(taskId, urls, callback);
122       });
124   apiFunctions.setHandleRequest('setDefaultTask',
125       function(taskId, entries, mimeTypes, callback) {
126         var urls = entries.map(function(entry) {
127           return fileBrowserHandlerNatives.GetEntryURL(entry);
128         });
129         fileManagerPrivateInternal.setDefaultTask(
130             taskId, urls, mimeTypes, callback);
131       });
133   apiFunctions.setHandleRequest('getFileTasks', function(entries, callback) {
134     var urls = entries.map(function(entry) {
135       return fileBrowserHandlerNatives.GetEntryURL(entry);
136     });
137     fileManagerPrivateInternal.getFileTasks(urls, callback);
138   });
140   apiFunctions.setHandleRequest('getShareUrl', function(entry, callback) {
141     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
142     fileManagerPrivateInternal.getShareUrl(url, callback);
143   });
145   apiFunctions.setHandleRequest('getDownloadUrl', function(entry, callback) {
146     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
147     fileManagerPrivateInternal.getDownloadUrl(url, callback);
148   });
150   apiFunctions.setHandleRequest('requestDriveShare', function(
151         entry, shareType, callback) {
152     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
153     fileManagerPrivateInternal.requestDriveShare(url, shareType, callback);
154   });
156   apiFunctions.setHandleRequest('setEntryTag', function(
157         entry, visibility, key, value, callback) {
158     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
159     fileManagerPrivateInternal.setEntryTag(
160         url, visibility, key, value, callback);
161   });
163   apiFunctions.setHandleRequest('cancelFileTransfers', function(
164         entries, callback) {
165     var urls = entries.map(function(entry) {
166       return fileBrowserHandlerNatives.cancelFileTransfers(entry);
167     });
168     fileManagerPrivateInternal.cancelFileTransfers(urls, callback);
169   });
171   apiFunctions.setHandleRequest('startCopy', function(
172         entry, parentEntry, newName, callback) {
173     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
174     var parentUrl = fileBrowserHandlerNatives.GetEntryURL(parentEntry);
175     fileManagerPrivateInternal.startCopy(
176         url, parentUrl, newName, callback);
177   });
179   apiFunctions.setHandleRequest('zipSelection', function(
180         parentEntry, entries, destName, callback) {
181     var parentUrl = fileBrowserHandlerNatives.GetEntryURL(parentEntry);
182     var urls = entries.map(function(entry) {
183       return fileBrowserHandlerNatives.cancelFileTransfers(entry);
184     });
185     fileManagerPrivateInternal.zipSelection(
186         parentUrl, urls, destName, callback);
187   });
189   apiFunctions.setHandleRequest('validatePathNameLength', function(
190         entry, name, callback) {
191     var url = fileBrowserHandlerNatives.GetEntryURL(entry);
192     fileManagerPrivateInternal.validatePathNameLength(url, name, callback);
193   });
196 eventBindings.registerArgumentMassager(
197     'fileManagerPrivate.onDirectoryChanged', function(args, dispatch) {
198   // Convert the entry arguments into a real Entry object.
199   args[0].entry = GetExternalFileEntry(args[0].entry);
200   dispatch(args);
203 exports.binding = binding.generate();