Add testing/scripts/OWNERS
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / file_manager_private_custom_bindings.js
blob9d374aea40526f5f509739ce21f73e992639e4e1
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 fileManagerPrivate API.
7 // Bindings
8 var binding = require('binding').Binding.create('fileManagerPrivate');
9 var eventBindings = require('event_bindings');
11 // Natives
12 var fileManagerPrivateNatives = requireNative('file_manager_private');
13 var fileBrowserHandlerNatives = requireNative('file_browser_handler');
15 // Internals
16 var fileManagerPrivateInternal =
17     require('binding').Binding.create('fileManagerPrivateInternal').generate();
19 // Shorthands
20 var GetFileSystem = fileManagerPrivateNatives.GetFileSystem;
21 var GetExternalFileEntry = fileBrowserHandlerNatives.GetExternalFileEntry;
23 binding.registerCustomHook(function(bindingsAPI) {
24   var apiFunctions = bindingsAPI.apiFunctions;
26   apiFunctions.setCustomCallback('requestFileSystem',
27                                  function(name, request, response) {
28     var fs = null;
29     if (response && !response.error)
30       fs = GetFileSystem(response.name, response.root_url);
31     if (request.callback)
32       request.callback(fs);
33     request.callback = null;
34   });
36   apiFunctions.setCustomCallback('searchDrive',
37                                  function(name, request, response) {
38     if (response && !response.error && response.entries) {
39       response.entries = response.entries.map(function(entry) {
40         return GetExternalFileEntry(entry);
41       });
42     }
44     // So |request.callback| doesn't break if response is not defined.
45     if (!response)
46       response = {};
48     if (request.callback)
49       request.callback(response.entries, response.nextFeed);
50     request.callback = null;
51   });
53   apiFunctions.setCustomCallback('searchDriveMetadata',
54                                  function(name, request, response) {
55     if (response && !response.error) {
56       for (var i = 0; i < response.length; i++) {
57         response[i].entry =
58             GetExternalFileEntry(response[i].entry);
59       }
60     }
62     // So |request.callback| doesn't break if response is not defined.
63     if (!response)
64       response = {};
66     if (request.callback)
67       request.callback(response);
68     request.callback = null;
69   });
71   apiFunctions.setHandleRequest('resolveIsolatedEntries',
72                                 function(entries, callback) {
73     var urls = entries.map(function(entry) {
74       return fileBrowserHandlerNatives.GetEntryURL(entry);
75     });
76     fileManagerPrivateInternal.resolveIsolatedEntries(urls, function(
77         entryDescriptions) {
78       callback(entryDescriptions.map(function(description) {
79         return GetExternalFileEntry(description);
80       }));
81     });
82   });
83 });
85 eventBindings.registerArgumentMassager(
86     'fileManagerPrivate.onDirectoryChanged', function(args, dispatch) {
87   // Convert the entry arguments into a real Entry object.
88   args[0].entry = GetExternalFileEntry(args[0].entry);
89   dispatch(args);
90 });
92 exports.binding = binding.generate();