1 | /* |
2 | * Copyright (C) 2007-2009, 2011, 2016, 2017 Apple Inc. All rights reserved. |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "JSDocument.h" |
22 | |
23 | #include "Frame.h" |
24 | #include "JSDOMWindowCustom.h" |
25 | #include "JSHTMLDocument.h" |
26 | #include "JSXMLDocument.h" |
27 | #include "NodeTraversal.h" |
28 | #include "SVGDocument.h" |
29 | #include <JavaScriptCore/HeapSnapshotBuilder.h> |
30 | |
31 | |
32 | namespace WebCore { |
33 | using namespace JSC; |
34 | |
35 | static inline JSValue createNewDocumentWrapper(ExecState& state, JSDOMGlobalObject& globalObject, Ref<Document>&& passedDocument) |
36 | { |
37 | auto& document = passedDocument.get(); |
38 | JSObject* wrapper; |
39 | if (document.isHTMLDocument()) |
40 | wrapper = createWrapper<HTMLDocument>(&globalObject, WTFMove(passedDocument)); |
41 | else if (document.isXMLDocument()) |
42 | wrapper = createWrapper<XMLDocument>(&globalObject, WTFMove(passedDocument)); |
43 | else |
44 | wrapper = createWrapper<Document>(&globalObject, WTFMove(passedDocument)); |
45 | |
46 | reportMemoryForDocumentIfFrameless(state, document); |
47 | |
48 | return wrapper; |
49 | } |
50 | |
51 | JSObject* cachedDocumentWrapper(ExecState& state, JSDOMGlobalObject& globalObject, Document& document) |
52 | { |
53 | if (auto* wrapper = getCachedWrapper(globalObject.world(), document)) |
54 | return wrapper; |
55 | |
56 | auto* window = document.domWindow(); |
57 | if (!window) |
58 | return nullptr; |
59 | |
60 | auto* documentGlobalObject = toJSDOMWindow(state.vm(), toJS(&state, *window)); |
61 | if (!documentGlobalObject) |
62 | return nullptr; |
63 | |
64 | // Creating a wrapper for domWindow might have created a wrapper for document as well. |
65 | return getCachedWrapper(documentGlobalObject->world(), document); |
66 | } |
67 | |
68 | void reportMemoryForDocumentIfFrameless(ExecState& state, Document& document) |
69 | { |
70 | // Make sure the document is kept around by the window object, and works right with the back/forward cache. |
71 | if (document.frame()) |
72 | return; |
73 | |
74 | VM& vm = state.vm(); |
75 | size_t memoryCost = 0; |
76 | for (Node* node = &document; node; node = NodeTraversal::next(*node)) |
77 | memoryCost += node->approximateMemoryCost(); |
78 | |
79 | // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated. |
80 | // https://bugs.webkit.org/show_bug.cgi?id=142595 |
81 | vm.heap.deprecatedReportExtraMemory(memoryCost); |
82 | } |
83 | |
84 | JSValue toJSNewlyCreated(ExecState* state, JSDOMGlobalObject* globalObject, Ref<Document>&& document) |
85 | { |
86 | return createNewDocumentWrapper(*state, *globalObject, WTFMove(document)); |
87 | } |
88 | |
89 | JSValue toJS(ExecState* state, JSDOMGlobalObject* globalObject, Document& document) |
90 | { |
91 | if (auto* wrapper = cachedDocumentWrapper(*state, *globalObject, document)) |
92 | return wrapper; |
93 | return toJSNewlyCreated(state, globalObject, Ref<Document>(document)); |
94 | } |
95 | |
96 | void JSDocument::visitAdditionalChildren(SlotVisitor& visitor) |
97 | { |
98 | visitor.addOpaqueRoot(static_cast<ScriptExecutionContext*>(&wrapped())); |
99 | } |
100 | |
101 | void JSDocument::heapSnapshot(JSCell* cell, HeapSnapshotBuilder& builder) |
102 | { |
103 | Base::heapSnapshot(cell, builder); |
104 | auto* thisObject = jsCast<JSDocument*>(cell); |
105 | builder.setLabelForCell(cell, thisObject->wrapped().url().string()); |
106 | } |
107 | |
108 | } // namespace WebCore |
109 | |