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

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: @r176065 Created 7 years, 11 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 var validate = require('schemaUtils').validate;
11
12 function DownloadsEvent(eventName, opt_argSchemas, opt_extraArgSchemas,
13 opt_eventOptions) {
14 if (typeof eventName != 'string')
15 throw new Error('chrome.DownloadsEvent requires an event name.');
16
17 this.eventName_ = eventName;
18 this.determiningFilename_ = (eventName == 'downloads.onDeterminingFilename');
19 this.argSchemas_ = opt_argSchemas;
20 this.extraArgSchemas_ = opt_extraArgSchemas;
21 this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions);
22 if (this.determiningFilename_) {
23 this.subEvents_ = [];
24 } else {
25 this.actualEvent_ =
26 new chrome.Event(eventName, opt_argSchemas, opt_eventOptions);
27 }
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 fur thus event.
vabr (Chromium) 2013/01/11 12:43:40 possibly a typo: "fur thus"
benjhayden 2013/01/11 21:21:27 Done.
38 DownloadsEvent.prototype.hasListeners = function() {
39 if (!this.eventOptions_.supportsListeners)
40 throw new Error('This event does not support listeners.');
41 return (this.determiningFilename_ ? (this.subEvents_.length > 0) :
42 this.actualEvent_.hasListeners());
43 };
44
45 DownloadsEvent.prototype.addListener = function(cb) {
46 if (!this.eventOptions_.supportsListeners)
47 throw new Error('This event does not support listeners.');
48 if (!this.determiningFilename_) {
49 this.actualEvent_.addListener(cb);
50 return;
51 }
52 var entry = {callback: cb};
53 entry.determiner_id = downloadsNatives.GetFilenameDeterminerId();
54 entry.subEvent = new chrome.Event(
55 this.eventName_ + '/' + entry.determiner_id, this.argSchemas_);
56 chromeHidden.internalAPIs.downloadsInternal.addFilenameDeterminer(
57 entry.determiner_id);
58 entry.subEventCallback = function() {
59 var download_id = arguments[0].id;
60 try {
61 var result = cb.apply(null, arguments);
62 if (typeof(result) == 'object' && result.filename) {
battre 2013/01/11 14:29:37 only if you pass a filename, you allow to set over
benjhayden 2013/01/11 21:21:27 Done.
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);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698