michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: set sw=4 ts=8 et tw=80 : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "FilePickerParent.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsDOMFile.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMFile.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIFile.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: #include "mozilla/unused.h" michael@0: #include "mozilla/dom/ContentParent.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "mozilla/dom/TabParent.h" michael@0: #include "mozilla/dom/ipc/Blob.h" michael@0: michael@0: using mozilla::unused; michael@0: using namespace mozilla::dom; michael@0: michael@0: NS_IMPL_ISUPPORTS(FilePickerParent::FilePickerShownCallback, michael@0: nsIFilePickerShownCallback); michael@0: michael@0: NS_IMETHODIMP michael@0: FilePickerParent::FilePickerShownCallback::Done(int16_t aResult) michael@0: { michael@0: if (mFilePickerParent) { michael@0: mFilePickerParent->Done(aResult); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: FilePickerParent::FilePickerShownCallback::Destroy() michael@0: { michael@0: mFilePickerParent = nullptr; michael@0: } michael@0: michael@0: FilePickerParent::~FilePickerParent() michael@0: { michael@0: } michael@0: michael@0: // Before sending a blob to the child, we need to get its size and modification michael@0: // date. Otherwise it will be sent as a "mystery blob" by michael@0: // GetOrCreateActorForBlob, which will cause problems for the child michael@0: // process. This runnable stat()s the file off the main thread. michael@0: // michael@0: // We run code in three places: michael@0: // 1. The main thread calls Dispatch() to start the runnable. michael@0: // 2. The stream transport thread stat()s the file in Run() and then dispatches michael@0: // the same runnable on the main thread. michael@0: // 3. The main thread sends the results over IPC. michael@0: FilePickerParent::FileSizeAndDateRunnable::FileSizeAndDateRunnable(FilePickerParent *aFPParent, michael@0: nsCOMArray& aDomfiles) michael@0: : mFilePickerParent(aFPParent) michael@0: { michael@0: mDomfiles.SwapElements(aDomfiles); michael@0: } michael@0: michael@0: bool michael@0: FilePickerParent::FileSizeAndDateRunnable::Dispatch() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: mEventTarget = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); michael@0: if (!mEventTarget) { michael@0: return false; michael@0: } michael@0: michael@0: nsresult rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL); michael@0: return NS_SUCCEEDED(rv); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FilePickerParent::FileSizeAndDateRunnable::Run() michael@0: { michael@0: // If we're on the main thread, then that means we're done. Just send the michael@0: // results. michael@0: if (NS_IsMainThread()) { michael@0: if (mFilePickerParent) { michael@0: mFilePickerParent->SendFiles(mDomfiles); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: // We're not on the main thread, so do the stat(). michael@0: for (unsigned i = 0; i < mDomfiles.Length(); i++) { michael@0: uint64_t size, lastModified; michael@0: mDomfiles[i]->GetSize(&size); michael@0: mDomfiles[i]->GetMozLastModifiedDate(&lastModified); michael@0: } michael@0: michael@0: // Dispatch ourselves back on the main thread. michael@0: if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) { michael@0: // It's hard to see how we can recover gracefully in this case. The child michael@0: // process is waiting for an IPC, but that can only happen on the main michael@0: // thread. michael@0: MOZ_CRASH(); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: FilePickerParent::FileSizeAndDateRunnable::Destroy() michael@0: { michael@0: mFilePickerParent = nullptr; michael@0: } michael@0: michael@0: void michael@0: FilePickerParent::SendFiles(const nsCOMArray& aDomfiles) michael@0: { michael@0: ContentParent* parent = static_cast(Manager()->Manager()); michael@0: InfallibleTArray files; michael@0: michael@0: for (unsigned i = 0; i < aDomfiles.Length(); i++) { michael@0: BlobParent* blob = parent->GetOrCreateActorForBlob(aDomfiles[i]); michael@0: if (blob) { michael@0: files.AppendElement(blob); michael@0: } michael@0: } michael@0: michael@0: InputFiles infiles; michael@0: infiles.filesParent().SwapElements(files); michael@0: unused << Send__delete__(this, infiles, mResult); michael@0: } michael@0: michael@0: void michael@0: FilePickerParent::Done(int16_t aResult) michael@0: { michael@0: mResult = aResult; michael@0: michael@0: if (mResult != nsIFilePicker::returnOK) { michael@0: unused << Send__delete__(this, void_t(), mResult); michael@0: return; michael@0: } michael@0: michael@0: nsCOMArray domfiles; michael@0: if (mMode == nsIFilePicker::modeOpenMultiple) { michael@0: nsCOMPtr iter; michael@0: NS_ENSURE_SUCCESS_VOID(mFilePicker->GetFiles(getter_AddRefs(iter))); michael@0: michael@0: nsCOMPtr supports; michael@0: bool loop = true; michael@0: while (NS_SUCCEEDED(iter->HasMoreElements(&loop)) && loop) { michael@0: iter->GetNext(getter_AddRefs(supports)); michael@0: if (supports) { michael@0: nsCOMPtr file = do_QueryInterface(supports); michael@0: nsCOMPtr domfile = new nsDOMFileFile(file); michael@0: domfiles.AppendElement(domfile); michael@0: } michael@0: } michael@0: } else { michael@0: nsCOMPtr file; michael@0: mFilePicker->GetFile(getter_AddRefs(file)); michael@0: if (file) { michael@0: nsCOMPtr domfile = new nsDOMFileFile(file); michael@0: domfiles.AppendElement(domfile); michael@0: } michael@0: } michael@0: michael@0: MOZ_ASSERT(!mRunnable); michael@0: mRunnable = new FileSizeAndDateRunnable(this, domfiles); michael@0: if (!mRunnable->Dispatch()) { michael@0: unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: FilePickerParent::CreateFilePicker() michael@0: { michael@0: mFilePicker = do_CreateInstance("@mozilla.org/filepicker;1"); michael@0: if (!mFilePicker) { michael@0: return false; michael@0: } michael@0: michael@0: Element* element = static_cast(Manager())->GetOwnerElement(); michael@0: if (!element) { michael@0: return false; michael@0: } michael@0: michael@0: nsCOMPtr window = do_QueryInterface(element->OwnerDoc()->GetWindow()); michael@0: if (!window) { michael@0: return false; michael@0: } michael@0: michael@0: return NS_SUCCEEDED(mFilePicker->Init(window, mTitle, mMode)); michael@0: } michael@0: michael@0: bool michael@0: FilePickerParent::RecvOpen(const int16_t& aSelectedType, michael@0: const bool& aAddToRecentDocs, michael@0: const nsString& aDefaultFile, michael@0: const nsString& aDefaultExtension, michael@0: const InfallibleTArray& aFilters, michael@0: const InfallibleTArray& aFilterNames) michael@0: { michael@0: if (!CreateFilePicker()) { michael@0: unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel); michael@0: return true; michael@0: } michael@0: michael@0: mFilePicker->SetAddToRecentDocs(aAddToRecentDocs); michael@0: michael@0: for (uint32_t i = 0; i < aFilters.Length(); ++i) { michael@0: mFilePicker->AppendFilter(aFilterNames[i], aFilters[i]); michael@0: } michael@0: michael@0: mFilePicker->SetDefaultString(aDefaultFile); michael@0: mFilePicker->SetDefaultExtension(aDefaultExtension); michael@0: mFilePicker->SetFilterIndex(aSelectedType); michael@0: michael@0: mCallback = new FilePickerShownCallback(this); michael@0: michael@0: mFilePicker->Open(mCallback); michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: FilePickerParent::ActorDestroy(ActorDestroyReason aWhy) michael@0: { michael@0: if (mCallback) { michael@0: mCallback->Destroy(); michael@0: mCallback = nullptr; michael@0: } michael@0: if (mRunnable) { michael@0: mRunnable->Destroy(); michael@0: mRunnable = nullptr; michael@0: } michael@0: }