1 | /* |
2 | * Copyright (C) 2016 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "JSDOMWindowProperties.h" |
28 | |
29 | #include "Frame.h" |
30 | #include "HTMLDocument.h" |
31 | #include "JSDOMBinding.h" |
32 | #include "JSDOMBindingSecurity.h" |
33 | #include "JSDOMConvertStrings.h" |
34 | #include "JSDOMWindowBase.h" |
35 | #include "JSElement.h" |
36 | #include "JSHTMLCollection.h" |
37 | |
38 | namespace WebCore { |
39 | |
40 | using namespace JSC; |
41 | |
42 | const ClassInfo JSDOMWindowProperties::s_info = { "WindowProperties" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSDOMWindowProperties) }; |
43 | |
44 | static bool jsDOMWindowPropertiesGetOwnPropertySlotNamedItemGetter(JSDOMWindowProperties* thisObject, DOMWindow& window, ExecState* exec, PropertyName propertyName, PropertySlot& slot) |
45 | { |
46 | // Check for child frames by name before built-in properties to match Mozilla. This does |
47 | // not match IE, but some sites end up naming frames things that conflict with window |
48 | // properties that are in Moz but not IE. Since we have some of these, we have to do it |
49 | // the Moz way. |
50 | if (auto* frame = window.frame()) { |
51 | if (auto* scopedChild = frame->tree().scopedChild(propertyNameToAtomicString(propertyName))) { |
52 | slot.setValue(thisObject, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum, toJS(exec, scopedChild->document()->domWindow())); |
53 | return true; |
54 | } |
55 | } |
56 | |
57 | if (!BindingSecurity::shouldAllowAccessToDOMWindow(exec, window, ThrowSecurityError)) |
58 | return false; |
59 | |
60 | // FIXME: Search the whole frame hierarchy somewhere around here. |
61 | // We need to test the correct priority order. |
62 | |
63 | // Allow shortcuts like 'Image1' instead of document.images.Image1 |
64 | auto* document = window.document(); |
65 | if (is<HTMLDocument>(document)) { |
66 | auto& htmlDocument = downcast<HTMLDocument>(*document); |
67 | auto* atomicPropertyName = propertyName.publicName(); |
68 | if (atomicPropertyName && htmlDocument.hasWindowNamedItem(*atomicPropertyName)) { |
69 | JSValue namedItem; |
70 | if (UNLIKELY(htmlDocument.windowNamedItemContainsMultipleElements(*atomicPropertyName))) { |
71 | Ref<HTMLCollection> collection = document->windowNamedItems(atomicPropertyName); |
72 | ASSERT(collection->length() > 1); |
73 | namedItem = toJS(exec, thisObject->globalObject(), collection); |
74 | } else |
75 | namedItem = toJS(exec, thisObject->globalObject(), htmlDocument.windowNamedItem(*atomicPropertyName)); |
76 | slot.setValue(thisObject, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::DontEnum, namedItem); |
77 | return true; |
78 | } |
79 | } |
80 | |
81 | return false; |
82 | } |
83 | |
84 | bool JSDOMWindowProperties::getOwnPropertySlot(JSObject* object, ExecState* state, PropertyName propertyName, PropertySlot& slot) |
85 | { |
86 | auto* thisObject = jsCast<JSDOMWindowProperties*>(object); |
87 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
88 | if (Base::getOwnPropertySlot(thisObject, state, propertyName, slot)) |
89 | return true; |
90 | JSValue proto = thisObject->getPrototypeDirect(state->vm()); |
91 | if (proto.isObject() && jsCast<JSObject*>(proto)->hasProperty(state, propertyName)) |
92 | return false; |
93 | |
94 | auto& vm = state->vm(); |
95 | |
96 | // FIXME: We should probably add support for JSRemoteDOMWindowBase too. |
97 | auto* jsWindow = jsDynamicCast<JSDOMWindowBase*>(vm, thisObject->globalObject()); |
98 | if (!jsWindow) |
99 | return false; |
100 | |
101 | auto& window = jsWindow->wrapped(); |
102 | return jsDOMWindowPropertiesGetOwnPropertySlotNamedItemGetter(thisObject, window, state, propertyName, slot); |
103 | } |
104 | |
105 | bool JSDOMWindowProperties::getOwnPropertySlotByIndex(JSObject* object, ExecState* state, unsigned index, PropertySlot& slot) |
106 | { |
107 | return getOwnPropertySlot(object, state, Identifier::from(state, index), slot); |
108 | } |
109 | |
110 | } // namespace WebCore |
111 | |