1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.importGlobalProperties(['File']);
function FilePicker() {
}
FilePicker.prototype = {
_mimeTypeFilter: 0,
_extensionsFilter: "",
_defaultString: "",
_domWin: null,
_defaultExtension: null,
_displayDirectory: null,
_filePath: null,
_promptActive: false,
_filterIndex: 0,
_addToRecentDocs: false,
_title: "",
init: function(aParent, aTitle, aMode) {
this._domWin = aParent;
this._mode = aMode;
this._title = aTitle;
Services.obs.addObserver(this, "FilePicker:Result", false);
let idService = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator);
this.guid = idService.generateUUID().toString();
if (aMode != Ci.nsIFilePicker.modeOpen && aMode != Ci.nsIFilePicker.modeOpenMultiple)
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
},
appendFilters: function(aFilterMask) {
if (aFilterMask & Ci.nsIFilePicker.filterAudio) {
this._mimeTypeFilter = "audio/*";
return;
}
if (aFilterMask & Ci.nsIFilePicker.filterImages) {
this._mimeTypeFilter = "image/*";
return;
}
if (aFilterMask & Ci.nsIFilePicker.filterVideo) {
this._mimeTypeFilter = "video/*";
return;
}
if (aFilterMask & Ci.nsIFilePicker.filterAll) {
this._mimeTypeFilter = "*/*";
return;
}
/* From BaseFilePicker.cpp */
if (aFilterMask & Ci.nsIFilePicker.filterHTML) {
this.appendFilter("*.html; *.htm; *.shtml; *.xhtml");
}
if (aFilterMask & Ci.nsIFilePicker.filterText) {
this.appendFilter("*.txt; *.text");
}
if (aFilterMask & Ci.nsIFilePicker.filterXML) {
this.appendFilter("*.xml");
}
if (aFilterMask & Ci.nsIFilePicker.xulFilter) {
this.appendFilter("*.xul");
}
if (aFilterMask & Ci.nsIFilePicker.xulFilter) {
this.appendFilter("..apps");
}
},
appendFilter: function(title, filter) {
if (this._extensionsFilter)
this._extensionsFilter += ", ";
this._extensionsFilter += filter;
},
get defaultString() {
return this._defaultString;
},
set defaultString(defaultString) {
this._defaultString = defaultString;
},
get defaultExtension() {
return this._defaultExtension;
},
set defaultExtension(defaultExtension) {
this._defaultExtension = defaultExtension;
},
get filterIndex() {
return this._filterIndex;
},
set filterIndex(val) {
this._filterIndex = val;
},
get displayDirectory() {
return this._displayDirectory;
},
set displayDirectory(dir) {
this._displayDirectory = dir;
},
get file() {
if (!this._filePath) {
return null;
}
return new FileUtils.File(this._filePath);
},
get fileURL() {
let file = this.getFile();
return Services.io.newFileURI(file);
},
get files() {
return this.getEnumerator([this.file], function(file) {
return file;
});
},
// We don't support directory selection yet.
get domFileOrDirectory() {
let f = this.file;
if (!f) {
return null;
}
let win = this._domWin;
if (win) {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
return utils.wrapDOMFile(f);
}
return File.createFromNsIFile(f);
},
get domFileOrDirectoryEnumerator() {
let win = this._domWin;
return this.getEnumerator([this.file], function(file) {
if (win) {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
return utils.wrapDOMFile(file);
}
return File.createFromNsIFile(file);
});
},
get addToRecentDocs() {
return this._addToRecentDocs;
},
set addToRecentDocs(val) {
this._addToRecentDocs = val;
},
get mode() {
return this._mode;
},
show: function() {
if (this._domWin) {
this.fireDialogEvent(this._domWin, "DOMWillOpenModalDialog");
let winUtils = this._domWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
winUtils.enterModalState();
}
this._promptActive = true;
this._sendMessage();
let thread = Services.tm.currentThread;
while (this._promptActive)
thread.processNextEvent(true);
delete this._promptActive;
if (this._domWin) {
let winUtils = this._domWin.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
winUtils.leaveModalState();
this.fireDialogEvent(this._domWin, "DOMModalDialogClosed");
}
if (this._filePath)
return Ci.nsIFilePicker.returnOK;
return Ci.nsIFilePicker.returnCancel;
},
open: function(callback) {
this._callback = callback;
this._sendMessage();
},
_sendMessage: function() {
let msg = {
type: "FilePicker:Show",
guid: this.guid,
title: this._title,
};
// Knowing the window lets us destroy any temp files when the tab is closed
// Other consumers of the file picker may have to either wait for Android
// to clean up the temp dir (not guaranteed) or clean up after themselves.
let win = Services.wm.getMostRecentWindow('navigator:browser');
let tab = win.BrowserApp.getTabForWindow(this._domWin.top)
if (tab) {
msg.tabId = tab.id;
}
if (!this._extensionsFilter && !this._mimeTypeFilter) {
// If neither filters is set show anything we can.
msg.mode = "mimeType";
msg.mimeType = "*/*";
} else if (this._extensionsFilter) {
msg.mode = "extension";
msg.extensions = this._extensionsFilter;
} else {
msg.mode = "mimeType";
msg.mimeType = this._mimeTypeFilter;
}
this.sendMessageToJava(msg);
},
sendMessageToJava: function(aMsg) {
Services.androidBridge.handleGeckoMessage(aMsg);
},
observe: function(aSubject, aTopic, aData) {
let data = JSON.parse(aData);
if (data.guid != this.guid)
return;
this._filePath = null;
if (data.file)
this._filePath = data.file;
this._promptActive = false;
if (this._callback) {
this._callback.done(this._filePath ? Ci.nsIFilePicker.returnOK : Ci.nsIFilePicker.returnCancel);
}
delete this._callback;
},
getEnumerator: function(files, mapFunction) {
return {
QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleEnumerator]),
mFiles: files,
mIndex: 0,
hasMoreElements: function() {
return (this.mIndex < this.mFiles.length);
},
getNext: function() {
if (this.mIndex >= this.mFiles.length) {
throw Components.results.NS_ERROR_FAILURE;
}
return mapFunction(this.mFiles[this.mIndex++]);
}
};
},
fireDialogEvent: function(aDomWin, aEventName) {
// accessing the document object can throw if this window no longer exists. See bug 789888.
try {
if (!aDomWin.document)
return;
let event = aDomWin.document.createEvent("Events");
event.initEvent(aEventName, true, true);
let winUtils = aDomWin.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
winUtils.dispatchEventToChromeOnly(aDomWin, event);
} catch(ex) {
}
},
classID: Components.ID("{18a4e042-7c7c-424b-a583-354e68553a7f}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFilePicker, Ci.nsIObserver])
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FilePicker]);
|