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.
5 // Custom bindings for the downloads API.
7 var binding
= require('binding').Binding
.create('downloads');
8 var downloadsInternal
= require('binding').Binding
.create(
9 'downloadsInternal').generate();
10 var eventBindings
= require('event_bindings');
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)
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})');
27 } else if ((typeof(result
.filename
) != 'string') ||
28 (result
.filename
.length
== 0)) {
29 console
.error('Error: "filename" parameter to suggest() must be a ' +
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"');
40 function suggestCallback(result
) {
42 console
.error('suggestCallback may not be called more than once.');
46 if (isValidResult(result
)) {
47 downloadsInternal
.determineFilename(
48 downloadId
, result
.filename
, result
.conflictAction
|| "");
50 downloadsInternal
.determineFilename(downloadId
, "", "");
54 var results
= dispatch([downloadItem
, suggestCallback
]);
55 var async
= (results
&&
57 (results
.results
.length
!= 0) &&
58 (results
.results
[0] === true));
59 if (suggestable
&& !async
)
66 exports
.binding
= binding
.generate();