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
) {
15 mediaGalleriesMetadata
= {}; // Clear any previous metadata.
18 for (var i
= 0; i
< response
.length
; i
++) {
19 var filesystem
= mediaGalleriesNatives
.GetMediaFileSystemObject(
21 $Array
.push(result
, filesystem
);
22 var metadata
= response
[i
];
24 mediaGalleriesMetadata
[filesystem
.name
] = metadata
;
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
36 apiFunctions
.setCustomCallback('getMediaFileSystems',
37 function(name
, request
, response
) {
38 var result
= createFileSystemObjectsAndUpdateMetadata(response
);
40 request
.callback(result
);
41 request
.callback
= null;
44 apiFunctions
.setCustomCallback('addScanResults',
45 function(name
, request
, response
) {
46 var result
= createFileSystemObjectsAndUpdateMetadata(response
);
48 request
.callback(result
);
49 request
.callback
= null;
52 apiFunctions
.setCustomCallback('addUserSelectedFolder',
53 function(name
, request
, response
) {
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
;
66 request
.callback(fileSystems
, selectedFileSystemName
);
67 request
.callback
= null;
70 apiFunctions
.setHandleRequest('getMediaFileSystemMetadata',
71 function(filesystem
) {
72 if (filesystem
&& filesystem
.name
&&
73 mediaGalleriesMetadata
[filesystem
.name
]) {
74 return mediaGalleriesMetadata
[filesystem
.name
];
79 apiFunctions
.setUpdateArgumentsPostValidate('getMetadata',
80 function(mediaFile
, options
, callback
) {
81 var blobUuid
= blobNatives
.GetBlobUuid(mediaFile
)
82 return [blobUuid
, options
, callback
];
86 exports
.binding
= binding
.generate();