18f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian/* 28f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * Copyright (C) 2008, 2009 Google Inc. All rights reserved. 38f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * 48f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * Redistribution and use in source and binary forms, with or without 58f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * modification, are permitted provided that the following conditions are 68f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * met: 78f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * 88f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * * Redistributions of source code must retain the above copyright 98f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * notice, this list of conditions and the following disclaimer. 108f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * * Redistributions in binary form must reproduce the above 118f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * copyright notice, this list of conditions and the following disclaimer 128f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * in the documentation and/or other materials provided with the 138f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * distribution. 148f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * * Neither the name of Google Inc. nor the names of its 158f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * contributors may be used to endorse or promote products derived from 168f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * this software without specific prior written permission. 178f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * 188f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 198f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 208f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 218f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 228f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 238f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 248f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 288f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian */ 308f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 318f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian#include "config.h" 325f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian#include "V8Utilities.h" 338f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 348f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian#include <v8.h> 358f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 36231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "Document.h" 37231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "Frame.h" 38231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "ScriptExecutionContext.h" 39231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "ScriptState.h" 40231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "V8Binding.h" 41dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include "V8BindingState.h" 428f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian#include "V8Proxy.h" 43231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "WorkerContext.h" 44231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "WorkerContextExecutionProxy.h" 458f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 468f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian#include <wtf/Assertions.h> 475f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian#include "Frame.h" 488f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 498f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qiannamespace WebCore { 508f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 518f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian// Use an array to hold dependents. It works like a ref-counted scheme. 525f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian// A value can be added more than once to the DOM object. 53231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) 548f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian{ 555f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex); 568f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian if (cache->IsNull() || cache->IsUndefined()) { 578f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian cache = v8::Array::New(); 585f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian object->SetInternalField(cacheIndex, cache); 598f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian } 608f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 618f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache); 628f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian cacheArray->Set(v8::Integer::New(cacheArray->Length()), value); 638f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian} 648f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 65231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockvoid removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) 668f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian{ 675f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex); 680bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch if (!cache->IsArray()) 690bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch return; 708f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache); 718f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian for (int i = cacheArray->Length() - 1; i >= 0; --i) { 728f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian v8::Local<v8::Value> cached = cacheArray->Get(v8::Integer::New(i)); 738f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian if (cached->StrictEquals(value)) { 748f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian cacheArray->Delete(i); 758f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian return; 768f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian } 778f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian } 788f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian} 79643ca7872b450ea4efacab6188849e5aac2ba161Steve Block 80643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockvoid transferHiddenDependency(v8::Handle<v8::Object> object, 81643ca7872b450ea4efacab6188849e5aac2ba161Steve Block EventListener* oldValue, 82643ca7872b450ea4efacab6188849e5aac2ba161Steve Block v8::Local<v8::Value> newValue, 83643ca7872b450ea4efacab6188849e5aac2ba161Steve Block int cacheIndex) 84643ca7872b450ea4efacab6188849e5aac2ba161Steve Block{ 85643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if (oldValue) { 86643ca7872b450ea4efacab6188849e5aac2ba161Steve Block V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue); 87643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if (oldListener) { 88643ca7872b450ea4efacab6188849e5aac2ba161Steve Block v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject(); 89643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if (!oldListenerObject.IsEmpty()) 90643ca7872b450ea4efacab6188849e5aac2ba161Steve Block removeHiddenDependency(object, oldListenerObject, cacheIndex); 91643ca7872b450ea4efacab6188849e5aac2ba161Steve Block } 92643ca7872b450ea4efacab6188849e5aac2ba161Steve Block } 93643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if (!newValue->IsNull() && !newValue->IsUndefined()) 94643ca7872b450ea4efacab6188849e5aac2ba161Steve Block createHiddenDependency(object, newValue, cacheIndex); 95643ca7872b450ea4efacab6188849e5aac2ba161Steve Block} 968f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian 975f1ab04193ad0130ca8204aadaceae083aca9881Feng Qianbool processingUserGesture() 985f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian{ 99dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch return V8BindingState::Only()->processingUserGesture(); 1005f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian} 1015f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian 102e458d70a0d18538346f41b503114c9ebe6b2ce12Leon ClarkeFrame* callingOrEnteredFrame() 103e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke{ 104f05b935882198ccf7d81675736e3aeb089c5113aBen Murdoch return V8BindingState::Only()->activeFrame(); 105e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke} 106e458d70a0d18538346f41b503114c9ebe6b2ce12Leon Clarke 1075f1ab04193ad0130ca8204aadaceae083aca9881Feng Qianbool shouldAllowNavigation(Frame* frame) 1085f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian{ 109dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch return V8BindingSecurity::shouldAllowNavigation(V8BindingState::Only(), frame); 1105f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian} 1115f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian 1125f1ab04193ad0130ca8204aadaceae083aca9881Feng QianKURL completeURL(const String& relativeURL) 1135f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian{ 1145abb8606fa57c3ebfc8b3c3dbc3fa4a25d2ae306Iain Merrick return completeURL(V8BindingState::Only(), relativeURL); 1155f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian} 1165f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian 117dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve BlockScriptExecutionContext* getScriptExecutionContext() 118231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block{ 119231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#if ENABLE(WORKERS) 120dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (WorkerScriptController* controller = WorkerScriptController::controllerForContext()) 121dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return controller->workerContext(); 122231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#endif 123231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 124dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if (Frame* frame = V8Proxy::retrieveFrameForCurrentContext()) 125d0825bca7fe65beaee391d30da42e937db621564Steve Block return frame->document()->scriptExecutionContext(); 126231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 127231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block return 0; 128231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block} 129231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 1302daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdochvoid throwTypeMismatchException() 1312daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch{ 1322daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch V8Proxy::throwError(V8Proxy::GeneralError, "TYPE_MISMATCH_ERR: DOM Exception 17"); 1332daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch} 1342daae5fd11344eaa88a0d92b0f6d65f8d2255c00Ben Murdoch 1358f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian} // namespace WebCore 136