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

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: @r184166 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;
Matt Perry 2013/02/22 22:53:06 Also print an error (console.error) explaining how
59 suggestable = false;
60 if ((typeof(result) == 'object') &&
61 result.filename &&
62 (typeof(result.filename) == 'string') &&
63 ((result.overwrite === undefined) ||
64 (typeof(result.overwrite) == 'boolean'))) {
65 chromeHidden.internalAPIs.downloadsInternal.determineFilename(
66 entry.determiner_id,
67 downloadId,
68 result.filename,
69 result.overwrite || false);
70 } else {
71 chromeHidden.internalAPIs.downloadsInternal.determineFilename(
72 entry.determiner_id,
73 downloadId);
74 }
75 }
76 try {
77 if (cb.apply(null, [downloadItem, suggest]) !== true)
78 suggest();
79 } catch (e) {
80 suggest();
81 throw e;
82 }
83 };
84 this.subEvents_.push(entry);
85 entry.subEvent.addListener(entry.subEventCallback);
86 };
87
88 DownloadsEvent.prototype.removeListener = function(cb) {
89 if (!this.eventOptions_.supportsListeners)
90 throw new Error('This event does not support listeners.');
91 var idx;
92 while ((idx = this.findListener_(cb)) >= 0) {
93 var e = this.subEvents_[idx];
94 e.subEvent.removeListener(e.subEventCallback);
95 if (e.subEvent.hasListeners()) {
96 console.error('Internal error: subEvent has orphaned listeners.');
97 }
98 this.subEvents_.splice(idx, 1);
99 }
100 };
101
102 DownloadsEvent.prototype.findListener_ = function(cb) {
103 for (var i in this.subEvents_) {
104 var e = this.subEvents_[i];
105 if (e.callback === cb) {
106 if (e.subEvent.findListener_(e.subEventCallback) > -1)
107 return i;
108 console.error('Internal error: subEvent has no callback.');
109 }
110 }
111
112 return -1;
113 };
114
115 DownloadsEvent.prototype.addRules = function(rules, opt_cb) {
116 if (!this.eventOptions_.supportsRules)
117 throw new Error('This event does not support rules.');
118 this.eventForRules_.addRules(rules, opt_cb);
119 }
120
121 DownloadsEvent.prototype.removeRules = function(ruleIdentifiers, opt_cb) {
122 if (!this.eventOptions_.supportsRules)
123 throw new Error('This event does not support rules.');
124 this.eventForRules_.removeRules(ruleIdentifiers, opt_cb);
125 }
126
127 DownloadsEvent.prototype.getRules = function(ruleIdentifiers, cb) {
128 if (!this.eventOptions_.supportsRules)
129 throw new Error('This event does not support rules.');
130 this.eventForRules_.getRules(ruleIdentifiers, cb);
131 }
132
133 chromeHidden.registerCustomEvent('downloads', DownloadsEvent);
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/downloads_custom_bindings.cc ('k') | chrome/renderer/resources/renderer_resources.grd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698