Web Inspector: $0 stops working after navigating to a different domain
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Nov 2015 00:19:26 +0000 (00:19 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Nov 2015 00:19:26 +0000 (00:19 +0000)
commit04dd8069c69383f832bd1e87dbfc05eecd6b28f5
treeca8f727b0570ba7df203eee9977cfb0104080b00
parentf1fa73fa0ad1f307a3f0c7bc29472dca8acbcae8
Web Inspector: $0 stops working after navigating to a different domain
https://bugs.webkit.org/show_bug.cgi?id=147962

Patch by Joseph Pecoraro <pecoraro@apple.com> on 2015-11-09
Reviewed by Brian Burg.

Source/JavaScriptCore:

Extract the per-GlobalObject cache of JSValue wrappers for
InjectedScriptHost objects to be reused by WebCore for its
CommandLineAPIHost objects injected into multiple contexts.

* CMakeLists.txt:
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
* JavaScriptCore.xcodeproj/project.pbxproj:
Add new files.

* inspector/PerGlobalObjectWrapperWorld.h:
* inspector/PerGlobalObjectWrapperWorld.cpp:
(Inspector::PerGlobalObjectWrapperWorld::getWrapper):
(Inspector::PerGlobalObjectWrapperWorld::addWrapper):
(Inspector::PerGlobalObjectWrapperWorld::clearAllWrappers):
Hold a bunch of per-global-object wrappers for an object
that will outlive the global object. This inspector does this
for host objects that it exposes into scripts it injects into
each execution context created by the page.

* inspector/InjectedScriptHost.cpp:
(Inspector::InjectedScriptHost::wrapper):
(Inspector::InjectedScriptHost::clearAllWrappers):
(Inspector::InjectedScriptHost::jsWrapper): Deleted.
(Inspector::clearWrapperFromValue): Deleted.
(Inspector::InjectedScriptHost::clearWrapper): Deleted.
Extract and simplify the Per-GlobalObject wrapping into a class.
Simplify object construction as well.

* inspector/InjectedScriptHost.h:
* inspector/InjectedScriptManager.cpp:
(Inspector::InjectedScriptManager::createInjectedScript):
(Inspector::InjectedScriptManager::discardInjectedScripts):
Make discarding virtual so subclasses may also discard injected scripts.

* inspector/JSInjectedScriptHost.cpp:
(Inspector::JSInjectedScriptHost::JSInjectedScriptHost):
(Inspector::JSInjectedScriptHost::releaseImpl): Deleted.
(Inspector::JSInjectedScriptHost::~JSInjectedScriptHost): Deleted.
(Inspector::toJS): Deleted.
(Inspector::toJSInjectedScriptHost): Deleted.
* inspector/JSInjectedScriptHost.h:
(Inspector::JSInjectedScriptHost::create):
(Inspector::JSInjectedScriptHost::impl):
Update this code originally copied from older generated bindings to
be more like new generated bindings and remove some now unused code.

Source/WebCore:

Test: http/tests/inspector/console/cross-domain-inspected-node-access.html

The inspector backend injects the CommandLineAPI Source with a
corresponding CommandLineAPIHost into each execution context
created by the page (main frame, sub frames, etc).

When creating the JSValue wrapper for the CommandLineAPIHost using
the generated toJS(...) DOM bindings, we were using the cached
CommandLineAPIHost wrapper values in the single DOMWrapperWorld shared
across all frames. This meant that the first time the wrapper was
needed it was created in context A. But when needed for context B
it was using the wrapper created in context A. Using this wrapper
in context B was producing unexpected cross-origin warnings.

The solution taken here, is to create a new JSValue wrapper for
the CommandLineAPIHost per execution context. This way each time
the CommandLineAPIHost wrapper is used in a frame, it is using
the one created for that frame.

The C++ host object being wrapped has a lifetime equivalent to
the Page. It does not change in this patch. The wrapper values
are cleared on page navigation or when the page is closed, and
will be garbage collected.

* WebCore.vcxproj/WebCore.vcxproj:
* WebCore.vcxproj/WebCore.vcxproj.filters:
* ForwardingHeaders/inspector/PerGlobalObjectWrapperWorld.h: Added.
New forwarding header.

* inspector/CommandLineAPIHost.h:
* inspector/CommandLineAPIHost.cpp:
(WebCore::CommandLineAPIHost::CommandLineAPIHost):
(WebCore::CommandLineAPIHost::wrapper):
Cached JSValue wrappers per GlobalObject.

(WebCore::CommandLineAPIHost::clearAllWrappers):
Clear any wrappers we have, including the $0 value itself
which we weren't explicitly clearing previously.

* inspector/CommandLineAPIModule.cpp:
(WebCore::CommandLineAPIModule::host):
Simplify creating the wrapper.

* inspector/WebInjectedScriptManager.h:
* inspector/WebInjectedScriptManager.cpp:
(WebCore::WebInjectedScriptManager::discardInjectedScripts):
When the main frame window object clears, also clear the
CommandLineAPI wrappers we may have created. Also take this
opportunity to clear any $0 value that may have pointed
to a value in the previous page.

LayoutTests:

* TestExpectations:
* http/tests/inspector/console/access-inspected-object-expected.txt: Removed.
* http/tests/inspector/console/access-inspected-object.html: Removed.
* http/tests/inspector/console/cross-domain-inspected-node-access-expected.txt: Added.
* http/tests/inspector/console/cross-domain-inspected-node-access.html: Added.
Rewrite the old test with the new testing infrastructure.
Test this particular case of cross origin CommandLineAPI usage ($0).

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@192186 268f45cc-cd09-0410-ab3c-d52691b4dbfc
28 files changed:
LayoutTests/ChangeLog
LayoutTests/TestExpectations
LayoutTests/http/tests/inspector/console/access-inspected-object-expected.txt [deleted file]
LayoutTests/http/tests/inspector/console/access-inspected-object.html [deleted file]
LayoutTests/http/tests/inspector/console/cross-domain-inspected-node-access-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/inspector/console/cross-domain-inspected-node-access.html [new file with mode: 0644]
Source/JavaScriptCore/CMakeLists.txt
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj
Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters
Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Source/JavaScriptCore/inspector/InjectedScriptHost.cpp
Source/JavaScriptCore/inspector/InjectedScriptHost.h
Source/JavaScriptCore/inspector/InjectedScriptManager.cpp
Source/JavaScriptCore/inspector/InjectedScriptManager.h
Source/JavaScriptCore/inspector/JSInjectedScriptHost.cpp
Source/JavaScriptCore/inspector/JSInjectedScriptHost.h
Source/JavaScriptCore/inspector/PerGlobalObjectWrapperWorld.cpp [new file with mode: 0644]
Source/JavaScriptCore/inspector/PerGlobalObjectWrapperWorld.h [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/ForwardingHeaders/inspector/PerGlobalObjectWrapperWorld.h [new file with mode: 0644]
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj
Source/WebCore/WebCore.vcxproj/WebCore.vcxproj.filters
Source/WebCore/inspector/CommandLineAPIHost.cpp
Source/WebCore/inspector/CommandLineAPIHost.h
Source/WebCore/inspector/CommandLineAPIModule.cpp
Source/WebCore/inspector/WebInjectedScriptManager.cpp
Source/WebCore/inspector/WebInjectedScriptManager.h