1 /*
2 * Copyright (C) 2008 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 *
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 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "JSHistoryCustom.h"
31
32 #include "Frame.h"
33 #include "History.h"
34 #include <runtime/JSFunction.h>
35 #include <runtime/PrototypeFunction.h>
36
37 using namespace JSC;
38
39 namespace WebCore {
40
nonCachingStaticBackFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)41 static JSValue nonCachingStaticBackFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
42 {
43 return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 0, propertyName, jsHistoryPrototypeFunctionBack);
44 }
45
nonCachingStaticForwardFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)46 static JSValue nonCachingStaticForwardFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
47 {
48 return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 0, propertyName, jsHistoryPrototypeFunctionForward);
49 }
50
nonCachingStaticGoFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)51 static JSValue nonCachingStaticGoFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
52 {
53 return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 1, propertyName, jsHistoryPrototypeFunctionGo);
54 }
55
getOwnPropertySlotDelegate(ExecState * exec,const Identifier & propertyName,PropertySlot & slot)56 bool JSHistory::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
57 {
58 // When accessing History cross-domain, functions are always the native built-in ones.
59 // See JSDOMWindow::getOwnPropertySlotDelegate for additional details.
60
61 // Our custom code is only needed to implement the Window cross-domain scheme, so if access is
62 // allowed, return false so the normal lookup will take place.
63 String message;
64 if (allowsAccessFromFrame(exec, impl()->frame(), message))
65 return false;
66
67 // Check for the few functions that we allow, even when called cross-domain.
68 const HashEntry* entry = JSHistoryPrototype::s_info.propHashTable(exec)->entry(exec, propertyName);
69 if (entry) {
70 // Allow access to back(), forward() and go() from any frame.
71 if (entry->attributes() & Function) {
72 if (entry->function() == jsHistoryPrototypeFunctionBack) {
73 slot.setCustom(this, nonCachingStaticBackFunctionGetter);
74 return true;
75 } else if (entry->function() == jsHistoryPrototypeFunctionForward) {
76 slot.setCustom(this, nonCachingStaticForwardFunctionGetter);
77 return true;
78 } else if (entry->function() == jsHistoryPrototypeFunctionGo) {
79 slot.setCustom(this, nonCachingStaticGoFunctionGetter);
80 return true;
81 }
82 }
83 } else {
84 // Allow access to toString() cross-domain, but always Object.toString.
85 if (propertyName == exec->propertyNames().toString) {
86 slot.setCustom(this, objectToStringFunctionGetter);
87 return true;
88 }
89 }
90
91 printErrorMessageForFrame(impl()->frame(), message);
92 slot.setUndefined();
93 return true;
94 }
95
getOwnPropertyDescriptorDelegate(ExecState * exec,const Identifier & propertyName,PropertyDescriptor & descriptor)96 bool JSHistory::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
97 {
98 if (!impl()->frame()) {
99 descriptor.setUndefined();
100 return true;
101 }
102
103 // Throw out all cross domain access
104 if (!allowsAccessFromFrame(exec, impl()->frame()))
105 return true;
106
107 // Check for the few functions that we allow, even when called cross-domain.
108 const HashEntry* entry = JSHistoryPrototype::s_info.propHashTable(exec)->entry(exec, propertyName);
109 if (entry) {
110 PropertySlot slot;
111 // Allow access to back(), forward() and go() from any frame.
112 if (entry->attributes() & Function) {
113 if (entry->function() == jsHistoryPrototypeFunctionBack) {
114 slot.setCustom(this, nonCachingStaticBackFunctionGetter);
115 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
116 return true;
117 } else if (entry->function() == jsHistoryPrototypeFunctionForward) {
118 slot.setCustom(this, nonCachingStaticForwardFunctionGetter);
119 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
120 return true;
121 } else if (entry->function() == jsHistoryPrototypeFunctionGo) {
122 slot.setCustom(this, nonCachingStaticGoFunctionGetter);
123 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
124 return true;
125 }
126 }
127 } else {
128 // Allow access to toString() cross-domain, but always Object.toString.
129 if (propertyName == exec->propertyNames().toString) {
130 PropertySlot slot;
131 slot.setCustom(this, objectToStringFunctionGetter);
132 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
133 return true;
134 }
135 }
136
137 descriptor.setUndefined();
138 return true;
139 }
140
putDelegate(ExecState * exec,const Identifier &,JSValue,PutPropertySlot &)141 bool JSHistory::putDelegate(ExecState* exec, const Identifier&, JSValue, PutPropertySlot&)
142 {
143 // Only allow putting by frames in the same origin.
144 if (!allowsAccessFromFrame(exec, impl()->frame()))
145 return true;
146 return false;
147 }
148
deleteProperty(ExecState * exec,const Identifier & propertyName)149 bool JSHistory::deleteProperty(ExecState* exec, const Identifier& propertyName)
150 {
151 // Only allow deleting by frames in the same origin.
152 if (!allowsAccessFromFrame(exec, impl()->frame()))
153 return false;
154 return Base::deleteProperty(exec, propertyName);
155 }
156
getOwnPropertyNames(ExecState * exec,PropertyNameArray & propertyNames,EnumerationMode mode)157 void JSHistory::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
158 {
159 // Only allow the history object to enumerated by frames in the same origin.
160 if (!allowsAccessFromFrame(exec, impl()->frame()))
161 return;
162 Base::getOwnPropertyNames(exec, propertyNames, mode);
163 }
164
pushState(ExecState * exec,const ArgList & args)165 JSValue JSHistory::pushState(ExecState* exec, const ArgList& args)
166 {
167 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, args.at(0));
168 if (exec->hadException())
169 return jsUndefined();
170
171 String title = valueToStringWithUndefinedOrNullCheck(exec, args.at(1));
172 if (exec->hadException())
173 return jsUndefined();
174
175 String url;
176 if (args.size() > 2) {
177 url = valueToStringWithUndefinedOrNullCheck(exec, args.at(2));
178 if (exec->hadException())
179 return jsUndefined();
180 }
181
182 ExceptionCode ec = 0;
183 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectPush, ec);
184 setDOMException(exec, ec);
185
186 return jsUndefined();
187 }
188
replaceState(ExecState * exec,const ArgList & args)189 JSValue JSHistory::replaceState(ExecState* exec, const ArgList& args)
190 {
191 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, args.at(0));
192 if (exec->hadException())
193 return jsUndefined();
194
195 String title = valueToStringWithUndefinedOrNullCheck(exec, args.at(1));
196 if (exec->hadException())
197 return jsUndefined();
198
199 String url;
200 if (args.size() > 2) {
201 url = valueToStringWithUndefinedOrNullCheck(exec, args.at(2));
202 if (exec->hadException())
203 return jsUndefined();
204 }
205
206 ExceptionCode ec = 0;
207 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectReplace, ec);
208 setDOMException(exec, ec);
209
210 return jsUndefined();
211 }
212
213 } // namespace WebCore
214