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 blobsAwaitingMetadata
= {};
12 var mediaGalleriesMetadata
= {};
14 function createFileSystemObjectsAndUpdateMetadata(response
) {
16 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
, callback
, response
) {
38 var result
= createFileSystemObjectsAndUpdateMetadata(response
);
43 apiFunctions
.setCustomCallback('addScanResults',
44 function(name
, request
, callback
, response
) {
45 var result
= createFileSystemObjectsAndUpdateMetadata(response
);
50 apiFunctions
.setCustomCallback('addUserSelectedFolder',
51 function(name
, request
, callback
, response
) {
53 var selectedFileSystemName
= "";
54 if (response
&& 'mediaFileSystems' in response
&&
55 'selectedFileSystemIndex' in response
) {
56 fileSystems
= createFileSystemObjectsAndUpdateMetadata(
57 response
['mediaFileSystems']);
58 var selectedFileSystemIndex
= response
['selectedFileSystemIndex'];
59 if (selectedFileSystemIndex
>= 0) {
60 selectedFileSystemName
= fileSystems
[selectedFileSystemIndex
].name
;
64 callback(fileSystems
, selectedFileSystemName
);
67 apiFunctions
.setCustomCallback('dropPermissionForMediaFileSystem',
68 function(name
, request
, callback
, response
) {
69 var galleryId
= response
;
72 for (var key
in mediaGalleriesMetadata
) {
73 if (mediaGalleriesMetadata
[key
].galleryId
== galleryId
) {
74 delete mediaGalleriesMetadata
[key
];
83 apiFunctions
.setHandleRequest('getMediaFileSystemMetadata',
84 function(filesystem
) {
85 if (filesystem
&& filesystem
.name
&&
86 filesystem
.name
in mediaGalleriesMetadata
) {
87 return mediaGalleriesMetadata
[filesystem
.name
];
93 'isMediaDevice': false,
98 apiFunctions
.setUpdateArgumentsPostValidate('getMetadata',
99 function(mediaFile
, options
, callback
) {
100 var blobUuid
= blobNatives
.GetBlobUuid(mediaFile
)
101 // Store the blob in a global object to keep its refcount nonzero -- this
102 // prevents the object from being garbage collected before any metadata
103 // parsing gets to occur (see crbug.com/415792).
104 blobsAwaitingMetadata
[blobUuid
] = mediaFile
;
105 return [blobUuid
, options
, callback
];
108 apiFunctions
.setCustomCallback('getMetadata',
109 function(name
, request
, callback
, response
) {
110 if (response
.attachedImagesBlobInfo
) {
111 for (var i
= 0; i
< response
.attachedImagesBlobInfo
.length
; i
++) {
112 var blobInfo
= response
.attachedImagesBlobInfo
[i
];
113 var blob
= blobNatives
.TakeBrowserProcessBlob(
114 blobInfo
.blobUUID
, blobInfo
.type
, blobInfo
.size
);
115 response
.metadata
.attachedImages
.push(blob
);
120 callback(response
.metadata
);
122 // The UUID was in position 0 in the setUpdateArgumentsPostValidate
124 var uuid
= request
.args
[0];
125 delete blobsAwaitingMetadata
[uuid
];
129 exports
.binding
= binding
.generate();