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 "DatabaseProvider.h" |
33 | #include "Document.h" |
34 | #include "IDBFactory.h" |
35 | #include "Page.h" |
36 | |
37 | namespace WebCore { |
38 | |
39 | DOMWindowIndexedDatabase::DOMWindowIndexedDatabase(DOMWindow* window) |
40 | : DOMWindowProperty(window) |
41 | { |
42 | } |
43 | |
44 | DOMWindowIndexedDatabase::~DOMWindowIndexedDatabase() = default; |
45 | |
46 | const char* DOMWindowIndexedDatabase::supplementName() |
47 | { |
48 | return "DOMWindowIndexedDatabase" ; |
49 | } |
50 | |
51 | DOMWindowIndexedDatabase* DOMWindowIndexedDatabase::from(DOMWindow* window) |
52 | { |
53 | DOMWindowIndexedDatabase* supplement = static_cast<DOMWindowIndexedDatabase*>(Supplement<DOMWindow>::from(window, supplementName())); |
54 | if (!supplement) { |
55 | auto newSupplement = std::make_unique<DOMWindowIndexedDatabase>(window); |
56 | supplement = newSupplement.get(); |
57 | provideTo(window, supplementName(), WTFMove(newSupplement)); |
58 | } |
59 | return supplement; |
60 | } |
61 | |
62 | IDBFactory* DOMWindowIndexedDatabase::indexedDB(DOMWindow& window) |
63 | { |
64 | return from(&window)->indexedDB(); |
65 | } |
66 | |
67 | IDBFactory* DOMWindowIndexedDatabase::indexedDB() |
68 | { |
69 | auto* window = this->window(); |
70 | if (!window) |
71 | return nullptr; |
72 | |
73 | auto* document = window->document(); |
74 | if (!document) |
75 | return nullptr; |
76 | |
77 | auto* page = document->page(); |
78 | if (!page) |
79 | return nullptr; |
80 | |
81 | if (!window->isCurrentlyDisplayedInFrame()) |
82 | return nullptr; |
83 | |
84 | if (!m_idbFactory) { |
85 | auto* connectionProxy = document->idbConnectionProxy(); |
86 | if (!connectionProxy) |
87 | return nullptr; |
88 | |
89 | m_idbFactory = IDBFactory::create(*connectionProxy); |
90 | } |
91 | |
92 | return m_idbFactory.get(); |
93 | } |
94 | |
95 | } // namespace WebCore |
96 | |
97 | #endif // ENABLE(INDEXED_DATABASE) |
98 | |