Chromium Code Reviews| 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 13 matching lines...) Expand all Loading... | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 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 <v8.h> | |
| 34 #include "bindings/core/v8/DOMWrapperWorld.h" | 35 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 35 #include "bindings/core/v8/ScopedPersistent.h" | 36 #include "bindings/core/v8/ScopedPersistent.h" |
| 36 #include "bindings/core/v8/ScriptState.h" | 37 #include "core/CoreExport.h" |
| 37 #include "platform/heap/Handle.h" | 38 #include "platform/heap/Handle.h" |
| 38 #include "wtf/RefPtr.h" | 39 #include "wtf/RefPtr.h" |
| 39 #include <v8.h> | |
| 40 | 40 |
| 41 namespace blink { | 41 namespace blink { |
| 42 | 42 |
| 43 class Frame; | 43 class Frame; |
| 44 class ScriptController; | |
| 45 | 44 |
| 46 // WindowProxy represents all the per-global object state for a Frame that | 45 // WindowProxy implements the split window model for a Frame. The split window |
| 47 // persist between navigations. | 46 // model consists of the inner global object and the outer global proxy. |
|
Yuki
2017/02/16 12:47:39
I'd like to have a note about which term in Blink
dcheng
2017/02/16 19:50:23
Done. I reworded a lot of this comment; hopefully,
| |
| 47 // | |
| 48 // ====== Inner Global Object ====== | |
| 49 // The inner global object contains the global for the script environment a | |
| 50 // Frame. In Blink, the inner global object has a 1:1 relationship with the | |
| 51 // DOMWindow implementation class. All methods and attributes defined on the | |
| 52 // Window interface are exposed via the inner global object. Variables defined | |
|
Yuki
2017/02/16 12:47:39
Variables defined by ???
dcheng
2017/02/16 19:50:23
Done =)
| |
| 53 // by | |
| 54 // | |
| 55 // The inner global object is not reused across navigations: each inner global | |
| 56 // object has an associated Document which does not change. On navigation, the | |
| 57 // new Document receives a new inner global object. | |
| 58 // | |
| 59 // There is one exception to the previous rule. If: | |
| 60 // - the previous Document is the initial empty document | |
| 61 // - the new Document is same-origin to the previous Document | |
| 62 // then the inner global object will be reused for the new Document. | |
| 63 // | |
| 64 // ====== Outer Global Proxy ==== | |
| 65 // The outer global proxy is reused across navigations. It implements the | |
| 66 // security checks for same-origin/cross-origin access to the Window interface. | |
|
Yuki
2017/02/16 12:47:39
Is this correct?
AccessCheckCallbackAndHandler (i
dcheng
2017/02/16 19:50:23
This is actually something I'm not sure about. Fro
| |
| 67 // When the check passes (i.e. the access is same-origin), the access is | |
| 68 // forwarded to the inner global object of the active Document in this | |
| 69 // WindowProxy's Frame). | |
| 70 // | |
| 71 // When the security check fails, the access is delegated to the outer global | |
| 72 // proxy's cross-origin interceptors. The cross-origin interceptors may choose | |
| 73 // to return a value (if the property is exposed cross-origin) or throw an | |
| 74 // exception otherwise. | |
| 75 // | |
| 76 // Note that the cross-origin interceptors are only used for cross-origin | |
| 77 // accesses: a same-origin access to a method that is available cross-origin, | |
| 78 // such as Window.postMessage, will be delegated to the inner global object. | |
| 79 // | |
| 80 // ====== LocalWindowProxy vs RemoteWindowProxy ====== | |
| 81 // In Blink, there is also the concept of a LocalWindowProxy and a | |
| 82 // RemoteWindowProxy. A LocalWindowProxy implements the split window model for a | |
| 83 // frame in the same process, while a RemoteWindowProxy implements the split | |
| 84 // window model for a frame in a different process. Note that while having a | |
| 85 // RemoteWindowProxy implies a cross-origin frame, the reverse is not true: a | |
| 86 // LocalWindowProxy could be same-origin or cross-origin. | |
|
Yuki
2017/02/16 12:47:39
I think that it's worth mentioning that we break t
dcheng
2017/02/16 19:50:23
Done.
| |
| 87 // | |
| 88 // ====== LocalWindowProxy ====== | |
| 89 // Since a LocalWindowProxy can represent a same-origin or cross-origin frame, | |
| 90 // the entire prototype chain must be available: | |
| 91 // | |
| 92 // outer global proxy | |
| 93 // -- has prototype --> inner global object | |
| 94 // -- has prototype --> Window.prototype | |
| 95 // -- has prototype --> WindowProperties [1] | |
| 96 // -- has prototype --> EventTarget.prototype | |
| 97 // -- has prototype --> Object.prototype | |
| 98 // -- has prototype --> null | |
| 99 // | |
| 100 // [1] WindowProperties is the named properties object of the Window interface. | |
| 101 // | |
| 102 // ====== RemoteWindowProxy ====== | |
| 103 // Since a RemoteWindowProxy only represents a cross-origin frame, it has a much | |
| 104 // simpler prototype chain. | |
| 105 // | |
| 106 // outer global proxy | |
| 107 // -- null | |
|
Yuki
2017/02/16 12:47:39
Is this really null? I thought it's an empty hand
dcheng
2017/02/16 19:50:23
From my printf debugging, I believe this is correc
Yuki
2017/02/17 06:49:31
Thanks for checking.
nit: you might want to make
dcheng
2017/02/17 07:16:52
Done.
| |
| 108 // | |
| 109 // Property access to get/set attributes and methods on the outer global proxy | |
| 110 // are redirected through the cross-origin interceptors, since any access will | |
| 111 // fail the security check, by definition. | |
| 112 // | |
| 113 // ====== Functions that use Window as the receiver object ====== | |
| 114 // Functions that use Window as the receiver object have to deal with an | |
| 115 // additional complication. Typically, v8 performs a check that the receiver | |
| 116 // object is compatible by walking the prototype chain and looking for a | |
| 117 // matching v8::ObjectTemplate. In this case, v8 will use Window's | |
| 118 // v8::ObjectTemplate to perform the receiver check. | |
| 119 // | |
| 120 // However, the outer global proxy's v8::ObjectTemplate is *not* the Window's | |
| 121 // v8::ObjectTemplate. Internally, v8 always creates a new v8::ObjectTemplate | |
| 122 // for each global proxy object (the proxy template) and sets the proxy | |
| 123 // template's constructor's prototype template to Window's v8::ObjectTemplate. | |
| 124 // | |
| 125 // For LocalWindowProxy, this is fine: the inner global object is still created | |
| 126 // using Window's v8::ObjectTemplate, so walking up the prototype chain will | |
| 127 // find a compatible receiver. | |
| 128 // | |
| 129 // However, RemoteWindowProxy doesn't have an inner global object, so the | |
| 130 // compatible receiver check will fail. To get around this, all method callbacks | |
| 131 // for the Window interface perform the receiver check in the Blink bindings | |
| 132 // using V8Window::hasInstance(). | |
| 133 // | |
| 134 // Note that this hack is only needed for method callbacks, since | |
| 135 // method/attribute getters/setters are handled by the cross-origin interceptor, | |
| 136 // which doesn't need to perform receiver checks. | |
|
Yuki
2017/02/16 12:47:39
Currently cross origin accessible attributes are i
dcheng
2017/02/16 19:50:23
This comment is attempting to document things as t
| |
| 137 // | |
| 138 // ====== References ====== | |
| 139 // https://wiki.mozilla.org/Gecko:SplitWindow | |
| 140 // https://whatwg.org/C/browsers.html#the-windowproxy-exotic-object | |
| 48 class WindowProxy : public GarbageCollectedFinalized<WindowProxy> { | 141 class WindowProxy : public GarbageCollectedFinalized<WindowProxy> { |
| 49 public: | 142 public: |
| 50 virtual ~WindowProxy(); | 143 virtual ~WindowProxy(); |
| 51 | 144 |
| 52 DECLARE_TRACE(); | 145 DECLARE_TRACE(); |
| 53 | 146 |
| 54 v8::Local<v8::Context> contextIfInitialized() const { | |
| 55 return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); | |
| 56 } | |
| 57 void initializeIfNeeded(); | 147 void initializeIfNeeded(); |
| 58 | 148 |
| 59 void clearForClose(); | 149 void clearForClose(); |
| 60 void clearForNavigation(); | 150 void clearForNavigation(); |
| 61 | 151 |
| 62 v8::Local<v8::Object> globalIfNotDetached(); | 152 CORE_EXPORT v8::Local<v8::Object> globalIfNotDetached(); |
| 63 v8::Local<v8::Object> releaseGlobal(); | 153 v8::Local<v8::Object> releaseGlobal(); |
| 64 void setGlobal(v8::Local<v8::Object>); | 154 void setGlobal(v8::Local<v8::Object>); |
| 65 | 155 |
| 66 // TODO(dcheng): Temporarily exposed to avoid include cycles. Remove the need | 156 // TODO(dcheng): Temporarily exposed to avoid include cycles. Remove the need |
| 67 // for this and remove this getter. | 157 // for this and remove this getter. |
| 68 DOMWrapperWorld& world() { return *m_world; } | 158 DOMWrapperWorld& world() { return *m_world; } |
| 69 | 159 |
| 70 protected: | 160 protected: |
| 71 // TODO(dcheng): Remove this friend declaration once LocalWindowProxyManager | |
| 72 // and ScriptController are merged. | |
| 73 friend class ScriptController; | |
| 74 | |
| 75 // A valid transition is from ContextUninitialized to ContextInitialized, | 161 // A valid transition is from ContextUninitialized to ContextInitialized, |
| 76 // and then ContextDetached. Other transitions are forbidden. | 162 // and then ContextDetached. Other transitions are forbidden. |
| 77 enum class Lifecycle { | 163 enum class Lifecycle { |
| 78 ContextUninitialized, | 164 ContextUninitialized, |
| 79 ContextInitialized, | 165 ContextInitialized, |
| 80 ContextDetached, | 166 ContextDetached, |
| 81 }; | 167 }; |
| 82 | 168 |
| 83 WindowProxy(v8::Isolate*, Frame&, RefPtr<DOMWrapperWorld>); | 169 WindowProxy(v8::Isolate*, Frame&, RefPtr<DOMWrapperWorld>); |
| 84 | 170 |
| 85 virtual void initialize() = 0; | 171 virtual void initialize() = 0; |
| 86 | 172 |
| 87 enum GlobalDetachmentBehavior { DoNotDetachGlobal, DetachGlobal }; | 173 enum GlobalDetachmentBehavior { DoNotDetachGlobal, DetachGlobal }; |
| 88 virtual void disposeContext(GlobalDetachmentBehavior); | 174 virtual void disposeContext(GlobalDetachmentBehavior) = 0; |
| 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(); | |
| 93 | 175 |
| 94 v8::Isolate* isolate() const { return m_isolate; } | 176 v8::Isolate* isolate() const { return m_isolate; } |
| 95 Frame* frame() const { return m_frame.get(); } | 177 Frame* frame() const { return m_frame.get(); } |
| 96 ScriptState* getScriptState() const { return m_scriptState.get(); } | 178 |
| 179 #if DCHECK_IS_ON() | |
| 180 void didAttachGlobalProxy() { m_isGlobalProxyAttached = true; } | |
| 181 void didDetachGlobalProxy() { m_isGlobalProxyAttached = false; } | |
| 182 #endif | |
| 97 | 183 |
| 98 private: | 184 private: |
| 99 v8::Isolate* const m_isolate; | 185 v8::Isolate* const m_isolate; |
| 100 const Member<Frame> m_frame; | 186 const Member<Frame> m_frame; |
| 187 #if DCHECK_IS_ON() | |
| 188 bool m_isGlobalProxyAttached = false; | |
| 189 #endif | |
| 101 | 190 |
| 102 protected: | 191 protected: |
| 103 // TODO(dcheng): Move this to LocalWindowProxy once RemoteWindowProxy uses | |
| 104 // remote contexts. | |
| 105 RefPtr<ScriptState> m_scriptState; | |
| 106 // TODO(dcheng): Consider making these private and using getters. | 192 // TODO(dcheng): Consider making these private and using getters. |
| 107 const RefPtr<DOMWrapperWorld> m_world; | 193 const RefPtr<DOMWrapperWorld> m_world; |
| 108 ScopedPersistent<v8::Object> m_globalProxy; | 194 ScopedPersistent<v8::Object> m_globalProxy; |
| 109 Lifecycle m_lifecycle; | 195 Lifecycle m_lifecycle; |
| 110 }; | 196 }; |
| 111 | 197 |
| 112 } // namespace blink | 198 } // namespace blink |
| 113 | 199 |
| 114 #endif // WindowProxy_h | 200 #endif // WindowProxy_h |
| OLD | NEW |