Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / file_system_custom_bindings.js
blob62a9ad6889f033218826e091121e4345e019de13
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];
27     });
28   }
29   $Array.forEach(['getDisplayPath', 'getWritableEntry', 'isWritableEntry'],
30                   bindFileEntryFunction);
32   $Array.forEach(['getWritableEntry', 'chooseEntry', 'restoreEntry'],
33                   function(functionName) {
34     bindFileEntryCallback(functionName, apiFunctions);
35   });
37   apiFunctions.setHandleRequest('retainEntry', function(fileEntry) {
38     var id = entryIdManager.getEntryId(fileEntry);
39     if (!id)
40       return '';
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, {});
46     return id;
47   });
49   apiFunctions.setHandleRequest('isRestorable',
50       function(id, callback) {
51     var savedEntry = entryIdManager.getEntryById(id);
52     if (savedEntry) {
53       sendRequest.safeCallbackApply(
54           'fileSystem.isRestorable',
55           {},
56           callback,
57           [true]);
58     } else {
59       sendRequest.sendRequest(
60           this.name, [id, callback], this.definition.parameters, {});
61     }
62   });
64   apiFunctions.setUpdateArgumentsPostValidate('restoreEntry',
65       function(id, callback) {
66     var savedEntry = entryIdManager.getEntryById(id);
67     if (savedEntry) {
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',
72           {},
73           callback,
74           [savedEntry]);
75       return [id, false, null];
76     } else {
77       // Ask the browser process for a new file entry for this id, to be passed
78       // to |callback|.
79       return [id, true, callback];
80     }
81   });
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);
89     }
90     sendRequest.safeCallbackApply(
91         'fileSystem.requestFileSystem',
92         request,
93         callback,
94         [fileSystem]);
95   });
97   apiFunctions.setCustomCallback('getVolumeList',
98       function(name, request, callback, response) {
99     var volumeList = response || null;
100     sendRequest.safeCallbackApply(
101         'fileSystem.getVolumeList',
102         request,
103         callback,
104         [volumeList]);
105   });
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);
112   };
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);
118   };
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);
124   };
127 exports.bindFileEntryCallback = bindFileEntryCallback;
128 exports.binding = binding.generate();