| 1 | /* |
| 2 | * Copyright (C) 2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "ActiveDOMObject.h" |
| 29 | #include "CSSFontFaceSet.h" |
| 30 | #include "DOMPromiseProxy.h" |
| 31 | #include "EventTarget.h" |
| 32 | #include "JSDOMPromiseDeferred.h" |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class DOMException; |
| 37 | |
| 38 | class FontFaceSet final : public RefCounted<FontFaceSet>, private CSSFontFaceSetClient, public EventTargetWithInlineData, private ActiveDOMObject { |
| 39 | WTF_MAKE_ISO_ALLOCATED(FontFaceSet); |
| 40 | public: |
| 41 | static Ref<FontFaceSet> create(Document&, const Vector<RefPtr<FontFace>>& initialFaces); |
| 42 | static Ref<FontFaceSet> create(Document&, CSSFontFaceSet& backing); |
| 43 | virtual ~FontFaceSet(); |
| 44 | |
| 45 | bool has(FontFace&) const; |
| 46 | size_t size() const; |
| 47 | FontFaceSet& add(FontFace&); |
| 48 | bool remove(FontFace&); |
| 49 | void clear(); |
| 50 | |
| 51 | using LoadPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<FontFace>>>; |
| 52 | void load(const String& font, const String& text, LoadPromise&&); |
| 53 | ExceptionOr<bool> check(const String& font, const String& text); |
| 54 | |
| 55 | enum class LoadStatus { Loading, Loaded }; |
| 56 | LoadStatus status() const; |
| 57 | |
| 58 | using ReadyPromise = DOMPromiseProxyWithResolveCallback<IDLInterface<FontFaceSet>>; |
| 59 | ReadyPromise& ready() { return m_readyPromise; } |
| 60 | |
| 61 | CSSFontFaceSet& backing() { return m_backing; } |
| 62 | |
| 63 | class Iterator { |
| 64 | public: |
| 65 | explicit Iterator(FontFaceSet&); |
| 66 | RefPtr<FontFace> next(); |
| 67 | |
| 68 | private: |
| 69 | Ref<FontFaceSet> m_target; |
| 70 | size_t m_index { 0 }; // FIXME: There needs to be a mechanism to handle when fonts are added or removed from the middle of the FontFaceSet. |
| 71 | }; |
| 72 | Iterator createIterator() { return Iterator(*this); } |
| 73 | |
| 74 | using RefCounted::ref; |
| 75 | using RefCounted::deref; |
| 76 | |
| 77 | private: |
| 78 | struct PendingPromise : RefCounted<PendingPromise> { |
| 79 | static Ref<PendingPromise> create(LoadPromise&& promise) |
| 80 | { |
| 81 | return adoptRef(*new PendingPromise(WTFMove(promise))); |
| 82 | } |
| 83 | ~PendingPromise(); |
| 84 | |
| 85 | private: |
| 86 | PendingPromise(LoadPromise&&); |
| 87 | |
| 88 | public: |
| 89 | Vector<Ref<FontFace>> faces; |
| 90 | LoadPromise promise; |
| 91 | bool hasReachedTerminalState { false }; |
| 92 | }; |
| 93 | |
| 94 | FontFaceSet(Document&, const Vector<RefPtr<FontFace>>&); |
| 95 | FontFaceSet(Document&, CSSFontFaceSet&); |
| 96 | |
| 97 | // CSSFontFaceSetClient |
| 98 | void startedLoading() final; |
| 99 | void completedLoading() final; |
| 100 | void faceFinished(CSSFontFace&, CSSFontFace::Status) final; |
| 101 | |
| 102 | // ActiveDOMObject |
| 103 | const char* activeDOMObjectName() const final { return "FontFaceSet" ; } |
| 104 | bool canSuspendForDocumentSuspension() const final; |
| 105 | |
| 106 | // EventTarget |
| 107 | EventTargetInterface eventTargetInterface() const final { return FontFaceSetEventTargetInterfaceType; } |
| 108 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
| 109 | void refEventTarget() final { ref(); } |
| 110 | void derefEventTarget() final { deref(); } |
| 111 | |
| 112 | // Callback for ReadyPromise. |
| 113 | FontFaceSet& readyPromiseResolve(); |
| 114 | |
| 115 | Ref<CSSFontFaceSet> m_backing; |
| 116 | HashMap<RefPtr<FontFace>, Vector<Ref<PendingPromise>>> m_pendingPromises; |
| 117 | ReadyPromise m_readyPromise; |
| 118 | }; |
| 119 | |
| 120 | } |
| 121 | |