OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Custom bindings for the downloads API. | 5 // Custom bindings for the downloads API. |
6 | 6 |
7 var binding = require('binding').Binding.create('downloads'); | 7 var binding = require('binding').Binding.create('downloads'); |
8 var downloadsInternal = require('binding').Binding.create( | 8 var downloadsInternal = require('binding').Binding.create( |
9 'downloadsInternal').generate(); | 9 'downloadsInternal').generate(); |
10 var eventBindings = require('event_bindings'); | 10 var eventBindings = require('event_bindings'); |
11 | 11 |
12 eventBindings.registerArgumentMassager( | 12 eventBindings.registerArgumentMassager( |
13 'downloads.onDeterminingFilename', | 13 'downloads.onDeterminingFilename', |
14 function massage_determining_filename(args, dispatch) { | 14 function massage_determining_filename(args, dispatch) { |
15 var downloadItem = args[0]; | 15 var downloadItem = args[0]; |
16 // Copy the id so that extensions can't change it. | 16 // Copy the id so that extensions can't change it. |
17 var downloadId = downloadItem.id; | 17 var downloadId = downloadItem.id; |
18 var suggestable = true; | 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 } |
19 function suggestCallback(result) { | 40 function suggestCallback(result) { |
20 if (!suggestable) { | 41 if (!suggestable) { |
21 console.error('suggestCallback may not be called more than once.'); | 42 console.error('suggestCallback may not be called more than once.'); |
22 return; | 43 return; |
23 } | 44 } |
24 suggestable = false; | 45 suggestable = false; |
25 if ((typeof(result) == 'object') && | 46 if (isValidResult(result)) { |
26 result.filename && | |
27 (typeof(result.filename) == 'string') && | |
28 ((result.conflict_action == undefined) || | |
29 (typeof(result.conflict_action) == 'string'))) { | |
30 downloadsInternal.determineFilename( | 47 downloadsInternal.determineFilename( |
31 downloadId, result.filename, result.conflict_action || ""); | 48 downloadId, result.filename, result.conflictAction || ""); |
32 } else { | 49 } else { |
33 downloadsInternal.determineFilename(downloadId, "", ""); | 50 downloadsInternal.determineFilename(downloadId, "", ""); |
34 } | 51 } |
35 } | 52 } |
36 try { | 53 try { |
37 var results = dispatch([downloadItem, suggestCallback]); | 54 var results = dispatch([downloadItem, suggestCallback]); |
38 var async = (results && | 55 var async = (results && |
39 results.results && | 56 results.results && |
40 (results.results.length != 0) && | 57 (results.results.length != 0) && |
41 (results.results[0] === true)); | 58 (results.results[0] === true)); |
42 if (suggestable && !async) | 59 if (suggestable && !async) |
43 suggestCallback(); | 60 suggestCallback(); |
44 } catch (e) { | 61 } catch (e) { |
45 suggestCallback(); | 62 suggestCallback(); |
46 throw e; | 63 throw e; |
47 } | 64 } |
48 }); | 65 }); |
49 exports.binding = binding.generate(); | 66 exports.binding = binding.generate(); |
OLD | NEW |