dom/ipc/FilePickerParent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: set sw=4 ts=8 et tw=80 :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "FilePickerParent.h"
michael@0 8 #include "nsComponentManagerUtils.h"
michael@0 9 #include "nsDOMFile.h"
michael@0 10 #include "nsNetCID.h"
michael@0 11 #include "nsIDocument.h"
michael@0 12 #include "nsIDOMFile.h"
michael@0 13 #include "nsIDOMWindow.h"
michael@0 14 #include "nsIFile.h"
michael@0 15 #include "nsISimpleEnumerator.h"
michael@0 16 #include "mozilla/unused.h"
michael@0 17 #include "mozilla/dom/ContentParent.h"
michael@0 18 #include "mozilla/dom/Element.h"
michael@0 19 #include "mozilla/dom/TabParent.h"
michael@0 20 #include "mozilla/dom/ipc/Blob.h"
michael@0 21
michael@0 22 using mozilla::unused;
michael@0 23 using namespace mozilla::dom;
michael@0 24
michael@0 25 NS_IMPL_ISUPPORTS(FilePickerParent::FilePickerShownCallback,
michael@0 26 nsIFilePickerShownCallback);
michael@0 27
michael@0 28 NS_IMETHODIMP
michael@0 29 FilePickerParent::FilePickerShownCallback::Done(int16_t aResult)
michael@0 30 {
michael@0 31 if (mFilePickerParent) {
michael@0 32 mFilePickerParent->Done(aResult);
michael@0 33 }
michael@0 34 return NS_OK;
michael@0 35 }
michael@0 36
michael@0 37 void
michael@0 38 FilePickerParent::FilePickerShownCallback::Destroy()
michael@0 39 {
michael@0 40 mFilePickerParent = nullptr;
michael@0 41 }
michael@0 42
michael@0 43 FilePickerParent::~FilePickerParent()
michael@0 44 {
michael@0 45 }
michael@0 46
michael@0 47 // Before sending a blob to the child, we need to get its size and modification
michael@0 48 // date. Otherwise it will be sent as a "mystery blob" by
michael@0 49 // GetOrCreateActorForBlob, which will cause problems for the child
michael@0 50 // process. This runnable stat()s the file off the main thread.
michael@0 51 //
michael@0 52 // We run code in three places:
michael@0 53 // 1. The main thread calls Dispatch() to start the runnable.
michael@0 54 // 2. The stream transport thread stat()s the file in Run() and then dispatches
michael@0 55 // the same runnable on the main thread.
michael@0 56 // 3. The main thread sends the results over IPC.
michael@0 57 FilePickerParent::FileSizeAndDateRunnable::FileSizeAndDateRunnable(FilePickerParent *aFPParent,
michael@0 58 nsCOMArray<nsIDOMFile>& aDomfiles)
michael@0 59 : mFilePickerParent(aFPParent)
michael@0 60 {
michael@0 61 mDomfiles.SwapElements(aDomfiles);
michael@0 62 }
michael@0 63
michael@0 64 bool
michael@0 65 FilePickerParent::FileSizeAndDateRunnable::Dispatch()
michael@0 66 {
michael@0 67 MOZ_ASSERT(NS_IsMainThread());
michael@0 68
michael@0 69 mEventTarget = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
michael@0 70 if (!mEventTarget) {
michael@0 71 return false;
michael@0 72 }
michael@0 73
michael@0 74 nsresult rv = mEventTarget->Dispatch(this, NS_DISPATCH_NORMAL);
michael@0 75 return NS_SUCCEEDED(rv);
michael@0 76 }
michael@0 77
michael@0 78 NS_IMETHODIMP
michael@0 79 FilePickerParent::FileSizeAndDateRunnable::Run()
michael@0 80 {
michael@0 81 // If we're on the main thread, then that means we're done. Just send the
michael@0 82 // results.
michael@0 83 if (NS_IsMainThread()) {
michael@0 84 if (mFilePickerParent) {
michael@0 85 mFilePickerParent->SendFiles(mDomfiles);
michael@0 86 }
michael@0 87 return NS_OK;
michael@0 88 }
michael@0 89
michael@0 90 // We're not on the main thread, so do the stat().
michael@0 91 for (unsigned i = 0; i < mDomfiles.Length(); i++) {
michael@0 92 uint64_t size, lastModified;
michael@0 93 mDomfiles[i]->GetSize(&size);
michael@0 94 mDomfiles[i]->GetMozLastModifiedDate(&lastModified);
michael@0 95 }
michael@0 96
michael@0 97 // Dispatch ourselves back on the main thread.
michael@0 98 if (NS_FAILED(NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL))) {
michael@0 99 // It's hard to see how we can recover gracefully in this case. The child
michael@0 100 // process is waiting for an IPC, but that can only happen on the main
michael@0 101 // thread.
michael@0 102 MOZ_CRASH();
michael@0 103 }
michael@0 104 return NS_OK;
michael@0 105 }
michael@0 106
michael@0 107 void
michael@0 108 FilePickerParent::FileSizeAndDateRunnable::Destroy()
michael@0 109 {
michael@0 110 mFilePickerParent = nullptr;
michael@0 111 }
michael@0 112
michael@0 113 void
michael@0 114 FilePickerParent::SendFiles(const nsCOMArray<nsIDOMFile>& aDomfiles)
michael@0 115 {
michael@0 116 ContentParent* parent = static_cast<ContentParent*>(Manager()->Manager());
michael@0 117 InfallibleTArray<PBlobParent*> files;
michael@0 118
michael@0 119 for (unsigned i = 0; i < aDomfiles.Length(); i++) {
michael@0 120 BlobParent* blob = parent->GetOrCreateActorForBlob(aDomfiles[i]);
michael@0 121 if (blob) {
michael@0 122 files.AppendElement(blob);
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 InputFiles infiles;
michael@0 127 infiles.filesParent().SwapElements(files);
michael@0 128 unused << Send__delete__(this, infiles, mResult);
michael@0 129 }
michael@0 130
michael@0 131 void
michael@0 132 FilePickerParent::Done(int16_t aResult)
michael@0 133 {
michael@0 134 mResult = aResult;
michael@0 135
michael@0 136 if (mResult != nsIFilePicker::returnOK) {
michael@0 137 unused << Send__delete__(this, void_t(), mResult);
michael@0 138 return;
michael@0 139 }
michael@0 140
michael@0 141 nsCOMArray<nsIDOMFile> domfiles;
michael@0 142 if (mMode == nsIFilePicker::modeOpenMultiple) {
michael@0 143 nsCOMPtr<nsISimpleEnumerator> iter;
michael@0 144 NS_ENSURE_SUCCESS_VOID(mFilePicker->GetFiles(getter_AddRefs(iter)));
michael@0 145
michael@0 146 nsCOMPtr<nsISupports> supports;
michael@0 147 bool loop = true;
michael@0 148 while (NS_SUCCEEDED(iter->HasMoreElements(&loop)) && loop) {
michael@0 149 iter->GetNext(getter_AddRefs(supports));
michael@0 150 if (supports) {
michael@0 151 nsCOMPtr<nsIFile> file = do_QueryInterface(supports);
michael@0 152 nsCOMPtr<nsIDOMFile> domfile = new nsDOMFileFile(file);
michael@0 153 domfiles.AppendElement(domfile);
michael@0 154 }
michael@0 155 }
michael@0 156 } else {
michael@0 157 nsCOMPtr<nsIFile> file;
michael@0 158 mFilePicker->GetFile(getter_AddRefs(file));
michael@0 159 if (file) {
michael@0 160 nsCOMPtr<nsIDOMFile> domfile = new nsDOMFileFile(file);
michael@0 161 domfiles.AppendElement(domfile);
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 MOZ_ASSERT(!mRunnable);
michael@0 166 mRunnable = new FileSizeAndDateRunnable(this, domfiles);
michael@0 167 if (!mRunnable->Dispatch()) {
michael@0 168 unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
michael@0 169 }
michael@0 170 }
michael@0 171
michael@0 172 bool
michael@0 173 FilePickerParent::CreateFilePicker()
michael@0 174 {
michael@0 175 mFilePicker = do_CreateInstance("@mozilla.org/filepicker;1");
michael@0 176 if (!mFilePicker) {
michael@0 177 return false;
michael@0 178 }
michael@0 179
michael@0 180 Element* element = static_cast<TabParent*>(Manager())->GetOwnerElement();
michael@0 181 if (!element) {
michael@0 182 return false;
michael@0 183 }
michael@0 184
michael@0 185 nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(element->OwnerDoc()->GetWindow());
michael@0 186 if (!window) {
michael@0 187 return false;
michael@0 188 }
michael@0 189
michael@0 190 return NS_SUCCEEDED(mFilePicker->Init(window, mTitle, mMode));
michael@0 191 }
michael@0 192
michael@0 193 bool
michael@0 194 FilePickerParent::RecvOpen(const int16_t& aSelectedType,
michael@0 195 const bool& aAddToRecentDocs,
michael@0 196 const nsString& aDefaultFile,
michael@0 197 const nsString& aDefaultExtension,
michael@0 198 const InfallibleTArray<nsString>& aFilters,
michael@0 199 const InfallibleTArray<nsString>& aFilterNames)
michael@0 200 {
michael@0 201 if (!CreateFilePicker()) {
michael@0 202 unused << Send__delete__(this, void_t(), nsIFilePicker::returnCancel);
michael@0 203 return true;
michael@0 204 }
michael@0 205
michael@0 206 mFilePicker->SetAddToRecentDocs(aAddToRecentDocs);
michael@0 207
michael@0 208 for (uint32_t i = 0; i < aFilters.Length(); ++i) {
michael@0 209 mFilePicker->AppendFilter(aFilterNames[i], aFilters[i]);
michael@0 210 }
michael@0 211
michael@0 212 mFilePicker->SetDefaultString(aDefaultFile);
michael@0 213 mFilePicker->SetDefaultExtension(aDefaultExtension);
michael@0 214 mFilePicker->SetFilterIndex(aSelectedType);
michael@0 215
michael@0 216 mCallback = new FilePickerShownCallback(this);
michael@0 217
michael@0 218 mFilePicker->Open(mCallback);
michael@0 219 return true;
michael@0 220 }
michael@0 221
michael@0 222 void
michael@0 223 FilePickerParent::ActorDestroy(ActorDestroyReason aWhy)
michael@0 224 {
michael@0 225 if (mCallback) {
michael@0 226 mCallback->Destroy();
michael@0 227 mCallback = nullptr;
michael@0 228 }
michael@0 229 if (mRunnable) {
michael@0 230 mRunnable->Destroy();
michael@0 231 mRunnable = nullptr;
michael@0 232 }
michael@0 233 }

mercurial