1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) 2007 Samuel Weinig <sam@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "JSDOMBinding.h"
#include "BindingSecurity.h"
#include "CachedScript.h"
#include "DOMObjectHashTableMap.h"
#include "DOMStringList.h"
#include "ExceptionCode.h"
#include "ExceptionHeaders.h"
#include "ExceptionInterfaces.h"
#include "Frame.h"
#include "JSDOMWindowCustom.h"
#include "JSExceptionBase.h"
#include "ScriptCallStack.h"
#include <interpreter/Interpreter.h>
#include <runtime/DateInstance.h>
#include <runtime/Error.h>
#include <runtime/ExceptionHelpers.h>
#include <runtime/JSFunction.h>
using namespace JSC;
namespace WebCore {
ASSERT_HAS_TRIVIAL_DESTRUCTOR(DOMConstructorObject);
ASSERT_HAS_TRIVIAL_DESTRUCTOR(DOMConstructorWithDocument);
const JSC::HashTable* getHashTableForGlobalData(JSGlobalData& globalData, const JSC::HashTable* staticTable)
{
return DOMObjectHashTableMap::mapFor(globalData).get(staticTable);
}
JSValue jsStringWithCacheSlowCase(ExecState* exec, JSStringCache& stringCache, StringImpl* stringImpl)
{
JSString* wrapper = JSC::jsString(exec, String(stringImpl));
weakAdd(stringCache, stringImpl, PassWeak<JSString>(wrapper, currentWorld(exec)->stringWrapperOwner(), stringImpl));
return wrapper;
}
JSValue jsStringOrNull(ExecState* exec, const String& s)
{
if (s.isNull())
return jsNull();
return jsStringWithCache(exec, s);
}
JSValue jsOwnedStringOrNull(ExecState* exec, const String& s)
{
if (s.isNull())
return jsNull();
return jsOwnedString(exec, s);
}
JSValue jsStringOrUndefined(ExecState* exec, const String& s)
{
if (s.isNull())
return jsUndefined();
return jsStringWithCache(exec, s);
}
JSValue jsString(ExecState* exec, const KURL& url)
{
return jsStringWithCache(exec, url.string());
}
JSValue jsStringOrNull(ExecState* exec, const KURL& url)
{
if (url.isNull())
return jsNull();
return jsStringWithCache(exec, url.string());
}
JSValue jsStringOrUndefined(ExecState* exec, const KURL& url)
{
if (url.isNull())
return jsUndefined();
return jsStringWithCache(exec, url.string());
}
AtomicStringImpl* findAtomicString(PropertyName propertyName)
{
StringImpl* impl = propertyName.publicName();
if (!impl)
return 0;
ASSERT(impl->existingHash());
return AtomicString::find(impl);
}
String valueToStringWithNullCheck(ExecState* exec, JSValue value)
{
if (value.isNull())
return String();
return value.toString(exec)->value(exec);
}
String valueToStringWithUndefinedOrNullCheck(ExecState* exec, JSValue value)
{
if (value.isUndefinedOrNull())
return String();
return value.toString(exec)->value(exec);
}
JSValue jsDateOrNull(ExecState* exec, double value)
{
if (!isfinite(value))
return jsNull();
return DateInstance::create(exec, exec->lexicalGlobalObject()->dateStructure(), value);
}
double valueToDate(ExecState* exec, JSValue value)
{
if (value.isNumber())
return value.asNumber();
if (!value.inherits(&DateInstance::s_info))
return std::numeric_limits<double>::quiet_NaN();
return static_cast<DateInstance*>(value.toObject(exec))->internalNumber();
}
JSC::JSValue jsArray(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, PassRefPtr<DOMStringList> stringList)
{
JSC::MarkedArgumentBuffer list;
if (stringList) {
for (unsigned i = 0; i < stringList->length(); ++i)
list.append(jsStringWithCache(exec, stringList->item(i)));
}
return JSC::constructArray(exec, 0, globalObject, list);
}
void reportException(ExecState* exec, JSValue exception, CachedScript* cachedScript)
{
if (isTerminatedExecutionException(exception))
return;
Interpreter::ErrorHandlingMode mode(exec);
String errorMessage = exception.toString(exec)->value(exec);
JSObject* exceptionObject = exception.toObject(exec);
int lineNumber = exceptionObject->get(exec, Identifier(exec, "line")).toInt32(exec);
String exceptionSourceURL = exceptionObject->get(exec, Identifier(exec, "sourceURL")).toString(exec)->value(exec);
exec->clearException();
if (ExceptionBase* exceptionBase = toExceptionBase(exception))
errorMessage = exceptionBase->message() + ": " + exceptionBase->description();
JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
if (JSDOMWindow* window = jsDynamicCast<JSDOMWindow*>(globalObject)) {
if (!window->impl()->isCurrentlyDisplayedInFrame())
return;
}
ScriptExecutionContext* scriptExecutionContext = globalObject->scriptExecutionContext();
scriptExecutionContext->reportException(errorMessage, lineNumber, exceptionSourceURL, 0, cachedScript);
}
void reportCurrentException(ExecState* exec)
{
JSValue exception = exec->exception();
exec->clearException();
reportException(exec, exception);
}
#define TRY_TO_CREATE_EXCEPTION(interfaceName) \
case interfaceName##Type: \
errorObject = toJS(exec, globalObject, interfaceName::create(description)); \
break;
void setDOMException(ExecState* exec, ExceptionCode ec)
{
if (!ec || exec->hadException())
return;
// FIXME: Handle other WebIDL exception types.
if (ec == TypeError) {
throwTypeError(exec);
return;
}
// FIXME: All callers to setDOMException need to pass in the right global object
// for now, we're going to assume the lexicalGlobalObject. Which is wrong in cases like this:
// frames[0].document.createElement(null, null); // throws an exception which should have the subframes prototypes.
JSDOMGlobalObject* globalObject = deprecatedGlobalObjectForPrototype(exec);
ExceptionCodeDescription description(ec);
JSValue errorObject;
switch (description.type) {
DOM_EXCEPTION_INTERFACES_FOR_EACH(TRY_TO_CREATE_EXCEPTION)
}
ASSERT(errorObject);
throwError(exec, errorObject);
}
#undef TRY_TO_CREATE_EXCEPTION
bool shouldAllowAccessToNode(ExecState* exec, Node* node)
{
return BindingSecurity::shouldAllowAccessToNode(exec, node);
}
bool shouldAllowAccessToFrame(ExecState* exec, Frame* target)
{
return BindingSecurity::shouldAllowAccessToFrame(exec, target);
}
bool shouldAllowAccessToFrame(ExecState* exec, Frame* frame, String& message)
{
if (!frame)
return false;
if (BindingSecurity::shouldAllowAccessToFrame(exec, frame, DoNotReportSecurityError))
return true;
message = frame->document()->domWindow()->crossDomainAccessErrorMessage(activeDOMWindow(exec));
return false;
}
bool shouldAllowAccessToDOMWindow(ExecState* exec, DOMWindow* target, String& message)
{
if (!target)
return false;
if (BindingSecurity::shouldAllowAccessToDOMWindow(exec, target, DoNotReportSecurityError))
return true;
message = target->crossDomainAccessErrorMessage(activeDOMWindow(exec));
return false;
}
void printErrorMessageForFrame(Frame* frame, const String& message)
{
if (!frame)
return;
frame->document()->domWindow()->printErrorMessage(message);
}
JSValue objectToStringFunctionGetter(ExecState* exec, JSValue, PropertyName propertyName)
{
return JSFunction::create(exec, exec->lexicalGlobalObject(), 0, propertyName.publicName(), objectProtoFuncToString);
}
Structure* getCachedDOMStructure(JSDOMGlobalObject* globalObject, const ClassInfo* classInfo)
{
JSDOMStructureMap& structures = globalObject->structures();
return structures.get(classInfo).get();
}
Structure* cacheDOMStructure(JSDOMGlobalObject* globalObject, Structure* structure, const ClassInfo* classInfo)
{
JSDOMStructureMap& structures = globalObject->structures();
ASSERT(!structures.contains(classInfo));
return structures.set(classInfo, WriteBarrier<Structure>(globalObject->globalData(), globalObject, structure)).iterator->value.get();
}
} // namespace WebCore
|