JSLocationCustom.cpp [plain text]
#include "config.h"
#include "JSLocationCustom.h"
#include "DOMWindow.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "JSDOMBinding.h"
#include "JSDOMWindowCustom.h"
#include "KURL.h"
#include "Location.h"
#include "ScriptController.h"
#include <runtime/PrototypeFunction.h>
using namespace JSC;
namespace WebCore {
static JSValuePtr nonCachingStaticReplaceFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
{
return new (exec) PrototypeFunction(exec, 1, propertyName, jsLocationPrototypeFunctionReplace);
}
static JSValuePtr nonCachingStaticReloadFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
{
return new (exec) PrototypeFunction(exec, 0, propertyName, jsLocationPrototypeFunctionReload);
}
static JSValuePtr nonCachingStaticAssignFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
{
return new (exec) PrototypeFunction(exec, 1, propertyName, jsLocationPrototypeFunctionAssign);
}
bool JSLocation::customGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
Frame* frame = impl()->frame();
if (!frame) {
slot.setUndefined();
return true;
}
String message;
if (allowsAccessFromFrame(exec, frame, message))
return false;
const HashEntry* entry = JSLocationPrototype::s_info.propHashTable(exec)->entry(exec, propertyName);
if (entry && (entry->attributes() & Function)) {
if (entry->function() == jsLocationPrototypeFunctionReplace) {
slot.setCustom(this, nonCachingStaticReplaceFunctionGetter);
return true;
} else if (entry->function() == jsLocationPrototypeFunctionReload) {
slot.setCustom(this, nonCachingStaticReloadFunctionGetter);
return true;
} else if (entry->function() == jsLocationPrototypeFunctionAssign) {
slot.setCustom(this, nonCachingStaticAssignFunctionGetter);
return true;
}
}
printErrorMessageForFrame(frame, message);
slot.setUndefined();
return true;
}
bool JSLocation::customPut(ExecState* exec, const Identifier& propertyName, JSValuePtr value, PutPropertySlot& slot)
{
Frame* frame = impl()->frame();
if (!frame)
return true;
bool sameDomainAccess = allowsAccessFromFrame(exec, frame);
const HashEntry* entry = JSLocation::s_info.propHashTable(exec)->entry(exec, propertyName);
if (!entry) {
if (sameDomainAccess)
JSObject::put(exec, propertyName, value, slot);
return true;
}
if (entry->propertyPutter() != setJSLocationHref && !sameDomainAccess)
return true;
return false;
}
bool JSLocation::deleteProperty(ExecState* exec, const Identifier& propertyName)
{
if (!allowsAccessFromFrame(exec, impl()->frame()))
return false;
return Base::deleteProperty(exec, propertyName);
}
bool JSLocation::customGetPropertyNames(ExecState* exec, PropertyNameArray&)
{
if (!allowsAccessFromFrame(exec, impl()->frame()))
return true;
return false;
}
static void navigateIfAllowed(ExecState* exec, Frame* frame, const KURL& url, bool lockHistory, bool lockBackForwardList)
{
Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
if (!url.protocolIs("javascript") || allowsAccessFromFrame(exec, frame)) {
bool userGesture = activeFrame->script()->processingUserGesture();
frame->loader()->scheduleLocationChange(url.string(), activeFrame->loader()->outgoingReferrer(), lockHistory, lockBackForwardList, userGesture);
}
}
void JSLocation::setHref(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
if (!activeFrame)
return;
if (!activeFrame->loader()->shouldAllowNavigation(frame))
return;
KURL url = activeFrame->loader()->completeURL(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setProtocol(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
url.setProtocol(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setHost(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
url.setHostAndPort(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setHostname(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
url.setHost(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setPort(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
const UString& portString = value.toString(exec);
int port = charactersToInt(portString.data(), portString.size());
if (port < 0 || port > 0xFFFF)
port = 0;
url.setPort(port);
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setPathname(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
url.setPath(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setSearch(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
url.setQuery(value.toString(exec));
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
void JSLocation::setHash(ExecState* exec, JSValuePtr value)
{
Frame* frame = impl()->frame();
ASSERT(frame);
KURL url = frame->loader()->url();
String oldRef = url.ref();
String str = value.toString(exec);
if (str.startsWith("#"))
str = str.substring(1);
if (oldRef == str || (oldRef.isNull() && str.isEmpty()))
return;
url.setRef(str);
navigateIfAllowed(exec, frame, url, !frame->script()->anyPageIsProcessingUserGesture(), false);
}
JSValuePtr JSLocation::replace(ExecState* exec, const ArgList& args)
{
Frame* frame = impl()->frame();
if (!frame)
return jsUndefined();
Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
if (!activeFrame)
return jsUndefined();
if (!activeFrame->loader()->shouldAllowNavigation(frame))
return jsUndefined();
navigateIfAllowed(exec, frame, activeFrame->loader()->completeURL(args.at(exec, 0).toString(exec)), true, true);
return jsUndefined();
}
JSValuePtr JSLocation::reload(ExecState* exec, const ArgList&)
{
Frame* frame = impl()->frame();
if (!frame)
return jsUndefined();
JSDOMWindow* window = toJSDOMWindow(frame);
if (!window->allowsAccessFrom(exec))
return jsUndefined();
if (!frame->loader()->url().protocolIs("javascript") || (window && window->allowsAccessFrom(exec))) {
bool userGesture = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame()->script()->processingUserGesture();
frame->loader()->scheduleRefresh(userGesture);
}
return jsUndefined();
}
JSValuePtr JSLocation::assign(ExecState* exec, const ArgList& args)
{
Frame* frame = impl()->frame();
if (!frame)
return jsUndefined();
Frame* activeFrame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
if (!activeFrame)
return jsUndefined();
if (!activeFrame->loader()->shouldAllowNavigation(frame))
return jsUndefined();
navigateIfAllowed(exec, frame, activeFrame->loader()->completeURL(args.at(exec, 0).toString(exec)), !frame->script()->anyPageIsProcessingUserGesture(), false);
return jsUndefined();
}
JSValuePtr JSLocation::toString(ExecState* exec, const ArgList&)
{
Frame* frame = impl()->frame();
if (!frame)
return jsUndefined();
if (!allowsAccessFromFrame(exec, frame))
return jsUndefined();
return jsString(exec, impl()->toString());
}
}