2 * Copyright (C) 2008, 2009 Google 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 are
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
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.
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.
32 #include "V8Utilities.h"
38 #include "ScriptExecutionContext.h"
39 #include "ScriptState.h"
40 #include "V8CustomBinding.h"
41 #include "V8Binding.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
46 #include <wtf/Assertions.h>
51 // Use an array to hold dependents. It works like a ref-counted scheme.
52 // A value can be added more than once to the DOM object.
53 void createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
55 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
56 if (cache->IsNull() || cache->IsUndefined()) {
57 cache = v8::Array::New();
58 object->SetInternalField(cacheIndex, cache);
61 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
62 cacheArray->Set(v8::Integer::New(cacheArray->Length()), value);
65 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
67 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
68 if (!cache->IsArray())
70 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
71 for (int i = cacheArray->Length() - 1; i >= 0; --i) {
72 v8::Local<v8::Value> cached = cacheArray->Get(v8::Integer::New(i));
73 if (cached->StrictEquals(value)) {
74 cacheArray->Delete(i);
80 bool processingUserGesture()
82 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
83 return frame && frame->script()->processingUserGesture();
86 bool shouldAllowNavigation(Frame* frame)
88 Frame* callingFrame = V8Proxy::retrieveFrameForCallingContext();
89 return callingFrame && callingFrame->loader()->shouldAllowNavigation(frame);
92 KURL completeURL(const String& relativeURL)
94 // For histoical reasons, we need to complete the URL using the dynamic frame.
95 Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
98 return frame->loader()->completeURL(relativeURL);
101 void navigateIfAllowed(Frame* frame, const KURL& url, bool lockHistory, bool lockBackForwardList)
103 Frame* callingFrame = V8Proxy::retrieveFrameForCallingContext();
107 if (!protocolIsJavaScript(url) || ScriptController::isSafeScript(frame))
108 frame->loader()->scheduleLocationChange(url.string(), callingFrame->loader()->outgoingReferrer(), lockHistory, lockBackForwardList, processingUserGesture());
111 ScriptExecutionContext* getScriptExecutionContext(ScriptState* scriptState)
114 WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
116 return proxy->workerContext()->scriptExecutionContext();
120 return scriptState->frame()->document()->scriptExecutionContext();
122 Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
124 return frame->document()->scriptExecutionContext();
130 void reportException(ScriptState* scriptState, v8::TryCatch& exceptionCatcher)
136 // There can be a situation that an exception is thrown without setting a message.
137 v8::Local<v8::Message> message = exceptionCatcher.Message();
138 if (message.IsEmpty())
139 errorMessage = toWebCoreString(exceptionCatcher.Exception()->ToString());
141 errorMessage = toWebCoreString(message->Get());
142 lineNumber = message->GetLineNumber();
143 sourceURL = toWebCoreString(message->GetScriptResourceName());
146 getScriptExecutionContext(scriptState)->reportException(errorMessage, lineNumber, sourceURL);
147 exceptionCatcher.Reset();
150 } // namespace WebCore