1 // Copyright (c) 2013 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. 4 5 // Custom bindings for the downloads API. 6 7 var binding = require('binding').Binding.create('downloads'); 8 var downloadsInternal = require('binding').Binding.create( 9 'downloadsInternal').generate(); 10 var eventBindings = require('event_bindings'); 11 12 eventBindings.registerArgumentMassager( 13 'downloads.onDeterminingFilename', 14 function massage_determining_filename(args, dispatch) { 15 var downloadItem = args[0]; 16 // Copy the id so that extensions can't change it. 17 var downloadId = downloadItem.id; 18 var suggestable = true; 19 function isValidResult(result) { 20 if (result === undefined) 21 return false; 22 if (typeof(result) != 'object') { 23 console.error('Error: Invocation of form suggest(' + typeof(result) + 24 ') doesn\'t match definition suggest({filename: string, ' + 25 'conflictAction: string})'); 26 return false; 27 } else if ((typeof(result.filename) != 'string') || 28 (result.filename.length == 0)) { 29 console.error('Error: "filename" parameter to suggest() must be a ' + 30 'non-empty string'); 31 return false; 32 } else if ([undefined, 'uniquify', 'overwrite', 'prompt'].indexOf( 33 result.conflictAction) < 0) { 34 console.error('Error: "conflictAction" parameter to suggest() must be ' + 35 'one of undefined, "uniquify", "overwrite", "prompt"'); 36 return false; 37 } 38 return true; 39 } 40 function suggestCallback(result) { 41 if (!suggestable) { 42 console.error('suggestCallback may not be called more than once.'); 43 return; 44 } 45 suggestable = false; 46 if (isValidResult(result)) { 47 downloadsInternal.determineFilename( 48 downloadId, result.filename, result.conflictAction || ""); 49 } else { 50 downloadsInternal.determineFilename(downloadId, "", ""); 51 } 52 } 53 try { 54 var results = dispatch([downloadItem, suggestCallback]); 55 var async = (results && 56 results.results && 57 (results.results.length != 0) && 58 (results.results[0] === true)); 59 if (suggestable && !async) 60 suggestCallback(); 61 } catch (e) { 62 suggestCallback(); 63 throw e; 64 } 65 }); 66 exports.binding = binding.generate(); 67