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 syncFileSystem API.
7 var binding = require('binding').Binding.create('syncFileSystem');
9 var eventBindings = require('event_bindings');
10 var fileSystemNatives = requireNative('file_system_natives');
11 var syncFileSystemNatives = requireNative('sync_file_system');
13 binding.registerCustomHook(function(bindingsAPI) {
14 var apiFunctions = bindingsAPI.apiFunctions;
16 // Functions which take in an [instanceOf=FileEntry].
17 function bindFileEntryFunction(functionName) {
18 apiFunctions.setUpdateArgumentsPostValidate(
19 functionName, function(entry, callback) {
20 var fileSystemUrl = entry.toURL();
21 return [fileSystemUrl, callback];
24 $Array.forEach(['getFileStatus'], bindFileEntryFunction);
26 // Functions which take in a FileEntry array.
27 function bindFileEntryArrayFunction(functionName) {
28 apiFunctions.setUpdateArgumentsPostValidate(
29 functionName, function(entries, callback) {
30 var fileSystemUrlArray = [];
31 for (var i=0; i < entries.length; i++) {
32 $Array.push(fileSystemUrlArray, entries[i].toURL());
34 return [fileSystemUrlArray, callback];
37 $Array.forEach(['getFileStatuses'], bindFileEntryArrayFunction);
39 // Functions which take in an [instanceOf=DOMFileSystem].
40 function bindFileSystemFunction(functionName) {
41 apiFunctions.setUpdateArgumentsPostValidate(
42 functionName, function(filesystem, callback) {
43 var fileSystemUrl = filesystem.root.toURL();
44 return [fileSystemUrl, callback];
47 $Array.forEach(['getUsageAndQuota'], bindFileSystemFunction);
49 // Functions which return an [instanceOf=DOMFileSystem].
50 apiFunctions.setCustomCallback('requestFileSystem',
51 function(name, request, response) {
54 result = syncFileSystemNatives.GetSyncFileSystemObject(
55 response.name, response.root);
58 request.callback(result);
59 request.callback = null;
62 // Functions which return an array of FileStatusInfo object
63 // which has [instanceOf=FileEntry].
64 apiFunctions.setCustomCallback('getFileStatuses',
65 function(name, request, response) {
68 for (var i = 0; i < response.length; i++) {
70 var entry = response[i].entry;
71 result.fileEntry = fileSystemNatives.GetFileEntry(
77 result.status = response[i].status;
78 result.error = response[i].error;
79 $Array.push(results, result);
83 request.callback(results);
84 request.callback = null;
88 eventBindings.registerArgumentMassager(
89 'syncFileSystem.onFileStatusChanged', function(args, dispatch) {
90 // Make FileEntry object using all the base string fields.
91 var fileEntry = fileSystemNatives.GetFileEntry(
92 args[0].fileSystemType,
93 args[0].fileSystemName,
98 // Combine into a single dictionary.
99 var fileInfo = new Object();
100 fileInfo.fileEntry = fileEntry;
101 fileInfo.status = args[1];
102 if (fileInfo.status == "synced") {
103 fileInfo.action = args[2];
104 fileInfo.direction = args[3];
106 dispatch([fileInfo]);
109 exports.binding = binding.generate();