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, PropertyName propertyName)
43 return JSFunction::create(exec, exec->lexicalGlobalObject(), 0, propertyName.publicName(), jsHistoryPrototypeFunctionBack);
46 static JSValue nonCachingStaticForwardFunctionGetter(ExecState* exec, JSValue, PropertyName propertyName)
48 return JSFunction::create(exec, exec->lexicalGlobalObject(), 0, propertyName.publicName(), jsHistoryPrototypeFunctionForward);
51 static JSValue nonCachingStaticGoFunctionGetter(ExecState* exec, JSValue, PropertyName propertyName)
53 return JSFunction::create(exec, exec->lexicalGlobalObject(), 1, propertyName.publicName(), jsHistoryPrototypeFunctionGo);
56 bool JSHistory::getOwnPropertySlotDelegate(ExecState* exec, PropertyName 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 (shouldAllowAccessToFrame(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() & JSC::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, PropertyName propertyName, PropertyDescriptor& descriptor)
98 if (!impl()->frame()) {
99 descriptor.setUndefined();
103 // Throw out all cross domain access
104 if (!shouldAllowAccessToFrame(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() & JSC::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, PropertyName, JSValue, PutPropertySlot&)
143 // Only allow putting by frames in the same origin.
144 if (!shouldAllowAccessToFrame(exec, impl()->frame()))
149 bool JSHistory::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
151 JSHistory* thisObject = jsCast<JSHistory*>(cell);
152 // Only allow deleting by frames in the same origin.
153 if (!shouldAllowAccessToFrame(exec, thisObject->impl()->frame()))
155 return Base::deleteProperty(thisObject, exec, propertyName);
158 void JSHistory::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
160 JSHistory* thisObject = jsCast<JSHistory*>(object);
161 // Only allow the history object to enumerated by frames in the same origin.
162 if (!shouldAllowAccessToFrame(exec, thisObject->impl()->frame()))
164 Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
167 JSValue JSHistory::state(ExecState *exec) const
169 History* history = static_cast<History*>(impl());
171 JSValue cachedValue = m_state.get();
172 if (!cachedValue.isEmpty() && !history->stateChanged())
175 SerializedScriptValue* serialized = history->state();
176 JSValue result = serialized ? serialized->deserialize(exec, globalObject(), 0) : jsNull();
177 const_cast<JSHistory*>(this)->m_state.set(exec->globalData(), this, result);
181 JSValue JSHistory::pushState(ExecState* exec)
183 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, exec->argument(0), 0, 0);
184 if (exec->hadException())
185 return jsUndefined();
187 String title = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(1));
188 if (exec->hadException())
189 return jsUndefined();
192 if (exec->argumentCount() > 2) {
193 url = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(2));
194 if (exec->hadException())
195 return jsUndefined();
198 ExceptionCode ec = 0;
199 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectPush, ec);
200 setDOMException(exec, ec);
204 return jsUndefined();
207 JSValue JSHistory::replaceState(ExecState* exec)
209 RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, exec->argument(0), 0, 0);
210 if (exec->hadException())
211 return jsUndefined();
213 String title = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(1));
214 if (exec->hadException())
215 return jsUndefined();
218 if (exec->argumentCount() > 2) {
219 url = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(2));
220 if (exec->hadException())
221 return jsUndefined();
224 ExceptionCode ec = 0;
225 impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectReplace, ec);
226 setDOMException(exec, ec);
230 return jsUndefined();
233 } // namespace WebCore