1/*
2 * Copyright (C) 2018 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#if ENABLE(CSS_PAINTING_API)
29
30#include "CSSPaintCallback.h"
31#include "WorkletGlobalScope.h"
32#include <JavaScriptCore/JSObject.h>
33#include <JavaScriptCore/Strong.h>
34
35namespace JSC {
36class JSObject;
37} // namespace JSC
38
39namespace WebCore {
40class JSDOMGlobalObject;
41
42class PaintWorkletGlobalScope : public WorkletGlobalScope {
43 WTF_MAKE_ISO_ALLOCATED(PaintWorkletGlobalScope);
44public:
45 static Ref<PaintWorkletGlobalScope> create(Document&, ScriptSourceCode&&);
46
47 ExceptionOr<void> registerPaint(JSC::ExecState&, JSDOMGlobalObject&, const String& name, JSC::Strong<JSC::JSObject> paintConstructor);
48 double devicePixelRatio() const;
49
50 // All paint definitions must be destroyed before the vm is destroyed, because otherwise they will point to freed memory.
51 struct PaintDefinition : public CanMakeWeakPtr<PaintDefinition> {
52 PaintDefinition(const AtomString& name, JSC::JSObject* paintConstructor, Ref<CSSPaintCallback>&&, Vector<String>&& inputProperties, Vector<String>&& inputArguments);
53
54 const AtomString name;
55 const JSC::JSObject* const paintConstructor;
56 const Ref<CSSPaintCallback> paintCallback;
57 const Vector<String> inputProperties;
58 const Vector<String> inputArguments;
59 };
60
61 HashMap<String, std::unique_ptr<PaintDefinition>>& paintDefinitionMap() { ASSERT(m_paintDefinitionLock.isLocked()); return m_paintDefinitionMap; }
62 Lock& paintDefinitionLock() { return m_paintDefinitionLock; }
63
64 void prepareForDestruction() final
65 {
66 {
67 auto locker = holdLock(paintDefinitionLock());
68 paintDefinitionMap().clear();
69 }
70 WorkletGlobalScope::prepareForDestruction();
71 }
72
73private:
74 PaintWorkletGlobalScope(Document&, ScriptSourceCode&&);
75
76 ~PaintWorkletGlobalScope()
77 {
78#if !ASSERT_DISABLED
79 auto locker = holdLock(paintDefinitionLock());
80 ASSERT(paintDefinitionMap().isEmpty());
81#endif
82 }
83
84 bool isPaintWorkletGlobalScope() const final { return true; }
85
86 HashMap<String, std::unique_ptr<PaintDefinition>> m_paintDefinitionMap;
87 Lock m_paintDefinitionLock;
88};
89
90} // namespace WebCore
91
92SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::PaintWorkletGlobalScope)
93static bool isType(const WebCore::ScriptExecutionContext& context) { return is<WebCore::WorkletGlobalScope>(context) && downcast<WebCore::WorkletGlobalScope>(context).isPaintWorkletGlobalScope(); }
94static bool isType(const WebCore::WorkletGlobalScope& context) { return context.isPaintWorkletGlobalScope(); }
95SPECIALIZE_TYPE_TRAITS_END()
96
97#endif // ENABLE(CSS_PAINTING_API)
98