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 // 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) 54 { 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); 59 } 60 61 v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache); 62 cacheArray->Set(v8::Integer::New(cacheArray->Length()), value); 63 } 64 65 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) 66 { 67 v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex); 68 if (!cache->IsArray()) 69 return; 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); 75 return; 76 } 77 } 78 } 79 80 void transferHiddenDependency(v8::Handle<v8::Object> object, 81 EventListener* oldValue, 82 v8::Local<v8::Value> newValue, 83 int cacheIndex) 84 { 85 if (oldValue) { 86 V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue); 87 if (oldListener) { 88 v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject(); 89 if (!oldListenerObject.IsEmpty()) 90 removeHiddenDependency(object, oldListenerObject, cacheIndex); 91 } 92 } 93 if (!newValue->IsNull() && !newValue->IsUndefined()) 94 createHiddenDependency(object, newValue, cacheIndex); 95 } 96 97 bool processingUserGesture() 98 { 99 return V8BindingState::Only()->processingUserGesture(); 100 } 101 102 Frame* callingOrEnteredFrame() 103 { 104 return V8BindingState::Only()->activeFrame(); 105 } 106 107 bool shouldAllowNavigation(Frame* frame) 108 { 109 return V8BindingSecurity::shouldAllowNavigation(V8BindingState::Only(), frame); 110 } 111 112 KURL completeURL(const String& relativeURL) 113 { 114 return completeURL(V8BindingState::Only(), relativeURL); 115 } 116 117 ScriptExecutionContext* getScriptExecutionContext() 118 { 119 #if ENABLE(WORKERS) 120 if (WorkerScriptController* controller = WorkerScriptController::controllerForContext()) 121 return controller->workerContext(); 122 #endif 123 124 if (Frame* frame = V8Proxy::retrieveFrameForCurrentContext()) 125 return frame->document()->scriptExecutionContext(); 126 127 return 0; 128 } 129 130 void throwTypeMismatchException() 131 { 132 V8Proxy::throwError(V8Proxy::GeneralError, "TYPE_MISMATCH_ERR: DOM Exception 17"); 133 } 134 135 } // namespace WebCore 136