NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / media_galleries_custom_bindings.js
blob8f4de453b46086b83a25b418b345deb53bb0d960
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 Media Gallery API.
7 var binding = require('binding').Binding.create('mediaGalleries');
8 var blobNatives = requireNative('blob_natives');
9 var mediaGalleriesNatives = requireNative('mediaGalleries');
11 var mediaGalleriesMetadata = {};
13 function createFileSystemObjectsAndUpdateMetadata(response) {
14 var result = null;
15 mediaGalleriesMetadata = {}; // Clear any previous metadata.
16 if (response) {
17 result = [];
18 for (var i = 0; i < response.length; i++) {
19 var filesystem = mediaGalleriesNatives.GetMediaFileSystemObject(
20 response[i].fsid);
21 $Array.push(result, filesystem);
22 var metadata = response[i];
23 delete metadata.fsid;
24 mediaGalleriesMetadata[filesystem.name] = metadata;
27 return result;
30 binding.registerCustomHook(function(bindingsAPI, extensionId) {
31 var apiFunctions = bindingsAPI.apiFunctions;
33 // getMediaFileSystems, addUserSelectedFolder, and addScanResults use a
34 // custom callback so that they can instantiate and return an array of file
35 // system objects.
36 apiFunctions.setCustomCallback('getMediaFileSystems',
37 function(name, request, response) {
38 var result = createFileSystemObjectsAndUpdateMetadata(response);
39 if (request.callback)
40 request.callback(result);
41 request.callback = null;
42 });
44 apiFunctions.setCustomCallback('addScanResults',
45 function(name, request, response) {
46 var result = createFileSystemObjectsAndUpdateMetadata(response);
47 if (request.callback)
48 request.callback(result);
49 request.callback = null;
50 });
52 apiFunctions.setCustomCallback('addUserSelectedFolder',
53 function(name, request, response) {
54 var fileSystems = [];
55 var selectedFileSystemName = "";
56 if (response && 'mediaFileSystems' in response &&
57 'selectedFileSystemIndex' in response) {
58 fileSystems = createFileSystemObjectsAndUpdateMetadata(
59 response['mediaFileSystems']);
60 var selectedFileSystemIndex = response['selectedFileSystemIndex'];
61 if (selectedFileSystemIndex >= 0) {
62 selectedFileSystemName = fileSystems[selectedFileSystemIndex].name;
65 if (request.callback)
66 request.callback(fileSystems, selectedFileSystemName);
67 request.callback = null;
68 });
70 apiFunctions.setHandleRequest('getMediaFileSystemMetadata',
71 function(filesystem) {
72 if (filesystem && filesystem.name &&
73 mediaGalleriesMetadata[filesystem.name]) {
74 return mediaGalleriesMetadata[filesystem.name];
76 return {};
77 });
79 apiFunctions.setUpdateArgumentsPostValidate('getMetadata',
80 function(mediaFile, options, callback) {
81 var blobUuid = blobNatives.GetBlobUuid(mediaFile)
82 return [blobUuid, options, callback];
83 });
84 });
86 exports.binding = binding.generate();