OLD | NEW |
(Empty) | |
| 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 downloadsNatives = requireNative('downloads'); |
| 8 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 9 var sendRequest = require('sendRequest').sendRequest; |
| 10 |
| 11 function DownloadsEvent(eventName, opt_argSchemas, opt_extraArgSchemas, |
| 12 opt_eventOptions) { |
| 13 if (typeof eventName != 'string') |
| 14 throw new Error('chrome.DownloadsEvent requires an event name.'); |
| 15 |
| 16 this.eventName_ = eventName; |
| 17 this.determiningFilename_ = (eventName == 'downloads.onDeterminingFilename'); |
| 18 this.argSchemas_ = opt_argSchemas; |
| 19 this.extraArgSchemas_ = opt_extraArgSchemas; |
| 20 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); |
| 21 if (this.determiningFilename_) { |
| 22 this.subEvents_ = []; |
| 23 } else { |
| 24 this.actualEvent_ = |
| 25 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); |
| 26 } |
| 27 } |
| 28 |
| 29 // Test if the given callback is registered for this event. |
| 30 DownloadsEvent.prototype.hasListener = function(cb) { |
| 31 if (!this.eventOptions_.supportsListeners) |
| 32 throw new Error('This event does not support listeners.'); |
| 33 return this.findListener_(cb) > -1; |
| 34 }; |
| 35 |
| 36 // Test if any callbacks are registered for this event. |
| 37 DownloadsEvent.prototype.hasListeners = function() { |
| 38 if (!this.eventOptions_.supportsListeners) |
| 39 throw new Error('This event does not support listeners.'); |
| 40 return (this.determiningFilename_ ? (this.subEvents_.length > 0) : |
| 41 this.actualEvent_.hasListeners()); |
| 42 }; |
| 43 |
| 44 DownloadsEvent.prototype.addListener = function(cb) { |
| 45 if (!this.eventOptions_.supportsListeners) |
| 46 throw new Error('This event does not support listeners.'); |
| 47 if (!this.determiningFilename_) { |
| 48 this.actualEvent_.addListener(cb); |
| 49 return; |
| 50 } |
| 51 var entry = {callback: cb}; |
| 52 entry.determiner_id = downloadsNatives.GetFilenameDeterminerId(); |
| 53 entry.subEvent = new chrome.Event( |
| 54 this.eventName_ + '/' + entry.determiner_id, this.argSchemas_); |
| 55 chromeHidden.internalAPIs.downloadsInternal.addFilenameDeterminer( |
| 56 entry.determiner_id); |
| 57 entry.subEventCallback = function() { |
| 58 var download_id = arguments[0].id; |
| 59 try { |
| 60 var result = cb.apply(null, arguments); |
| 61 if ((typeof(result) == 'object') && |
| 62 result.filename) { |
| 63 chromeHidden.internalAPIs.downloadsInternal.determineFilename( |
| 64 entry.determiner_id, |
| 65 download_id, |
| 66 result.filename, |
| 67 result.overwrite || false); |
| 68 } else { |
| 69 chromeHidden.internalAPIs.downloadsInternal.determineFilename( |
| 70 entry.determiner_id, |
| 71 download_id); |
| 72 } |
| 73 } catch (e) { |
| 74 chromeHidden.internalAPIs.downloadsInternal.determineFilename( |
| 75 entry.determiner_id, |
| 76 download_id); |
| 77 throw e; |
| 78 } |
| 79 }; |
| 80 this.subEvents_.push(entry); |
| 81 entry.subEvent.addListener(entry.subEventCallback); |
| 82 }; |
| 83 |
| 84 DownloadsEvent.prototype.removeListener = function(cb) { |
| 85 if (!this.eventOptions_.supportsListeners) |
| 86 throw new Error('This event does not support listeners.'); |
| 87 if (!this.determiningFilename_) { |
| 88 this.actualEvent_.removeListener(cb); |
| 89 return; |
| 90 } |
| 91 var idx; |
| 92 while ((idx = this.findListener_(cb)) >= 0) { |
| 93 var e = this.subEvents_[idx]; |
| 94 e.subEvent.removeListener(e.subEventCallback); |
| 95 chromeHidden.internalAPIs.downloadsInternal.removeFilenameDeterminer( |
| 96 e.determiner_id); |
| 97 if (e.subEvent.hasListeners()) { |
| 98 console.error('Internal error: subEvent has orphaned listeners.'); |
| 99 } |
| 100 this.subEvents_.splice(idx, 1); |
| 101 } |
| 102 }; |
| 103 |
| 104 DownloadsEvent.prototype.findListener_ = function(cb) { |
| 105 if (!this.determiningFilename_) { |
| 106 return this.actualEvent_.findListener(cb); |
| 107 } |
| 108 for (var i in this.subEvents_) { |
| 109 var e = this.subEvents_[i]; |
| 110 if (e.callback === cb) { |
| 111 if (e.subEvent.findListener_(e.subEventCallback) > -1) |
| 112 return i; |
| 113 console.error('Internal error: subEvent has no callback.'); |
| 114 } |
| 115 } |
| 116 |
| 117 return -1; |
| 118 }; |
| 119 |
| 120 DownloadsEvent.prototype.addRules = function(rules, opt_cb) { |
| 121 if (!this.eventOptions_.supportsRules) |
| 122 throw new Error('This event does not support rules.'); |
| 123 this.eventForRules_.addRules(rules, opt_cb); |
| 124 } |
| 125 |
| 126 DownloadsEvent.prototype.removeRules = function(ruleIdentifiers, opt_cb) { |
| 127 if (!this.eventOptions_.supportsRules) |
| 128 throw new Error('This event does not support rules.'); |
| 129 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); |
| 130 } |
| 131 |
| 132 DownloadsEvent.prototype.getRules = function(ruleIdentifiers, cb) { |
| 133 if (!this.eventOptions_.supportsRules) |
| 134 throw new Error('This event does not support rules.'); |
| 135 this.eventForRules_.getRules(ruleIdentifiers, cb); |
| 136 } |
| 137 |
| 138 chromeHidden.registerCustomEvent('downloads', DownloadsEvent); |
OLD | NEW |