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"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "nsIDocument.h"
|
2014-02-25 01:09:03 +04:00
|
|
|
#include "nsIDOMFile.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "mozilla/unused.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.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"
|
|
|
|
#include "mozilla/dom/TabParent.h"
|
2014-09-27 03:21:57 +04:00
|
|
|
#include "mozilla/dom/ipc/BlobParent.h"
|
2014-02-18 04:30:06 +04:00
|
|
|
|
|
|
|
using mozilla::unused;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePickerParent::~FilePickerParent()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
// Before sending a blob to the child, we need to get its size and modification
|
|
|
|
// date. Otherwise it will be sent as a "mystery blob" by
|
|
|
|
// GetOrCreateActorForBlob, which will cause problems for the child
|
|
|
|
// process. This runnable stat()s the file off the main thread.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
FilePickerParent::FileSizeAndDateRunnable::FileSizeAndDateRunnable(FilePickerParent *aFPParent,
|
2015-05-11 23:17:58 +03:00
|
|
|
nsTArray<nsRefPtr<FileImpl>>& aFiles)
|
2014-02-25 01:09:03 +04:00
|
|
|
: mFilePickerParent(aFPParent)
|
|
|
|
{
|
2015-05-11 23:17:58 +03:00
|
|
|
mFiles.SwapElements(aFiles);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FilePickerParent::FileSizeAndDateRunnable::Dispatch()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
FilePickerParent::FileSizeAndDateRunnable::Run()
|
|
|
|
{
|
|
|
|
// If we're on the main thread, then that means we're done. Just send the
|
|
|
|
// results.
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (mFilePickerParent) {
|
2015-05-11 23:17:58 +03:00
|
|
|
mFilePickerParent->SendFiles(mFiles);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're not on the main thread, so do the stat().
|
2015-05-11 23:17:58 +03:00
|
|
|
for (unsigned i = 0; i < mFiles.Length(); i++) {
|
2015-03-19 00:21:02 +03:00
|
|
|
ErrorResult rv;
|
2015-05-11 23:17:58 +03:00
|
|
|
mFiles[i]->GetSize(rv);
|
|
|
|
mFiles[i]->GetLastModified(rv);
|
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();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FilePickerParent::FileSizeAndDateRunnable::Destroy()
|
|
|
|
{
|
|
|
|
mFilePickerParent = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-11 23:17:58 +03:00
|
|
|
FilePickerParent::SendFiles(const nsTArray<nsRefPtr<FileImpl>>& aFiles)
|
2014-02-25 01:09:03 +04:00
|
|
|
{
|
2015-02-06 00:47:32 +03:00
|
|
|
nsIContentParent* parent = TabParent::GetFrom(Manager())->Manager();
|
2015-05-11 23:17:58 +03:00
|
|
|
InfallibleTArray<PBlobParent*> files;
|
2014-02-25 01:09:03 +04:00
|
|
|
|
2015-05-11 23:17:58 +03:00
|
|
|
for (unsigned i = 0; i < aFiles.Length(); i++) {
|
2015-05-12 15:09:51 +03:00
|
|
|
BlobParent* blobParent = parent->GetOrCreateActorForFileImpl(aFiles[i]);
|
|
|
|
if (blobParent) {
|
|
|
|
files.AppendElement(blobParent);
|
2014-02-25 01:09:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:17:58 +03:00
|
|
|
InputFiles infiles;
|
|
|
|
infiles.filesParent().SwapElements(files);
|
|
|
|
unused << Send__delete__(this, infiles, 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) {
|
|
|
|
unused << Send__delete__(this, void_t(), mResult);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
|
2015-05-11 23:17:58 +03:00
|
|
|
nsTArray<nsRefPtr<FileImpl>> files;
|
2014-02-18 04:30:06 +04:00
|
|
|
if (mMode == nsIFilePicker::modeOpenMultiple) {
|
|
|
|
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);
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2015-05-11 23:17:58 +03:00
|
|
|
nsRefPtr<FileImpl> fileimpl = new FileImplFile(file);
|
|
|
|
files.AppendElement(fileimpl);
|
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) {
|
2015-05-11 23:17:58 +03:00
|
|
|
nsRefPtr<FileImpl> fileimpl = new FileImplFile(file);
|
|
|
|
files.AppendElement(fileimpl);
|
2014-04-23 00:00:03 +04:00
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 01:09:03 +04:00
|
|
|
MOZ_ASSERT(!mRunnable);
|
2015-05-11 23:17:58 +03:00
|
|
|
mRunnable = new FileSizeAndDateRunnable(this, files);
|
2014-02-25 01:09:03 +04:00
|
|
|
if (!mRunnable->Dispatch()) {
|
|
|
|
unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
|
|
|
|
}
|
2014-02-18 04:30:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FilePickerParent::CreateFilePicker()
|
|
|
|
{
|
|
|
|
mFilePicker = do_CreateInstance("@mozilla.org/filepicker;1");
|
|
|
|
if (!mFilePicker) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-06 00:47:32 +03:00
|
|
|
Element* element = TabParent::GetFrom(Manager())->GetOwnerElement();
|
2014-02-18 04:30:06 +04:00
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(element->OwnerDoc()->GetWindow());
|
|
|
|
if (!window) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(mFilePicker->Init(window, mTitle, mMode));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FilePickerParent::RecvOpen(const int16_t& aSelectedType,
|
|
|
|
const bool& aAddToRecentDocs,
|
|
|
|
const nsString& aDefaultFile,
|
|
|
|
const nsString& aDefaultExtension,
|
2015-01-16 22:58:52 +03:00
|
|
|
InfallibleTArray<nsString>&& aFilters,
|
|
|
|
InfallibleTArray<nsString>&& aFilterNames,
|
2014-11-09 22:26:14 +03:00
|
|
|
const nsString& aDisplayDirectory)
|
2014-02-18 04:30:06 +04:00
|
|
|
{
|
|
|
|
if (!CreateFilePicker()) {
|
|
|
|
unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFilePicker->SetAddToRecentDocs(aAddToRecentDocs);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aFilters.Length(); ++i) {
|
|
|
|
mFilePicker->AppendFilter(aFilterNames[i], aFilters[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
mFilePicker->SetDefaultString(aDefaultFile);
|
|
|
|
mFilePicker->SetDefaultExtension(aDefaultExtension);
|
|
|
|
mFilePicker->SetFilterIndex(aSelectedType);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 04:30:06 +04:00
|
|
|
mCallback = new FilePickerShownCallback(this);
|
|
|
|
|
|
|
|
mFilePicker->Open(mCallback);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|