LCOV - code coverage report
Current view: top level - dom/filesystem/compat - FileSystemDirectoryReader.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 78 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 20 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : /* This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
       5             :  * You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : #include "FileSystemDirectoryReader.h"
       8             : #include "CallbackRunnables.h"
       9             : #include "FileSystemFileEntry.h"
      10             : #include "mozilla/dom/FileBinding.h"
      11             : #include "mozilla/dom/FileSystemUtils.h"
      12             : #include "mozilla/dom/Directory.h"
      13             : #include "mozilla/dom/DirectoryBinding.h"
      14             : #include "mozilla/dom/Promise.h"
      15             : #include "mozilla/dom/PromiseNativeHandler.h"
      16             : 
      17             : namespace mozilla {
      18             : namespace dom {
      19             : 
      20             : namespace {
      21             : 
      22             : class PromiseHandler final : public PromiseNativeHandler
      23             : {
      24             : public:
      25             :   NS_DECL_ISUPPORTS
      26             : 
      27           0 :   PromiseHandler(FileSystemDirectoryEntry* aParentEntry,
      28             :                  FileSystem* aFileSystem,
      29             :                  FileSystemEntriesCallback* aSuccessCallback,
      30             :                  ErrorCallback* aErrorCallback)
      31           0 :     : mParentEntry(aParentEntry)
      32             :     , mFileSystem(aFileSystem)
      33             :     , mSuccessCallback(aSuccessCallback)
      34           0 :     , mErrorCallback(aErrorCallback)
      35             :   {
      36           0 :     MOZ_ASSERT(aParentEntry);
      37           0 :     MOZ_ASSERT(aFileSystem);
      38           0 :     MOZ_ASSERT(aSuccessCallback);
      39           0 :   }
      40             : 
      41             :   virtual void
      42           0 :   ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
      43             :   {
      44           0 :     if(NS_WARN_IF(!aValue.isObject())) {
      45           0 :       return;
      46             :     }
      47             : 
      48           0 :     JS::Rooted<JSObject*> obj(aCx, &aValue.toObject());
      49             : 
      50             :     uint32_t length;
      51           0 :     if (NS_WARN_IF(!JS_GetArrayLength(aCx, obj, &length))) {
      52           0 :       return;
      53             :     }
      54             : 
      55           0 :     Sequence<OwningNonNull<FileSystemEntry>> sequence;
      56           0 :     if (NS_WARN_IF(!sequence.SetLength(length, fallible))) {
      57           0 :       return;
      58             :     }
      59             : 
      60           0 :     for (uint32_t i = 0; i < length; ++i) {
      61           0 :       JS::Rooted<JS::Value> value(aCx);
      62           0 :       if (NS_WARN_IF(!JS_GetElement(aCx, obj, i, &value))) {
      63           0 :         return;
      64             :       }
      65             : 
      66           0 :       if(NS_WARN_IF(!value.isObject())) {
      67           0 :         return;
      68             :       }
      69             : 
      70           0 :       JS::Rooted<JSObject*> valueObj(aCx, &value.toObject());
      71             : 
      72           0 :       RefPtr<File> file;
      73           0 :       if (NS_SUCCEEDED(UNWRAP_OBJECT(File, valueObj, file))) {
      74             :         RefPtr<FileSystemFileEntry> entry =
      75           0 :           new FileSystemFileEntry(mParentEntry->GetParentObject(), file,
      76           0 :                                   mParentEntry, mFileSystem);
      77           0 :         sequence[i] = entry;
      78           0 :         continue;
      79             :       }
      80             : 
      81           0 :       RefPtr<Directory> directory;
      82           0 :       if (NS_WARN_IF(NS_FAILED(UNWRAP_OBJECT(Directory, valueObj,
      83             :                                              directory)))) {
      84           0 :         return;
      85             :       }
      86             : 
      87             :       RefPtr<FileSystemDirectoryEntry> entry =
      88           0 :         new FileSystemDirectoryEntry(mParentEntry->GetParentObject(), directory,
      89           0 :                                      mParentEntry, mFileSystem);
      90           0 :       sequence[i] = entry;
      91             :     }
      92             : 
      93           0 :     mSuccessCallback->HandleEvent(sequence);
      94             :   }
      95             : 
      96             :   virtual void
      97           0 :   RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
      98             :   {
      99           0 :     if (mErrorCallback) {
     100             :       RefPtr<ErrorCallbackRunnable> runnable =
     101           0 :         new ErrorCallbackRunnable(mParentEntry->GetParentObject(),
     102             :                                   mErrorCallback,
     103           0 :                                   NS_ERROR_DOM_INVALID_STATE_ERR);
     104             : 
     105           0 :       FileSystemUtils::DispatchRunnable(mParentEntry->GetParentObject(),
     106           0 :                                         runnable.forget());
     107             :     }
     108           0 :   }
     109             : 
     110             : private:
     111           0 :   ~PromiseHandler() {}
     112             : 
     113             :   RefPtr<FileSystemDirectoryEntry> mParentEntry;
     114             :   RefPtr<FileSystem> mFileSystem;
     115             :   RefPtr<FileSystemEntriesCallback> mSuccessCallback;
     116             :   RefPtr<ErrorCallback> mErrorCallback;
     117             : };
     118             : 
     119           0 : NS_IMPL_ISUPPORTS0(PromiseHandler);
     120             : 
     121             : } // anonymous namespace
     122             : 
     123           0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(FileSystemDirectoryReader, mParentEntry,
     124             :                                       mDirectory, mFileSystem)
     125             : 
     126           0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(FileSystemDirectoryReader)
     127           0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(FileSystemDirectoryReader)
     128             : 
     129           0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemDirectoryReader)
     130           0 :   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     131           0 :   NS_INTERFACE_MAP_ENTRY(nsISupports)
     132           0 : NS_INTERFACE_MAP_END
     133             : 
     134           0 : FileSystemDirectoryReader::FileSystemDirectoryReader(FileSystemDirectoryEntry* aParentEntry,
     135             :                                                      FileSystem* aFileSystem,
     136           0 :                                                      Directory* aDirectory)
     137             :   : mParentEntry(aParentEntry)
     138             :   , mFileSystem(aFileSystem)
     139             :   , mDirectory(aDirectory)
     140           0 :   , mAlreadyRead(false)
     141             : {
     142           0 :   MOZ_ASSERT(aParentEntry);
     143           0 :   MOZ_ASSERT(aFileSystem);
     144           0 : }
     145             : 
     146           0 : FileSystemDirectoryReader::~FileSystemDirectoryReader()
     147           0 : {}
     148             : 
     149             : JSObject*
     150           0 : FileSystemDirectoryReader::WrapObject(JSContext* aCx,
     151             :                                       JS::Handle<JSObject*> aGivenProto)
     152             : {
     153           0 :   return FileSystemDirectoryReaderBinding::Wrap(aCx, this, aGivenProto);
     154             : }
     155             : 
     156             : void
     157           0 : FileSystemDirectoryReader::ReadEntries(FileSystemEntriesCallback& aSuccessCallback,
     158             :                                        const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
     159             :                                        ErrorResult& aRv)
     160             : {
     161           0 :   MOZ_ASSERT(mDirectory);
     162             : 
     163           0 :   if (mAlreadyRead) {
     164             :     RefPtr<EmptyEntriesCallbackRunnable> runnable =
     165           0 :       new EmptyEntriesCallbackRunnable(&aSuccessCallback);
     166             : 
     167           0 :     FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
     168           0 :     return;
     169             :   }
     170             : 
     171             :   // This object can be used only once.
     172           0 :   mAlreadyRead = true;
     173             : 
     174           0 :   ErrorResult rv;
     175           0 :   RefPtr<Promise> promise = mDirectory->GetFilesAndDirectories(rv);
     176           0 :   if (NS_WARN_IF(rv.Failed())) {
     177           0 :     ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
     178           0 :                               rv.StealNSResult());
     179           0 :     return;
     180             :   }
     181             : 
     182             :   RefPtr<PromiseHandler> handler =
     183             :     new PromiseHandler(mParentEntry, mFileSystem, &aSuccessCallback,
     184           0 :                        aErrorCallback.WasPassed()
     185           0 :                          ? &aErrorCallback.Value() : nullptr);
     186           0 :   promise->AppendNativeHandler(handler);
     187             : }
     188             : 
     189             : } // dom namespace
     190             : } // mozilla namespace

Generated by: LCOV version 1.13