Bumping gaia.json for 3 gaia revision(s) a=gaia-bump
[gecko.git] / widget / xpwidgets / nsFilePickerProxy.cpp
blobec2956a9724cd7dd18d819b0ee2317ace126eba1
1 /* -*- Mode: C++; tab-width: 4; 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 "nsFilePickerProxy.h"
8 #include "nsComponentManagerUtils.h"
9 #include "nsNetUtil.h"
10 #include "nsIFile.h"
11 #include "nsDOMFile.h"
12 #include "mozilla/dom/TabChild.h"
13 #include "mozilla/dom/ipc/Blob.h"
15 using namespace mozilla::dom;
17 NS_IMPL_ISUPPORTS(nsFilePickerProxy, nsIFilePicker)
19 nsFilePickerProxy::nsFilePickerProxy()
23 nsFilePickerProxy::~nsFilePickerProxy()
27 NS_IMETHODIMP
28 nsFilePickerProxy::Init(nsIDOMWindow* aParent, const nsAString& aTitle,
29 int16_t aMode)
31 TabChild* tabChild = TabChild::GetFrom(aParent);
32 if (!tabChild) {
33 return NS_ERROR_FAILURE;
36 mMode = aMode;
38 NS_ADDREF_THIS();
39 tabChild->SendPFilePickerConstructor(this, nsString(aTitle), aMode);
40 return NS_OK;
43 void
44 nsFilePickerProxy::InitNative(nsIWidget* aParent, const nsAString& aTitle)
48 NS_IMETHODIMP
49 nsFilePickerProxy::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
51 mFilterNames.AppendElement(aTitle);
52 mFilters.AppendElement(aFilter);
53 return NS_OK;
56 NS_IMETHODIMP
57 nsFilePickerProxy::GetDefaultString(nsAString& aDefaultString)
59 aDefaultString = mDefault;
60 return NS_OK;
63 NS_IMETHODIMP
64 nsFilePickerProxy::SetDefaultString(const nsAString& aDefaultString)
66 mDefault = aDefaultString;
67 return NS_OK;
70 NS_IMETHODIMP
71 nsFilePickerProxy::GetDefaultExtension(nsAString& aDefaultExtension)
73 aDefaultExtension = mDefaultExtension;
74 return NS_OK;
77 NS_IMETHODIMP
78 nsFilePickerProxy::SetDefaultExtension(const nsAString& aDefaultExtension)
80 mDefaultExtension = aDefaultExtension;
81 return NS_OK;
84 NS_IMETHODIMP
85 nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex)
87 *aFilterIndex = mSelectedType;
88 return NS_OK;
91 NS_IMETHODIMP
92 nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex)
94 mSelectedType = aFilterIndex;
95 return NS_OK;
98 /* readonly attribute nsIFile file; */
99 NS_IMETHODIMP
100 nsFilePickerProxy::GetFile(nsIFile** aFile)
102 MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomfile");
103 return NS_ERROR_FAILURE;
106 /* readonly attribute nsIFileURL fileURL; */
107 NS_IMETHODIMP
108 nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
110 MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomfile");
111 return NS_ERROR_FAILURE;
114 /* readonly attribute nsISimpleEnumerator files; */
115 NS_IMETHODIMP
116 nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
118 MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomfiles");
119 return NS_ERROR_FAILURE;
122 NS_IMETHODIMP
123 nsFilePickerProxy::Show(int16_t* aReturn)
125 MOZ_ASSERT(false, "Show is unimplemented; use Open");
126 return NS_ERROR_NOT_IMPLEMENTED;
129 NS_IMETHODIMP
130 nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback)
132 mCallback = aCallback;
134 SendOpen(mSelectedType, mAddToRecentDocs, mDefault,
135 mDefaultExtension, mFilters, mFilterNames);
137 return NS_OK;
140 bool
141 nsFilePickerProxy::Recv__delete__(const MaybeInputFiles& aFiles,
142 const int16_t& aResult)
144 if (aFiles.type() == MaybeInputFiles::TInputFiles) {
145 const InfallibleTArray<PBlobChild*>& files = aFiles.get_InputFiles().filesChild();
146 for (uint32_t i = 0; i < files.Length(); ++i) {
147 BlobChild* actor = static_cast<BlobChild*>(files[i]);
148 nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob();
149 nsCOMPtr<nsIDOMFile> file(do_QueryInterface(blob));
150 NS_ENSURE_TRUE(file, true);
151 mDomfiles.AppendObject(file);
155 if (mCallback) {
156 mCallback->Done(aResult);
157 mCallback = nullptr;
160 return true;
163 NS_IMETHODIMP
164 nsFilePickerProxy::GetDomfile(nsIDOMFile** aDomfile)
166 *aDomfile = nullptr;
167 if (mDomfiles.IsEmpty()) {
168 return NS_OK;
171 MOZ_ASSERT(mDomfiles.Length() == 1);
172 nsCOMPtr<nsIDOMFile> domfile = mDomfiles[0];
173 domfile.forget(aDomfile);
174 return NS_OK;
177 NS_IMETHODIMP
178 nsFilePickerProxy::GetDomfiles(nsISimpleEnumerator** aDomfiles)
180 return NS_NewArrayEnumerator(aDomfiles, mDomfiles);