summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/resources/extensions/sync_file_system_custom_bindings.js
blob: 03c6caa0aa5fb8a03be8a82f25273b1a2a8bf149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Custom bindings for the syncFileSystem API.

var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
var fileSystemNatives = requireNative('file_system_natives');
var syncFileSystemNatives = requireNative('sync_file_system');

chromeHidden.registerCustomHook('syncFileSystem', function(bindingsAPI) {
  var apiFunctions = bindingsAPI.apiFunctions;

  // Functions which take in an [instanceOf=FileEntry].
  function bindFileEntryFunction(functionName) {
    apiFunctions.setUpdateArgumentsPostValidate(
        functionName, function(entry, callback) {
      var fileSystemUrl = entry.toURL();
      return [fileSystemUrl, callback];
    });
  }
  ['getFileStatus']
      .forEach(bindFileEntryFunction);

  // Functions which take in an [instanceOf=DOMFileSystem].
  function bindFileSystemFunction(functionName) {
    apiFunctions.setUpdateArgumentsPostValidate(
        functionName, function(filesystem, callback) {
      var fileSystemUrl = filesystem.root.toURL();
      return [fileSystemUrl, callback];
    });
  }
  ['deleteFileSystem', 'getUsageAndQuota']
      .forEach(bindFileSystemFunction);

  // Functions which return an [instanceOf=DOMFileSystem].
  apiFunctions.setCustomCallback('requestFileSystem',
                                 function(name, request, response) {
    var result = null;
    if (response) {
      result = syncFileSystemNatives.GetSyncFileSystemObject(
          response.name, response.root);
    }
    if (request.callback)
      request.callback(result);
    request.callback = null;
  });
});

chromeHidden.Event.registerArgumentMassager(
    'syncFileSystem.onFileStatusChanged', function(args, dispatch) {
  // Make FileEntry object using all the base string fields.
  var fileSystemType = args[0];
  var fileSystemName = args[1];
  var rootUrl = args[2];
  var filePath = args[3];
  var fileEntry = fileSystemNatives.GetFileEntry(fileSystemType,
      fileSystemName, rootUrl, filePath, false);

  // Combine into a single dictionary.
  var fileInfo = new Object();
  fileInfo.fileEntry = fileEntry;
  fileInfo.status = args[4];
  if (fileInfo.status == "synced") {
    fileInfo.action = args[5];
    fileInfo.direction = args[6];
  }
  dispatch([fileInfo]);
});