Bug 1751217 Part 3: Make HDR-capable macOS screens report 30 pixelDepth. r=mstange
[gecko.git] / widget / nsBaseFilePicker.cpp
blob4b5c8b0602d42465825022d223b60a6eb61ae731
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsCOMPtr.h"
8 #include "nsPIDOMWindow.h"
9 #include "nsIInterfaceRequestorUtils.h"
10 #include "nsIWidget.h"
12 #include "nsIStringBundle.h"
13 #include "nsString.h"
14 #include "nsCOMArray.h"
15 #include "nsIFile.h"
16 #include "nsEnumeratorUtils.h"
17 #include "mozilla/dom/Directory.h"
18 #include "mozilla/dom/File.h"
19 #include "mozilla/Components.h"
20 #include "WidgetUtils.h"
21 #include "nsSimpleEnumerator.h"
22 #include "nsThreadUtils.h"
24 #include "nsBaseFilePicker.h"
26 using namespace mozilla::widget;
27 using namespace mozilla::dom;
29 #define FILEPICKER_TITLES "chrome://global/locale/filepicker.properties"
30 #define FILEPICKER_FILTERS "chrome://global/content/filepicker.properties"
32 namespace {
34 nsresult LocalFileToDirectoryOrBlob(nsPIDOMWindowInner* aWindow,
35 bool aIsDirectory, nsIFile* aFile,
36 nsISupports** aResult) {
37 MOZ_ASSERT(aWindow);
39 if (aIsDirectory) {
40 #ifdef DEBUG
41 bool isDir;
42 aFile->IsDirectory(&isDir);
43 MOZ_ASSERT(isDir);
44 #endif
46 RefPtr<Directory> directory = Directory::Create(aWindow->AsGlobal(), aFile);
47 MOZ_ASSERT(directory);
49 directory.forget(aResult);
50 return NS_OK;
53 RefPtr<File> file = File::CreateFromFile(aWindow->AsGlobal(), aFile);
54 if (NS_WARN_IF(!file)) {
55 return NS_ERROR_FAILURE;
58 file.forget(aResult);
59 return NS_OK;
62 } // anonymous namespace
64 /**
65 * A runnable to dispatch from the main thread to the main thread to display
66 * the file picker while letting the showAsync method return right away.
68 class nsBaseFilePicker::AsyncShowFilePicker : public mozilla::Runnable {
69 public:
70 AsyncShowFilePicker(nsBaseFilePicker* aFilePicker,
71 nsIFilePickerShownCallback* aCallback)
72 : mozilla::Runnable("AsyncShowFilePicker"),
73 mFilePicker(aFilePicker),
74 mCallback(aCallback) {}
76 NS_IMETHOD Run() override {
77 NS_ASSERTION(NS_IsMainThread(),
78 "AsyncShowFilePicker should be on the main thread!");
80 // It's possible that some widget implementations require GUI operations
81 // to be on the main thread, so that's why we're not dispatching to another
82 // thread and calling back to the main after it's done.
83 int16_t result = nsIFilePicker::returnCancel;
84 nsresult rv = mFilePicker->Show(&result);
85 if (NS_FAILED(rv)) {
86 NS_ERROR("FilePicker's Show() implementation failed!");
89 if (mCallback) {
90 mCallback->Done(result);
92 return NS_OK;
95 private:
96 RefPtr<nsBaseFilePicker> mFilePicker;
97 RefPtr<nsIFilePickerShownCallback> mCallback;
100 class nsBaseFilePickerEnumerator : public nsSimpleEnumerator {
101 public:
102 nsBaseFilePickerEnumerator(nsPIDOMWindowOuter* aParent,
103 nsISimpleEnumerator* iterator, int16_t aMode)
104 : mIterator(iterator),
105 mParent(aParent->GetCurrentInnerWindow()),
106 mMode(aMode) {}
108 const nsID& DefaultInterface() override { return NS_GET_IID(nsIFile); }
110 NS_IMETHOD
111 GetNext(nsISupports** aResult) override {
112 nsCOMPtr<nsISupports> tmp;
113 nsresult rv = mIterator->GetNext(getter_AddRefs(tmp));
114 NS_ENSURE_SUCCESS(rv, rv);
116 if (!tmp) {
117 return NS_OK;
120 nsCOMPtr<nsIFile> localFile = do_QueryInterface(tmp);
121 if (!localFile) {
122 return NS_ERROR_FAILURE;
125 if (!mParent) {
126 return NS_ERROR_FAILURE;
129 return LocalFileToDirectoryOrBlob(
130 mParent, mMode == nsIFilePicker::modeGetFolder, localFile, aResult);
133 NS_IMETHOD
134 HasMoreElements(bool* aResult) override {
135 return mIterator->HasMoreElements(aResult);
138 private:
139 nsCOMPtr<nsISimpleEnumerator> mIterator;
140 nsCOMPtr<nsPIDOMWindowInner> mParent;
141 int16_t mMode;
144 nsBaseFilePicker::nsBaseFilePicker()
145 : mAddToRecentDocs(true), mMode(nsIFilePicker::modeOpen) {}
147 nsBaseFilePicker::~nsBaseFilePicker() = default;
149 NS_IMETHODIMP nsBaseFilePicker::Init(mozIDOMWindowProxy* aParent,
150 const nsAString& aTitle, int16_t aMode) {
151 MOZ_ASSERT(aParent,
152 "Null parent passed to filepicker, no file "
153 "picker for you!");
155 mParent = nsPIDOMWindowOuter::From(aParent);
157 nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(mParent);
158 NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
160 mMode = aMode;
161 InitNative(widget, aTitle);
163 return NS_OK;
166 NS_IMETHODIMP
167 nsBaseFilePicker::Open(nsIFilePickerShownCallback* aCallback) {
168 nsCOMPtr<nsIRunnable> filePickerEvent =
169 new AsyncShowFilePicker(this, aCallback);
170 return NS_DispatchToMainThread(filePickerEvent);
173 NS_IMETHODIMP
174 nsBaseFilePicker::AppendFilters(int32_t aFilterMask) {
175 nsCOMPtr<nsIStringBundleService> stringService =
176 mozilla::components::StringBundle::Service();
177 if (!stringService) return NS_ERROR_FAILURE;
179 nsCOMPtr<nsIStringBundle> titleBundle, filterBundle;
181 nsresult rv = stringService->CreateBundle(FILEPICKER_TITLES,
182 getter_AddRefs(titleBundle));
183 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
185 rv = stringService->CreateBundle(FILEPICKER_FILTERS,
186 getter_AddRefs(filterBundle));
187 if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
189 nsAutoString title;
190 nsAutoString filter;
192 if (aFilterMask & filterAll) {
193 titleBundle->GetStringFromName("allTitle", title);
194 filterBundle->GetStringFromName("allFilter", filter);
195 AppendFilter(title, filter);
197 if (aFilterMask & filterHTML) {
198 titleBundle->GetStringFromName("htmlTitle", title);
199 filterBundle->GetStringFromName("htmlFilter", filter);
200 AppendFilter(title, filter);
202 if (aFilterMask & filterText) {
203 titleBundle->GetStringFromName("textTitle", title);
204 filterBundle->GetStringFromName("textFilter", filter);
205 AppendFilter(title, filter);
207 if (aFilterMask & filterImages) {
208 titleBundle->GetStringFromName("imageTitle", title);
209 filterBundle->GetStringFromName("imageFilter", filter);
210 AppendFilter(title, filter);
212 if (aFilterMask & filterAudio) {
213 titleBundle->GetStringFromName("audioTitle", title);
214 filterBundle->GetStringFromName("audioFilter", filter);
215 AppendFilter(title, filter);
217 if (aFilterMask & filterVideo) {
218 titleBundle->GetStringFromName("videoTitle", title);
219 filterBundle->GetStringFromName("videoFilter", filter);
220 AppendFilter(title, filter);
222 if (aFilterMask & filterXML) {
223 titleBundle->GetStringFromName("xmlTitle", title);
224 filterBundle->GetStringFromName("xmlFilter", filter);
225 AppendFilter(title, filter);
227 if (aFilterMask & filterXUL) {
228 titleBundle->GetStringFromName("xulTitle", title);
229 filterBundle->GetStringFromName("xulFilter", filter);
230 AppendFilter(title, filter);
232 if (aFilterMask & filterApps) {
233 titleBundle->GetStringFromName("appsTitle", title);
234 // Pass the magic string "..apps" to the platform filepicker, which it
235 // should recognize and do the correct platform behavior for.
236 AppendFilter(title, u"..apps"_ns);
238 return NS_OK;
241 NS_IMETHODIMP nsBaseFilePicker::AppendRawFilter(const nsAString& aFilter) {
242 mRawFilters.AppendElement(aFilter);
243 return NS_OK;
246 NS_IMETHODIMP nsBaseFilePicker::GetCapture(int16_t* aCapture) {
247 *aCapture = 0;
248 return NS_OK;
251 NS_IMETHODIMP nsBaseFilePicker::SetCapture(int16_t aCapture) { return NS_OK; }
253 // Set the filter index
254 NS_IMETHODIMP nsBaseFilePicker::GetFilterIndex(int32_t* aFilterIndex) {
255 *aFilterIndex = 0;
256 return NS_OK;
259 NS_IMETHODIMP nsBaseFilePicker::SetFilterIndex(int32_t aFilterIndex) {
260 return NS_OK;
263 NS_IMETHODIMP nsBaseFilePicker::GetFiles(nsISimpleEnumerator** aFiles) {
264 NS_ENSURE_ARG_POINTER(aFiles);
265 nsCOMArray<nsIFile> files;
266 nsresult rv;
268 // if we get into the base class, the platform
269 // doesn't implement GetFiles() yet.
270 // so we fake it.
271 nsCOMPtr<nsIFile> file;
272 rv = GetFile(getter_AddRefs(file));
273 NS_ENSURE_SUCCESS(rv, rv);
275 files.AppendObject(file);
277 return NS_NewArrayEnumerator(aFiles, files, NS_GET_IID(nsIFile));
280 // Set the display directory
281 NS_IMETHODIMP nsBaseFilePicker::SetDisplayDirectory(nsIFile* aDirectory) {
282 // if displaySpecialDirectory has been previously called, let's abort this
283 // operation.
284 if (!mDisplaySpecialDirectory.IsEmpty()) {
285 return NS_OK;
288 if (!aDirectory) {
289 mDisplayDirectory = nullptr;
290 return NS_OK;
292 nsCOMPtr<nsIFile> directory;
293 nsresult rv = aDirectory->Clone(getter_AddRefs(directory));
294 if (NS_FAILED(rv)) return rv;
295 mDisplayDirectory = directory;
296 return NS_OK;
299 // Get the display directory
300 NS_IMETHODIMP nsBaseFilePicker::GetDisplayDirectory(nsIFile** aDirectory) {
301 *aDirectory = nullptr;
303 // if displaySpecialDirectory has been previously called, let's abort this
304 // operation.
305 if (!mDisplaySpecialDirectory.IsEmpty()) {
306 return NS_OK;
309 if (!mDisplayDirectory) return NS_OK;
310 nsCOMPtr<nsIFile> directory;
311 nsresult rv = mDisplayDirectory->Clone(getter_AddRefs(directory));
312 if (NS_FAILED(rv)) {
313 return rv;
315 directory.forget(aDirectory);
316 return NS_OK;
319 // Set the display special directory
320 NS_IMETHODIMP nsBaseFilePicker::SetDisplaySpecialDirectory(
321 const nsAString& aDirectory) {
322 // if displayDirectory has been previously called, let's abort this operation.
323 if (mDisplayDirectory && mDisplaySpecialDirectory.IsEmpty()) {
324 return NS_OK;
327 mDisplaySpecialDirectory = aDirectory;
328 if (mDisplaySpecialDirectory.IsEmpty()) {
329 mDisplayDirectory = nullptr;
330 return NS_OK;
333 return NS_GetSpecialDirectory(
334 NS_ConvertUTF16toUTF8(mDisplaySpecialDirectory).get(),
335 getter_AddRefs(mDisplayDirectory));
338 // Get the display special directory
339 NS_IMETHODIMP nsBaseFilePicker::GetDisplaySpecialDirectory(
340 nsAString& aDirectory) {
341 aDirectory = mDisplaySpecialDirectory;
342 return NS_OK;
345 NS_IMETHODIMP
346 nsBaseFilePicker::GetAddToRecentDocs(bool* aFlag) {
347 *aFlag = mAddToRecentDocs;
348 return NS_OK;
351 NS_IMETHODIMP
352 nsBaseFilePicker::SetAddToRecentDocs(bool aFlag) {
353 mAddToRecentDocs = aFlag;
354 return NS_OK;
357 NS_IMETHODIMP
358 nsBaseFilePicker::GetMode(int16_t* aMode) {
359 *aMode = mMode;
360 return NS_OK;
363 NS_IMETHODIMP
364 nsBaseFilePicker::SetOkButtonLabel(const nsAString& aLabel) {
365 mOkButtonLabel = aLabel;
366 return NS_OK;
369 NS_IMETHODIMP
370 nsBaseFilePicker::GetOkButtonLabel(nsAString& aLabel) {
371 aLabel = mOkButtonLabel;
372 return NS_OK;
375 NS_IMETHODIMP
376 nsBaseFilePicker::GetDomFileOrDirectory(nsISupports** aValue) {
377 nsCOMPtr<nsIFile> localFile;
378 nsresult rv = GetFile(getter_AddRefs(localFile));
379 NS_ENSURE_SUCCESS(rv, rv);
381 if (!localFile) {
382 *aValue = nullptr;
383 return NS_OK;
386 auto* innerParent = mParent ? mParent->GetCurrentInnerWindow() : nullptr;
388 if (!innerParent) {
389 return NS_ERROR_FAILURE;
392 return LocalFileToDirectoryOrBlob(
393 innerParent, mMode == nsIFilePicker::modeGetFolder, localFile, aValue);
396 NS_IMETHODIMP
397 nsBaseFilePicker::GetDomFileOrDirectoryEnumerator(
398 nsISimpleEnumerator** aValue) {
399 nsCOMPtr<nsISimpleEnumerator> iter;
400 nsresult rv = GetFiles(getter_AddRefs(iter));
401 NS_ENSURE_SUCCESS(rv, rv);
403 RefPtr<nsBaseFilePickerEnumerator> retIter =
404 new nsBaseFilePickerEnumerator(mParent, iter, mMode);
406 retIter.forget(aValue);
407 return NS_OK;