Chromium Code Reviews| Index: chrome/renderer/resources/extensions/downloads_custom_bindings.js |
| diff --git a/chrome/renderer/resources/extensions/downloads_custom_bindings.js b/chrome/renderer/resources/extensions/downloads_custom_bindings.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d0d3d4419824646906d2e106a060c4c136d127ea |
| --- /dev/null |
| +++ b/chrome/renderer/resources/extensions/downloads_custom_bindings.js |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Custom bindings for the downloads API. |
| + |
| +var downloadsNatives = requireNative('downloads'); |
| +var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| +var sendRequest = require('sendRequest').sendRequest; |
| + |
| +function DownloadsEvent(eventName, |
| + opt_argSchemas, |
| + ignored_extraArgSchemas, |
| + opt_eventOptions) { |
| + if (typeof eventName != 'string') |
| + throw new Error('chrome.DownloadsEvent requires an event name.'); |
| + // onDeterminingFilename is the only special event in chrome.downloads. |
| + if (eventName != 'downloads.onDeterminingFilename') { |
| + return new chrome.Event(eventName, opt_argSchemas, opt_eventOptions); |
| + } |
| + |
| + this.eventName_ = eventName; |
| + // ExtensionDownloadsEventRouter can't send a SuggestFilenameCallback, so |
| + // slice it out of the event schema, leaving only the DownloadItem. |
| + this.argSchemas_ = opt_argSchemas.slice(0, 1); |
| + this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions); |
| + this.subEvents_ = []; |
| +} |
| + |
| +// Test if the given callback is registered for this event. |
| +DownloadsEvent.prototype.hasListener = function(cb) { |
| + if (!this.eventOptions_.supportsListeners) |
| + throw new Error('This event does not support listeners.'); |
| + return this.findListener_(cb) > -1; |
| +}; |
| + |
| +// Test if any callbacks are registered for this event. |
| +DownloadsEvent.prototype.hasListeners = function() { |
| + if (!this.eventOptions_.supportsListeners) |
| + throw new Error('This event does not support listeners.'); |
| + return (this.subEvents_.length > 0); |
| +}; |
| + |
| +DownloadsEvent.prototype.addListener = function(cb) { |
| + if (!this.eventOptions_.supportsListeners) |
| + throw new Error('This event does not support listeners.'); |
| + var entry = {callback: cb}; |
| + entry.determiner_id = downloadsNatives.GetFilenameDeterminerId(); |
| + entry.subEvent = new chrome.Event( |
| + this.eventName_ + '/' + entry.determiner_id, this.argSchemas_); |
| + entry.subEventCallback = function() { |
| + var downloadItem = arguments[0]; |
| + // Copy the id so that extensions can't change downloadItem.id. |
| + var downloadId = downloadItem.id; |
| + var suggestable = true; |
| + function suggest(result) { |
| + if (!suggestable) |
| + return; |
|
Matt Perry
2013/02/22 22:53:06
Also print an error (console.error) explaining how
|
| + suggestable = false; |
| + if ((typeof(result) == 'object') && |
| + result.filename && |
| + (typeof(result.filename) == 'string') && |
| + ((result.overwrite === undefined) || |
| + (typeof(result.overwrite) == 'boolean'))) { |
| + chromeHidden.internalAPIs.downloadsInternal.determineFilename( |
| + entry.determiner_id, |
| + downloadId, |
| + result.filename, |
| + result.overwrite || false); |
| + } else { |
| + chromeHidden.internalAPIs.downloadsInternal.determineFilename( |
| + entry.determiner_id, |
| + downloadId); |
| + } |
| + } |
| + try { |
| + if (cb.apply(null, [downloadItem, suggest]) !== true) |
| + suggest(); |
| + } catch (e) { |
| + suggest(); |
| + throw e; |
| + } |
| + }; |
| + this.subEvents_.push(entry); |
| + entry.subEvent.addListener(entry.subEventCallback); |
| +}; |
| + |
| +DownloadsEvent.prototype.removeListener = function(cb) { |
| + if (!this.eventOptions_.supportsListeners) |
| + throw new Error('This event does not support listeners.'); |
| + var idx; |
| + while ((idx = this.findListener_(cb)) >= 0) { |
| + var e = this.subEvents_[idx]; |
| + e.subEvent.removeListener(e.subEventCallback); |
| + if (e.subEvent.hasListeners()) { |
| + console.error('Internal error: subEvent has orphaned listeners.'); |
| + } |
| + this.subEvents_.splice(idx, 1); |
| + } |
| +}; |
| + |
| +DownloadsEvent.prototype.findListener_ = function(cb) { |
| + for (var i in this.subEvents_) { |
| + var e = this.subEvents_[i]; |
| + if (e.callback === cb) { |
| + if (e.subEvent.findListener_(e.subEventCallback) > -1) |
| + return i; |
| + console.error('Internal error: subEvent has no callback.'); |
| + } |
| + } |
| + |
| + return -1; |
| +}; |
| + |
| +DownloadsEvent.prototype.addRules = function(rules, opt_cb) { |
| + if (!this.eventOptions_.supportsRules) |
| + throw new Error('This event does not support rules.'); |
| + this.eventForRules_.addRules(rules, opt_cb); |
| +} |
| + |
| +DownloadsEvent.prototype.removeRules = function(ruleIdentifiers, opt_cb) { |
| + if (!this.eventOptions_.supportsRules) |
| + throw new Error('This event does not support rules.'); |
| + this.eventForRules_.removeRules(ruleIdentifiers, opt_cb); |
| +} |
| + |
| +DownloadsEvent.prototype.getRules = function(ruleIdentifiers, cb) { |
| + if (!this.eventOptions_.supportsRules) |
| + throw new Error('This event does not support rules.'); |
| + this.eventForRules_.getRules(ruleIdentifiers, cb); |
| +} |
| + |
| +chromeHidden.registerCustomEvent('downloads', DownloadsEvent); |