Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2011 Google Inc. All rights reserved. |
|
dcheng
2017/01/11 08:35:16
I was unable to get the diffbase on Rietveld to wo
| |
| 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 |
| 11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. | 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
| 17 * | 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 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 #include "bindings/core/v8/WindowProxy.h" |
| 32 #define WindowProxy_h | |
| 33 | 32 |
| 33 #include "bindings/core/v8/ConditionalFeatures.h" | |
| 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/ScriptController.h" |
| 36 #include "bindings/core/v8/ScriptState.h" | 36 #include "bindings/core/v8/ToV8.h" |
| 37 #include "bindings/core/v8/V8Binding.h" | |
| 38 #include "bindings/core/v8/V8DOMActivityLogger.h" | |
| 39 #include "bindings/core/v8/V8Document.h" | |
| 40 #include "bindings/core/v8/V8GCForContextDispose.h" | |
| 41 #include "bindings/core/v8/V8HTMLCollection.h" | |
| 42 #include "bindings/core/v8/V8HTMLDocument.h" | |
| 43 #include "bindings/core/v8/V8HiddenValue.h" | |
| 44 #include "bindings/core/v8/V8Initializer.h" | |
| 45 #include "bindings/core/v8/V8ObjectConstructor.h" | |
| 46 #include "bindings/core/v8/V8PagePopupControllerBinding.h" | |
| 47 #include "bindings/core/v8/V8PrivateProperty.h" | |
| 48 #include "bindings/core/v8/V8Window.h" | |
| 49 #include "core/frame/LocalFrame.h" | |
| 50 #include "core/frame/csp/ContentSecurityPolicy.h" | |
| 51 #include "core/html/DocumentNameCollection.h" | |
| 52 #include "core/html/HTMLCollection.h" | |
| 53 #include "core/html/HTMLIFrameElement.h" | |
| 54 #include "core/inspector/InspectorInstrumentation.h" | |
| 55 #include "core/inspector/MainThreadDebugger.h" | |
| 56 #include "core/loader/DocumentLoader.h" | |
| 57 #include "core/loader/FrameLoader.h" | |
| 58 #include "core/loader/FrameLoaderClient.h" | |
| 59 #include "core/origin_trials/OriginTrialContext.h" | |
| 60 #include "platform/Histogram.h" | |
| 61 #include "platform/RuntimeEnabledFeatures.h" | |
| 62 #include "platform/ScriptForbiddenScope.h" | |
| 37 #include "platform/heap/Handle.h" | 63 #include "platform/heap/Handle.h" |
| 64 #include "platform/instrumentation/tracing/TraceEvent.h" | |
| 38 #include "platform/weborigin/SecurityOrigin.h" | 65 #include "platform/weborigin/SecurityOrigin.h" |
| 39 #include "wtf/HashMap.h" | 66 #include "public/platform/Platform.h" |
| 40 #include "wtf/PassRefPtr.h" | 67 #include "wtf/Assertions.h" |
| 41 #include "wtf/RefPtr.h" | 68 #include "wtf/StringExtras.h" |
| 42 #include "wtf/text/AtomicString.h" | 69 #include "wtf/text/CString.h" |
| 70 #include <algorithm> | |
| 71 #include <utility> | |
| 72 #include <v8-debug.h> | |
| 43 #include <v8.h> | 73 #include <v8.h> |
| 44 | 74 |
| 45 namespace blink { | 75 namespace blink { |
| 46 | 76 |
| 47 class Frame; | 77 RemoteWindowProxy::~RemoteWindowProxy() { |
| 48 class HTMLDocument; | 78 // clearForClose() or clearForNavigation() must be invoked before destruction |
| 49 class SecurityOrigin; | 79 // starts. |
| 80 DCHECK(m_lifecycle != Lifecycle::ContextInitialized); | |
| 81 } | |
| 50 | 82 |
| 51 // WindowProxy represents all the per-global object state for a Frame that | 83 RemoteWindowProxy::RemoteWindowProxy(RemoteFrame& frame, |
| 52 // persist between navigations. | 84 v8::Isolate* isolate, |
| 53 class WindowProxy final : public GarbageCollectedFinalized<WindowProxy> { | 85 RefPtr<DOMWrapperWorld> world) |
| 54 public: | 86 : WindowProxy(frame, isolate, std::move(world)) {} |
| 55 static WindowProxy* create(v8::Isolate*, Frame*, DOMWrapperWorld&); | |
| 56 | 87 |
| 57 ~WindowProxy(); | 88 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { |
| 58 DECLARE_TRACE(); | 89 if (m_lifecycle != Lifecycle::ContextInitialized) |
| 90 return; | |
| 59 | 91 |
| 60 v8::Local<v8::Context> contextIfInitialized() const { | 92 WindowProxy::disposeContext(behavior); |
| 61 return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); | 93 } |
| 94 | |
| 95 void RemoteWindowProxy::initialize() { | |
| 96 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", | |
| 97 frame()->isMainFrame()); | |
| 98 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( | |
| 99 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" | |
| 100 : "Blink.Binding.InitializeNonMainWindowProxy"); | |
| 101 | |
| 102 ScriptForbiddenScope::AllowUserAgentScript allowScript; | |
| 103 | |
| 104 v8::HandleScope handleScope(isolate()); | |
| 105 | |
| 106 createContext(); | |
| 107 | |
| 108 ScriptState::Scope scope(m_scriptState.get()); | |
| 109 v8::Local<v8::Context> context = m_scriptState->context(); | |
| 110 if (m_globalProxy.isEmpty()) { | |
| 111 m_globalProxy.set(isolate(), context->Global()); | |
| 112 CHECK(!m_globalProxy.isEmpty()); | |
| 62 } | 113 } |
| 63 ScriptState* getScriptState() const { return m_scriptState.get(); } | |
| 64 | 114 |
| 65 // Update document object of the frame. | 115 setupWindowPrototypeChain(); |
| 66 void updateDocument(); | |
| 67 | 116 |
| 68 void namedItemAdded(HTMLDocument*, const AtomicString&); | 117 // Remote frames always require a full canAccess() check. |
|
dcheng
2017/01/11 08:35:16
Moving this here allows us to cleanup the logic in
| |
| 69 void namedItemRemoved(HTMLDocument*, const AtomicString&); | 118 context->UseDefaultSecurityToken(); |
| 119 } | |
| 70 | 120 |
| 71 // Update the security origin of a document | 121 void RemoteWindowProxy::createContext() { |
|
Yuki
2017/01/11 10:09:24
Can we unify the implementation between local and
dcheng
2017/01/11 10:35:28
I intentionally split this, because RemoteWindowPr
| |
| 72 // (e.g., after setting docoument.domain). | 122 // Create a new v8::Context with the window object as the global object |
| 73 void updateSecurityOrigin(SecurityOrigin*); | 123 // (aka the inner global). Reuse the global proxy object (aka the outer |
| 124 // global) if it already exists. See the comments in | |
| 125 // setupWindowPrototypeChain for the structure of the prototype chain of | |
| 126 // the global object. | |
| 127 v8::Local<v8::ObjectTemplate> globalTemplate = | |
| 128 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); | |
| 129 CHECK(!globalTemplate.IsEmpty()); | |
| 74 | 130 |
| 75 void initializeIfNeeded(); | 131 v8::Local<v8::Context> context; |
| 132 { | |
| 133 V8PerIsolateData::UseCounterDisabledScope useCounterDisabled( | |
| 134 V8PerIsolateData::from(isolate())); | |
| 135 context = v8::Context::New(isolate(), nullptr, globalTemplate, | |
| 136 m_globalProxy.newLocal(isolate())); | |
| 137 } | |
| 138 CHECK(!context.IsEmpty()); | |
| 76 | 139 |
| 77 void clearForNavigation(); | 140 m_scriptState = ScriptState::create(context, m_world); |
| 78 void clearForClose(); | |
| 79 | 141 |
| 80 v8::Local<v8::Object> globalIfNotDetached(); | 142 // TODO(haraken): Currently we cannot enable the following DCHECK because |
| 81 v8::Local<v8::Object> releaseGlobal(); | 143 // an already detached window proxy can be re-initialized. This is wrong. |
| 82 void setGlobal(v8::Local<v8::Object>); | 144 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); |
| 83 | 145 m_lifecycle = Lifecycle::ContextInitialized; |
| 84 DOMWrapperWorld& world() { return *m_world; } | 146 DCHECK(m_scriptState->contextIsValid()); |
| 85 | 147 } |
| 86 private: | |
| 87 // A valid transition is from ContextUninitialized to ContextInitialized, | |
| 88 // and then ContextDetached. Other transitions are forbidden. | |
| 89 enum class Lifecycle { | |
| 90 ContextUninitialized, | |
| 91 ContextInitialized, | |
| 92 ContextDetached, | |
| 93 }; | |
| 94 | |
| 95 WindowProxy(Frame*, PassRefPtr<DOMWrapperWorld>, v8::Isolate*); | |
| 96 void initialize(); | |
| 97 | |
| 98 enum GlobalDetachmentBehavior { DoNotDetachGlobal, DetachGlobal }; | |
| 99 void disposeContext(GlobalDetachmentBehavior); | |
| 100 | |
| 101 void setSecurityToken(SecurityOrigin*); | |
| 102 | |
| 103 // The JavaScript wrapper for the document object is cached on the global | |
| 104 // object for fast access. UpdateDocumentProperty sets the wrapper | |
| 105 // for the current document on the global object. | |
| 106 void updateDocumentProperty(); | |
| 107 | |
| 108 // Updates Activity Logger for the current context. | |
| 109 void updateActivityLogger(); | |
| 110 | |
| 111 // Creates a new v8::Context with the window wrapper object as the global | |
| 112 // object (aka the inner global). Note that the window wrapper and its | |
| 113 // prototype chain do not get fully initialized yet, e.g. the window | |
| 114 // wrapper is not yet associated with the native DOMWindow object. | |
| 115 void createContext(); | |
| 116 | |
| 117 // Associates the window wrapper and its prototype chain with the native | |
| 118 // DOMWindow object. Also does some more Window-specific initialization. | |
| 119 void setupWindowPrototypeChain(); | |
| 120 | |
| 121 Member<Frame> m_frame; | |
| 122 v8::Isolate* m_isolate; | |
| 123 RefPtr<ScriptState> m_scriptState; | |
| 124 RefPtr<DOMWrapperWorld> m_world; | |
| 125 ScopedPersistent<v8::Object> m_globalProxy; | |
| 126 Lifecycle m_lifecycle; | |
| 127 }; | |
| 128 | 148 |
| 129 } // namespace blink | 149 } // namespace blink |
| 130 | |
| 131 #endif // WindowProxy_h | |
| OLD | NEW |