1/*
2 * Copyright (C) 2011, Google 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 are met:
6 *
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 GOOGLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
24 */
25
26#include "config.h"
27#include "DOMWindowIndexedDatabase.h"
28
29#if ENABLE(INDEXED_DATABASE)
30
31#include "DOMWindow.h"
32#include "Document.h"
33#include "IDBFactory.h"
34#include "Page.h"
35#include "PageGroupIndexedDatabase.h"
36#include "SecurityOrigin.h"
37
38namespace WebCore {
39
40DOMWindowIndexedDatabase::DOMWindowIndexedDatabase(DOMWindow* window)
41    : DOMWindowProperty(window->frame())
42    , m_window(window)
43{
44}
45
46DOMWindowIndexedDatabase::~DOMWindowIndexedDatabase()
47{
48}
49
50const char* DOMWindowIndexedDatabase::supplementName()
51{
52    return "DOMWindowIndexedDatabase";
53}
54
55DOMWindowIndexedDatabase* DOMWindowIndexedDatabase::from(DOMWindow* window)
56{
57    DOMWindowIndexedDatabase* supplement = static_cast<DOMWindowIndexedDatabase*>(Supplement<DOMWindow>::from(window, supplementName()));
58    if (!supplement) {
59        supplement = new DOMWindowIndexedDatabase(window);
60        provideTo(window, supplementName(), adoptPtr(supplement));
61    }
62    return supplement;
63}
64
65void DOMWindowIndexedDatabase::disconnectFrameForPageCache()
66{
67    m_suspendedIDBFactory = m_idbFactory.release();
68    DOMWindowProperty::disconnectFrameForPageCache();
69}
70
71void DOMWindowIndexedDatabase::reconnectFrameFromPageCache(Frame* frame)
72{
73    DOMWindowProperty::reconnectFrameFromPageCache(frame);
74    m_idbFactory = m_suspendedIDBFactory.release();
75}
76
77void DOMWindowIndexedDatabase::willDestroyGlobalObjectInCachedFrame()
78{
79    m_suspendedIDBFactory = nullptr;
80    DOMWindowProperty::willDestroyGlobalObjectInCachedFrame();
81}
82
83void DOMWindowIndexedDatabase::willDestroyGlobalObjectInFrame()
84{
85    m_idbFactory = nullptr;
86    DOMWindowProperty::willDestroyGlobalObjectInFrame();
87}
88
89void DOMWindowIndexedDatabase::willDetachGlobalObjectFromFrame()
90{
91    m_idbFactory = nullptr;
92    DOMWindowProperty::willDetachGlobalObjectFromFrame();
93}
94
95IDBFactory* DOMWindowIndexedDatabase::indexedDB(DOMWindow* window)
96{
97    return from(window)->indexedDB();
98}
99
100IDBFactory* DOMWindowIndexedDatabase::indexedDB()
101{
102    Document* document = m_window->document();
103    if (!document)
104        return 0;
105
106    Page* page = document->page();
107    if (!page)
108        return 0;
109
110    if (!m_window->isCurrentlyDisplayedInFrame())
111        return 0;
112
113    if (!m_idbFactory)
114        m_idbFactory = IDBFactory::create(PageGroupIndexedDatabase::from(page->group())->factoryBackend());
115    return m_idbFactory.get();
116}
117
118} // namespace WebCore
119
120#endif // ENABLE(INDEXED_DATABASE)
121