1/*
2 * Copyright (C) 2008 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 "CSSImageGeneratorValue.h"
29#include "CanvasBase.h"
30#include "HTMLCanvasElement.h"
31#include "RenderElement.h"
32
33namespace WebCore {
34
35class Document;
36
37class CSSCanvasValue final : public CSSImageGeneratorValue {
38public:
39 static Ref<CSSCanvasValue> create(const String& name) { return adoptRef(*new CSSCanvasValue(name)); }
40 ~CSSCanvasValue();
41
42 String customCSSText() const;
43
44 RefPtr<Image> image(RenderElement*, const FloatSize&);
45 bool isFixedSize() const { return true; }
46 FloatSize fixedSize(const RenderElement*);
47
48 HTMLCanvasElement* element() const { return m_element; }
49
50 bool isPending() const { return false; }
51 void loadSubimages(CachedResourceLoader&, const ResourceLoaderOptions&) { }
52
53 bool equals(const CSSCanvasValue&) const;
54
55 // NOTE: We put the CanvasObserver in a member instead of inheriting from it
56 // to avoid adding a vptr to CSSCanvasValue.
57 class CanvasObserverProxy final : public CanvasObserver {
58 public:
59 explicit CanvasObserverProxy(CSSCanvasValue& ownerValue)
60 : m_ownerValue(ownerValue)
61 {
62 }
63
64 bool isCanvasObserverProxy() const final { return true; }
65
66 const CSSCanvasValue& ownerValue() const { return m_ownerValue; }
67
68 private:
69 void canvasChanged(CanvasBase& canvasBase, const FloatRect& changedRect) final
70 {
71 ASSERT(is<HTMLCanvasElement>(canvasBase));
72 m_ownerValue.canvasChanged(downcast<HTMLCanvasElement>(canvasBase), changedRect);
73 }
74 void canvasResized(CanvasBase& canvasBase) final
75 {
76 ASSERT(is<HTMLCanvasElement>(canvasBase));
77 m_ownerValue.canvasResized(downcast<HTMLCanvasElement>(canvasBase));
78 }
79 void canvasDestroyed(CanvasBase& canvasBase) final
80 {
81 ASSERT(is<HTMLCanvasElement>(canvasBase));
82 m_ownerValue.canvasDestroyed(downcast<HTMLCanvasElement>(canvasBase));
83 }
84
85 CSSCanvasValue& m_ownerValue;
86 };
87
88private:
89 explicit CSSCanvasValue(const String& name)
90 : CSSImageGeneratorValue(CanvasClass)
91 , m_canvasObserver(*this)
92 , m_name(name)
93 {
94 }
95
96 void canvasChanged(HTMLCanvasElement&, const FloatRect& changedRect);
97 void canvasResized(HTMLCanvasElement&);
98 void canvasDestroyed(HTMLCanvasElement&);
99
100 HTMLCanvasElement* element(Document&);
101
102 CanvasObserverProxy m_canvasObserver;
103
104 // The name of the canvas.
105 String m_name;
106 // The document supplies the element and owns it.
107 HTMLCanvasElement* m_element { nullptr };
108};
109
110} // namespace WebCore
111
112SPECIALIZE_TYPE_TRAITS_CSS_VALUE(CSSCanvasValue, isCanvasValue())
113
114SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::CSSCanvasValue::CanvasObserverProxy)
115 static bool isType(const WebCore::CanvasObserver& canvasObserver) { return canvasObserver.isCanvasObserverProxy(); }
116SPECIALIZE_TYPE_TRAITS_END()
117
118