Bug 910384 - [e10s] Use blobs for cross-cross file picker (r=bent)

This commit is contained in:
Bill McCloskey 2014-02-24 13:09:03 -08:00
parent 34a32fa34a
commit 40617f3160
7 changed files with 168 additions and 47 deletions

View File

@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=4 ts=8 et tw=80 : */
/* 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

View File

@ -1,4 +1,4 @@
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8 -*- */
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
/* vim: set sw=4 ts=8 et tw=80 ft=cpp : */
/* 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

View File

@ -6,13 +6,18 @@
#include "FilePickerParent.h"
#include "nsComponentManagerUtils.h"
#include "nsDOMFile.h"
#include "nsNetCID.h"
#include "nsIDocument.h"
#include "nsIDOMFile.h"
#include "nsIDOMWindow.h"
#include "nsIFile.h"
#include "nsISimpleEnumerator.h"
#include "mozilla/unused.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/TabParent.h"
#include "mozilla/dom/ipc/Blob.h"
using mozilla::unused;
using namespace mozilla::dom;
@ -39,10 +44,96 @@ FilePickerParent::~FilePickerParent()
{
}
// 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,
nsCOMArray<nsIDOMFile>& aDomfiles)
: mFilePickerParent(aFPParent)
{
mDomfiles.SwapElements(aDomfiles);
}
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) {
mFilePickerParent->SendFiles(mDomfiles);
}
return NS_OK;
}
// We're not on the main thread, so do the stat().
for (unsigned i = 0; i < mDomfiles.Length(); i++) {
uint64_t size, lastModified;
mDomfiles[i]->GetSize(&size);
mDomfiles[i]->GetMozLastModifiedDate(&lastModified);
}
// Dispatch ourselves back on the main thread.
if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) {
// 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
FilePickerParent::SendFiles(const nsCOMArray<nsIDOMFile>& aDomfiles)
{
ContentParent* parent = static_cast<ContentParent*>(Manager()->Manager());
InfallibleTArray<PBlobParent*> files;
for (unsigned i = 0; i < aDomfiles.Length(); i++) {
BlobParent* blob = parent->GetOrCreateActorForBlob(aDomfiles[i]);
if (blob) {
files.AppendElement(blob);
}
}
InputFiles infiles;
infiles.filesParent().SwapElements(files);
unused << Send__delete__(this, infiles, mResult);
}
void
FilePickerParent::Done(int16_t aResult)
{
InfallibleTArray<nsString> files;
mResult = aResult;
nsCOMArray<nsIDOMFile> domfiles;
if (mMode == nsIFilePicker::modeOpenMultiple) {
nsCOMPtr<nsISimpleEnumerator> iter;
@ -52,26 +143,21 @@ FilePickerParent::Done(int16_t aResult)
bool loop = true;
while (NS_SUCCEEDED(iter->HasMoreElements(&loop)) && loop) {
iter->GetNext(getter_AddRefs(file));
if (file) {
nsAutoString path;
if (NS_SUCCEEDED(file->GetPath(path))) {
files.AppendElement(path);
}
}
nsCOMPtr<nsIDOMFile> domfile = new nsDOMFileFile(file);
domfiles.AppendElement(domfile);
}
} else {
nsCOMPtr<nsIFile> file;
mFilePicker->GetFile(getter_AddRefs(file));
if (file) {
nsAutoString path;
if (NS_SUCCEEDED(file->GetPath(path))) {
files.AppendElement(path);
}
}
nsCOMPtr<nsIDOMFile> domfile = new nsDOMFileFile(file);
domfiles.AppendElement(domfile);
}
unused << Send__delete__(this, InputFiles(files), aResult);
MOZ_ASSERT(!mRunnable);
mRunnable = new FileSizeAndDateRunnable(this, domfiles);
if (!mRunnable->Dispatch()) {
unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
}
}
bool
@ -129,5 +215,10 @@ FilePickerParent::ActorDestroy(ActorDestroyReason aWhy)
{
if (mCallback) {
mCallback->Destroy();
mCallback = nullptr;
}
if (mRunnable) {
mRunnable->Destroy();
mRunnable = nullptr;
}
}

View File

@ -7,7 +7,11 @@
#ifndef mozilla_dom_FilePickerParent_h
#define mozilla_dom_FilePickerParent_h
#include "nsIDOMFile.h"
#include "nsIEventTarget.h"
#include "nsIFilePicker.h"
#include "nsCOMArray.h"
#include "nsThreadUtils.h"
#include "mozilla/dom/PFilePickerParent.h"
namespace mozilla {
@ -25,6 +29,7 @@ class FilePickerParent : public PFilePickerParent
virtual ~FilePickerParent();
void Done(int16_t aResult);
void SendFiles(const nsCOMArray<nsIDOMFile>& aDomfiles);
virtual bool RecvOpen(const int16_t& aSelectedType,
const bool& aAddToRecentDocs,
@ -55,11 +60,26 @@ class FilePickerParent : public PFilePickerParent
private:
bool CreateFilePicker();
class FileSizeAndDateRunnable : public nsRunnable
{
FilePickerParent* mFilePickerParent;
nsCOMArray<nsIDOMFile> mDomfiles;
nsCOMPtr<nsIEventTarget> mEventTarget;
public:
FileSizeAndDateRunnable(FilePickerParent *aFPParent, nsCOMArray<nsIDOMFile>& aDomfiles);
bool Dispatch();
NS_IMETHOD Run();
void Destroy();
};
nsRefPtr<FileSizeAndDateRunnable> mRunnable;
nsRefPtr<FilePickerShownCallback> mCallback;
nsCOMPtr<nsIFilePicker> mFilePicker;
nsString mTitle;
int16_t mMode;
int16_t mResult;
};
} // namespace dom

View File

@ -4,6 +4,7 @@
* 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 protocol PBlob;
include protocol PBrowser;
using struct mozilla::void_t from "ipc/IPCMessageUtils.h";
@ -13,7 +14,7 @@ namespace dom {
struct InputFiles
{
nsString[] files;
PBlob[] files;
};
union MaybeInputFiles

View File

@ -8,7 +8,9 @@
#include "nsComponentManagerUtils.h"
#include "nsNetUtil.h"
#include "nsIFile.h"
#include "nsDOMFile.h"
#include "mozilla/dom/TabChild.h"
#include "mozilla/dom/ipc/Blob.h"
using namespace mozilla::dom;
@ -97,48 +99,30 @@ nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex)
NS_IMETHODIMP
nsFilePickerProxy::GetFile(nsIFile** aFile)
{
NS_ENSURE_ARG_POINTER(aFile);
*aFile = nullptr;
if (mFiles.IsEmpty()) {
return NS_OK;
}
nsCOMPtr<nsIFile> file = mFiles[0];
file.forget(aFile);
return NS_OK;
MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomfile");
return NS_ERROR_FAILURE;
}
/* readonly attribute nsIFileURL fileURL; */
NS_IMETHODIMP
nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
{
nsCOMPtr<nsIFile> file;
GetFile(getter_AddRefs(file));
nsCOMPtr<nsIURI> uri;
NS_NewFileURI(getter_AddRefs(uri), file);
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
return CallQueryInterface(uri, aFileURL);
MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomfile");
return NS_ERROR_FAILURE;
}
/* readonly attribute nsISimpleEnumerator files; */
NS_IMETHODIMP
nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
{
NS_ENSURE_ARG_POINTER(aFiles);
if (mMode == nsIFilePicker::modeOpenMultiple) {
return NS_NewArrayEnumerator(aFiles, mFiles);
}
MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomfiles");
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsFilePickerProxy::Show(int16_t* aReturn)
{
MOZ_ASSERT(false, "Show is unimplemented; use Open");
return NS_ERROR_NOT_IMPLEMENTED;
}
@ -158,12 +142,13 @@ nsFilePickerProxy::Recv__delete__(const MaybeInputFiles& aFiles,
const int16_t& aResult)
{
if (aFiles.type() == MaybeInputFiles::TInputFiles) {
const InfallibleTArray<nsString>& files = aFiles.get_InputFiles().files();
const InfallibleTArray<PBlobChild*>& files = aFiles.get_InputFiles().filesChild();
for (uint32_t i = 0; i < files.Length(); ++i) {
nsCOMPtr<nsIFile> file(do_CreateInstance("@mozilla.org/file/local;1"));
BlobChild* actor = static_cast<BlobChild*>(files[i]);
nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob();
nsCOMPtr<nsIDOMFile> file(do_QueryInterface(blob));
NS_ENSURE_TRUE(file, true);
file->InitWithPath(files[i]);
mFiles.AppendObject(file);
mDomfiles.AppendObject(file);
}
}
@ -174,3 +159,23 @@ nsFilePickerProxy::Recv__delete__(const MaybeInputFiles& aFiles,
return true;
}
NS_IMETHODIMP
nsFilePickerProxy::GetDomfile(nsIDOMFile** aDomfile)
{
*aDomfile = nullptr;
if (mDomfiles.IsEmpty()) {
return NS_OK;
}
MOZ_ASSERT(mDomfiles.Length() == 1);
nsCOMPtr<nsIDOMFile> domfile = mDomfiles[0];
domfile.forget(aDomfile);
return NS_OK;
}
NS_IMETHODIMP
nsFilePickerProxy::GetDomfiles(nsISimpleEnumerator** aDomfiles)
{
return NS_NewArrayEnumerator(aDomfiles, mDomfiles);
}

View File

@ -43,6 +43,10 @@ public:
NS_IMETHODIMP GetFile(nsIFile** aFile);
NS_IMETHODIMP GetFileURL(nsIURI** aFileURL);
NS_IMETHODIMP GetFiles(nsISimpleEnumerator** aFiles);
NS_IMETHODIMP GetDomfile(nsIDOMFile** aFile);
NS_IMETHODIMP GetDomfiles(nsISimpleEnumerator** aFiles);
NS_IMETHODIMP Show(int16_t* aReturn);
NS_IMETHODIMP Open(nsIFilePickerShownCallback* aCallback);
@ -54,7 +58,7 @@ private:
~nsFilePickerProxy();
void InitNative(nsIWidget*, const nsAString&);
nsCOMArray<nsIFile> mFiles;
nsCOMArray<nsIDOMFile> mDomfiles;
nsCOMPtr<nsIFilePickerShownCallback> mCallback;
int16_t mSelectedType;