OLD | NEW |
| (Empty) |
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 // Custom binding for the fileSystem API. | |
6 | |
7 var binding = apiBridge || require('binding').Binding.create('fileSystem'); | |
8 var sendRequest = bindingUtil ? | |
9 $Function.bind(bindingUtil.sendRequest, bindingUtil) : | |
10 require('sendRequest').sendRequest; | |
11 var getFileBindingsForApi = | |
12 require('fileEntryBindingUtil').getFileBindingsForApi; | |
13 var fileBindings = getFileBindingsForApi('fileSystem'); | |
14 var bindFileEntryCallback = fileBindings.bindFileEntryCallback; | |
15 var entryIdManager = fileBindings.entryIdManager; | |
16 var fileSystemNatives = requireNative('file_system_natives'); | |
17 var safeCallbackApply = require('uncaught_exception_handler').safeCallbackApply; | |
18 | |
19 binding.registerCustomHook(function(bindingsAPI) { | |
20 var apiFunctions = bindingsAPI.apiFunctions; | |
21 var fileSystem = bindingsAPI.compiledApi; | |
22 | |
23 function bindFileEntryFunction(functionName) { | |
24 apiFunctions.setUpdateArgumentsPostValidate( | |
25 functionName, function(fileEntry, callback) { | |
26 var fileSystemName = fileEntry.filesystem.name; | |
27 var relativePath = $String.slice(fileEntry.fullPath, 1); | |
28 return [fileSystemName, relativePath, callback]; | |
29 }); | |
30 } | |
31 $Array.forEach(['getDisplayPath', 'getWritableEntry', 'isWritableEntry'], | |
32 bindFileEntryFunction); | |
33 | |
34 $Array.forEach(['getWritableEntry', 'chooseEntry', 'restoreEntry'], | |
35 function(functionName) { | |
36 bindFileEntryCallback(functionName, apiFunctions); | |
37 }); | |
38 | |
39 apiFunctions.setHandleRequest('retainEntry', function(fileEntry) { | |
40 var id = entryIdManager.getEntryId(fileEntry); | |
41 if (!id) | |
42 return ''; | |
43 var fileSystemName = fileEntry.filesystem.name; | |
44 var relativePath = $String.slice(fileEntry.fullPath, 1); | |
45 | |
46 sendRequest('fileSystem.retainEntry', [id, fileSystemName, relativePath], | |
47 bindingUtil ? undefined : this.definition.parameters); | |
48 return id; | |
49 }); | |
50 | |
51 apiFunctions.setHandleRequest('isRestorable', | |
52 function(id, callback) { | |
53 var savedEntry = entryIdManager.getEntryById(id); | |
54 if (savedEntry) { | |
55 safeCallbackApply('fileSystem.isRestorable', {}, callback, [true]); | |
56 } else { | |
57 sendRequest('fileSystem.isRestorable', [id, callback], | |
58 bindingUtil ? undefined : this.definition.parameters); | |
59 } | |
60 }); | |
61 | |
62 apiFunctions.setUpdateArgumentsPostValidate('restoreEntry', | |
63 function(id, callback) { | |
64 var savedEntry = entryIdManager.getEntryById(id); | |
65 if (savedEntry) { | |
66 // We already have a file entry for this id so pass it to the callback and | |
67 // send a request to the browser to move it to the back of the LRU. | |
68 safeCallbackApply('fileSystem.restoreEntry', {}, callback, [savedEntry]); | |
69 return [id, false, null]; | |
70 } else { | |
71 // Ask the browser process for a new file entry for this id, to be passed | |
72 // to |callback|. | |
73 return [id, true, callback]; | |
74 } | |
75 }); | |
76 | |
77 apiFunctions.setCustomCallback('requestFileSystem', | |
78 function(name, request, callback, response) { | |
79 var fileSystem; | |
80 if (response && response.file_system_id) { | |
81 fileSystem = fileSystemNatives.GetIsolatedFileSystem( | |
82 response.file_system_id, response.file_system_path); | |
83 } | |
84 safeCallbackApply('fileSystem.requestFileSystem', request, callback, | |
85 [fileSystem]); | |
86 }); | |
87 | |
88 // TODO(benwells): Remove these deprecated versions of the functions. | |
89 fileSystem.getWritableFileEntry = function() { | |
90 console.log("chrome.fileSystem.getWritableFileEntry is deprecated"); | |
91 console.log("Please use chrome.fileSystem.getWritableEntry instead"); | |
92 $Function.apply(fileSystem.getWritableEntry, this, arguments); | |
93 }; | |
94 | |
95 fileSystem.isWritableFileEntry = function() { | |
96 console.log("chrome.fileSystem.isWritableFileEntry is deprecated"); | |
97 console.log("Please use chrome.fileSystem.isWritableEntry instead"); | |
98 $Function.apply(fileSystem.isWritableEntry, this, arguments); | |
99 }; | |
100 | |
101 fileSystem.chooseFile = function() { | |
102 console.log("chrome.fileSystem.chooseFile is deprecated"); | |
103 console.log("Please use chrome.fileSystem.chooseEntry instead"); | |
104 $Function.apply(fileSystem.chooseEntry, this, arguments); | |
105 }; | |
106 }); | |
107 | |
108 if (!apiBridge) | |
109 exports.$set('binding', binding.generate()); | |
OLD | NEW |