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 fileSystem API.
7 var binding = require('binding').Binding.create('fileSystem');
8 var sendRequest = require('sendRequest');
10 var getFileBindingsForApi =
11 require('fileEntryBindingUtil').getFileBindingsForApi;
12 var fileBindings = getFileBindingsForApi('fileSystem');
13 var bindFileEntryCallback = fileBindings.bindFileEntryCallback;
14 var entryIdManager = fileBindings.entryIdManager;
15 var fileSystemNatives = requireNative('file_system_natives');
17 binding.registerCustomHook(function(bindingsAPI) {
18 var apiFunctions = bindingsAPI.apiFunctions;
19 var fileSystem = bindingsAPI.compiledApi;
21 function bindFileEntryFunction(functionName) {
22 apiFunctions.setUpdateArgumentsPostValidate(
23 functionName, function(fileEntry, callback) {
24 var fileSystemName = fileEntry.filesystem.name;
25 var relativePath = $String.slice(fileEntry.fullPath, 1);
26 return [fileSystemName, relativePath, callback];
29 $Array.forEach(['getDisplayPath', 'getWritableEntry', 'isWritableEntry'],
30 bindFileEntryFunction);
32 $Array.forEach(['getWritableEntry', 'chooseEntry', 'restoreEntry'],
33 function(functionName) {
34 bindFileEntryCallback(functionName, apiFunctions);
37 apiFunctions.setHandleRequest('retainEntry', function(fileEntry) {
38 var id = entryIdManager.getEntryId(fileEntry);
41 var fileSystemName = fileEntry.filesystem.name;
42 var relativePath = $String.slice(fileEntry.fullPath, 1);
44 sendRequest.sendRequest(this.name, [id, fileSystemName, relativePath],
45 this.definition.parameters, {});
49 apiFunctions.setHandleRequest('isRestorable',
50 function(id, callback) {
51 var savedEntry = entryIdManager.getEntryById(id);
53 sendRequest.safeCallbackApply(
54 'fileSystem.isRestorable',
59 sendRequest.sendRequest(
60 this.name, [id, callback], this.definition.parameters, {});
64 apiFunctions.setUpdateArgumentsPostValidate('restoreEntry',
65 function(id, callback) {
66 var savedEntry = entryIdManager.getEntryById(id);
68 // We already have a file entry for this id so pass it to the callback and
69 // send a request to the browser to move it to the back of the LRU.
70 sendRequest.safeCallbackApply(
71 'fileSystem.restoreEntry',
75 return [id, false, null];
77 // Ask the browser process for a new file entry for this id, to be passed
79 return [id, true, callback];
83 apiFunctions.setCustomCallback('requestFileSystem',
84 function(name, request, callback, response) {
85 var fileSystem = null;
86 if (response && response.file_system_id) {
87 fileSystem = fileSystemNatives.GetIsolatedFileSystem(
88 response.file_system_id, response.file_system_path);
90 sendRequest.safeCallbackApply(
91 'fileSystem.requestFileSystem',
97 apiFunctions.setCustomCallback('getVolumeList',
98 function(name, request, callback, response) {
99 var volumeList = response || null;
100 sendRequest.safeCallbackApply(
101 'fileSystem.getVolumeList',
107 // TODO(benwells): Remove these deprecated versions of the functions.
108 fileSystem.getWritableFileEntry = function() {
109 console.log("chrome.fileSystem.getWritableFileEntry is deprecated");
110 console.log("Please use chrome.fileSystem.getWritableEntry instead");
111 $Function.apply(fileSystem.getWritableEntry, this, arguments);
114 fileSystem.isWritableFileEntry = function() {
115 console.log("chrome.fileSystem.isWritableFileEntry is deprecated");
116 console.log("Please use chrome.fileSystem.isWritableEntry instead");
117 $Function.apply(fileSystem.isWritableEntry, this, arguments);
120 fileSystem.chooseFile = function() {
121 console.log("chrome.fileSystem.chooseFile is deprecated");
122 console.log("Please use chrome.fileSystem.chooseEntry instead");
123 $Function.apply(fileSystem.chooseEntry, this, arguments);
127 exports.bindFileEntryCallback = bindFileEntryCallback;
128 exports.binding = binding.generate();