1/*
2 * Copyright (C) 2017 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#include "config.h"
27#include "CanvasBase.h"
28
29#include "CSSCanvasValue.h"
30#include "CanvasRenderingContext.h"
31#include "Element.h"
32#include "FloatRect.h"
33#include "InspectorInstrumentation.h"
34#include <wtf/Vector.h>
35
36namespace WebCore {
37
38CanvasBase::CanvasBase()
39{
40}
41
42CanvasBase::~CanvasBase()
43{
44 ASSERT(!m_context); // Should have been set to null by base class.
45 ASSERT(m_didNotifyObserversCanvasDestroyed);
46 ASSERT(m_observers.isEmpty());
47}
48
49CanvasRenderingContext* CanvasBase::renderingContext() const
50{
51 return m_context.get();
52}
53
54void CanvasBase::addObserver(CanvasObserver& observer)
55{
56 m_observers.add(&observer);
57
58 if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
59 InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
60}
61
62void CanvasBase::removeObserver(CanvasObserver& observer)
63{
64 m_observers.remove(&observer);
65
66 if (is<CSSCanvasValue::CanvasObserverProxy>(observer))
67 InspectorInstrumentation::didChangeCSSCanvasClientNodes(*this);
68}
69
70void CanvasBase::notifyObserversCanvasChanged(const FloatRect& rect)
71{
72 for (auto& observer : copyToVector(m_observers))
73 observer->canvasChanged(*this, rect);
74}
75
76void CanvasBase::notifyObserversCanvasResized()
77{
78 for (auto& observer : copyToVector(m_observers))
79 observer->canvasResized(*this);
80}
81
82void CanvasBase::notifyObserversCanvasDestroyed()
83{
84 ASSERT(!m_didNotifyObserversCanvasDestroyed);
85
86 for (auto& observer : copyToVector(m_observers))
87 observer->canvasDestroyed(*this);
88
89 m_observers.clear();
90
91#ifndef NDEBUG
92 m_didNotifyObserversCanvasDestroyed = true;
93#endif
94}
95
96HashSet<Element*> CanvasBase::cssCanvasClients() const
97{
98 HashSet<Element*> cssCanvasClients;
99 for (auto& observer : m_observers) {
100 if (!is<CSSCanvasValue::CanvasObserverProxy>(observer))
101 continue;
102
103 auto clients = downcast<CSSCanvasValue::CanvasObserverProxy>(observer)->ownerValue().clients();
104 for (auto& entry : clients) {
105 if (RefPtr<Element> element = entry.key->element())
106 cssCanvasClients.add(element.get());
107 }
108 }
109 return cssCanvasClients;
110}
111
112bool CanvasBase::callTracingActive() const
113{
114 return m_context && m_context->callTracingActive();
115}
116
117}
118