Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / renderer / resources / extensions / media_galleries_custom_bindings.js
blob7c52a3826fd85687c1bf9e069c657022cd4d6677
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) {
15 var result = [];
16 mediaGalleriesMetadata = {}; // Clear any previous metadata.
17 if (response) {
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, callback, response) {
38 var result = createFileSystemObjectsAndUpdateMetadata(response);
39 if (callback)
40 callback(result);
41 });
43 apiFunctions.setCustomCallback('addScanResults',
44 function(name, request, callback, response) {
45 var result = createFileSystemObjectsAndUpdateMetadata(response);
46 if (callback)
47 callback(result);
48 });
50 apiFunctions.setCustomCallback('addUserSelectedFolder',
51 function(name, request, callback, response) {
52 var fileSystems = [];
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;
63 if (callback)
64 callback(fileSystems, selectedFileSystemName);
65 });
67 apiFunctions.setCustomCallback('dropPermissionForMediaFileSystem',
68 function(name, request, callback, response) {
69 var galleryId = response;
71 if (galleryId) {
72 for (var key in mediaGalleriesMetadata) {
73 if (mediaGalleriesMetadata[key].galleryId == galleryId) {
74 delete mediaGalleriesMetadata[key];
75 break;
79 if (callback)
80 callback();
81 });
83 apiFunctions.setHandleRequest('getMediaFileSystemMetadata',
84 function(filesystem) {
85 if (filesystem && filesystem.name &&
86 filesystem.name in mediaGalleriesMetadata) {
87 return mediaGalleriesMetadata[filesystem.name];
89 return {
90 'name': '',
91 'galleryId': '',
92 'isRemovable': false,
93 'isMediaDevice': false,
94 'isAvailable': false,
96 });
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);
119 if (callback)
120 callback(response.metadata);
122 // The UUID was in position 0 in the setUpdateArgumentsPostValidate
123 // function.
124 var uuid = request.args[0];
125 delete blobsAwaitingMetadata[uuid];
129 exports.binding = binding.generate();