- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / renderer / resources / extensions / sync_file_system_custom_bindings.js
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 syncFileSystem API.
6
7 var binding = require('binding').Binding.create('syncFileSystem');
8
9 var eventBindings = require('event_bindings');
10 var fileSystemNatives = requireNative('file_system_natives');
11 var syncFileSystemNatives = requireNative('sync_file_system');
12
13 binding.registerCustomHook(function(bindingsAPI) {
14   var apiFunctions = bindingsAPI.apiFunctions;
15
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];
22     });
23   }
24   $Array.forEach(['getFileStatus'], bindFileEntryFunction);
25
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());
33       }
34       return [fileSystemUrlArray, callback];
35     });
36   }
37   $Array.forEach(['getFileStatuses'], bindFileEntryArrayFunction);
38
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];
45     });
46   }
47   $Array.forEach(['getUsageAndQuota'], bindFileSystemFunction);
48
49   // Functions which return an [instanceOf=DOMFileSystem].
50   apiFunctions.setCustomCallback('requestFileSystem',
51                                  function(name, request, response) {
52     var result = null;
53     if (response) {
54       result = syncFileSystemNatives.GetSyncFileSystemObject(
55           response.name, response.root);
56     }
57     if (request.callback)
58       request.callback(result);
59     request.callback = null;
60   });
61
62   // Functions which return an array of FileStatusInfo object
63   // which has [instanceOf=FileEntry].
64   apiFunctions.setCustomCallback('getFileStatuses',
65                                  function(name, request, response) {
66     var results = [];
67     if (response) {
68       for (var i = 0; i < response.length; i++) {
69         var result = {};
70         var entry = response[i].entry;
71         result.fileEntry = fileSystemNatives.GetFileEntry(
72             entry.fileSystemType,
73             entry.fileSystemName,
74             entry.rootUrl,
75             entry.filePath,
76             entry.isDirectory);
77         result.status = response[i].status;
78         result.error = response[i].error;
79         $Array.push(results, result);
80       }
81     }
82     if (request.callback)
83       request.callback(results);
84     request.callback = null;
85   });
86 });
87
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,
94       args[0].rootUrl,
95       args[0].filePath,
96       args[0].isDirectory);
97
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];
105   }
106   dispatch([fileInfo]);
107 });
108
109 exports.binding = binding.generate();