2014-02-18 00:30:06 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2010-10-20 15:19:24 +00:00
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* 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/. */
|
2010-10-20 15:19:24 +00:00
|
|
|
|
|
|
|
#include "nsFilePickerProxy.h"
|
2014-02-18 00:30:06 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIFile.h"
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-02-18 00:30:06 +00:00
|
|
|
#include "mozilla/dom/TabChild.h"
|
2014-09-26 23:21:57 +00:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
2010-10-20 15:19:24 +00:00
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
using namespace mozilla::dom;
|
2010-10-20 15:19:24 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsFilePickerProxy, nsIFilePicker)
|
2010-10-20 15:19:24 +00:00
|
|
|
|
|
|
|
nsFilePickerProxy::nsFilePickerProxy()
|
2014-02-18 00:30:06 +00:00
|
|
|
{
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFilePickerProxy::~nsFilePickerProxy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-18 00:30:06 +00:00
|
|
|
nsFilePickerProxy::Init(nsIDOMWindow* aParent, const nsAString& aTitle,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t aMode)
|
2010-10-20 15:19:24 +00:00
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
TabChild* tabChild = TabChild::GetFrom(aParent);
|
|
|
|
if (!tabChild) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
mParent = do_QueryInterface(aParent);
|
2014-10-10 16:00:34 +00:00
|
|
|
if (!mParent->IsInnerWindow()) {
|
|
|
|
mParent = mParent->GetCurrentInnerWindow();
|
|
|
|
}
|
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
mMode = aMode;
|
2010-10-20 15:19:24 +00:00
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
tabChild->SendPFilePickerConstructor(this, nsString(aTitle), aMode);
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
void
|
|
|
|
nsFilePickerProxy::InitNative(nsIWidget* aParent, const nsAString& aTitle)
|
2010-10-20 15:19:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
|
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
mFilterNames.AppendElement(aTitle);
|
|
|
|
mFilters.AppendElement(aFilter);
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetDefaultString(nsAString& aDefaultString)
|
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
aDefaultString = mDefault;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::SetDefaultString(const nsAString& aDefaultString)
|
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
mDefault = aDefaultString;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetDefaultExtension(nsAString& aDefaultExtension)
|
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
aDefaultExtension = mDefaultExtension;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::SetDefaultExtension(const nsAString& aDefaultExtension)
|
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
mDefaultExtension = aDefaultExtension;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex)
|
2010-10-20 15:19:24 +00:00
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
*aFilterIndex = mSelectedType;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex)
|
2010-10-20 15:19:24 +00:00
|
|
|
{
|
2014-02-18 00:30:06 +00:00
|
|
|
mSelectedType = aFilterIndex;
|
|
|
|
return NS_OK;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-06 02:08:30 +00:00
|
|
|
nsFilePickerProxy::GetFile(nsIFile** aFile)
|
2010-10-20 15:19:24 +00:00
|
|
|
{
|
2014-02-24 21:09:03 +00:00
|
|
|
MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomfile");
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
|
|
|
|
{
|
2014-02-24 21:09:03 +00:00
|
|
|
MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomfile");
|
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
|
|
|
|
{
|
2014-02-24 21:09:03 +00:00
|
|
|
MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomfiles");
|
2014-02-18 00:30:06 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::Show(int16_t* aReturn)
|
|
|
|
{
|
2014-02-24 21:09:03 +00:00
|
|
|
MOZ_ASSERT(false, "Show is unimplemented; use Open");
|
2014-02-18 00:30:06 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback)
|
|
|
|
{
|
|
|
|
mCallback = aCallback;
|
|
|
|
|
2014-11-09 19:26:14 +00:00
|
|
|
nsString displayDirectory;
|
|
|
|
if (mDisplayDirectory) {
|
|
|
|
mDisplayDirectory->GetPath(displayDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
SendOpen(mSelectedType, mAddToRecentDocs, mDefault, mDefaultExtension,
|
|
|
|
mFilters, mFilterNames, displayDirectory);
|
2014-02-18 00:30:06 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsFilePickerProxy::Recv__delete__(const MaybeInputFiles& aFiles,
|
|
|
|
const int16_t& aResult)
|
|
|
|
{
|
|
|
|
if (aFiles.type() == MaybeInputFiles::TInputFiles) {
|
2015-05-12 12:11:03 +00:00
|
|
|
const InfallibleTArray<PBlobChild*>& blobs = aFiles.get_InputFiles().blobsChild();
|
|
|
|
for (uint32_t i = 0; i < blobs.Length(); ++i) {
|
|
|
|
BlobChild* actor = static_cast<BlobChild*>(blobs[i]);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl = actor->GetBlobImpl();
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_ENSURE_TRUE(blobImpl, true);
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
if (!blobImpl->IsFile()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = File::Create(mParent, blobImpl);
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(file);
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-05-18 13:52:26 +00:00
|
|
|
mDomfiles.AppendElement(file);
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
2014-02-18 00:30:06 +00:00
|
|
|
}
|
2010-10-20 15:19:24 +00:00
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Done(aResult);
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
2010-10-20 15:19:24 +00:00
|
|
|
|
2014-02-18 00:30:06 +00:00
|
|
|
return true;
|
2010-10-20 15:19:24 +00:00
|
|
|
}
|
2014-02-24 21:09:03 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-18 13:52:26 +00:00
|
|
|
nsFilePickerProxy::GetDomfile(nsISupports** aDomfile)
|
2014-02-24 21:09:03 +00:00
|
|
|
{
|
|
|
|
*aDomfile = nullptr;
|
|
|
|
if (mDomfiles.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mDomfiles.Length() == 1);
|
2015-05-18 13:52:26 +00:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = mDomfiles[0].get();
|
|
|
|
blob.forget(aDomfile);
|
2014-02-24 21:09:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-18 13:52:26 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SimpleEnumerator final : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
explicit SimpleEnumerator(const nsTArray<RefPtr<File>>& aFiles)
|
2015-05-18 13:52:26 +00:00
|
|
|
: mFiles(aFiles)
|
|
|
|
, mIndex(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
HasMoreElements(bool* aRetvalue) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRetvalue);
|
2015-05-20 15:18:47 +00:00
|
|
|
*aRetvalue = mIndex < mFiles.Length();
|
2015-05-18 13:52:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
GetNext(nsISupports** aSupports) override
|
|
|
|
{
|
2015-05-20 15:18:47 +00:00
|
|
|
NS_ENSURE_TRUE(mIndex < mFiles.Length(), NS_ERROR_FAILURE);
|
|
|
|
|
2015-05-18 13:52:26 +00:00
|
|
|
nsCOMPtr<nsIDOMBlob> blob = mFiles[mIndex++].get();
|
|
|
|
blob.forget(aSupports);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SimpleEnumerator()
|
|
|
|
{}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<File>> mFiles;
|
2015-05-18 13:52:26 +00:00
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SimpleEnumerator, nsISimpleEnumerator)
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace
|
2015-05-18 13:52:26 +00:00
|
|
|
|
2014-02-24 21:09:03 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetDomfiles(nsISimpleEnumerator** aDomfiles)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<SimpleEnumerator> enumerator = new SimpleEnumerator(mDomfiles);
|
2015-05-18 13:52:26 +00:00
|
|
|
enumerator.forget(aDomfiles);
|
|
|
|
return NS_OK;
|
2014-02-24 21:09:03 +00:00
|
|
|
}
|