/*
 * Copyright (C) 2021 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <WebCore/ActiveDOMObject.h>
#include <WebCore/FileSystemHandleGlobalIdentifier.h>
#include <WebCore/FileSystemHandleIdentifier.h>
#include <WebCore/FileSystemHandleKind.h>
#include <WebCore/FileSystemStorageConnection.h>
#include <WebCore/IDLTypes.h>
#include <wtf/CompletionHandler.h>
#include <wtf/RefCounted.h>
#include <wtf/TZoneMalloc.h>

namespace WebCore {

template<typename> class DOMPromiseDeferred;

class FileSystemHandle : public ActiveDOMObject, public RefCounted<FileSystemHandle> {
    WTF_MAKE_TZONE_ALLOCATED(FileSystemHandle);
public:
    void ref() const final { RefCounted::ref(); }
    void deref() const final { RefCounted::deref(); }

    virtual ~FileSystemHandle();

    using Kind = FileSystemHandleKind;
    Kind kind() const { return m_kind; }
    const String& name() const LIFETIME_BOUND { return m_name; }
    FileSystemHandleIdentifier identifier() const { return *m_identifier; }
    FileSystemHandleGlobalIdentifier globalIdentifier() const { return m_globalIdentifier; }
    bool isClosed() const { return m_isClosed; }
    bool isUnresolved() const { return m_isUnresolved; }
    void close();

    void isSameEntry(FileSystemHandle&, DOMPromiseDeferred<IDLBoolean>&&);
    void move(FileSystemHandle&, const String& newName, DOMPromiseDeferred<void>&&);

    void markAsUnresolved(ClientOrigin&&, Ref<FileSystemStorageConnection>&&);
    void ensureIdentifier(CompletionHandler<void(bool)>&&);

    size_t connectionHandleCount() const { return m_connection ? m_connection->handleCount() : 0; }

protected:
    FileSystemHandle(ScriptExecutionContext&, Kind, String&& name, FileSystemHandleGlobalIdentifier, Markable<FileSystemHandleIdentifier>, RefPtr<FileSystemStorageConnection>&&);
    FileSystemStorageConnection& connection() { ASSERT(m_connection); return *m_connection; }

private:
    // ActiveDOMObject.
    void stop() final;
    void resolveEnsureIdentifierCallbacks(bool);

    Kind m_kind { Kind::File };
    String m_name;
    Markable<FileSystemHandleIdentifier> m_identifier;
    RefPtr<FileSystemStorageConnection> m_connection;
    FileSystemHandleGlobalIdentifier m_globalIdentifier;
    ClientOrigin m_origin;
    bool m_isClosed { false };
    bool m_isUnresolved { false };
    Vector<CompletionHandler<void(bool)>> m_ensureIdentifierCallbacks;
};

} // namespace WebCore
