Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: chrome/renderer/resources/extensions/downloads_custom_bindings.js

Issue 11574006: Implement chrome.downloads.onDeterminingFilename() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r183850 Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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,
12 opt_argSchemas,
13 ignored_extraArgSchemas,
14 opt_eventOptions) {
15 if (typeof eventName != 'string')
16 throw new Error('chrome.DownloadsEvent requires an event name.');
17 // onDeterminingFilename is the only special event in chrome.downloads.
18 if (eventName != 'downloads.onDeterminingFilename') {
19 return new chrome.Event(eventName, opt_argSchemas, opt_eventOptions);
20 }
21
22 this.eventName_ = eventName;
23 // ExtensionDownloadsEventRouter can't send a SuggestFilenameCallback, so
24 // slice it out of the event schema, leaving only the DownloadItem.
25 this.argSchemas_ = opt_argSchemas.slice(0, 1);
26 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions);
27 this.subEvents_ = [];
28 }
29
30 // Test if the given callback is registered for this event.
31 DownloadsEvent.prototype.hasListener = function(cb) {
32 if (!this.eventOptions_.supportsListeners)
33 throw new Error('This event does not support listeners.');
34 return this.findListener_(cb) > -1;
35 };
36
37 // Test if any callbacks are registered for this event.
38 DownloadsEvent.prototype.hasListeners = function() {
39 if (!this.eventOptions_.supportsListeners)
40 throw new Error('This event does not support listeners.');
41 return (this.subEvents_.length > 0);
42 };
43
44 DownloadsEvent.prototype.addListener = function(cb) {
45 if (!this.eventOptions_.supportsListeners)
46 throw new Error('This event does not support listeners.');
47 var entry = {callback: cb};
48 entry.determiner_id = downloadsNatives.GetFilenameDeterminerId();
49 entry.subEvent = new chrome.Event(
50 this.eventName_ + '/' + entry.determiner_id, this.argSchemas_);
51 entry.subEventCallback = function() {
52 var downloadItem = arguments[0];
53 // Copy the id so that extensions can't change downloadItem.id.
54 var downloadId = downloadItem.id;
55 var suggestable = true;
56 function suggest(result) {
57 if (!suggestable)
58 return;
59 if ((typeof(result) == 'object') &&
60 result.filename &&
61 (typeof(result.filename) == 'string') &&
62 ((result.overwrite === undefined) ||
63 (typeof(result.overwrite) == 'boolean'))) {
64 chromeHidden.internalAPIs.downloadsInternal.determineFilename(
65 entry.determiner_id,
66 downloadId,
67 result.filename,
68 result.overwrite || false);
69 } else {
70 chromeHidden.internalAPIs.downloadsInternal.determineFilename(
71 entry.determiner_id,
72 downloadId);
73 }
74 }
75 try {
76 cb.apply(null, [downloadItem, suggest]);
77 } catch (e) {
78 chromeHidden.internalAPIs.downloadsInternal.determineFilename(
79 entry.determiner_id,
80 downloadId);
81 suggestable = false;
82 throw e;
83 }
84 };
85 this.subEvents_.push(entry);
86 entry.subEvent.addListener(entry.subEventCallback);
87 };
88
89 DownloadsEvent.prototype.removeListener = function(cb) {
90 if (!this.eventOptions_.supportsListeners)
91 throw new Error('This event does not support listeners.');
92 var idx;
93 while ((idx = this.findListener_(cb)) >= 0) {
94 var e = this.subEvents_[idx];
95 e.subEvent.removeListener(e.subEventCallback);
96 if (e.subEvent.hasListeners()) {
97 console.error('Internal error: subEvent has orphaned listeners.');
98 }
99 this.subEvents_.splice(idx, 1);
100 }
101 };
102
103 DownloadsEvent.prototype.findListener_ = function(cb) {
104 for (var i in this.subEvents_) {
105 var e = this.subEvents_[i];
106 if (e.callback === cb) {
107 if (e.subEvent.findListener_(e.subEventCallback) > -1)
108 return i;
109 console.error('Internal error: subEvent has no callback.');
110 }
111 }
112
113 return -1;
114 };
115
116 DownloadsEvent.prototype.addRules = function(rules, opt_cb) {
117 if (!this.eventOptions_.supportsRules)
118 throw new Error('This event does not support rules.');
119 this.eventForRules_.addRules(rules, opt_cb);
120 }
121
122 DownloadsEvent.prototype.removeRules = function(ruleIdentifiers, opt_cb) {
123 if (!this.eventOptions_.supportsRules)
124 throw new Error('This event does not support rules.');
125 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb);
126 }
127
128 DownloadsEvent.prototype.getRules = function(ruleIdentifiers, cb) {
129 if (!this.eventOptions_.supportsRules)
130 throw new Error('This event does not support rules.');
131 this.eventForRules_.getRules(ruleIdentifiers, cb);
132 }
133
134 chromeHidden.registerCustomEvent('downloads', DownloadsEvent);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698