#include "config.h"
#include "JSLocation.h"
#include "DOMWindow.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "PlatformString.h"
#include "kjs_proxy.h"
#include "kjs_window.h"
#include "JSLocation.lut.h"
using namespace KJS;
namespace WebCore {
const ClassInfo JSLocation::info = { "Location", 0, &JSLocationTable };
JSLocation::JSLocation(JSObject* , Frame* frame)
: DOMObject(jsNull()) , m_frame(frame)
{
}
JSValue* JSLocation::getValueProperty(ExecState* exec, int token) const
{
const KURL& url = m_frame->loader()->url();
switch (token) {
case Hash:
return jsString(url.ref().isNull() ? "" : "#" + url.ref());
case Host: {
UString str = url.host();
if (url.port())
str += ":" + String::number((int)url.port());
return jsString(str);
}
case Hostname:
return jsString(url.host());
case Href:
if (!url.hasPath())
return jsString(url.prettyURL() + "/");
return jsString(url.prettyURL());
case Pathname:
return jsString(url.path().isEmpty() ? "/" : url.path());
case Port:
return jsString(url.port() ? String::number((int)url.port()) : "");
case Protocol:
return jsString(url.protocol() + ":");
case Search:
return jsString(url.query());
default:
ASSERT_NOT_REACHED();
return jsUndefined();
}
}
bool JSLocation::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
if (customGetOwnPropertySlot(exec, propertyName, slot))
return true;
return getStaticPropertySlot<JSLocation, JSObject>(exec, &JSLocationTable, this, propertyName, slot);
}
bool JSLocation::customGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
String message;
if (allowsAccessFromFrame(exec, m_frame, message))
return false;
const HashEntry* entry = Lookup::findEntry(&JSLocationTable, propertyName);
if (entry && (entry->attr & Function)
&& (entry->value.functionValue == jsLocationProtoFuncReplace
|| entry->value.functionValue == jsLocationProtoFuncReload
|| entry->value.functionValue == jsLocationProtoFuncAssign)) {
slot.setStaticEntry(this, entry, nonCachingStaticFunctionGetter);
return true;
}
printErrorMessageForFrame(m_frame, message);
slot.setUndefined(this);
return true;
}
void JSLocation::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)
{
if (!m_frame)
return;
DeprecatedString str = value->toString(exec);
KURL url = m_frame->loader()->url();
bool sameDomainAccess = allowsAccessFromFrame(exec, m_frame);
const HashEntry* entry = Lookup::findEntry(&JSLocationTable, propertyName);
if (entry) {
if (entry->value.intValue != Href && !sameDomainAccess)
return;
switch (entry->value.intValue) {
case Href: {
Frame* frame = Window::retrieveActive(exec)->impl()->frame();
if (!frame)
return;
if (!frame->loader()->shouldAllowNavigation(m_frame))
return;
url = frame->loader()->completeURL(str);
break;
}
case Hash: {
if (str.startsWith("#"))
str = str.mid(1);
if (url.ref() == str)
return;
url.setRef(str);
break;
}
case Host: {
url.setHostAndPort(str);
break;
}
case Hostname:
url.setHost(str);
break;
case Pathname:
url.setPath(str);
break;
case Port:
url.setPort(str.toUInt());
break;
case Protocol:
url.setProtocol(str);
break;
case Search:
url.setQuery(str);
break;
default:
return;
}
} else {
if (sameDomainAccess)
JSObject::put(exec, propertyName, value, attr);
return;
}
Frame* activeFrame = Window::retrieveActive(exec)->impl()->frame();
if (!url.deprecatedString().startsWith("javascript:", false) || sameDomainAccess) {
bool userGesture = activeFrame->scriptProxy()->processingUserGesture();
m_frame->loader()->scheduleLocationChange(url.string(), activeFrame->loader()->outgoingReferrer(), false, userGesture);
}
}
bool JSLocation::deleteProperty(ExecState* exec, const Identifier& propertyName)
{
if (!allowsAccessFromFrame(exec, m_frame))
return false;
return Base::deleteProperty(exec, propertyName);
}
void JSLocation::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
{
if (!allowsAccessFromFrame(exec, m_frame))
return;
Base::getPropertyNames(exec, propertyNames);
}
JSValue* jsLocationProtoFuncReplace(ExecState* exec, JSObject* thisObj, const List& args)
{
if (!thisObj->inherits(&JSLocation::info))
return throwError(exec, TypeError);
JSLocation* location = static_cast<JSLocation*>(thisObj);
Frame* frame = location->frame();
if (!frame)
return jsUndefined();
Frame* activeFrame = Window::retrieveActive(exec)->impl()->frame();
if (activeFrame) {
if (!activeFrame->loader()->shouldAllowNavigation(frame))
return jsUndefined();
DeprecatedString str = args[0]->toString(exec);
const Window* window = Window::retrieveWindow(frame);
if (!str.startsWith("javascript:", false) || (window && window->allowsAccessFrom(exec))) {
bool userGesture = activeFrame->scriptProxy()->processingUserGesture();
frame->loader()->scheduleLocationChange(activeFrame->loader()->completeURL(str).string(), activeFrame->loader()->outgoingReferrer(), true, userGesture);
}
}
return jsUndefined();
}
JSValue* jsLocationProtoFuncReload(ExecState* exec, JSObject* thisObj, const List& args)
{
if (!thisObj->inherits(&JSLocation::info))
return throwError(exec, TypeError);
JSLocation* location = static_cast<JSLocation*>(thisObj);
Frame* frame = location->frame();
if (!frame)
return jsUndefined();
Window* window = Window::retrieveWindow(frame);
if (!window->allowsAccessFrom(exec))
return jsUndefined();
if (!frame->loader()->url().deprecatedString().startsWith("javascript:", false) || (window && window->allowsAccessFrom(exec))) {
bool userGesture = Window::retrieveActive(exec)->impl()->frame()->scriptProxy()->processingUserGesture();
frame->loader()->scheduleRefresh(userGesture);
}
return jsUndefined();
}
JSValue* jsLocationProtoFuncAssign(ExecState* exec, JSObject* thisObj, const List& args)
{
if (!thisObj->inherits(&JSLocation::info))
return throwError(exec, TypeError);
JSLocation* location = static_cast<JSLocation*>(thisObj);
Frame* frame = location->frame();
if (!frame)
return jsUndefined();
Frame* activeFrame = Window::retrieveActive(exec)->impl()->frame();
if (activeFrame) {
if (!activeFrame->loader()->shouldAllowNavigation(frame))
return jsUndefined();
const Window* window = Window::retrieveWindow(frame);
String dstUrl = activeFrame->loader()->completeURL(args[0]->toString(exec)).string();
if (!dstUrl.startsWith("javascript:", false) || (window && window->allowsAccessFrom(exec))) {
bool userGesture = activeFrame->scriptProxy()->processingUserGesture();
frame->loader()->scheduleLocationChange(dstUrl, activeFrame->loader()->outgoingReferrer(), false, userGesture);
}
}
return jsUndefined();
}
JSValue* jsLocationProtoFuncToString(ExecState* exec, JSObject* thisObj, const List& args)
{
if (!thisObj->inherits(&JSLocation::info))
return throwError(exec, TypeError);
JSLocation* location = static_cast<JSLocation*>(thisObj);
Frame* frame = location->frame();
if (!frame)
return jsUndefined();
if (!allowsAccessFromFrame(exec, frame))
return jsUndefined();
const KURL& url = frame->loader()->url();
if (!url.hasPath())
return jsString(url.prettyURL() + "/");
return jsString(url.prettyURL());
}
}