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