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 "EventTarget.h"
|
---|
31 | #include "IDLTypes.h"
|
---|
32 | #include <wtf/UniqueRef.h>
|
---|
33 |
|
---|
34 | namespace WebCore {
|
---|
35 |
|
---|
36 | template<typename IDLType> class DOMPromiseDeferred;
|
---|
37 | template<typename IDLType> class DOMPromiseProxyWithResolveCallback;
|
---|
38 |
|
---|
39 | class DOMException;
|
---|
40 |
|
---|
41 | class FontFaceSet final : public RefCounted<FontFaceSet>, private CSSFontFaceSet::FontEventClient, public EventTargetWithInlineData, public ActiveDOMObject {
|
---|
42 | WTF_MAKE_ISO_ALLOCATED(FontFaceSet);
|
---|
43 | public:
|
---|
44 | static Ref<FontFaceSet> create(ScriptExecutionContext&, const Vector<RefPtr<FontFace>>& initialFaces);
|
---|
45 | static Ref<FontFaceSet> create(ScriptExecutionContext&, CSSFontFaceSet& backing);
|
---|
46 | virtual ~FontFaceSet();
|
---|
47 |
|
---|
48 | bool has(FontFace&) const;
|
---|
49 | size_t size();
|
---|
50 | ExceptionOr<FontFaceSet&> add(FontFace&);
|
---|
51 | bool remove(FontFace&);
|
---|
52 | void clear();
|
---|
53 |
|
---|
54 | using LoadPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<FontFace>>>;
|
---|
55 | void load(const String& font, const String& text, LoadPromise&&);
|
---|
56 | ExceptionOr<bool> check(const String& font, const String& text);
|
---|
57 |
|
---|
58 | enum class LoadStatus { Loading, Loaded };
|
---|
59 | LoadStatus status() const;
|
---|
60 |
|
---|
61 | using ReadyPromise = DOMPromiseProxyWithResolveCallback<IDLInterface<FontFaceSet>>;
|
---|
62 | ReadyPromise& ready() { return m_readyPromise.get(); }
|
---|
63 | void documentDidFinishLoading();
|
---|
64 |
|
---|
65 | CSSFontFaceSet& backing() { return m_backing; }
|
---|
66 |
|
---|
67 | class Iterator {
|
---|
68 | public:
|
---|
69 | explicit Iterator(FontFaceSet&);
|
---|
70 | RefPtr<FontFace> next();
|
---|
71 |
|
---|
72 | private:
|
---|
73 | Ref<FontFaceSet> m_target;
|
---|
74 | 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.
|
---|
75 | };
|
---|
76 | Iterator createIterator() { return Iterator(*this); }
|
---|
77 |
|
---|
78 | using RefCounted::ref;
|
---|
79 | using RefCounted::deref;
|
---|
80 |
|
---|
81 | private:
|
---|
82 | struct PendingPromise : RefCounted<PendingPromise> {
|
---|
83 | static Ref<PendingPromise> create(LoadPromise&& promise)
|
---|
84 | {
|
---|
85 | return adoptRef(*new PendingPromise(WTFMove(promise)));
|
---|
86 | }
|
---|
87 | ~PendingPromise();
|
---|
88 |
|
---|
89 | private:
|
---|
90 | PendingPromise(LoadPromise&&);
|
---|
91 |
|
---|
92 | public:
|
---|
93 | Vector<Ref<FontFace>> faces;
|
---|
94 | UniqueRef<LoadPromise> promise;
|
---|
95 | bool hasReachedTerminalState { false };
|
---|
96 | };
|
---|
97 |
|
---|
98 | FontFaceSet(ScriptExecutionContext&, const Vector<RefPtr<FontFace>>&);
|
---|
99 | FontFaceSet(ScriptExecutionContext&, CSSFontFaceSet&);
|
---|
100 |
|
---|
101 | // CSSFontFaceSet::FontEventClient
|
---|
102 | void faceFinished(CSSFontFace&, CSSFontFace::Status) final;
|
---|
103 | void startedLoading() final;
|
---|
104 | void completedLoading() final;
|
---|
105 |
|
---|
106 | // ActiveDOMObject
|
---|
107 | const char* activeDOMObjectName() const final { return "FontFaceSet"; }
|
---|
108 |
|
---|
109 | // EventTarget
|
---|
110 | EventTargetInterface eventTargetInterface() const final { return FontFaceSetEventTargetInterfaceType; }
|
---|
111 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
|
---|
112 | void refEventTarget() final { ref(); }
|
---|
113 | void derefEventTarget() final { deref(); }
|
---|
114 |
|
---|
115 | // Callback for ReadyPromise.
|
---|
116 | FontFaceSet& readyPromiseResolve();
|
---|
117 |
|
---|
118 | Ref<CSSFontFaceSet> m_backing;
|
---|
119 | HashMap<RefPtr<FontFace>, Vector<Ref<PendingPromise>>> m_pendingPromises;
|
---|
120 | UniqueRef<ReadyPromise> m_readyPromise;
|
---|
121 |
|
---|
122 | bool m_isDocumentLoaded { true };
|
---|
123 | };
|
---|
124 |
|
---|
125 | }
|
---|