DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (7e962631ba42)

VCS Links

SimpleEnumerator

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * 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
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsFilePickerProxy.h"
#include "nsComponentManagerUtils.h"
#include "nsIFile.h"
#include "mozilla/dom/Directory.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/TabChild.h"
#include "mozilla/dom/IPCBlobUtils.h"

using namespace mozilla::dom;

NS_IMPL_ISUPPORTS(nsFilePickerProxy, nsIFilePicker)

nsFilePickerProxy::nsFilePickerProxy()
  : mSelectedType(0)
  , mIPCActive(false)
{
}

nsFilePickerProxy::~nsFilePickerProxy()
{
}

NS_IMETHODIMP
nsFilePickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
                        int16_t aMode)
{
  TabChild* tabChild = TabChild::GetFrom(aParent);
  if (!tabChild) {
    return NS_ERROR_FAILURE;
  }

  mParent = nsPIDOMWindowOuter::From(aParent);

  mMode = aMode;

  NS_ADDREF_THIS();
  tabChild->SendPFilePickerConstructor(this, nsString(aTitle), aMode);

  mIPCActive = true;
  return NS_OK;
}

void
nsFilePickerProxy::InitNative(nsIWidget* aParent, const nsAString& aTitle)
{
}

NS_IMETHODIMP
nsFilePickerProxy::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
{
  mFilterNames.AppendElement(aTitle);
  mFilters.AppendElement(aFilter);
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::GetDefaultString(nsAString& aDefaultString)
{
  aDefaultString = mDefault;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::SetDefaultString(const nsAString& aDefaultString)
{
  mDefault = aDefaultString;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::GetDefaultExtension(nsAString& aDefaultExtension)
{
  aDefaultExtension = mDefaultExtension;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::SetDefaultExtension(const nsAString& aDefaultExtension)
{
  mDefaultExtension = aDefaultExtension;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::GetFilterIndex(int32_t* aFilterIndex)
{
  *aFilterIndex = mSelectedType;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::SetFilterIndex(int32_t aFilterIndex)
{
  mSelectedType = aFilterIndex;
  return NS_OK;
}

NS_IMETHODIMP
nsFilePickerProxy::GetFile(nsIFile** aFile)
{
  MOZ_ASSERT(false, "GetFile is unimplemented; use GetDomFileOrDirectory");
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsFilePickerProxy::GetFileURL(nsIURI** aFileURL)
{
  MOZ_ASSERT(false, "GetFileURL is unimplemented; use GetDomFileOrDirectory");
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsFilePickerProxy::GetFiles(nsISimpleEnumerator** aFiles)
{
  MOZ_ASSERT(false, "GetFiles is unimplemented; use GetDomFileOrDirectoryEnumerator");
  return NS_ERROR_FAILURE;
}

nsresult
nsFilePickerProxy::Show(int16_t* aReturn)
{
  MOZ_ASSERT(false, "Show is unimplemented; use Open");
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback)
{
  mCallback = aCallback;

  nsString displayDirectory;
  if (mDisplayDirectory) {
    mDisplayDirectory->GetPath(displayDirectory);
  }

  if (!mIPCActive) {
    return NS_ERROR_FAILURE;
  }

  SendOpen(mSelectedType, mAddToRecentDocs, mDefault, mDefaultExtension,
           mFilters, mFilterNames, displayDirectory, mDisplaySpecialDirectory,
           mOkButtonLabel);

  return NS_OK;
}

mozilla::ipc::IPCResult
nsFilePickerProxy::Recv__delete__(const MaybeInputData& aData,
                                  const int16_t& aResult)
{
  if (aData.type() == MaybeInputData::TInputBlobs) {
    const InfallibleTArray<IPCBlob>& blobs = aData.get_InputBlobs().blobs();
    for (uint32_t i = 0; i < blobs.Length(); ++i) {
      RefPtr<BlobImpl> blobImpl = IPCBlobUtils::Deserialize(blobs[i]);
      NS_ENSURE_TRUE(blobImpl, IPC_OK());

      if (!blobImpl->IsFile()) {
        return IPC_OK();
      }

      nsPIDOMWindowInner* inner =
        mParent ? mParent->GetCurrentInnerWindow() : nullptr;
      RefPtr<File> file = File::Create(inner, blobImpl);
      MOZ_ASSERT(file);

      OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement();
      element->SetAsFile() = file;
    }
  } else if (aData.type() == MaybeInputData::TInputDirectory) {
    nsCOMPtr<nsIFile> file;
    const nsAString& path(aData.get_InputDirectory().directoryPath());
    nsresult rv = NS_NewLocalFile(path, true, getter_AddRefs(file));
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return IPC_OK();
    }

    RefPtr<Directory> directory =
      Directory::Create(mParent->GetCurrentInnerWindow(), file);
    MOZ_ASSERT(directory);

    OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement();
    element->SetAsDirectory() = directory;
  }

  if (mCallback) {
    mCallback->Done(aResult);
    mCallback = nullptr;
  }

  return IPC_OK();
}

NS_IMETHODIMP
nsFilePickerProxy::GetDomFileOrDirectory(nsISupports** aValue)
{
  *aValue = nullptr;
  if (mFilesOrDirectories.IsEmpty()) {
    return NS_OK;
  }

  MOZ_ASSERT(mFilesOrDirectories.Length() == 1);

  if (mFilesOrDirectories[0].IsFile()) {
    nsCOMPtr<nsIDOMBlob> blob = mFilesOrDirectories[0].GetAsFile().get();
    blob.forget(aValue);
    return NS_OK;
  }

  MOZ_ASSERT(mFilesOrDirectories[0].IsDirectory());
  RefPtr<Directory> directory = mFilesOrDirectories[0].GetAsDirectory();
  directory.forget(aValue);
  return NS_OK;
}

namespace {

class SimpleEnumerator final : public nsISimpleEnumerator
{
public:
  NS_DECL_ISUPPORTS

  explicit
  SimpleEnumerator(const nsTArray<OwningFileOrDirectory>& aFilesOrDirectories)
    : mFilesOrDirectories(aFilesOrDirectories)
    , mIndex(0)
  {}

  NS_IMETHOD
  HasMoreElements(bool* aRetvalue) override
  {
    MOZ_ASSERT(aRetvalue);
    *aRetvalue = mIndex < mFilesOrDirectories.Length();
    return NS_OK;
  }

  NS_IMETHOD
  GetNext(nsISupports** aValue) override
  {
    NS_ENSURE_TRUE(mIndex < mFilesOrDirectories.Length(), NS_ERROR_FAILURE);

    uint32_t index = mIndex++;

    if (mFilesOrDirectories[index].IsFile()) {
      nsCOMPtr<nsIDOMBlob> blob = mFilesOrDirectories[index].GetAsFile().get();
      blob.forget(aValue);
      return NS_OK;
    }

    MOZ_ASSERT(mFilesOrDirectories[index].IsDirectory());
    RefPtr<Directory> directory = mFilesOrDirectories[index].GetAsDirectory();
    directory.forget(aValue);
    return NS_OK;
  }

private:
  ~SimpleEnumerator()
  {}

  nsTArray<mozilla::dom::OwningFileOrDirectory> mFilesOrDirectories;
  uint32_t mIndex;
};

NS_IMPL_ISUPPORTS(SimpleEnumerator, nsISimpleEnumerator)

} // namespace

NS_IMETHODIMP
nsFilePickerProxy::GetDomFileOrDirectoryEnumerator(nsISimpleEnumerator** aDomfiles)
{
  RefPtr<SimpleEnumerator> enumerator =
    new SimpleEnumerator(mFilesOrDirectories);
  enumerator.forget(aDomfiles);
  return NS_OK;
}

void
nsFilePickerProxy::ActorDestroy(ActorDestroyReason aWhy)
{
  mIPCActive = false;

  if (mCallback) {
    mCallback->Done(nsIFilePicker::returnCancel);
    mCallback = nullptr;
  }
}