2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "JSHistoryCustom.h"
34 #include "SerializedScriptValue.h"
35 #include <runtime/JSFunction.h>
41 static JSValue nonCachingStaticBackFunctionGetter(ExecState* exec, JSValue, const Identifier& propertyName)
43 return JSFunction::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->functionStructure(), 0, propertyName, jsHistoryPrototypeFunctionBack);
46 static JSValue nonCachingStaticForwardFunctionGetter(ExecState* exec, JSValue, const Identifier& propertyName)
48 return JSFunction::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->functionStructure(), 0, propertyName, jsHistoryPrototypeFunctionForward);
51 static JSValue nonCachingStaticGoFunctionGetter(ExecState* exec, JSValue, const Identifier& propertyName)
53 return JSFunction::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->functionStructure(), 1, propertyName, jsHistoryPrototypeFunctionGo);
56 bool JSHistory::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
58 // When accessing History cross-domain, functions are always the native built-in ones.
59 // See JSDOMWindow::getOwnPropertySlotDelegate for additional details.
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.
64 if (allowsAccessFromFrame(exec, impl()->frame(), message))
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);
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);
75 } else if (entry->function() == jsHistoryPrototypeFunctionForward) {
76 slot.setCustom(this, nonCachingStaticForwardFunctionGetter);
78 } else if (entry->function() == jsHistoryPrototypeFunctionGo) {
79 slot.setCustom(this, nonCachingStaticGoFunctionGetter);
84 // Allow access to toString() cross-domain, but always Object.toString.
85 if (propertyName == exec->propertyNames().toString) {
86 slot.setCustom(this, objectToStringFunctionGetter);
91 printErrorMessageForFrame(impl()->frame(), message);
96 bool JSHistory::getOwnPropertyDescriptorDelegate(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
98 if (!impl()->frame()) {
99 descriptor.setUndefined();
103 // Throw out all cross domain access
104 if (!allowsAccessFromFrame(exec, impl()->frame()))
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);
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());
117 } else if (entry->function() == jsHistoryPrototypeFunctionForward) {
118 slot.setCustom(this, nonCachingStaticForwardFunctionGetter);
119 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
121 } else if (entry->function() == jsHistoryPrototypeFunctionGo) {
122 slot.setCustom(this, nonCachingStaticGoFunctionGetter);
123 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
128 // Allow access to toString() cross-domain, but always Object.toString.
129 if (propertyName == exec->propertyNames().toString) {
131 slot.setCustom(this, objectToStringFunctionGetter);
132 descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());
137 descriptor.setUndefined();
141 bool JSHistory::putDelegate(ExecState* exec, const Identifier&, JSValue, PutPropertySlot&)
143 // Only allow putting by frames in the same origin.
144 if (!allowsAccessFromFrame(exec, impl()->frame()))
149 bool JSHistory::deleteProperty(ExecState* exec, const Identifier& propertyName)
151 // Only allow deleting by frames in the same origin.
152 if (!allowsAccessFromFrame(exec, impl()->frame()))
154 return Base::deleteProperty(exec, propertyName);
157 void JSHistory::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
159 // Only allow the history object to enumerated by frames in the same origin.
160 if (!allowsAccessFromFrame(exec, impl()->frame()))
162 Base::getOwnPropertyNames(exec, propertyNames, mode);
165 JSValue JSHistory::pushState(ExecState* exec)
167 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, exec->argument(0));
168 if (exec->hadException())
169 return jsUndefined();
171 String title = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(1));
172 if (exec->hadException())
173 return jsUndefined();
176 if (exec->argumentCount() > 2) {
177 url = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(2));
178 if (exec->hadException())
179 return jsUndefined();
182 ExceptionCode ec = 0;
183 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectPush, ec);
184 setDOMException(exec, ec);
186 return jsUndefined();
189 JSValue JSHistory::replaceState(ExecState* exec)
191 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, exec->argument(0));
192 if (exec->hadException())
193 return jsUndefined();
195 String title = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(1));
196 if (exec->hadException())
197 return jsUndefined();
200 if (exec->argumentCount() > 2) {
201 url = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(2));
202 if (exec->hadException())
203 return jsUndefined();
206 ExceptionCode ec = 0;
207 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectReplace, ec);
208 setDOMException(exec, ec);
210 return jsUndefined();
213 } // namespace WebCore