• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "JSHistoryCustom.h"
31 
32 #include "Frame.h"
33 #include "History.h"
34 #include <runtime/PrototypeFunction.h>
35 
36 using namespace JSC;
37 
38 namespace WebCore {
39 
nonCachingStaticBackFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)40 static JSValue nonCachingStaticBackFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
41 {
42     return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 0, propertyName, jsHistoryPrototypeFunctionBack);
43 }
44 
nonCachingStaticForwardFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)45 static JSValue nonCachingStaticForwardFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
46 {
47     return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 0, propertyName, jsHistoryPrototypeFunctionForward);
48 }
49 
nonCachingStaticGoFunctionGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot &)50 static JSValue nonCachingStaticGoFunctionGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot&)
51 {
52     return new (exec) NativeFunctionWrapper(exec, exec->lexicalGlobalObject()->prototypeFunctionStructure(), 1, propertyName, jsHistoryPrototypeFunctionGo);
53 }
54 
getOwnPropertySlotDelegate(ExecState * exec,const Identifier & propertyName,PropertySlot & slot)55 bool JSHistory::getOwnPropertySlotDelegate(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
56 {
57     // When accessing History cross-domain, functions are always the native built-in ones.
58     // See JSDOMWindow::getOwnPropertySlotDelegate for additional details.
59 
60     // Our custom code is only needed to implement the Window cross-domain scheme, so if access is
61     // allowed, return false so the normal lookup will take place.
62     String message;
63     if (allowsAccessFromFrame(exec, impl()->frame(), message))
64         return false;
65 
66     // Check for the few functions that we allow, even when called cross-domain.
67     const HashEntry* entry = JSHistoryPrototype::s_info.propHashTable(exec)->entry(exec, propertyName);
68     if (entry) {
69         // Allow access to back(), forward() and go() from any frame.
70         if (entry->attributes() & Function) {
71             if (entry->function() == jsHistoryPrototypeFunctionBack) {
72                 slot.setCustom(this, nonCachingStaticBackFunctionGetter);
73                 return true;
74             } else if (entry->function() == jsHistoryPrototypeFunctionForward) {
75                 slot.setCustom(this, nonCachingStaticForwardFunctionGetter);
76                 return true;
77             } else if (entry->function() == jsHistoryPrototypeFunctionGo) {
78                 slot.setCustom(this, nonCachingStaticGoFunctionGetter);
79                 return true;
80             }
81         }
82     } else {
83         // Allow access to toString() cross-domain, but always Object.toString.
84         if (propertyName == exec->propertyNames().toString) {
85             slot.setCustom(this, objectToStringFunctionGetter);
86             return true;
87         }
88     }
89 
90     printErrorMessageForFrame(impl()->frame(), message);
91     slot.setUndefined();
92     return true;
93 }
94 
putDelegate(ExecState * exec,const Identifier &,JSValue,PutPropertySlot &)95 bool JSHistory::putDelegate(ExecState* exec, const Identifier&, JSValue, PutPropertySlot&)
96 {
97     // Only allow putting by frames in the same origin.
98     if (!allowsAccessFromFrame(exec, impl()->frame()))
99         return true;
100     return false;
101 }
102 
deleteProperty(ExecState * exec,const Identifier & propertyName)103 bool JSHistory::deleteProperty(ExecState* exec, const Identifier& propertyName)
104 {
105     // Only allow deleting by frames in the same origin.
106     if (!allowsAccessFromFrame(exec, impl()->frame()))
107         return false;
108     return Base::deleteProperty(exec, propertyName);
109 }
110 
getPropertyNames(ExecState * exec,PropertyNameArray & propertyNames)111 void JSHistory::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames)
112 {
113     // Only allow the history object to enumerated by frames in the same origin.
114     if (!allowsAccessFromFrame(exec, impl()->frame()))
115         return;
116     Base::getPropertyNames(exec, propertyNames);
117 }
118 
119 } // namespace WebCore
120