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 "V8Binding.h"
41 #include "V8BindingState.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
46 #include <wtf/Assertions.h>
51 V8LocalContext::V8LocalContext()
52 : m_context(v8::Context::New())
54 V8BindingPerIsolateData::ensureInitialized(v8::Isolate::GetCurrent());
59 V8LocalContext::~V8LocalContext()
65 // Use an array to hold dependents. It works like a ref-counted scheme.
66 // A value can be added more than once to the DOM object.
67 void createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
69 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
70 if (cache->IsNull() || cache->IsUndefined()) {
71 cache = v8::Array::New();
72 object->SetInternalField(cacheIndex, cache);
75 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
76 cacheArray->Set(v8::Integer::New(cacheArray->Length()), value);
79 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
81 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
82 if (!cache->IsArray())
84 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
85 for (int i = cacheArray->Length() - 1; i >= 0; --i) {
86 v8::Local<v8::Value> cached = cacheArray->Get(v8::Integer::New(i));
87 if (cached->StrictEquals(value)) {
88 cacheArray->Delete(i);
94 void transferHiddenDependency(v8::Handle<v8::Object> object,
95 EventListener* oldValue,
96 v8::Local<v8::Value> newValue,
100 V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue);
102 v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject();
103 if (!oldListenerObject.IsEmpty())
104 removeHiddenDependency(object, oldListenerObject, cacheIndex);
107 if (!newValue->IsNull() && !newValue->IsUndefined())
108 createHiddenDependency(object, newValue, cacheIndex);
111 Frame* callingOrEnteredFrame()
113 return V8BindingState::Only()->activeFrame();
116 bool shouldAllowNavigation(Frame* frame)
118 return V8BindingSecurity::shouldAllowNavigation(V8BindingState::Only(), frame);
121 KURL completeURL(const String& relativeURL)
123 return completeURL(V8BindingState::Only(), relativeURL);
126 ScriptExecutionContext* getScriptExecutionContext()
129 if (WorkerScriptController* controller = WorkerScriptController::controllerForContext())
130 return controller->workerContext();
133 if (Frame* frame = V8Proxy::retrieveFrameForCurrentContext())
134 return frame->document()->scriptExecutionContext();
139 void throwTypeMismatchException()
141 V8Proxy::throwError(V8Proxy::GeneralError, "TYPE_MISMATCH_ERR: DOM Exception 17");
144 } // namespace WebCore