⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 192186 in webkit


Ignore:
Timestamp:
Nov 9, 2015, 4:19:26 PM (11 years ago)
Author:
commit-queue@webkit.org
Message:

Web Inspector: $0 stops working after navigating to a different domain
https://bugs.webkit.org/show_bug.cgi?id=147962

Patch by Joseph Pecoraro <Joseph Pecoraro> 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.

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).

Location:
trunk
Files:
3 added
2 deleted
21 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r192182 r192186  
     12015-11-09  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: $0 stops working after navigating to a different domain
     4        https://bugs.webkit.org/show_bug.cgi?id=147962
     5
     6        Reviewed by Brian Burg.
     7
     8        * TestExpectations:
     9        * http/tests/inspector/console/access-inspected-object-expected.txt: Removed.
     10        * http/tests/inspector/console/access-inspected-object.html: Removed.
     11        * http/tests/inspector/console/cross-domain-inspected-node-access-expected.txt: Added.
     12        * http/tests/inspector/console/cross-domain-inspected-node-access.html: Added.
     13        Rewrite the old test with the new testing infrastructure.
     14        Test this particular case of cross origin CommandLineAPI usage ($0).
     15
    1162015-11-09  Ryan Haddad  <ryanhaddad@apple.com>
    217
  • trunk/LayoutTests/TestExpectations

    r192132 r192186  
    121121
    122122# These tests will be rewritten, just skip them until that time.
    123 webkit.org/b/148036 http/tests/inspector/console/ [ Skip ]
    124123webkit.org/b/148036 http/tests/inspector/css/ [ Skip ]
    125124webkit.org/b/148036 http/tests/inspector/page/ [ Skip ]
  • trunk/Source/JavaScriptCore/CMakeLists.txt

    r192135 r192186  
    399399    inspector/JSJavaScriptCallFramePrototype.cpp
    400400    inspector/JavaScriptCallFrame.cpp
     401    inspector/PerGlobalObjectWrapperWorld.cpp
    401402    inspector/ScriptArguments.cpp
    402403    inspector/ScriptCallFrame.cpp
  • trunk/Source/JavaScriptCore/ChangeLog

    r192183 r192186  
     12015-11-09  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: $0 stops working after navigating to a different domain
     4        https://bugs.webkit.org/show_bug.cgi?id=147962
     5
     6        Reviewed by Brian Burg.
     7
     8        Extract the per-GlobalObject cache of JSValue wrappers for
     9        InjectedScriptHost objects to be reused by WebCore for its
     10        CommandLineAPIHost objects injected into multiple contexts.
     11
     12        * CMakeLists.txt:
     13        * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
     14        * JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
     15        * JavaScriptCore.xcodeproj/project.pbxproj:
     16        Add new files.
     17
     18        * inspector/PerGlobalObjectWrapperWorld.h:
     19        * inspector/PerGlobalObjectWrapperWorld.cpp:
     20        (Inspector::PerGlobalObjectWrapperWorld::getWrapper):
     21        (Inspector::PerGlobalObjectWrapperWorld::addWrapper):
     22        (Inspector::PerGlobalObjectWrapperWorld::clearAllWrappers):
     23        Hold a bunch of per-global-object wrappers for an object
     24        that will outlive the global object. This inspector does this
     25        for host objects that it exposes into scripts it injects into
     26        each execution context created by the page.
     27
     28        * inspector/InjectedScriptHost.cpp:
     29        (Inspector::InjectedScriptHost::wrapper):
     30        (Inspector::InjectedScriptHost::clearAllWrappers):
     31        (Inspector::InjectedScriptHost::jsWrapper): Deleted.
     32        (Inspector::clearWrapperFromValue): Deleted.
     33        (Inspector::InjectedScriptHost::clearWrapper): Deleted.
     34        Extract and simplify the Per-GlobalObject wrapping into a class.
     35        Simplify object construction as well.
     36
     37        * inspector/InjectedScriptHost.h:
     38        * inspector/InjectedScriptManager.cpp:
     39        (Inspector::InjectedScriptManager::createInjectedScript):
     40        (Inspector::InjectedScriptManager::discardInjectedScripts):
     41        Make discarding virtual so subclasses may also discard injected scripts.
     42
     43        * inspector/JSInjectedScriptHost.cpp:
     44        (Inspector::JSInjectedScriptHost::JSInjectedScriptHost):
     45        (Inspector::JSInjectedScriptHost::releaseImpl): Deleted.
     46        (Inspector::JSInjectedScriptHost::~JSInjectedScriptHost): Deleted.
     47        (Inspector::toJS): Deleted.
     48        (Inspector::toJSInjectedScriptHost): Deleted.
     49        * inspector/JSInjectedScriptHost.h:
     50        (Inspector::JSInjectedScriptHost::create):
     51        (Inspector::JSInjectedScriptHost::impl):
     52        Update this code originally copied from older generated bindings to
     53        be more like new generated bindings and remove some now unused code.
     54
    1552015-11-08  Filip Pizlo  <fpizlo@apple.com>
    256
  • trunk/Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj

    r191905 r192186  
    609609    <ClCompile Include="..\inspector\JSJavaScriptCallFramePrototype.cpp" />
    610610    <ClCompile Include="..\inspector\JavaScriptCallFrame.cpp" />
     611    <ClCompile Include="..\inspector\PerGlobalObjectWrapperWorld.cpp" />
    611612    <ClCompile Include="..\inspector\ScriptArguments.cpp" />
    612613    <ClCompile Include="..\inspector\ScriptCallFrame.cpp" />
     
    14191420    <ClInclude Include="..\inspector\JSJavaScriptCallFramePrototype.h" />
    14201421    <ClInclude Include="..\inspector\JavaScriptCallFrame.h" />
     1422    <ClInclude Include="..\inspector\PerGlobalObjectWrapperWorld.h" />
    14211423    <ClInclude Include="..\inspector\ScriptArguments.h" />
    14221424    <ClInclude Include="..\inspector\ScriptBreakpoint.h" />
  • trunk/Source/JavaScriptCore/JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters

    r191905 r192186  
    17321732      <Filter>inspector</Filter>
    17331733    </ClCompile>
     1734    <ClCompile Include="..\inspector\PerGlobalObjectWrapperWorld.cpp">
     1735      <Filter>inspector</Filter>
     1736    </ClCompile>
    17341737    <ClCompile Include="..\profiler\ProfilerJettisonReason.cpp">
    17351738      <Filter>profiler</Filter>
     
    43264329    </ClInclude>
    43274330    <ClInclude Include="..\inspector\JSJavaScriptCallFrame.h">
     4331      <Filter>inspector</Filter>
     4332    </ClInclude>
     4333    <ClInclude Include="..\inspector\PerGlobalObjectWrapperWorld.h">
    43284334      <Filter>inspector</Filter>
    43294335    </ClInclude>
  • trunk/Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj

    r192183 r192186  
    15231523                A59455931824744700CC3843 /* JSGlobalObjectDebuggable.h in Headers */ = {isa = PBXBuildFile; fileRef = A59455911824744700CC3843 /* JSGlobalObjectDebuggable.h */; };
    15241524                A5945595182479EB00CC3843 /* InspectorFrontendChannel.h in Headers */ = {isa = PBXBuildFile; fileRef = A5945594182479EB00CC3843 /* InspectorFrontendChannel.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1525                A5AB49DC1BEC8082007020FB /* PerGlobalObjectWrapperWorld.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5AB49DA1BEC8079007020FB /* PerGlobalObjectWrapperWorld.cpp */; };
     1526                A5AB49DD1BEC8086007020FB /* PerGlobalObjectWrapperWorld.h in Headers */ = {isa = PBXBuildFile; fileRef = A5AB49DB1BEC8079007020FB /* PerGlobalObjectWrapperWorld.h */; settings = {ATTRIBUTES = (Private, ); }; };
    15251527                A5B6A74D18C6DBA600F11E91 /* ConsoleClient.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A5B6A74C18C6DBA600F11E91 /* ConsoleClient.cpp */; };
    15261528                A5BA15E8182340B300A82E69 /* RemoteInspector.h in Headers */ = {isa = PBXBuildFile; fileRef = A5BA15E1182340B300A82E69 /* RemoteInspector.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    19431945                FE0D4A061AB8DD0A002F54BF /* ExecutionTimeLimitTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE0D4A041AB8DD0A002F54BF /* ExecutionTimeLimitTest.cpp */; };
    19441946                FE0D4A091ABA2437002F54BF /* GlobalContextWithFinalizerTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE0D4A071ABA2437002F54BF /* GlobalContextWithFinalizerTest.cpp */; };
    1945                 FE1220271BE7F58C0039E6F2 /* JITAddGenerator.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1220261BE7F5640039E6F2 /* JITAddGenerator.h */; settings = {ASSET_TAGS = (); }; };
    1946                 FE1220281BE7F5910039E6F2 /* JITAddGenerator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1220251BE7F5640039E6F2 /* JITAddGenerator.cpp */; settings = {ASSET_TAGS = (); }; };
     1947                FE1220271BE7F58C0039E6F2 /* JITAddGenerator.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1220261BE7F5640039E6F2 /* JITAddGenerator.h */; };
     1948                FE1220281BE7F5910039E6F2 /* JITAddGenerator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1220251BE7F5640039E6F2 /* JITAddGenerator.cpp */; };
    19471949                FE1C0FFD1B193E9800B53FCA /* Exception.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1C0FFC1B193E9800B53FCA /* Exception.h */; settings = {ATTRIBUTES = (Private, ); }; };
    19481950                FE1C0FFF1B194FD100B53FCA /* Exception.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1C0FFE1B194FD100B53FCA /* Exception.cpp */; };
     
    19561958                FE3913551B794F8A00EDAF71 /* LiveObjectData.h in Headers */ = {isa = PBXBuildFile; fileRef = FE3913511B794AC900EDAF71 /* LiveObjectData.h */; settings = {ATTRIBUTES = (Private, ); }; };
    19571959                FE3913561B794F8F00EDAF71 /* LiveObjectList.h in Headers */ = {isa = PBXBuildFile; fileRef = FE3913531B794AC900EDAF71 /* LiveObjectList.h */; settings = {ATTRIBUTES = (Private, ); }; };
    1958                 FE4238901BE18C3C00514737 /* JITSubGenerator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE42388F1BE18C1200514737 /* JITSubGenerator.cpp */; settings = {ASSET_TAGS = (); }; };
     1960                FE4238901BE18C3C00514737 /* JITSubGenerator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE42388F1BE18C1200514737 /* JITSubGenerator.cpp */; };
    19591961                FE4BFF2B1AD476E700088F87 /* FunctionOverrides.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE4BFF291AD476E700088F87 /* FunctionOverrides.cpp */; };
    19601962                FE4BFF2C1AD476E700088F87 /* FunctionOverrides.h in Headers */ = {isa = PBXBuildFile; fileRef = FE4BFF2A1AD476E700088F87 /* FunctionOverrides.h */; };
     
    36053607                A59455911824744700CC3843 /* JSGlobalObjectDebuggable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSGlobalObjectDebuggable.h; sourceTree = "<group>"; };
    36063608                A5945594182479EB00CC3843 /* InspectorFrontendChannel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InspectorFrontendChannel.h; sourceTree = "<group>"; };
     3609                A5AB49DA1BEC8079007020FB /* PerGlobalObjectWrapperWorld.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PerGlobalObjectWrapperWorld.cpp; sourceTree = "<group>"; };
     3610                A5AB49DB1BEC8079007020FB /* PerGlobalObjectWrapperWorld.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PerGlobalObjectWrapperWorld.h; sourceTree = "<group>"; };
    36073611                A5B6A74C18C6DBA600F11E91 /* ConsoleClient.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ConsoleClient.cpp; sourceTree = "<group>"; };
    36083612                A5BA15E1182340B300A82E69 /* RemoteInspector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RemoteInspector.h; sourceTree = "<group>"; };
     
    64306434                                A503FA17188E0FB000110F14 /* JSJavaScriptCallFramePrototype.cpp */,
    64316435                                A503FA18188E0FB000110F14 /* JSJavaScriptCallFramePrototype.h */,
     6436                                A5AB49DA1BEC8079007020FB /* PerGlobalObjectWrapperWorld.cpp */,
     6437                                A5AB49DB1BEC8079007020FB /* PerGlobalObjectWrapperWorld.h */,
    64326438                                A5FD0065189AFE9C00633231 /* ScriptArguments.cpp */,
    64336439                                A5FD0066189AFE9C00633231 /* ScriptArguments.h */,
     
    72327238                                A5EA70EE19F5B5C40098F5EC /* JSContextRefInspectorSupport.h in Headers */,
    72337239                                A5D2E665195E174000A518E7 /* JSContextRefInternal.h in Headers */,
     7240                                A5AB49DD1BEC8086007020FB /* PerGlobalObjectWrapperWorld.h in Headers */,
    72347241                                148CD1D8108CF902008163C6 /* JSContextRefPrivate.h in Headers */,
    72357242                                A72028B81797601E0098028C /* JSCTestRunnerUtils.h in Headers */,
     
    87218728                                14469DDE107EC7E700650446 /* Lookup.cpp in Sources */,
    87228729                                0F4680CC14BBB17A00BFE272 /* LowLevelInterpreter.cpp in Sources */,
     8730                                A5AB49DC1BEC8082007020FB /* PerGlobalObjectWrapperWorld.cpp in Sources */,
    87238731                                14B723B212D7DA46003BD5ED /* MachineStackMarker.cpp in Sources */,
    87248732                                0FEB3ECF16237F6C00AB67AD /* MacroAssembler.cpp in Sources */,
  • trunk/Source/JavaScriptCore/inspector/InjectedScriptHost.cpp

    r178820 r192186  
    2727#include "InjectedScriptHost.h"
    2828
     29#include "JSCInlines.h"
    2930#include "JSInjectedScriptHost.h"
    3031
     
    3738}
    3839
    39 JSValue InjectedScriptHost::jsWrapper(ExecState* exec, JSGlobalObject* globalObject)
     40JSValue InjectedScriptHost::wrapper(ExecState* exec, JSGlobalObject* globalObject)
    4041{
    41     auto key = std::make_pair(exec, globalObject);
    42     auto it = m_wrappers.find(key);
    43     if (it != m_wrappers.end())
    44         return it->value.get();
     42    JSValue value = m_wrappers.getWrapper(globalObject);
     43    if (value)
     44        return value;
    4545
    46     JSValue jsValue = toJS(exec, globalObject, this);
    47     if (!jsValue.isObject())
    48         return jsValue;
     46    JSObject* prototype = JSInjectedScriptHost::createPrototype(exec->vm(), globalObject);
     47    Structure* structure = JSInjectedScriptHost::createStructure(exec->vm(), globalObject, prototype);
     48    JSInjectedScriptHost* injectedScriptHost = JSInjectedScriptHost::create(exec->vm(), structure, Ref<InjectedScriptHost>(*this));
     49    m_wrappers.addWrapper(globalObject, injectedScriptHost);
    4950
    50     JSObject* jsObject = jsValue.toObject(exec, globalObject);
    51     Strong<JSObject> wrapper(exec->vm(), jsObject);
    52     m_wrappers.add(key, wrapper);
    53 
    54     return jsValue;
    55 }
    56 
    57 static void clearWrapperFromValue(JSValue value)
    58 {
    59     JSInjectedScriptHost* jsInjectedScriptHost = toJSInjectedScriptHost(value);
    60     ASSERT(jsInjectedScriptHost);
    61     if (jsInjectedScriptHost)
    62         jsInjectedScriptHost->releaseImpl();
    63 }
    64 
    65 void InjectedScriptHost::clearWrapper(ExecState* exec, JSGlobalObject* globalObject)
    66 {
    67     auto key = std::make_pair(exec, globalObject);
    68     clearWrapperFromValue(m_wrappers.take(key).get());
     51    return injectedScriptHost;
    6952}
    7053
    7154void InjectedScriptHost::clearAllWrappers()
    7255{
    73     for (auto& wrapper : m_wrappers)
    74         clearWrapperFromValue(wrapper.value.get());
    75 
    76     m_wrappers.clear();
     56    m_wrappers.clearAllWrappers();
    7757}
    7858
  • trunk/Source/JavaScriptCore/inspector/InjectedScriptHost.h

    r185346 r192186  
    2828
    2929#include "JSCJSValueInlines.h"
    30 #include "Strong.h"
    31 #include "StrongInlines.h"
     30#include "inspector/PerGlobalObjectWrapperWorld.h"
    3231#include <wtf/HashMap.h>
    3332#include <wtf/RefCounted.h>
     
    4342    virtual bool isHTMLAllCollection(JSC::JSValue) { return false; }
    4443
    45     JSC::JSValue jsWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
    46     void clearWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
     44    JSC::JSValue wrapper(JSC::ExecState*, JSC::JSGlobalObject*);
    4745    void clearAllWrappers();
    4846
    4947private:
    50     HashMap<std::pair<JSC::ExecState*, JSC::JSGlobalObject*>, JSC::Strong<JSC::JSObject>> m_wrappers;
     48    PerGlobalObjectWrapperWorld m_wrappers;
    5149};
    5250
  • trunk/Source/JavaScriptCore/inspector/InjectedScriptManager.cpp

    r192064 r192186  
    6161}
    6262
     63void InjectedScriptManager::discardInjectedScripts()
     64{
     65    m_injectedScriptHost->clearAllWrappers();
     66    m_idToInjectedScript.clear();
     67    m_scriptStateToId.clear();
     68}
     69
    6370InjectedScriptHost* InjectedScriptManager::injectedScriptHost()
    6471{
     
    108115}
    109116
    110 void InjectedScriptManager::discardInjectedScripts()
    111 {
    112     m_injectedScriptHost->clearAllWrappers();
    113     m_idToInjectedScript.clear();
    114     m_scriptStateToId.clear();
    115 }
    116 
    117117void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
    118118{
     
    152152
    153153    MarkedArgumentBuffer args;
    154     args.append(m_injectedScriptHost->jsWrapper(scriptState, globalObject));
     154    args.append(m_injectedScriptHost->wrapper(scriptState, globalObject));
    155155    args.append(globalThisValue);
    156156    args.append(jsNumber(id));
  • trunk/Source/JavaScriptCore/inspector/InjectedScriptManager.h

    r192064 r192186  
    5555
    5656    virtual void disconnect();
     57    virtual void discardInjectedScripts();
    5758
    5859    InjectedScriptHost* injectedScriptHost();
     
    6364    int injectedScriptIdFor(JSC::ExecState*);
    6465    InjectedScript injectedScriptForObjectId(const String& objectId);
    65     void discardInjectedScripts();
    6666    void releaseObjectGroup(const String& objectGroup);
    6767    void clearExceptionValue();
  • trunk/Source/JavaScriptCore/inspector/JSInjectedScriptHost.cpp

    r187959 r192186  
    6161const ClassInfo JSInjectedScriptHost::s_info = { "InjectedScriptHost", &Base::s_info, 0, CREATE_METHOD_TABLE(JSInjectedScriptHost) };
    6262
    63 JSInjectedScriptHost::JSInjectedScriptHost(VM& vm, Structure* structure, PassRefPtr<InjectedScriptHost> impl)
     63JSInjectedScriptHost::JSInjectedScriptHost(VM& vm, Structure* structure, Ref<InjectedScriptHost>&& impl)
    6464    : JSDestructibleObject(vm, structure)
    65     , m_impl(impl.leakRef())
     65    , m_wrapped(WTF::move(impl))
    6666{
    6767}
     
    8282    JSInjectedScriptHost* thisObject = static_cast<JSInjectedScriptHost*>(cell);
    8383    thisObject->JSInjectedScriptHost::~JSInjectedScriptHost();
    84 }
    85 
    86 void JSInjectedScriptHost::releaseImpl()
    87 {
    88     if (auto impl = std::exchange(m_impl, nullptr))
    89         impl->deref();
    90 }
    91 
    92 JSInjectedScriptHost::~JSInjectedScriptHost()
    93 {
    94     releaseImpl();
    9584}
    9685
     
    485474}
    486475
    487 JSValue toJS(ExecState* exec, JSGlobalObject* globalObject, InjectedScriptHost* impl)
    488 {
    489     if (!impl)
    490         return jsNull();
    491 
    492     JSObject* prototype = JSInjectedScriptHost::createPrototype(exec->vm(), globalObject);
    493     Structure* structure = JSInjectedScriptHost::createStructure(exec->vm(), globalObject, prototype);
    494     JSInjectedScriptHost* injectedScriptHost = JSInjectedScriptHost::create(exec->vm(), structure, impl);
    495 
    496     return injectedScriptHost;
    497 }
    498 
    499 JSInjectedScriptHost* toJSInjectedScriptHost(JSValue value)
    500 {
    501     return value.inherits(JSInjectedScriptHost::info()) ? jsCast<JSInjectedScriptHost*>(value) : nullptr;
    502 }
    503 
    504476} // namespace Inspector
  • trunk/Source/JavaScriptCore/inspector/JSInjectedScriptHost.h

    r183025 r192186  
    2929#include "JSDestructibleObject.h"
    3030
    31 namespace JSC {
    32 class WeakMapData;
    33 }
    34 
    3531namespace Inspector {
    3632
     
    4945    }
    5046
    51     static JSInjectedScriptHost* create(JSC::VM& vm, JSC::Structure* structure, PassRefPtr<InjectedScriptHost> impl)
     47    static JSInjectedScriptHost* create(JSC::VM& vm, JSC::Structure* structure, Ref<InjectedScriptHost>&& impl)
    5248    {
    53         JSInjectedScriptHost* instance = new (NotNull, JSC::allocateCell<JSInjectedScriptHost>(vm.heap)) JSInjectedScriptHost(vm, structure, impl);
     49        JSInjectedScriptHost* instance = new (NotNull, JSC::allocateCell<JSInjectedScriptHost>(vm.heap)) JSInjectedScriptHost(vm, structure, WTF::move(impl));
    5450        instance->finishCreation(vm);
    5551        return instance;
     
    5955    static void destroy(JSC::JSCell*);
    6056
    61     InjectedScriptHost& impl() const { return *m_impl; }
    62     void releaseImpl();
     57    InjectedScriptHost& impl() const { return const_cast<InjectedScriptHost&>(m_wrapped.get()); }
    6358
    6459    // Attributes.
     
    8176
    8277private:
    83     JSInjectedScriptHost(JSC::VM&, JSC::Structure*, PassRefPtr<InjectedScriptHost>);
    84     ~JSInjectedScriptHost();
     78    JSInjectedScriptHost(JSC::VM&, JSC::Structure*, Ref<InjectedScriptHost>&&);
    8579
    86     InjectedScriptHost* m_impl;
     80    Ref<InjectedScriptHost> m_wrapped;
    8781};
    88 
    89 JSC::JSValue toJS(JSC::ExecState*, JSC::JSGlobalObject*, InjectedScriptHost*);
    90 JSInjectedScriptHost* toJSInjectedScriptHost(JSC::JSValue);
    9182
    9283} // namespace Inspector
  • trunk/Source/JavaScriptCore/inspector/PerGlobalObjectWrapperWorld.cpp

    r192185 r192186  
    2424 */
    2525
    26 #ifndef InjectedScriptHost_h
    27 #define InjectedScriptHost_h
     26#include "config.h"
     27#include "PerGlobalObjectWrapperWorld.h"
    2828
    29 #include "JSCJSValueInlines.h"
    30 #include "Strong.h"
    31 #include "StrongInlines.h"
    32 #include <wtf/HashMap.h>
    33 #include <wtf/RefCounted.h>
     29using namespace JSC;
    3430
    3531namespace Inspector {
    3632
    37 class JS_EXPORT_PRIVATE InjectedScriptHost : public RefCounted<InjectedScriptHost> {
    38 public:
    39     static Ref<InjectedScriptHost> create() { return adoptRef(*new InjectedScriptHost); }
    40     virtual ~InjectedScriptHost();
     33JSValue PerGlobalObjectWrapperWorld::getWrapper(JSGlobalObject* globalObject)
     34{
     35    auto it = m_wrappers.find(globalObject);
     36    if (it != m_wrappers.end())
     37        return it->value.get();
     38    return JSValue();
     39}
    4140
    42     virtual JSC::JSValue subtype(JSC::ExecState*, JSC::JSValue) { return JSC::jsUndefined(); }
    43     virtual bool isHTMLAllCollection(JSC::JSValue) { return false; }
     41void PerGlobalObjectWrapperWorld::addWrapper(JSGlobalObject* globalObject, JSObject* object)
     42{
     43    Strong<JSObject> wrapper(globalObject->vm(), object);
     44    m_wrappers.add(globalObject, wrapper);
     45}
    4446
    45     JSC::JSValue jsWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
    46     void clearWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
    47     void clearAllWrappers();
    48 
    49 private:
    50     HashMap<std::pair<JSC::ExecState*, JSC::JSGlobalObject*>, JSC::Strong<JSC::JSObject>> m_wrappers;
    51 };
     47void PerGlobalObjectWrapperWorld::clearAllWrappers()
     48{
     49    m_wrappers.clear();
     50}
    5251
    5352} // namespace Inspector
    54 
    55 #endif // !defined(InjectedScriptHost_h)
  • trunk/Source/JavaScriptCore/inspector/PerGlobalObjectWrapperWorld.h

    r192185 r192186  
    11/*
    2  * Copyright (C) 2013 Apple Inc. All rights reserved.
     2 * Copyright (C) 2015 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2424 */
    2525
    26 #ifndef InjectedScriptHost_h
    27 #define InjectedScriptHost_h
     26#ifndef PerGlobalObjectWrapperWorld_h
     27#define PerGlobalObjectWrapperWorld_h
    2828
    2929#include "JSCJSValueInlines.h"
     
    3131#include "StrongInlines.h"
    3232#include <wtf/HashMap.h>
    33 #include <wtf/RefCounted.h>
    3433
    3534namespace Inspector {
    3635
    37 class JS_EXPORT_PRIVATE InjectedScriptHost : public RefCounted<InjectedScriptHost> {
     36class JS_EXPORT_PRIVATE PerGlobalObjectWrapperWorld {
    3837public:
    39     static Ref<InjectedScriptHost> create() { return adoptRef(*new InjectedScriptHost); }
    40     virtual ~InjectedScriptHost();
    41 
    42     virtual JSC::JSValue subtype(JSC::ExecState*, JSC::JSValue) { return JSC::jsUndefined(); }
    43     virtual bool isHTMLAllCollection(JSC::JSValue) { return false; }
    44 
    45     JSC::JSValue jsWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
    46     void clearWrapper(JSC::ExecState*, JSC::JSGlobalObject*);
     38    JSC::JSValue getWrapper(JSC::JSGlobalObject*);
     39    void addWrapper(JSC::JSGlobalObject*, JSC::JSObject*);
    4740    void clearAllWrappers();
    4841
    4942private:
    50     HashMap<std::pair<JSC::ExecState*, JSC::JSGlobalObject*>, JSC::Strong<JSC::JSObject>> m_wrappers;
     43    HashMap<JSC::JSGlobalObject*, JSC::Strong<JSC::JSObject>> m_wrappers;
    5144};
    5245
    5346} // namespace Inspector
    5447
    55 #endif // !defined(InjectedScriptHost_h)
     48#endif // !defined(PerGlobalObjectWrapperWorld_h)
  • trunk/Source/WebCore/ChangeLog

    r192176 r192186  
     12015-11-09  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: $0 stops working after navigating to a different domain
     4        https://bugs.webkit.org/show_bug.cgi?id=147962
     5
     6        Reviewed by Brian Burg.
     7
     8        Test: http/tests/inspector/console/cross-domain-inspected-node-access.html
     9
     10        The inspector backend injects the CommandLineAPI Source with a
     11        corresponding CommandLineAPIHost into each execution context
     12        created by the page (main frame, sub frames, etc).
     13
     14        When creating the JSValue wrapper for the CommandLineAPIHost using
     15        the generated toJS(...) DOM bindings, we were using the cached
     16        CommandLineAPIHost wrapper values in the single DOMWrapperWorld shared
     17        across all frames. This meant that the first time the wrapper was
     18        needed it was created in context A. But when needed for context B
     19        it was using the wrapper created in context A. Using this wrapper
     20        in context B was producing unexpected cross-origin warnings.
     21
     22        The solution taken here, is to create a new JSValue wrapper for
     23        the CommandLineAPIHost per execution context. This way each time
     24        the CommandLineAPIHost wrapper is used in a frame, it is using
     25        the one created for that frame.
     26
     27        The C++ host object being wrapped has a lifetime equivalent to
     28        the Page. It does not change in this patch. The wrapper values
     29        are cleared on page navigation or when the page is closed, and
     30        will be garbage collected.
     31
     32        * WebCore.vcxproj/WebCore.vcxproj:
     33        * WebCore.vcxproj/WebCore.vcxproj.filters:
     34        * ForwardingHeaders/inspector/PerGlobalObjectWrapperWorld.h: Added.
     35        New forwarding header.
     36
     37        * inspector/CommandLineAPIHost.h:
     38        * inspector/CommandLineAPIHost.cpp:
     39        (WebCore::CommandLineAPIHost::CommandLineAPIHost):
     40        (WebCore::CommandLineAPIHost::wrapper):
     41        Cached JSValue wrappers per GlobalObject.
     42
     43        (WebCore::CommandLineAPIHost::clearAllWrappers):
     44        Clear any wrappers we have, including the $0 value itself
     45        which we weren't explicitly clearing previously.
     46
     47        * inspector/CommandLineAPIModule.cpp:
     48        (WebCore::CommandLineAPIModule::host):
     49        Simplify creating the wrapper.
     50
     51        * inspector/WebInjectedScriptManager.h:
     52        * inspector/WebInjectedScriptManager.cpp:
     53        (WebCore::WebInjectedScriptManager::discardInjectedScripts):
     54        When the main frame window object clears, also clear the
     55        CommandLineAPI wrappers we may have created. Also take this
     56        opportunity to clear any $0 value that may have pointed
     57        to a value in the previous page.
     58
    1592015-11-09  Per Arne Vollan  <peavo@outlook.com>
    260
  • trunk/Source/WebCore/WebCore.vcxproj/WebCore.vcxproj

    r192017 r192186  
    2043620436    <ClInclude Include="..\ForwardingHeaders\inspector\InspectorProtocolTypes.h" />
    2043720437    <ClInclude Include="..\ForwardingHeaders\inspector\InspectorValues.h" />
     20438    <ClInclude Include="..\ForwardingHeaders\inspector\PerGlobalObjectWrapperWorld.h" />
    2043820439    <ClInclude Include="..\ForwardingHeaders\inspector\ScriptArguments.h" />
    2043920440    <ClInclude Include="..\ForwardingHeaders\inspector\ScriptBreakpoint.h" />
  • trunk/Source/WebCore/WebCore.vcxproj/WebCore.vcxproj.filters

    r192017 r192186  
    1178311783      <Filter>ForwardingHeaders\inspector</Filter>
    1178411784    </ClInclude>
     11785    <ClInclude Include="..\ForwardingHeaders\inspector\PerGlobalObjectWrapperWorld.h">
     11786      <Filter>ForwardingHeaders\inspector</Filter>
     11787    </ClInclude>
    1178511788    <ClInclude Include="..\ForwardingHeaders\inspector\ScriptArguments.h">
    1178611789      <Filter>ForwardingHeaders\inspector</Filter>
  • trunk/Source/WebCore/inspector/CommandLineAPIHost.cpp

    r189964 r192186  
    3333
    3434#include "Database.h"
    35 #include "Element.h"
    36 #include "Frame.h"
    37 #include "FrameLoader.h"
    38 #include "HTMLFrameOwnerElement.h"
    3935#include "InspectorDOMAgent.h"
    4036#include "InspectorDOMStorageAgent.h"
    4137#include "InspectorDatabaseAgent.h"
    42 #include <inspector/InspectorFrontendDispatchers.h>
     38#include "JSCommandLineAPIHost.h"
     39#include "JSDOMGlobalObject.h"
    4340#include "Pasteboard.h"
    4441#include "Storage.h"
    45 #include "markup.h"
    4642#include <bindings/ScriptValue.h>
    4743#include <inspector/InspectorValues.h>
    4844#include <inspector/agents/InspectorAgent.h>
    4945#include <inspector/agents/InspectorConsoleAgent.h>
     46#include <runtime/JSCInlines.h>
    5047#include <wtf/RefPtr.h>
    5148#include <wtf/StdLibExtras.h>
    5249
     50using namespace JSC;
    5351using namespace Inspector;
    5452
     
    6159
    6260CommandLineAPIHost::CommandLineAPIHost()
    63     : m_inspectorAgent(nullptr)
    64     , m_consoleAgent(nullptr)
    65     , m_domAgent(nullptr)
    66     , m_domStorageAgent(nullptr)
    67     , m_databaseAgent(nullptr)
     61    : m_inspectedObject(std::make_unique<InspectableObject>())
    6862{
    69     m_inspectedObject = std::make_unique<InspectableObject>();
    7063}
    7164
     
    144137}
    145138
     139JSValue CommandLineAPIHost::wrapper(ExecState* exec, JSDOMGlobalObject* globalObject)
     140{
     141    JSValue value = m_wrappers.getWrapper(globalObject);
     142    if (value)
     143        return value;
     144
     145    JSObject* prototype = JSCommandLineAPIHost::createPrototype(exec->vm(), globalObject);
     146    Structure* structure = JSCommandLineAPIHost::createStructure(exec->vm(), globalObject, prototype);
     147    JSCommandLineAPIHost* commandLineAPIHost = JSCommandLineAPIHost::create(structure, globalObject, Ref<CommandLineAPIHost>(*this));
     148    m_wrappers.addWrapper(globalObject, commandLineAPIHost);
     149
     150    return commandLineAPIHost;
     151}
     152
     153void CommandLineAPIHost::clearAllWrappers()
     154{
     155    m_wrappers.clearAllWrappers();
     156    m_inspectedObject = std::make_unique<InspectableObject>();
     157}
     158
    146159} // namespace WebCore
  • trunk/Source/WebCore/inspector/CommandLineAPIHost.h

    r180715 r192186  
    3232
    3333#include "ScriptState.h"
    34 #include <runtime/ConsoleTypes.h>
     34#include <inspector/PerGlobalObjectWrapperWorld.h>
    3535#include <wtf/RefCounted.h>
    3636#include <wtf/Vector.h>
     
    3939namespace Deprecated {
    4040class ScriptValue;
     41}
     42
     43namespace JSC {
     44class JSValue;
    4145}
    4246
     
    5458class InspectorDOMStorageAgent;
    5559class InspectorDatabaseAgent;
     60class JSDOMGlobalObject;
    5661class Node;
    5762class Storage;
     
    98103    String storageIdImpl(Storage*);
    99104
     105    JSC::JSValue wrapper(JSC::ExecState*, JSDOMGlobalObject*);
     106    void clearAllWrappers();
     107
    100108private:
    101109    CommandLineAPIHost();
    102110
    103     Inspector::InspectorAgent* m_inspectorAgent;
    104     Inspector::InspectorConsoleAgent* m_consoleAgent;
    105     InspectorDOMAgent* m_domAgent;
    106     InspectorDOMStorageAgent* m_domStorageAgent;
    107     InspectorDatabaseAgent* m_databaseAgent;
     111    Inspector::InspectorAgent* m_inspectorAgent {nullptr};
     112    Inspector::InspectorConsoleAgent* m_consoleAgent {nullptr};
     113    InspectorDOMAgent* m_domAgent {nullptr};
     114    InspectorDOMStorageAgent* m_domStorageAgent {nullptr};
     115    InspectorDatabaseAgent* m_databaseAgent {nullptr};
    108116
    109117    std::unique_ptr<InspectableObject> m_inspectedObject; // $0
     118    Inspector::PerGlobalObjectWrapperWorld m_wrappers;
    110119};
    111120
  • trunk/Source/WebCore/inspector/CommandLineAPIModule.cpp

    r192064 r192186  
    2828
    2929#include "CommandLineAPIModuleSource.h"
    30 #include "DOMWrapperWorld.h"
    31 #include "JSCommandLineAPIHost.h"
     30#include "JSDOMGlobalObject.h"
    3231#include "WebInjectedScriptManager.h"
    3332#include <inspector/InjectedScript.h>
     
    5453}
    5554
    56 JSC::JSValue CommandLineAPIModule::host(InjectedScriptManager* injectedScriptManager, JSC::ExecState* exec) const
     55JSValue CommandLineAPIModule::host(InjectedScriptManager* injectedScriptManager, ExecState* exec) const
    5756{
    5857    // CommandLineAPIModule should only ever be used by a WebInjectedScriptManager.
    5958    WebInjectedScriptManager* pageInjectedScriptManager = static_cast<WebInjectedScriptManager*>(injectedScriptManager);
    6059    ASSERT(pageInjectedScriptManager->commandLineAPIHost());
     60
    6161    JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
    62     return toJS(exec, globalObject, pageInjectedScriptManager->commandLineAPIHost());
     62    return pageInjectedScriptManager->commandLineAPIHost()->wrapper(exec, globalObject);
    6363}
    6464
  • trunk/Source/WebCore/inspector/WebInjectedScriptManager.cpp

    r192064 r192186  
    4848}
    4949
     50void WebInjectedScriptManager::discardInjectedScripts()
     51{
     52    InjectedScriptManager::discardInjectedScripts();
     53
     54    m_commandLineAPIHost->clearAllWrappers();
     55}
     56
    5057void WebInjectedScriptManager::didCreateInjectedScript(const Inspector::InjectedScript& injectedScript)
    5158{
  • trunk/Source/WebCore/inspector/WebInjectedScriptManager.h

    r192064 r192186  
    4343
    4444    virtual void disconnect() override;
     45    virtual void discardInjectedScripts() override;
    4546
    4647    void discardInjectedScriptsFor(DOMWindow*);
Note: See TracChangeset for help on using the changeset viewer.