4f783b6153e358c1a3fb8742200107b0e244b8e5
[WebKit-https.git] / Source / WebCore / bindings / v8 / V8Utilities.cpp
1 /*
2  * Copyright (C) 2008, 2009 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 "config.h"
32 #include "V8Utilities.h"
33
34 #include <v8.h>
35
36 #include "Document.h"
37 #include "Frame.h"
38 #include "ScriptExecutionContext.h"
39 #include "ScriptState.h"
40 #include "V8Binding.h"
41 #include "V8BindingState.h"
42 #include "V8Proxy.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
45
46 #include <wtf/Assertions.h>
47 #include "Frame.h"
48
49 namespace WebCore {
50
51 V8LocalContext::V8LocalContext()
52     : m_context(v8::Context::New())
53 {
54     V8BindingPerIsolateData::ensureInitialized(v8::Isolate::GetCurrent());
55     m_context->Enter();
56 }
57
58
59 V8LocalContext::~V8LocalContext()
60 {
61     m_context->Exit();
62     m_context.Dispose();
63 }
64
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)
68 {
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);
73     }
74
75     v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
76     cacheArray->Set(v8::Integer::New(cacheArray->Length()), value);
77 }
78
79 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
80 {
81     v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
82     if (!cache->IsArray())
83         return;
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);
89             return;
90         }
91     }
92 }
93     
94 void transferHiddenDependency(v8::Handle<v8::Object> object,
95                               EventListener* oldValue, 
96                               v8::Local<v8::Value> newValue, 
97                               int cacheIndex)
98 {
99     if (oldValue) {
100         V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue);
101         if (oldListener) {
102             v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject();
103             if (!oldListenerObject.IsEmpty())
104                 removeHiddenDependency(object, oldListenerObject, cacheIndex);
105         }
106     }
107     if (!newValue->IsNull() && !newValue->IsUndefined())
108         createHiddenDependency(object, newValue, cacheIndex);
109 }
110
111 Frame* callingOrEnteredFrame()
112 {
113     return V8BindingState::Only()->activeFrame();
114 }
115
116 bool shouldAllowNavigation(Frame* frame)
117 {
118     return V8BindingSecurity::shouldAllowNavigation(V8BindingState::Only(), frame);
119 }
120
121 KURL completeURL(const String& relativeURL)
122 {
123     return completeURL(V8BindingState::Only(), relativeURL);
124 }
125
126 ScriptExecutionContext* getScriptExecutionContext()
127 {
128 #if ENABLE(WORKERS)
129     if (WorkerScriptController* controller = WorkerScriptController::controllerForContext())
130         return controller->workerContext();
131 #endif
132
133     if (Frame* frame = V8Proxy::retrieveFrameForCurrentContext())
134         return frame->document()->scriptExecutionContext();
135
136     return 0;
137 }
138
139 void throwTypeMismatchException()
140 {
141     V8Proxy::throwError(V8Proxy::GeneralError, "TYPE_MISMATCH_ERR: DOM Exception 17");
142 }
143
144 } // namespace WebCore