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"
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()
28 nsFilePickerProxy::Init(nsIDOMWindow
* aParent
, const nsAString
& aTitle
,
31 TabChild
* tabChild
= TabChild::GetFrom(aParent
);
33 return NS_ERROR_FAILURE
;
39 tabChild
->SendPFilePickerConstructor(this, nsString(aTitle
), aMode
);
44 nsFilePickerProxy::InitNative(nsIWidget
* aParent
, const nsAString
& aTitle
)
49 nsFilePickerProxy::AppendFilter(const nsAString
& aTitle
, const nsAString
& aFilter
)
51 mFilterNames
.AppendElement(aTitle
);
52 mFilters
.AppendElement(aFilter
);
57 nsFilePickerProxy::GetDefaultString(nsAString
& aDefaultString
)
59 aDefaultString
= mDefault
;
64 nsFilePickerProxy::SetDefaultString(const nsAString
& aDefaultString
)
66 mDefault
= aDefaultString
;
71 nsFilePickerProxy::GetDefaultExtension(nsAString
& aDefaultExtension
)
73 aDefaultExtension
= mDefaultExtension
;
78 nsFilePickerProxy::SetDefaultExtension(const nsAString
& aDefaultExtension
)
80 mDefaultExtension
= aDefaultExtension
;
85 nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex
)
87 *aFilterIndex
= mSelectedType
;
92 nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex
)
94 mSelectedType
= aFilterIndex
;
98 /* readonly attribute nsIFile file; */
100 nsFilePickerProxy::GetFile(nsIFile
** aFile
)
102 MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomfile");
103 return NS_ERROR_FAILURE
;
106 /* readonly attribute nsIFileURL fileURL; */
108 nsFilePickerProxy::GetFileURL(nsIURI
** aFileURL
)
110 MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomfile");
111 return NS_ERROR_FAILURE
;
114 /* readonly attribute nsISimpleEnumerator files; */
116 nsFilePickerProxy::GetFiles(nsISimpleEnumerator
** aFiles
)
118 MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomfiles");
119 return NS_ERROR_FAILURE
;
123 nsFilePickerProxy::Show(int16_t* aReturn
)
125 MOZ_ASSERT(false, "Show is unimplemented; use Open");
126 return NS_ERROR_NOT_IMPLEMENTED
;
130 nsFilePickerProxy::Open(nsIFilePickerShownCallback
* aCallback
)
132 mCallback
= aCallback
;
134 SendOpen(mSelectedType
, mAddToRecentDocs
, mDefault
,
135 mDefaultExtension
, mFilters
, mFilterNames
);
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
);
156 mCallback
->Done(aResult
);
164 nsFilePickerProxy::GetDomfile(nsIDOMFile
** aDomfile
)
167 if (mDomfiles
.IsEmpty()) {
171 MOZ_ASSERT(mDomfiles
.Length() == 1);
172 nsCOMPtr
<nsIDOMFile
> domfile
= mDomfiles
[0];
173 domfile
.forget(aDomfile
);
178 nsFilePickerProxy::GetDomfiles(nsISimpleEnumerator
** aDomfiles
)
180 return NS_NewArrayEnumerator(aDomfiles
, mDomfiles
);