1/*
2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "DOMWindowNotifications.h"
29
30#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
31
32#include "DOMWindow.h"
33#include "Document.h"
34#include "NotificationCenter.h"
35#include "NotificationController.h"
36#include "Page.h"
37
38namespace WebCore {
39
40DOMWindowNotifications::DOMWindowNotifications(DOMWindow* window)
41    : DOMWindowProperty(window->frame())
42    , m_window(window)
43{
44}
45
46DOMWindowNotifications::~DOMWindowNotifications()
47{
48}
49
50const char* DOMWindowNotifications::supplementName()
51{
52    return "DOMWindowNotifications";
53}
54
55DOMWindowNotifications* DOMWindowNotifications::from(DOMWindow* window)
56{
57    DOMWindowNotifications* supplement = static_cast<DOMWindowNotifications*>(Supplement<DOMWindow>::from(window, supplementName()));
58    if (!supplement) {
59        auto newSupplement = std::make_unique<DOMWindowNotifications>(window);
60        supplement = newSupplement.get();
61        provideTo(window, supplementName(), WTF::move(newSupplement));
62    }
63    return supplement;
64}
65
66NotificationCenter* DOMWindowNotifications::webkitNotifications(DOMWindow* window)
67{
68    return DOMWindowNotifications::from(window)->webkitNotifications();
69}
70
71void DOMWindowNotifications::disconnectFrameForPageCache()
72{
73    m_suspendedNotificationCenter = m_notificationCenter.release();
74    DOMWindowProperty::disconnectFrameForPageCache();
75}
76
77void DOMWindowNotifications::reconnectFrameFromPageCache(Frame* frame)
78{
79    DOMWindowProperty::reconnectFrameFromPageCache(frame);
80    m_notificationCenter = m_suspendedNotificationCenter.release();
81}
82
83void DOMWindowNotifications::willDestroyGlobalObjectInCachedFrame()
84{
85    m_suspendedNotificationCenter = nullptr;
86    DOMWindowProperty::willDestroyGlobalObjectInCachedFrame();
87}
88
89void DOMWindowNotifications::willDestroyGlobalObjectInFrame()
90{
91    m_notificationCenter = nullptr;
92    DOMWindowProperty::willDestroyGlobalObjectInFrame();
93}
94
95void DOMWindowNotifications::willDetachGlobalObjectFromFrame()
96{
97    m_notificationCenter = nullptr;
98    DOMWindowProperty::willDetachGlobalObjectFromFrame();
99}
100
101NotificationCenter* DOMWindowNotifications::webkitNotifications()
102{
103    if (!m_window->isCurrentlyDisplayedInFrame())
104        return 0;
105
106    if (m_notificationCenter)
107        return m_notificationCenter.get();
108
109    Document* document = m_window->document();
110    if (!document)
111        return 0;
112
113    Page* page = document->page();
114    if (!page)
115        return 0;
116
117    NotificationClient* provider = NotificationController::clientFrom(page);
118    if (provider)
119        m_notificationCenter = NotificationCenter::create(document, provider);
120
121    return m_notificationCenter.get();
122}
123
124} // namespace WebCore
125
126#endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
127