2014-02-18 04:30:06 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2010-10-20 19:19:24 +04:00
|
|
|
*
|
2012-05-21 15:12:37 +04: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 19:19:24 +04:00
|
|
|
|
|
|
|
#include "nsFilePickerProxy.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIFile.h"
|
2016-03-20 13:56:10 +03:00
|
|
|
#include "mozilla/dom/Directory.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "mozilla/dom/TabChild.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
2010-10-20 19:19:24 +04:00
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
using namespace mozilla::dom;
|
2010-10-20 19:19:24 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFilePickerProxy, nsIFilePicker)
|
2010-10-20 19:19:24 +04:00
|
|
|
|
|
|
|
nsFilePickerProxy::nsFilePickerProxy()
|
2016-01-12 21:16:59 +03:00
|
|
|
: mSelectedType(0)
|
2014-02-18 04:30:06 +04:00
|
|
|
{
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFilePickerProxy::~nsFilePickerProxy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsFilePickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t aMode)
|
2010-10-20 19:19:24 +04:00
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
TabChild* tabChild = TabChild::GetFrom(aParent);
|
|
|
|
if (!tabChild) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
mParent = nsPIDOMWindowOuter::From(aParent);
|
2014-10-10 20:00:34 +04:00
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
mMode = aMode;
|
2010-10-20 19:19:24 +04:00
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
tabChild->SendPFilePickerConstructor(this, nsString(aTitle), aMode);
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
void
|
|
|
|
nsFilePickerProxy::InitNative(nsIWidget* aParent, const nsAString& aTitle)
|
2010-10-20 19:19:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
|
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
mFilterNames.AppendElement(aTitle);
|
|
|
|
mFilters.AppendElement(aFilter);
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetDefaultString(nsAString& aDefaultString)
|
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
aDefaultString = mDefault;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::SetDefaultString(const nsAString& aDefaultString)
|
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
mDefault = aDefaultString;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetDefaultExtension(nsAString& aDefaultExtension)
|
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
aDefaultExtension = mDefaultExtension;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::SetDefaultExtension(const nsAString& aDefaultExtension)
|
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
mDefaultExtension = aDefaultExtension;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex)
|
2010-10-20 19:19:24 +04:00
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
*aFilterIndex = mSelectedType;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex)
|
2010-10-20 19:19:24 +04:00
|
|
|
{
|
2014-02-18 04:30:06 +04:00
|
|
|
mSelectedType = aFilterIndex;
|
|
|
|
return NS_OK;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-06 06:08:30 +04:00
|
|
|
nsFilePickerProxy::GetFile(nsIFile** aFile)
|
2010-10-20 19:19:24 +04:00
|
|
|
{
|
2016-01-12 22:18:15 +03:00
|
|
|
MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomFileOrDirectory");
|
2014-02-25 01:09:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
|
|
|
|
{
|
2016-01-12 22:18:15 +03:00
|
|
|
MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomFileOrDirectory");
|
2014-02-25 01:09:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
|
|
|
|
{
|
2016-01-12 22:18:15 +03:00
|
|
|
MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomFileOrDirectoryEnumerator");
|
2014-02-18 04:30:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::Show(int16_t* aReturn)
|
|
|
|
{
|
2014-02-25 01:09:03 +04:00
|
|
|
MOZ_ASSERT(false, "Show is unimplemented; use Open");
|
2014-02-18 04:30:06 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback)
|
|
|
|
{
|
|
|
|
mCallback = aCallback;
|
|
|
|
|
2014-11-09 22:26:14 +03:00
|
|
|
nsString displayDirectory;
|
|
|
|
if (mDisplayDirectory) {
|
|
|
|
mDisplayDirectory->GetPath(displayDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
SendOpen(mSelectedType, mAddToRecentDocs, mDefault, mDefaultExtension,
|
2016-08-24 02:07:21 +03:00
|
|
|
mFilters, mFilterNames, displayDirectory, mOkButtonLabel);
|
2014-02-18 04:30:06 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2016-03-20 13:56:10 +03:00
|
|
|
nsFilePickerProxy::Recv__delete__(const MaybeInputData& aData,
|
2014-02-18 04:30:06 +04:00
|
|
|
const int16_t& aResult)
|
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
if (aData.type() == MaybeInputData::TInputBlobs) {
|
|
|
|
const InfallibleTArray<PBlobChild*>& blobs = aData.get_InputBlobs().blobsChild();
|
2015-05-12 15:11:03 +03:00
|
|
|
for (uint32_t i = 0; i < blobs.Length(); ++i) {
|
|
|
|
BlobChild* actor = static_cast<BlobChild*>(blobs[i]);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = actor->GetBlobImpl();
|
2016-11-15 06:26:00 +03:00
|
|
|
NS_ENSURE_TRUE(blobImpl, IPC_OK());
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
if (!blobImpl->IsFile()) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-05-12 15:09:51 +03:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:02:26 +03:00
|
|
|
nsPIDOMWindowInner* inner =
|
|
|
|
mParent ? mParent->GetCurrentInnerWindow() : nullptr;
|
|
|
|
RefPtr<File> file = File::Create(inner, blobImpl);
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(file);
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement();
|
|
|
|
element->SetAsFile() = file;
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
} else if (aData.type() == MaybeInputData::TInputDirectory) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2017-01-21 09:46:44 +03:00
|
|
|
const nsAString& path(aData.get_InputDirectory().directoryPath());
|
|
|
|
nsresult rv = NS_NewLocalFile(path, true, getter_AddRefs(file));
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-03-20 13:56:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Directory> directory =
|
2016-04-18 10:32:30 +03:00
|
|
|
Directory::Create(mParent->GetCurrentInnerWindow(), file);
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(directory);
|
|
|
|
|
|
|
|
OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement();
|
|
|
|
element->SetAsDirectory() = directory;
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
2010-10-20 19:19:24 +04:00
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Done(aResult);
|
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
2010-10-20 19:19:24 +04:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2010-10-20 19:19:24 +04:00
|
|
|
}
|
2014-02-25 01:09:03 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-12 22:18:15 +03:00
|
|
|
nsFilePickerProxy::GetDomFileOrDirectory(nsISupports** aValue)
|
2014-02-25 01:09:03 +04:00
|
|
|
{
|
2016-01-12 22:18:15 +03:00
|
|
|
*aValue = nullptr;
|
|
|
|
if (mFilesOrDirectories.IsEmpty()) {
|
2014-02-25 01:09:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-12 22:18:15 +03:00
|
|
|
MOZ_ASSERT(mFilesOrDirectories.Length() == 1);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
if (mFilesOrDirectories[0].IsFile()) {
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = mFilesOrDirectories[0].GetAsFile().get();
|
|
|
|
blob.forget(aValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mFilesOrDirectories[0].IsDirectory());
|
|
|
|
RefPtr<Directory> directory = mFilesOrDirectories[0].GetAsDirectory();
|
|
|
|
directory.forget(aValue);
|
2014-02-25 01:09:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-18 16:52:26 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SimpleEnumerator final : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
explicit
|
|
|
|
SimpleEnumerator(const nsTArray<OwningFileOrDirectory>& aFilesOrDirectories)
|
|
|
|
: mFilesOrDirectories(aFilesOrDirectories)
|
2015-05-18 16:52:26 +03:00
|
|
|
, mIndex(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
HasMoreElements(bool* aRetvalue) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRetvalue);
|
2016-03-20 13:56:10 +03:00
|
|
|
*aRetvalue = mIndex < mFilesOrDirectories.Length();
|
2015-05-18 16:52:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2016-03-20 13:56:10 +03:00
|
|
|
GetNext(nsISupports** aValue) override
|
2015-05-18 16:52:26 +03:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
NS_ENSURE_TRUE(mIndex < mFilesOrDirectories.Length(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
uint32_t index = mIndex++;
|
|
|
|
|
|
|
|
if (mFilesOrDirectories[index].IsFile()) {
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = mFilesOrDirectories[index].GetAsFile().get();
|
|
|
|
blob.forget(aValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-05-20 18:18:47 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(mFilesOrDirectories[index].IsDirectory());
|
|
|
|
RefPtr<Directory> directory = mFilesOrDirectories[index].GetAsDirectory();
|
|
|
|
directory.forget(aValue);
|
2015-05-18 16:52:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SimpleEnumerator()
|
|
|
|
{}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsTArray<mozilla::dom::OwningFileOrDirectory> mFilesOrDirectories;
|
2015-05-18 16:52:26 +03:00
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(SimpleEnumerator, nsISimpleEnumerator)
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-05-18 16:52:26 +03:00
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
NS_IMETHODIMP
|
2016-01-12 22:18:15 +03:00
|
|
|
nsFilePickerProxy::GetDomFileOrDirectoryEnumerator(nsISimpleEnumerator** aDomfiles)
|
2014-02-25 01:09:03 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
RefPtr<SimpleEnumerator> enumerator =
|
|
|
|
new SimpleEnumerator(mFilesOrDirectories);
|
2015-05-18 16:52:26 +03:00
|
|
|
enumerator.forget(aDomfiles);
|
|
|
|
return NS_OK;
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|