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 "mozilla/dom/File.h"
12 #include "mozilla/dom/TabChild.h"
13 #include "mozilla/dom/ipc/BlobChild.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
;
36 mParent
= do_QueryInterface(aParent
);
37 if (!mParent
->IsInnerWindow()) {
38 mParent
= mParent
->GetCurrentInnerWindow();
44 tabChild
->SendPFilePickerConstructor(this, nsString(aTitle
), aMode
);
49 nsFilePickerProxy::InitNative(nsIWidget
* aParent
, const nsAString
& aTitle
)
54 nsFilePickerProxy::AppendFilter(const nsAString
& aTitle
, const nsAString
& aFilter
)
56 mFilterNames
.AppendElement(aTitle
);
57 mFilters
.AppendElement(aFilter
);
62 nsFilePickerProxy::GetDefaultString(nsAString
& aDefaultString
)
64 aDefaultString
= mDefault
;
69 nsFilePickerProxy::SetDefaultString(const nsAString
& aDefaultString
)
71 mDefault
= aDefaultString
;
76 nsFilePickerProxy::GetDefaultExtension(nsAString
& aDefaultExtension
)
78 aDefaultExtension
= mDefaultExtension
;
83 nsFilePickerProxy::SetDefaultExtension(const nsAString
& aDefaultExtension
)
85 mDefaultExtension
= aDefaultExtension
;
90 nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex
)
92 *aFilterIndex
= mSelectedType
;
97 nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex
)
99 mSelectedType
= aFilterIndex
;
103 /* readonly attribute nsIFile file; */
105 nsFilePickerProxy::GetFile(nsIFile
** aFile
)
107 MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomfile");
108 return NS_ERROR_FAILURE
;
111 /* readonly attribute nsIFileURL fileURL; */
113 nsFilePickerProxy::GetFileURL(nsIURI
** aFileURL
)
115 MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomfile");
116 return NS_ERROR_FAILURE
;
119 /* readonly attribute nsISimpleEnumerator files; */
121 nsFilePickerProxy::GetFiles(nsISimpleEnumerator
** aFiles
)
123 MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomfiles");
124 return NS_ERROR_FAILURE
;
128 nsFilePickerProxy::Show(int16_t* aReturn
)
130 MOZ_ASSERT(false, "Show is unimplemented; use Open");
131 return NS_ERROR_NOT_IMPLEMENTED
;
135 nsFilePickerProxy::Open(nsIFilePickerShownCallback
* aCallback
)
137 mCallback
= aCallback
;
139 nsString displayDirectory
;
140 if (mDisplayDirectory
) {
141 mDisplayDirectory
->GetPath(displayDirectory
);
144 SendOpen(mSelectedType
, mAddToRecentDocs
, mDefault
, mDefaultExtension
,
145 mFilters
, mFilterNames
, displayDirectory
);
151 nsFilePickerProxy::Recv__delete__(const MaybeInputFiles
& aFiles
,
152 const int16_t& aResult
)
154 if (aFiles
.type() == MaybeInputFiles::TInputFiles
) {
155 const InfallibleTArray
<PBlobChild
*>& files
= aFiles
.get_InputFiles().filesChild();
156 for (uint32_t i
= 0; i
< files
.Length(); ++i
) {
157 BlobChild
* actor
= static_cast<BlobChild
*>(files
[i
]);
158 nsRefPtr
<FileImpl
> blobImpl
= actor
->GetBlobImpl();
159 NS_ENSURE_TRUE(blobImpl
, true);
161 nsCOMPtr
<nsIDOMBlob
> blob
= new File(mParent
, blobImpl
);
162 nsCOMPtr
<nsIDOMFile
> file(do_QueryInterface(blob
));
163 NS_ENSURE_TRUE(file
, true);
165 mDomfiles
.AppendObject(file
);
170 mCallback
->Done(aResult
);
178 nsFilePickerProxy::GetDomfile(nsIDOMFile
** aDomfile
)
181 if (mDomfiles
.IsEmpty()) {
185 MOZ_ASSERT(mDomfiles
.Length() == 1);
186 nsCOMPtr
<nsIDOMFile
> domfile
= mDomfiles
[0];
187 domfile
.forget(aDomfile
);
192 nsFilePickerProxy::GetDomfiles(nsISimpleEnumerator
** aDomfiles
)
194 return NS_NewArrayEnumerator(aDomfiles
, mDomfiles
);