| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef WindowProxy_h | 31 #ifndef WindowProxy_h |
| 32 #define WindowProxy_h | 32 #define WindowProxy_h |
| 33 | 33 |
| 34 #include "bindings/core/v8/DOMWrapperWorld.h" | 34 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 35 #include "bindings/core/v8/ScopedPersistent.h" | 35 #include "bindings/core/v8/ScopedPersistent.h" |
| 36 #include "core/CoreExport.h" | 36 #include "bindings/core/v8/ScriptState.h" |
| 37 #include "platform/heap/Handle.h" | 37 #include "platform/heap/Handle.h" |
| 38 #include "v8/include/v8.h" | 38 #include "v8/include/v8.h" |
| 39 #include "wtf/RefPtr.h" | 39 #include "wtf/RefPtr.h" |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 class Frame; | 43 class Frame; |
| 44 class ScriptController; |
| 44 | 45 |
| 45 // WindowProxy implements the split window model of a window for a frame. In the | 46 // WindowProxy represents all the per-global object state for a Frame that |
| 46 // HTML standard, the split window model is composed of the Window interface | 47 // persist between navigations. |
| 47 // (the inner global object) and the WindowProxy interface (the outer global | |
| 48 // proxy). | |
| 49 // | |
| 50 // The Window interface is backed by the Blink DOMWindow C++ implementation. | |
| 51 // In contrast, the WindowProxy interface does not have a corresponding | |
| 52 // C++ implementation in Blink: the WindowProxy class defined here only manages | |
| 53 // context initialization and detach. Instead, the behavior of the WindowProxy | |
| 54 // interface is defined by JSGlobalProxy in v8 and the prototype chain set up | |
| 55 // during context initialization. | |
| 56 // | |
| 57 // ====== Inner Global Object ====== | |
| 58 // The inner global object is the global for the script environment of a Frame. | |
| 59 // Since Window and Document also have a 1:1 relationship, this means that each | |
| 60 // inner global object has an associated Document which does not change. On | |
| 61 // navigation, the new Document receives a new inner global object. | |
| 62 // | |
| 63 // However, there is one exception to the 1:1 DOMWindow:Document rule. If: | |
| 64 // - the previous Document is the initial empty document | |
| 65 // - the new Document is same-origin to the previous Document | |
| 66 // then the inner global object will be reused for the new Document. This is the | |
| 67 // only case where the associated Document of an inner global object can change. | |
| 68 // | |
| 69 // All methods and attributes defined on the Window interface are exposed via | |
| 70 // the inner global object. Global variables defined by script running in the | |
| 71 // Document also live on the inner global object. | |
| 72 // | |
| 73 // ====== Outer Global Proxy ==== | |
| 74 // The outer global proxy is reused across navigations. It implements the | |
| 75 // security checks for same-origin/cross-origin access to the Window interface. | |
| 76 // When the check passes (i.e. the access is same-origin), the access is | |
| 77 // forwarded to the inner global object of the active Document in this | |
| 78 // WindowProxy's Frame). | |
| 79 // | |
| 80 // When the security check fails, the access is delegated to the outer global | |
| 81 // proxy's cross-origin interceptors. The cross-origin interceptors may choose | |
| 82 // to return a value (if the property is exposed cross-origin) or throw an | |
| 83 // exception otherwise. | |
| 84 // | |
| 85 // Note that the cross-origin interceptors are only used for cross-origin | |
| 86 // accesses: a same-origin access to a method that is available cross-origin, | |
| 87 // such as Window.postMessage, will be delegated to the inner global object. | |
| 88 // | |
| 89 // ====== LocalWindowProxy vs RemoteWindowProxy ====== | |
| 90 // WindowProxy has two concrete subclasses: | |
| 91 // - LocalWindowProxy: implements the split window model for a frame in the same | |
| 92 // process, i.e. a LocalFrame. | |
| 93 // - RemoteWindowProxy: implements the split window model for a frame in a | |
| 94 // different process, i.e. a RemoteFrame. | |
| 95 // | |
| 96 // While having a RemoteFrame implies the frame must be cross-origin, the | |
| 97 // opposite is not true: a LocalFrame can be same-origin or cross-origin. One | |
| 98 // additional complexity (which slightly violates the HTML standard): it is | |
| 99 // possible to have SecurityOrigin::canAccess() return true for a RemoteFrame's | |
| 100 // security origin; however, it is important to still deny access as if the | |
| 101 // frame were cross-origin. This is due to complexities in the process | |
| 102 // allocation model for renderer processes. See https://crbug.com/601629. | |
| 103 // | |
| 104 // ====== LocalWindowProxy/RemoteWindowProxy ====== | |
| 105 // Currently, the prototype chain for LocalWindowProxy and RemoteWindowProxy | |
| 106 // look the same: | |
| 107 // | |
| 108 // outer global proxy | |
| 109 // -- has prototype --> inner global object | |
| 110 // -- has prototype --> Window.prototype | |
| 111 // -- has prototype --> WindowProperties [1] | |
| 112 // -- has prototype --> EventTarget.prototype | |
| 113 // -- has prototype --> Object.prototype | |
| 114 // -- has prototype --> null | |
| 115 // | |
| 116 // [1] WindowProperties is the named properties object of the Window interface. | |
| 117 // | |
| 118 // There is work in progress to refactor RemoteWindowProxy to use remote v8 | |
| 119 // contexts, to reduce the overhead of remote frames. | |
| 120 // | |
| 121 // ====== References ====== | |
| 122 // https://wiki.mozilla.org/Gecko:SplitWindow | |
| 123 // https://whatwg.org/C/browsers.html#the-windowproxy-exotic-object | |
| 124 class WindowProxy : public GarbageCollectedFinalized<WindowProxy> { | 48 class WindowProxy : public GarbageCollectedFinalized<WindowProxy> { |
| 125 public: | 49 public: |
| 126 virtual ~WindowProxy(); | 50 virtual ~WindowProxy(); |
| 127 | 51 |
| 128 DECLARE_TRACE(); | 52 DECLARE_TRACE(); |
| 129 | 53 |
| 54 v8::Local<v8::Context> contextIfInitialized() const { |
| 55 return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); |
| 56 } |
| 130 void initializeIfNeeded(); | 57 void initializeIfNeeded(); |
| 131 | 58 |
| 132 void clearForClose(); | 59 void clearForClose(); |
| 133 void clearForNavigation(); | 60 void clearForNavigation(); |
| 134 | 61 |
| 135 CORE_EXPORT v8::Local<v8::Object> globalIfNotDetached(); | 62 v8::Local<v8::Object> globalIfNotDetached(); |
| 136 v8::Local<v8::Object> releaseGlobal(); | 63 v8::Local<v8::Object> releaseGlobal(); |
| 137 void setGlobal(v8::Local<v8::Object>); | 64 void setGlobal(v8::Local<v8::Object>); |
| 138 | 65 |
| 139 // TODO(dcheng): Temporarily exposed to avoid include cycles. Remove the need | 66 // TODO(dcheng): Temporarily exposed to avoid include cycles. Remove the need |
| 140 // for this and remove this getter. | 67 // for this and remove this getter. |
| 141 DOMWrapperWorld& world() { return *m_world; } | 68 DOMWrapperWorld& world() { return *m_world; } |
| 142 | 69 |
| 143 protected: | 70 protected: |
| 71 // TODO(dcheng): Remove this friend declaration once LocalWindowProxyManager |
| 72 // and ScriptController are merged. |
| 73 friend class ScriptController; |
| 74 |
| 144 // A valid transition is from ContextUninitialized to ContextInitialized, | 75 // A valid transition is from ContextUninitialized to ContextInitialized, |
| 145 // and then ContextDetached. Other transitions are forbidden. | 76 // and then ContextDetached. Other transitions are forbidden. |
| 146 enum class Lifecycle { | 77 enum class Lifecycle { |
| 147 ContextUninitialized, | 78 ContextUninitialized, |
| 148 ContextInitialized, | 79 ContextInitialized, |
| 149 ContextDetached, | 80 ContextDetached, |
| 150 }; | 81 }; |
| 151 | 82 |
| 152 WindowProxy(v8::Isolate*, Frame&, RefPtr<DOMWrapperWorld>); | 83 WindowProxy(v8::Isolate*, Frame&, RefPtr<DOMWrapperWorld>); |
| 153 | 84 |
| 154 virtual void initialize() = 0; | 85 virtual void initialize() = 0; |
| 155 | 86 |
| 156 enum GlobalDetachmentBehavior { DoNotDetachGlobal, DetachGlobal }; | 87 enum GlobalDetachmentBehavior { DoNotDetachGlobal, DetachGlobal }; |
| 157 virtual void disposeContext(GlobalDetachmentBehavior) = 0; | 88 virtual void disposeContext(GlobalDetachmentBehavior); |
| 89 |
| 90 // Associates the window wrapper and its prototype chain with the native |
| 91 // DOMWindow object. Also does some more Window-specific initialization. |
| 92 void setupWindowPrototypeChain(); |
| 158 | 93 |
| 159 v8::Isolate* isolate() const { return m_isolate; } | 94 v8::Isolate* isolate() const { return m_isolate; } |
| 160 Frame* frame() const { return m_frame.get(); } | 95 Frame* frame() const { return m_frame.get(); } |
| 161 | 96 ScriptState* getScriptState() const { return m_scriptState.get(); } |
| 162 #if DCHECK_IS_ON() | |
| 163 void didAttachGlobalProxy() { m_isGlobalProxyAttached = true; } | |
| 164 void didDetachGlobalProxy() { m_isGlobalProxyAttached = false; } | |
| 165 #endif | |
| 166 | 97 |
| 167 private: | 98 private: |
| 168 v8::Isolate* const m_isolate; | 99 v8::Isolate* const m_isolate; |
| 169 const Member<Frame> m_frame; | 100 const Member<Frame> m_frame; |
| 170 #if DCHECK_IS_ON() | |
| 171 bool m_isGlobalProxyAttached = false; | |
| 172 #endif | |
| 173 | 101 |
| 174 protected: | 102 protected: |
| 103 // TODO(dcheng): Move this to LocalWindowProxy once RemoteWindowProxy uses |
| 104 // remote contexts. |
| 105 RefPtr<ScriptState> m_scriptState; |
| 175 // TODO(dcheng): Consider making these private and using getters. | 106 // TODO(dcheng): Consider making these private and using getters. |
| 176 const RefPtr<DOMWrapperWorld> m_world; | 107 const RefPtr<DOMWrapperWorld> m_world; |
| 177 ScopedPersistent<v8::Object> m_globalProxy; | 108 ScopedPersistent<v8::Object> m_globalProxy; |
| 178 Lifecycle m_lifecycle; | 109 Lifecycle m_lifecycle; |
| 179 }; | 110 }; |
| 180 | 111 |
| 181 } // namespace blink | 112 } // namespace blink |
| 182 | 113 |
| 183 #endif // WindowProxy_h | 114 #endif // WindowProxy_h |
| OLD | NEW |