WebCore/bindings/v8/V8Utilities.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "V8Proxy.h"
       
    42 #include "WorkerContext.h"
       
    43 #include "WorkerContextExecutionProxy.h"
       
    44 
       
    45 #include <wtf/Assertions.h>
       
    46 #include "Frame.h"
       
    47 
       
    48 namespace WebCore {
       
    49 
       
    50 // Use an array to hold dependents. It works like a ref-counted scheme.
       
    51 // A value can be added more than once to the DOM object.
       
    52 void createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
       
    53 {
       
    54     v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
       
    55     if (cache->IsNull() || cache->IsUndefined()) {
       
    56         cache = v8::Array::New();
       
    57         object->SetInternalField(cacheIndex, cache);
       
    58     }
       
    59 
       
    60     v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
       
    61     cacheArray->Set(v8::Integer::New(cacheArray->Length()), value);
       
    62 }
       
    63 
       
    64 void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex)
       
    65 {
       
    66     v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex);
       
    67     if (!cache->IsArray())
       
    68         return;
       
    69     v8::Local<v8::Array> cacheArray = v8::Local<v8::Array>::Cast(cache);
       
    70     for (int i = cacheArray->Length() - 1; i >= 0; --i) {
       
    71         v8::Local<v8::Value> cached = cacheArray->Get(v8::Integer::New(i));
       
    72         if (cached->StrictEquals(value)) {
       
    73             cacheArray->Delete(i);
       
    74             return;
       
    75         }
       
    76     }
       
    77 }
       
    78     
       
    79 void transferHiddenDependency(v8::Handle<v8::Object> object,
       
    80                               EventListener* oldValue, 
       
    81                               v8::Local<v8::Value> newValue, 
       
    82                               int cacheIndex)
       
    83 {
       
    84     if (oldValue) {
       
    85         V8AbstractEventListener* oldListener = V8AbstractEventListener::cast(oldValue);
       
    86         if (oldListener) {
       
    87             v8::Local<v8::Object> oldListenerObject = oldListener->getExistingListenerObject();
       
    88             if (!oldListenerObject.IsEmpty())
       
    89                 removeHiddenDependency(object, oldListenerObject, cacheIndex);
       
    90         }
       
    91     }
       
    92     if (!newValue->IsNull() && !newValue->IsUndefined())
       
    93         createHiddenDependency(object, newValue, cacheIndex);
       
    94 }
       
    95     
       
    96 
       
    97 bool processingUserGesture()
       
    98 {
       
    99     Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
       
   100     return frame && frame->script()->processingUserGesture();
       
   101 }
       
   102 
       
   103 Frame* callingOrEnteredFrame()
       
   104 {
       
   105     Frame* frame = V8Proxy::retrieveFrameForCallingContext();
       
   106     if (!frame) {
       
   107         // Unfortunately, when processing script from a plug-in, we might not
       
   108         // have a calling context.  In those cases, we fall back to the
       
   109         // entered context for security checks.
       
   110         // FIXME: We need a better API for retrieving frames that abstracts
       
   111         //        away this concern.
       
   112         frame = V8Proxy::retrieveFrameForEnteredContext();
       
   113     }
       
   114     return frame;
       
   115 }
       
   116 
       
   117 bool shouldAllowNavigation(Frame* frame)
       
   118 {
       
   119     Frame* callingOrEntered = callingOrEnteredFrame();
       
   120     return callingOrEntered && callingOrEntered->loader()->shouldAllowNavigation(frame);
       
   121 }
       
   122 
       
   123 KURL completeURL(const String& relativeURL)
       
   124 {
       
   125     // For histoical reasons, we need to complete the URL using the dynamic frame.
       
   126     Frame* frame = V8Proxy::retrieveFrameForEnteredContext();
       
   127     if (!frame)
       
   128         return KURL();
       
   129     return frame->loader()->completeURL(relativeURL);
       
   130 }
       
   131 
       
   132 void navigateIfAllowed(Frame* frame, const KURL& url, bool lockHistory, bool lockBackForwardList)
       
   133 {
       
   134     Frame* callingOrEntered = callingOrEnteredFrame();
       
   135     if (!callingOrEntered)
       
   136         return;
       
   137     if (!protocolIsJavaScript(url) || ScriptController::isSafeScript(frame))
       
   138         frame->redirectScheduler()->scheduleLocationChange(url.string(), callingOrEntered->loader()->outgoingReferrer(), lockHistory, lockBackForwardList, processingUserGesture());
       
   139 }
       
   140 
       
   141 ScriptExecutionContext* getScriptExecutionContext()
       
   142 {
       
   143 #if ENABLE(WORKERS)
       
   144     if (WorkerScriptController* controller = WorkerScriptController::controllerForContext())
       
   145         return controller->workerContext();
       
   146 #endif
       
   147 
       
   148     if (Frame* frame = V8Proxy::retrieveFrameForCurrentContext())
       
   149         return frame->document()->scriptExecutionContext();
       
   150 
       
   151     return 0;
       
   152 }
       
   153 
       
   154 } // namespace WebCore