2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2014-02-18 04:30:06 +04:00
|
|
|
* 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/. */
|
|
|
|
|
|
|
|
#include "FilePickerParent.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-02-25 01:09:03 +04:00
|
|
|
#include "nsNetCID.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "mozilla/dom/FileBlobImpl.h"
|
2017-03-16 10:53:49 +03:00
|
|
|
#include "mozilla/dom/FileSystemSecurity.h"
|
2014-02-25 01:09:03 +04:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2019-04-10 00:38:15 +03:00
|
|
|
#include "mozilla/dom/BrowserParent.h"
|
2017-04-25 15:07:30 +03:00
|
|
|
#include "mozilla/dom/IPCBlobUtils.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2014-02-18 04:30:06 +04:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(FilePickerParent::FilePickerShownCallback,
|
|
|
|
nsIFilePickerShownCallback);
|
2014-02-18 04:30:06 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FilePickerParent::FilePickerShownCallback::Done(int16_t aResult) {
|
|
|
|
if (mFilePickerParent) {
|
|
|
|
mFilePickerParent->Done(aResult);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilePickerParent::FilePickerShownCallback::Destroy() {
|
|
|
|
mFilePickerParent = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:11:40 +03:00
|
|
|
FilePickerParent::~FilePickerParent() = default;
|
2014-02-18 04:30:06 +04:00
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
// We run code in three places:
|
|
|
|
// 1. The main thread calls Dispatch() to start the runnable.
|
|
|
|
// 2. The stream transport thread stat()s the file in Run() and then dispatches
|
|
|
|
// the same runnable on the main thread.
|
|
|
|
// 3. The main thread sends the results over IPC.
|
2017-06-12 22:34:10 +03:00
|
|
|
FilePickerParent::IORunnable::IORunnable(FilePickerParent* aFPParent,
|
2020-08-04 14:27:07 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIFile>>&& aFiles,
|
2016-03-20 13:56:10 +03:00
|
|
|
bool aIsDirectory)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("dom::FilePickerParent::IORunnable"),
|
|
|
|
mFilePickerParent(aFPParent),
|
2020-08-04 14:27:07 +03:00
|
|
|
mFiles(std::move(aFiles)),
|
2017-06-12 22:34:10 +03:00
|
|
|
mIsDirectory(aIsDirectory) {
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT_IF(aIsDirectory, mFiles.Length() == 1);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
bool FilePickerParent::IORunnable::Dispatch() {
|
2014-02-25 01:09:03 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mEventTarget = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
if (!mEventTarget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-03-20 13:56:10 +03:00
|
|
|
FilePickerParent::IORunnable::Run() {
|
2014-02-25 01:09:03 +04:00
|
|
|
// If we're on the main thread, then that means we're done. Just send the
|
|
|
|
// results.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (mFilePickerParent) {
|
2016-03-20 13:56:10 +03:00
|
|
|
mFilePickerParent->SendFilesOrDirectories(mResults);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
// We're not on the main thread, so do the IO.
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mFiles.Length(); ++i) {
|
|
|
|
if (mIsDirectory) {
|
|
|
|
nsAutoString path;
|
|
|
|
nsresult rv = mFiles[i]->GetPath(path);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplOrString* data = mResults.AppendElement();
|
|
|
|
data->mType = BlobImplOrString::eDirectoryPath;
|
|
|
|
data->mDirectoryPath = path;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = new FileBlobImpl(mFiles[i]);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
blobImpl->GetSize(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2016-07-22 14:01:41 +03:00
|
|
|
error.SuppressException();
|
2016-03-20 13:56:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
blobImpl->GetLastModified(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2016-07-22 14:01:41 +03:00
|
|
|
error.SuppressException();
|
2016-03-20 13:56:10 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplOrString* data = mResults.AppendElement();
|
|
|
|
data->mType = BlobImplOrString::eBlobImpl;
|
|
|
|
data->mBlobImpl = blobImpl;
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch ourselves back on the main thread.
|
2014-05-23 23:53:17 +04:00
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(this))) {
|
2014-02-25 01:09:03 +04:00
|
|
|
// It's hard to see how we can recover gracefully in this case. The child
|
|
|
|
// process is waiting for an IPC, but that can only happen on the main
|
|
|
|
// thread.
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
void FilePickerParent::IORunnable::Destroy() { mFilePickerParent = nullptr; }
|
2014-02-25 01:09:03 +04:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
void FilePickerParent::SendFilesOrDirectories(
|
|
|
|
const nsTArray<BlobImplOrString>& aData) {
|
2019-04-10 00:38:15 +03:00
|
|
|
ContentParent* parent = BrowserParent::GetFrom(Manager())->Manager();
|
2017-03-16 10:53:49 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
if (mMode == nsIFilePicker::modeGetFolder) {
|
|
|
|
MOZ_ASSERT(aData.Length() <= 1);
|
|
|
|
if (aData.IsEmpty()) {
|
|
|
|
Unused << Send__delete__(this, void_t(), mResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aData[0].mType == BlobImplOrString::eDirectoryPath);
|
|
|
|
|
2017-03-16 10:53:49 +03:00
|
|
|
// Let's inform the security singleton about the given access of this tab on
|
|
|
|
// this directory path.
|
|
|
|
RefPtr<FileSystemSecurity> fss = FileSystemSecurity::GetOrCreate();
|
|
|
|
fss->GrantAccessToContentProcess(parent->ChildID(),
|
|
|
|
aData[0].mDirectoryPath);
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
InputDirectory input;
|
|
|
|
input.directoryPath() = aData[0].mDirectoryPath;
|
|
|
|
Unused << Send__delete__(this, input, mResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 06:27:27 +03:00
|
|
|
nsTArray<IPCBlob> ipcBlobs;
|
2014-02-25 01:09:03 +04:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
for (unsigned i = 0; i < aData.Length(); i++) {
|
2017-04-25 15:07:30 +03:00
|
|
|
IPCBlob ipcBlob;
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aData[i].mType == BlobImplOrString::eBlobImpl);
|
2017-04-25 15:07:30 +03:00
|
|
|
nsresult rv = IPCBlobUtils::Serialize(aData[i].mBlobImpl, parent, ipcBlob);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
break;
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
2017-04-25 15:07:30 +03:00
|
|
|
|
|
|
|
ipcBlobs.AppendElement(ipcBlob);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
InputBlobs inblobs;
|
2020-08-04 14:27:07 +03:00
|
|
|
inblobs.blobs() = std::move(ipcBlobs);
|
2017-04-25 15:07:30 +03:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, inblobs, mResult);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
void FilePickerParent::Done(int16_t aResult) {
|
2014-02-25 01:09:03 +04:00
|
|
|
mResult = aResult;
|
|
|
|
|
2014-04-23 00:00:03 +04:00
|
|
|
if (mResult != nsIFilePicker::returnOK) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, void_t(), mResult);
|
2014-04-23 00:00:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIFile>> files;
|
2015-08-17 05:03:49 +03:00
|
|
|
if (mMode == nsIFilePicker::modeOpenMultiple) {
|
2014-02-18 04:30:06 +04:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> iter;
|
|
|
|
NS_ENSURE_SUCCESS_VOID(mFilePicker->GetFiles(getter_AddRefs(iter)));
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
nsCOMPtr<nsISupports> supports;
|
2014-02-18 04:30:06 +04:00
|
|
|
bool loop = true;
|
|
|
|
while (NS_SUCCEEDED(iter->HasMoreElements(&loop)) && loop) {
|
2014-03-15 23:00:17 +04:00
|
|
|
iter->GetNext(getter_AddRefs(supports));
|
2014-04-23 00:00:03 +04:00
|
|
|
if (supports) {
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(supports);
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(file);
|
|
|
|
files.AppendElement(file);
|
2014-04-23 00:00:03 +04:00
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
mFilePicker->GetFile(getter_AddRefs(file));
|
2014-04-23 00:00:03 +04:00
|
|
|
if (file) {
|
2016-03-20 13:56:10 +03:00
|
|
|
files.AppendElement(file);
|
2014-04-23 00:00:03 +04:00
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
if (files.IsEmpty()) {
|
|
|
|
Unused << Send__delete__(this, void_t(), mResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
MOZ_ASSERT(!mRunnable);
|
2020-08-04 14:27:07 +03:00
|
|
|
mRunnable = new IORunnable(this, std::move(files),
|
|
|
|
mMode == nsIFilePicker::modeGetFolder);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2015-07-10 20:55:52 +03:00
|
|
|
// Dispatch to background thread to do I/O:
|
2014-02-25 01:09:03 +04:00
|
|
|
if (!mRunnable->Dispatch()) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FilePickerParent::CreateFilePicker() {
|
|
|
|
mFilePicker = do_CreateInstance("@mozilla.org/filepicker;1");
|
|
|
|
if (!mFilePicker) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:38:15 +03:00
|
|
|
Element* element = BrowserParent::GetFrom(Manager())->GetOwnerElement();
|
2014-02-18 04:30:06 +04:00
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> window = element->OwnerDoc()->GetWindow();
|
2014-02-18 04:30:06 +04:00
|
|
|
if (!window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(mFilePicker->Init(window, mTitle, mMode));
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult FilePickerParent::RecvOpen(
|
2014-02-18 04:30:06 +04:00
|
|
|
const int16_t& aSelectedType, const bool& aAddToRecentDocs,
|
|
|
|
const nsString& aDefaultFile, const nsString& aDefaultExtension,
|
2019-07-10 06:27:27 +03:00
|
|
|
nsTArray<nsString>&& aFilters, nsTArray<nsString>&& aFilterNames,
|
|
|
|
nsTArray<nsString>&& aRawFilters, const nsString& aDisplayDirectory,
|
2019-07-30 21:13:05 +03:00
|
|
|
const nsString& aDisplaySpecialDirectory, const nsString& aOkButtonLabel,
|
|
|
|
const int16_t& aCapture) {
|
2014-02-18 04:30:06 +04:00
|
|
|
if (!CreateFilePicker()) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mFilePicker->SetAddToRecentDocs(aAddToRecentDocs);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aFilters.Length(); ++i) {
|
|
|
|
mFilePicker->AppendFilter(aFilterNames[i], aFilters[i]);
|
|
|
|
}
|
|
|
|
|
2019-01-24 19:21:05 +03:00
|
|
|
for (uint32_t i = 0; i < aRawFilters.Length(); ++i) {
|
|
|
|
mFilePicker->AppendRawFilter(aRawFilters[i]);
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
mFilePicker->SetDefaultString(aDefaultFile);
|
|
|
|
mFilePicker->SetDefaultExtension(aDefaultExtension);
|
|
|
|
mFilePicker->SetFilterIndex(aSelectedType);
|
2016-08-24 02:07:21 +03:00
|
|
|
mFilePicker->SetOkButtonLabel(aOkButtonLabel);
|
2019-07-30 21:13:05 +03:00
|
|
|
mFilePicker->SetCapture(aCapture);
|
2014-02-18 04:30:06 +04:00
|
|
|
|
2014-11-09 22:26:14 +03:00
|
|
|
if (!aDisplayDirectory.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIFile> localFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
|
|
|
|
if (localFile) {
|
|
|
|
localFile->InitWithPath(aDisplayDirectory);
|
|
|
|
mFilePicker->SetDisplayDirectory(localFile);
|
|
|
|
}
|
2017-04-26 19:20:19 +03:00
|
|
|
} else if (!aDisplaySpecialDirectory.IsEmpty()) {
|
|
|
|
mFilePicker->SetDisplaySpecialDirectory(aDisplaySpecialDirectory);
|
2014-11-09 22:26:14 +03:00
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
mCallback = new FilePickerShownCallback(this);
|
|
|
|
|
|
|
|
mFilePicker->Open(mCallback);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilePickerParent::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Destroy();
|
2014-02-25 01:09:03 +04:00
|
|
|
mCallback = nullptr;
|
|
|
|
}
|
|
|
|
if (mRunnable) {
|
|
|
|
mRunnable->Destroy();
|
|
|
|
mRunnable = nullptr;
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
}
|