| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009, 2011 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "sky/engine/config.h" | |
| 32 #include "bindings/core/v8/V8Window.h" | |
| 33 | |
| 34 #include "bindings/core/v8/V8Node.h" | |
| 35 #include "sky/engine/bindings/core/v8/BindingSecurity.h" | |
| 36 #include "sky/engine/bindings/core/v8/ExceptionMessages.h" | |
| 37 #include "sky/engine/bindings/core/v8/ExceptionState.h" | |
| 38 #include "sky/engine/bindings/core/v8/ScheduledAction.h" | |
| 39 #include "sky/engine/bindings/core/v8/ScriptController.h" | |
| 40 #include "sky/engine/bindings/core/v8/ScriptSourceCode.h" | |
| 41 #include "sky/engine/bindings/core/v8/SerializedScriptValue.h" | |
| 42 #include "sky/engine/bindings/core/v8/V8Binding.h" | |
| 43 #include "sky/engine/bindings/core/v8/V8GCForContextDispose.h" | |
| 44 #include "sky/engine/bindings/core/v8/V8HiddenValue.h" | |
| 45 #include "sky/engine/core/dom/Element.h" | |
| 46 #include "sky/engine/core/dom/ExceptionCode.h" | |
| 47 #include "sky/engine/core/dom/Node.h" | |
| 48 #include "sky/engine/core/frame/DOMTimer.h" | |
| 49 #include "sky/engine/core/frame/DOMWindowTimers.h" | |
| 50 #include "sky/engine/core/frame/FrameView.h" | |
| 51 #include "sky/engine/core/frame/LocalDOMWindow.h" | |
| 52 #include "sky/engine/core/frame/LocalFrame.h" | |
| 53 #include "sky/engine/core/frame/Settings.h" | |
| 54 #include "sky/engine/platform/PlatformScreen.h" | |
| 55 #include "sky/engine/core/inspector/ScriptCallStack.h" | |
| 56 #include "sky/engine/wtf/ArrayBuffer.h" | |
| 57 #include "sky/engine/wtf/Assertions.h" | |
| 58 #include "sky/engine/wtf/OwnPtr.h" | |
| 59 | |
| 60 namespace blink { | |
| 61 | |
| 62 // FIXME: There is a lot of duplication with SetTimeoutOrInterval() in V8WorkerG
lobalScopeCustom.cpp. | |
| 63 // We should refactor this. | |
| 64 static void windowSetTimeoutImpl(const v8::FunctionCallbackInfo<v8::Value>& info
, bool singleShot, ExceptionState& exceptionState) | |
| 65 { | |
| 66 int argumentCount = info.Length(); | |
| 67 | |
| 68 if (argumentCount < 1) | |
| 69 return; | |
| 70 | |
| 71 LocalDOMWindow* impl = V8Window::toNative(info.Holder()); | |
| 72 if (!impl->frame() || !impl->document()) { | |
| 73 exceptionState.throwDOMException(InvalidAccessError, "No script context
is available in which to execute the script."); | |
| 74 return; | |
| 75 } | |
| 76 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); | |
| 77 v8::Handle<v8::Value> function = info[0]; | |
| 78 String functionString; | |
| 79 if (!function->IsFunction()) { | |
| 80 if (function->IsString()) { | |
| 81 functionString = toCoreString(function.As<v8::String>()); | |
| 82 } else { | |
| 83 v8::Handle<v8::String> v8String = function->ToString(); | |
| 84 | |
| 85 // Bail out if string conversion failed. | |
| 86 if (v8String.IsEmpty()) | |
| 87 return; | |
| 88 | |
| 89 functionString = toCoreString(v8String); | |
| 90 } | |
| 91 | |
| 92 // Don't allow setting timeouts to run empty functions! | |
| 93 // (Bug 1009597) | |
| 94 if (!functionString.length()) | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), impl->fram
e(), exceptionState)) | |
| 99 return; | |
| 100 | |
| 101 OwnPtr<ScheduledAction> action; | |
| 102 if (function->IsFunction()) { | |
| 103 int paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; | |
| 104 OwnPtr<v8::Local<v8::Value>[]> params; | |
| 105 if (paramCount > 0) { | |
| 106 params = adoptArrayPtr(new v8::Local<v8::Value>[paramCount]); | |
| 107 for (int i = 0; i < paramCount; i++) { | |
| 108 // parameters must be globalized | |
| 109 params[i] = info[i+2]; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 // params is passed to action, and released in action's destructor | |
| 114 ASSERT(impl->frame()); | |
| 115 action = adoptPtr(new ScheduledAction(scriptState, v8::Handle<v8::Functi
on>::Cast(function), paramCount, params.get(), info.GetIsolate())); | |
| 116 } else { | |
| 117 ASSERT(impl->frame()); | |
| 118 action = adoptPtr(new ScheduledAction(scriptState, functionString, KURL(
), info.GetIsolate())); | |
| 119 } | |
| 120 | |
| 121 int32_t timeout = argumentCount >= 2 ? info[1]->Int32Value() : 0; | |
| 122 int timerId; | |
| 123 if (singleShot) | |
| 124 timerId = DOMWindowTimers::setTimeout(*impl, action.release(), timeout); | |
| 125 else | |
| 126 timerId = DOMWindowTimers::setInterval(*impl, action.release(), timeout)
; | |
| 127 | |
| 128 // Try to do the idle notification before the timeout expires to get better | |
| 129 // use of any idle time. Aim for the middle of the interval for simplicity. | |
| 130 if (timeout >= 0) { | |
| 131 double maximumFireInterval = static_cast<double>(timeout) / 1000 / 2; | |
| 132 V8GCForContextDispose::instanceTemplate().notifyIdleSooner(maximumFireIn
terval); | |
| 133 } | |
| 134 | |
| 135 v8SetReturnValue(info, timerId); | |
| 136 } | |
| 137 | |
| 138 // FIXME(fqian): returning string is cheating, and we should | |
| 139 // fix this by calling toString function on the receiver. | |
| 140 // However, V8 implements toString in JavaScript, which requires | |
| 141 // switching context of receiver. I consider it is dangerous. | |
| 142 void V8Window::toStringMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& i
nfo) | |
| 143 { | |
| 144 v8::Handle<v8::Object> domWrapper = V8Window::findInstanceInPrototypeChain(i
nfo.This(), info.GetIsolate()); | |
| 145 if (domWrapper.IsEmpty()) { | |
| 146 v8SetReturnValue(info, info.This()->ObjectProtoToString()); | |
| 147 return; | |
| 148 } | |
| 149 v8SetReturnValue(info, domWrapper->ObjectProtoToString()); | |
| 150 } | |
| 151 | |
| 152 void V8Window::namedPropertyGetterCustom(v8::Local<v8::String> name, const v8::P
ropertyCallbackInfo<v8::Value>& info) | |
| 153 { | |
| 154 } | |
| 155 | |
| 156 void V8Window::setTimeoutMethodCustom(const v8::FunctionCallbackInfo<v8::Value>&
info) | |
| 157 { | |
| 158 ExceptionState exceptionState(ExceptionState::ExecutionContext, "setTimeout"
, "Window", info.Holder(), info.GetIsolate()); | |
| 159 windowSetTimeoutImpl(info, true, exceptionState); | |
| 160 exceptionState.throwIfNeeded(); | |
| 161 } | |
| 162 | |
| 163 | |
| 164 void V8Window::setIntervalMethodCustom(const v8::FunctionCallbackInfo<v8::Value>
& info) | |
| 165 { | |
| 166 ExceptionState exceptionState(ExceptionState::ExecutionContext, "setInterval
", "Window", info.Holder(), info.GetIsolate()); | |
| 167 windowSetTimeoutImpl(info, false, exceptionState); | |
| 168 exceptionState.throwIfNeeded(); | |
| 169 } | |
| 170 | |
| 171 bool V8Window::namedSecurityCheckCustom(v8::Local<v8::Object> host, v8::Local<v8
::Value> key, v8::AccessType type, v8::Local<v8::Value>) | |
| 172 { | |
| 173 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 174 v8::Handle<v8::Object> window = V8Window::findInstanceInPrototypeChain(host,
isolate); | |
| 175 if (window.IsEmpty()) | |
| 176 return false; // the frame is gone. | |
| 177 | |
| 178 LocalDOMWindow* targetWindow = V8Window::toNative(window); | |
| 179 | |
| 180 ASSERT(targetWindow); | |
| 181 | |
| 182 LocalFrame* target = targetWindow->frame(); | |
| 183 if (!target) | |
| 184 return false; | |
| 185 | |
| 186 return BindingSecurity::shouldAllowAccessToFrame(isolate, target, DoNotRepor
tSecurityError); | |
| 187 } | |
| 188 | |
| 189 bool V8Window::indexedSecurityCheckCustom(v8::Local<v8::Object> host, uint32_t i
ndex, v8::AccessType type, v8::Local<v8::Value>) | |
| 190 { | |
| 191 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 192 v8::Handle<v8::Object> window = V8Window::findInstanceInPrototypeChain(host,
isolate); | |
| 193 if (window.IsEmpty()) | |
| 194 return false; | |
| 195 | |
| 196 LocalDOMWindow* targetWindow = V8Window::toNative(window); | |
| 197 | |
| 198 ASSERT(targetWindow); | |
| 199 | |
| 200 LocalFrame* target = targetWindow->frame(); | |
| 201 if (!target) | |
| 202 return false; | |
| 203 | |
| 204 return BindingSecurity::shouldAllowAccessToFrame(isolate, target, DoNotRepor
tSecurityError); | |
| 205 } | |
| 206 | |
| 207 v8::Handle<v8::Value> toV8(LocalDOMWindow* window, v8::Handle<v8::Object> creati
onContext, v8::Isolate* isolate) | |
| 208 { | |
| 209 // Notice that we explicitly ignore creationContext because the LocalDOMWind
ow is its own creationContext. | |
| 210 | |
| 211 if (!window) | |
| 212 return v8::Null(isolate); | |
| 213 // Initializes environment of a frame, and return the global object | |
| 214 // of the frame. | |
| 215 LocalFrame* frame = window->frame(); | |
| 216 if (!frame) | |
| 217 return v8Undefined(); | |
| 218 | |
| 219 v8::Handle<v8::Context> context = toV8Context(frame, DOMWrapperWorld::curren
t(isolate)); | |
| 220 if (context.IsEmpty()) | |
| 221 return v8Undefined(); | |
| 222 | |
| 223 v8::Handle<v8::Object> global = context->Global(); | |
| 224 ASSERT(!global.IsEmpty()); | |
| 225 return global; | |
| 226 } | |
| 227 | |
| 228 } // namespace blink | |
| OLD | NEW |