OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009, 2011 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 |
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(v8::Isolate* isolate, |
48 class HTMLDocument; | 78 RemoteFrame& frame, |
49 class SecurityOrigin; | 79 RefPtr<DOMWrapperWorld> world) |
| 80 : WindowProxy(isolate, frame, std::move(world)) {} |
50 | 81 |
51 // WindowProxy represents all the per-global object state for a Frame that | 82 void RemoteWindowProxy::disposeContext(GlobalDetachmentBehavior behavior) { |
52 // persist between navigations. | 83 if (m_lifecycle != Lifecycle::ContextInitialized) |
53 class WindowProxy final : public GarbageCollectedFinalized<WindowProxy> { | 84 return; |
54 public: | |
55 static WindowProxy* create(v8::Isolate*, Frame*, DOMWrapperWorld&); | |
56 | 85 |
57 ~WindowProxy(); | 86 WindowProxy::disposeContext(behavior); |
58 DECLARE_TRACE(); | 87 } |
59 | 88 |
60 v8::Local<v8::Context> contextIfInitialized() const { | 89 void RemoteWindowProxy::initialize() { |
61 return m_scriptState ? m_scriptState->context() : v8::Local<v8::Context>(); | 90 TRACE_EVENT1("v8", "RemoteWindowProxy::initialize", "isMainWindow", |
| 91 frame()->isMainFrame()); |
| 92 SCOPED_BLINK_UMA_HISTOGRAM_TIMER( |
| 93 frame()->isMainFrame() ? "Blink.Binding.InitializeMainWindowProxy" |
| 94 : "Blink.Binding.InitializeNonMainWindowProxy"); |
| 95 |
| 96 ScriptForbiddenScope::AllowUserAgentScript allowScript; |
| 97 |
| 98 v8::HandleScope handleScope(isolate()); |
| 99 |
| 100 createContext(); |
| 101 |
| 102 ScriptState::Scope scope(m_scriptState.get()); |
| 103 v8::Local<v8::Context> context = m_scriptState->context(); |
| 104 if (m_globalProxy.isEmpty()) { |
| 105 m_globalProxy.set(isolate(), context->Global()); |
| 106 CHECK(!m_globalProxy.isEmpty()); |
62 } | 107 } |
63 ScriptState* getScriptState() const { return m_scriptState.get(); } | |
64 | 108 |
65 // Update document object of the frame. | 109 setupWindowPrototypeChain(); |
66 void updateDocument(); | |
67 | 110 |
68 void namedItemAdded(HTMLDocument*, const AtomicString&); | 111 // Remote frames always require a full canAccess() check. |
69 void namedItemRemoved(HTMLDocument*, const AtomicString&); | 112 context->UseDefaultSecurityToken(); |
| 113 } |
70 | 114 |
71 // Update the security origin of a document | 115 void RemoteWindowProxy::createContext() { |
72 // (e.g., after setting docoument.domain). | 116 // Create a new v8::Context with the window object as the global object |
73 void updateSecurityOrigin(SecurityOrigin*); | 117 // (aka the inner global). Reuse the global proxy object (aka the outer |
| 118 // global) if it already exists. See the comments in |
| 119 // setupWindowPrototypeChain for the structure of the prototype chain of |
| 120 // the global object. |
| 121 v8::Local<v8::ObjectTemplate> globalTemplate = |
| 122 V8Window::domTemplate(isolate(), *m_world)->InstanceTemplate(); |
| 123 CHECK(!globalTemplate.IsEmpty()); |
74 | 124 |
75 void initializeIfNeeded(); | 125 v8::Local<v8::Context> context; |
| 126 { |
| 127 V8PerIsolateData::UseCounterDisabledScope useCounterDisabled( |
| 128 V8PerIsolateData::from(isolate())); |
| 129 context = v8::Context::New(isolate(), nullptr, globalTemplate, |
| 130 m_globalProxy.newLocal(isolate())); |
| 131 } |
| 132 CHECK(!context.IsEmpty()); |
76 | 133 |
77 void clearForNavigation(); | 134 m_scriptState = ScriptState::create(context, m_world); |
78 void clearForClose(); | |
79 | 135 |
80 v8::Local<v8::Object> globalIfNotDetached(); | 136 // TODO(haraken): Currently we cannot enable the following DCHECK because |
81 v8::Local<v8::Object> releaseGlobal(); | 137 // an already detached window proxy can be re-initialized. This is wrong. |
82 void setGlobal(v8::Local<v8::Object>); | 138 // DCHECK(m_lifecycle == Lifecycle::ContextUninitialized); |
83 | 139 m_lifecycle = Lifecycle::ContextInitialized; |
84 DOMWrapperWorld& world() { return *m_world; } | 140 DCHECK(m_scriptState->contextIsValid()); |
85 | 141 } |
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 | 142 |
129 } // namespace blink | 143 } // namespace blink |
130 | |
131 #endif // WindowProxy_h | |
OLD | NEW |