Changeset 258850 in webkit


Ignore:
Timestamp:
Mar 23, 2020 9:51:24 AM (4 years ago)
Author:
Chris Dumez
Message:

Port window.postMessage to the HTML event loop
https://bugs.webkit.org/show_bug.cgi?id=209359

Reviewed by Alex Christensen.

LayoutTests/imported/w3c:

Rebaseline test now that the logging ordering is slightly different.

  • web-platform-tests/html/browsers/origin/cross-origin-objects/cross-origin-objects-expected.txt:

Source/WebCore:

Port window.postMessage to the HTML event loop instead of using a 0-timer.

  • inspector/InspectorInstrumentation.cpp:

(WebCore::InspectorInstrumentation::willPostMessageImpl):
(WebCore::InspectorInstrumentation::didPostMessageImpl):
(WebCore::InspectorInstrumentation::didFailPostMessageImpl):
(WebCore::InspectorInstrumentation::willDispatchPostMessageImpl):
(WebCore::InspectorInstrumentation::didDispatchPostMessageImpl):

  • inspector/InspectorInstrumentation.h:

(WebCore::InspectorInstrumentation::willPostMessage):
(WebCore::InspectorInstrumentation::didPostMessage):
(WebCore::InspectorInstrumentation::didFailPostMessage):
(WebCore::InspectorInstrumentation::willDispatchPostMessage):
(WebCore::InspectorInstrumentation::didDispatchPostMessage):

  • inspector/agents/WebDebuggerAgent.cpp:

(WebCore::WebDebuggerAgent::willPostMessage):
(WebCore::WebDebuggerAgent::didPostMessage):
(WebCore::WebDebuggerAgent::didFailPostMessage):
(WebCore::WebDebuggerAgent::willDispatchPostMessage):
(WebCore::WebDebuggerAgent::didDispatchPostMessage):
(WebCore::WebDebuggerAgent::didClearAsyncStackTraceData):

  • inspector/agents/WebDebuggerAgent.h:
  • page/DOMWindow.cpp:

(WebCore::DOMWindow::postMessage):
(WebCore::PostMessageTimer::PostMessageTimer): Deleted.
(WebCore::PostMessageTimer::event): Deleted.
(WebCore::PostMessageTimer::targetOrigin const): Deleted.
(WebCore::PostMessageTimer::stackTrace const): Deleted.
(WebCore::DOMWindow::postMessageTimerFired): Deleted.

  • page/DOMWindow.h:

LayoutTests:

Update tests so that it does not run postMessage tests in parallel on the 2 different-origin iframes,
since the ordering would not be guaranteed and it would cause flakiness.

  • http/tests/security/postMessage/resources/recv.js:

(recv):

  • http/tests/security/postMessage/target-origin-expected.txt:
  • http/tests/security/postMessage/target-origin.html:
Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r258845 r258850  
     12020-03-23  Chris Dumez  <cdumez@apple.com>
     2
     3        Port window.postMessage to the HTML event loop
     4        https://bugs.webkit.org/show_bug.cgi?id=209359
     5
     6        Reviewed by Alex Christensen.
     7
     8        Update tests so that it does not run postMessage tests in parallel on the 2 different-origin iframes,
     9        since the ordering would not be guaranteed and it would cause flakiness.
     10
     11        * http/tests/security/postMessage/resources/recv.js:
     12        (recv):
     13        * http/tests/security/postMessage/target-origin-expected.txt:
     14        * http/tests/security/postMessage/target-origin.html:
     15
    1162020-03-23  Per Arne Vollan  <pvollan@apple.com>
    217
  • trunk/LayoutTests/http/tests/security/postMessage/resources/recv.js

    r120174 r258850  
    1010    log(msg);
    1111
     12    if (e.data.match(/data="runNextTest"/))
     13        runNextTest();
     14
    1215    if (e.data.match(/data="done"/) && window.testRunner)
    1316        testRunner.notifyDone();
  • trunk/LayoutTests/http/tests/security/postMessage/target-origin-expected.txt

    r253497 r258850  
    1818
    1919waiting...
    20 Error sending message to //. SyntaxError: The string did not match the expected pattern.
    2120Received message: data="Received message: data="Trying origin=http://localhost:8000" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
    2221Received message: data="Received message: data="Trying origin=http://localhost:8000/" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
     
    2625Received message: data="Received message: data="Trying origin=http://user:pass@localhost:8000/foo?bar#baz" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
    2726Received message: data="Received message: data="Trying origin=*" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
     27Received message: data="Received message: data="runNextTest" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
     28Error sending message to //. SyntaxError: The string did not match the expected pattern.
    2829Received message: data="Received message: data="Trying origin=/" origin="http://127.0.0.1:8000"" origin="http://127.0.0.1:8000"
    29 Received message: data="Received message: data="done" origin="http://127.0.0.1:8000"" origin="http://localhost:8000"
     30Received message: data="Received message: data="done" origin="http://127.0.0.1:8000"" origin="http://127.0.0.1:8000"
  • trunk/LayoutTests/http/tests/security/postMessage/target-origin.html

    r120174 r258850  
    1919}
    2020
     21function runNextTest() {
     22    var win127 = document.getElementById('iframe-127').contentWindow;
     23
     24    // Should succeed:
     25    tryPostMessage(win127, "/");
     26
     27    // Should throw syntax error:
     28    tryPostMessage(win127, "//");
     29
     30    win127.postMessage('done', '*');
     31}
     32
    2133function test() {
    2234    var winLocalhost = document.getElementById('iframe-localhost').contentWindow;
    23     var win127= document.getElementById('iframe-127').contentWindow;
    2435
    2536    // Should succeed:
     
    3142    tryPostMessage(winLocalhost, "http://user:pass@localhost:8000/foo?bar#baz");
    3243    tryPostMessage(winLocalhost, "*");
    33     tryPostMessage(win127, "/");
    3444
    3545    // Should fail:
     
    4454    tryPostMessage(winLocalhost, null);
    4555    tryPostMessage(winLocalhost, undefined);
    46     tryPostMessage(win127, "//");
    47 
    48     winLocalhost.postMessage('done', '*');
     56   
     57    winLocalhost.postMessage('runNextTest', '*');
    4958}
    5059</script>
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r258769 r258850  
     12020-03-23  Chris Dumez  <cdumez@apple.com>
     2
     3        Port window.postMessage to the HTML event loop
     4        https://bugs.webkit.org/show_bug.cgi?id=209359
     5
     6        Reviewed by Alex Christensen.
     7
     8        Rebaseline test now that the logging ordering is slightly different.
     9
     10        * web-platform-tests/html/browsers/origin/cross-origin-objects/cross-origin-objects-expected.txt:
     11
    1122020-03-20  youenn fablet  <youenn@apple.com>
    213
  • trunk/LayoutTests/imported/w3c/web-platform-tests/html/browsers/origin/cross-origin-objects/cross-origin-objects-expected.txt

    r253497 r258850  
    88CONSOLE MESSAGE: line 302: Blocked a frame with origin "http://localhost:8800" from accessing a frame with origin "http://127.0.0.1:8800". Protocols, domains, and ports must match.
    99CONSOLE MESSAGE: line 302: Blocked a frame with origin "http://localhost:8800" from accessing a frame with origin "http://127.0.0.1:8800". Protocols, domains, and ports must match.
     10CONSOLE MESSAGE: Unable to post message to http://does-not.exist. Recipient has origin http://localhost:8800.
     11
    1012CONSOLE MESSAGE: Unable to post message to http://does-not.exist. Recipient has origin http://127.0.0.1:8800.
    1113
    1214CONSOLE MESSAGE: Unable to post message to http://localhost:8800. Recipient has origin http://127.0.0.1:8800.
    13 
    14 CONSOLE MESSAGE: Unable to post message to http://does-not.exist. Recipient has origin http://localhost:8800.
    1515
    1616CONSOLE MESSAGE: Unable to post message to http://does-not.exist. Recipient has origin http://127.0.0.1:8800.
  • trunk/Source/WebCore/ChangeLog

    r258847 r258850  
     12020-03-23  Chris Dumez  <cdumez@apple.com>
     2
     3        Port window.postMessage to the HTML event loop
     4        https://bugs.webkit.org/show_bug.cgi?id=209359
     5
     6        Reviewed by Alex Christensen.
     7
     8        Port window.postMessage to the HTML event loop instead of using a 0-timer.
     9
     10        * inspector/InspectorInstrumentation.cpp:
     11        (WebCore::InspectorInstrumentation::willPostMessageImpl):
     12        (WebCore::InspectorInstrumentation::didPostMessageImpl):
     13        (WebCore::InspectorInstrumentation::didFailPostMessageImpl):
     14        (WebCore::InspectorInstrumentation::willDispatchPostMessageImpl):
     15        (WebCore::InspectorInstrumentation::didDispatchPostMessageImpl):
     16        * inspector/InspectorInstrumentation.h:
     17        (WebCore::InspectorInstrumentation::willPostMessage):
     18        (WebCore::InspectorInstrumentation::didPostMessage):
     19        (WebCore::InspectorInstrumentation::didFailPostMessage):
     20        (WebCore::InspectorInstrumentation::willDispatchPostMessage):
     21        (WebCore::InspectorInstrumentation::didDispatchPostMessage):
     22        * inspector/agents/WebDebuggerAgent.cpp:
     23        (WebCore::WebDebuggerAgent::willPostMessage):
     24        (WebCore::WebDebuggerAgent::didPostMessage):
     25        (WebCore::WebDebuggerAgent::didFailPostMessage):
     26        (WebCore::WebDebuggerAgent::willDispatchPostMessage):
     27        (WebCore::WebDebuggerAgent::didDispatchPostMessage):
     28        (WebCore::WebDebuggerAgent::didClearAsyncStackTraceData):
     29        * inspector/agents/WebDebuggerAgent.h:
     30        * page/DOMWindow.cpp:
     31        (WebCore::DOMWindow::postMessage):
     32        (WebCore::PostMessageTimer::PostMessageTimer): Deleted.
     33        (WebCore::PostMessageTimer::event): Deleted.
     34        (WebCore::PostMessageTimer::targetOrigin const): Deleted.
     35        (WebCore::PostMessageTimer::stackTrace const): Deleted.
     36        (WebCore::DOMWindow::postMessageTimerFired): Deleted.
     37        * page/DOMWindow.h:
     38
    1392020-03-23  Zalan Bujtas  <zalan@apple.com>
    240
  • trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp

    r255396 r258850  
    362362}
    363363
    364 void InspectorInstrumentation::didPostMessageImpl(InstrumentingAgents& instrumentingAgents, const TimerBase& timer, JSC::JSGlobalObject& state)
    365 {
    366     if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
    367         webDebuggerAgent->didPostMessage(timer, state);
    368 }
    369 
    370 void InspectorInstrumentation::didFailPostMessageImpl(InstrumentingAgents& instrumentingAgents, const TimerBase& timer)
    371 {
    372     if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
    373         webDebuggerAgent->didFailPostMessage(timer);
    374 }
    375 
    376 void InspectorInstrumentation::willDispatchPostMessageImpl(InstrumentingAgents& instrumentingAgents, const TimerBase& timer)
    377 {
    378     if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
    379         webDebuggerAgent->willDispatchPostMessage(timer);
    380 }
    381 
    382 void InspectorInstrumentation::didDispatchPostMessageImpl(InstrumentingAgents& instrumentingAgents, const TimerBase& timer)
    383 {
    384     if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
    385         webDebuggerAgent->didDispatchPostMessage(timer);
     364int InspectorInstrumentation::willPostMessageImpl(InstrumentingAgents& instrumentingAgents)
     365{
     366    if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
     367        return webDebuggerAgent->willPostMessage();
     368    return 0;
     369}
     370
     371void InspectorInstrumentation::didPostMessageImpl(InstrumentingAgents& instrumentingAgents, int postMessageIdentifier, JSC::JSGlobalObject& state)
     372{
     373    if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
     374        webDebuggerAgent->didPostMessage(postMessageIdentifier, state);
     375}
     376
     377void InspectorInstrumentation::didFailPostMessageImpl(InstrumentingAgents& instrumentingAgents, int postMessageIdentifier)
     378{
     379    if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
     380        webDebuggerAgent->didFailPostMessage(postMessageIdentifier);
     381}
     382
     383void InspectorInstrumentation::willDispatchPostMessageImpl(InstrumentingAgents& instrumentingAgents, int postMessageIdentifier)
     384{
     385    if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
     386        webDebuggerAgent->willDispatchPostMessage(postMessageIdentifier);
     387}
     388
     389void InspectorInstrumentation::didDispatchPostMessageImpl(InstrumentingAgents& instrumentingAgents, int postMessageIdentifier)
     390{
     391    if (auto* webDebuggerAgent = instrumentingAgents.webDebuggerAgent())
     392        webDebuggerAgent->didDispatchPostMessage(postMessageIdentifier);
    386393}
    387394
  • trunk/Source/WebCore/inspector/InspectorInstrumentation.h

    r255396 r258850  
    156156    static void didRemoveTimer(ScriptExecutionContext&, int timerId);
    157157
    158     static void didPostMessage(Frame&, TimerBase&, JSC::JSGlobalObject&);
    159     static void didFailPostMessage(Frame&, TimerBase&);
    160     static void willDispatchPostMessage(Frame&, TimerBase&);
    161     static void didDispatchPostMessage(Frame&, TimerBase&);
     158    static int willPostMessage(Frame&);
     159    static void didPostMessage(Frame&, int postTimerIdentifier, JSC::JSGlobalObject&);
     160    static void didFailPostMessage(Frame&, int postTimerIdentifier);
     161    static void willDispatchPostMessage(Frame&, int postTimerIdentifier);
     162    static void didDispatchPostMessage(Frame&, int postTimerIdentifier);
    162163
    163164    static void willCallFunction(ScriptExecutionContext*, const String& scriptName, int scriptLine, int scriptColumn);
     
    374375    static void didRemoveTimerImpl(InstrumentingAgents&, int timerId, ScriptExecutionContext&);
    375376
    376     static void didPostMessageImpl(InstrumentingAgents&, const TimerBase&, JSC::JSGlobalObject&);
    377     static void didFailPostMessageImpl(InstrumentingAgents&, const TimerBase&);
    378     static void willDispatchPostMessageImpl(InstrumentingAgents&, const TimerBase&);
    379     static void didDispatchPostMessageImpl(InstrumentingAgents&, const TimerBase&);
     377    static int willPostMessageImpl(InstrumentingAgents&);
     378    static void didPostMessageImpl(InstrumentingAgents&, int postMessageIdentifier, JSC::JSGlobalObject&);
     379    static void didFailPostMessageImpl(InstrumentingAgents&, int postMessageIdentifier);
     380    static void willDispatchPostMessageImpl(InstrumentingAgents&, int postMessageIdentifier);
     381    static void didDispatchPostMessageImpl(InstrumentingAgents&, int postMessageIdentifier);
    380382
    381383    static void willCallFunctionImpl(InstrumentingAgents&, const String& scriptName, int scriptLine, int scriptColumn, ScriptExecutionContext*);
     
    808810}
    809811
    810 inline void InspectorInstrumentation::didPostMessage(Frame& frame, TimerBase& timer, JSC::JSGlobalObject& state)
    811 {
    812     FAST_RETURN_IF_NO_FRONTENDS(void());
    813     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
    814         didPostMessageImpl(*instrumentingAgents, timer, state);
    815 }
    816 
    817 inline void InspectorInstrumentation::didFailPostMessage(Frame& frame, TimerBase& timer)
    818 {
    819     FAST_RETURN_IF_NO_FRONTENDS(void());
    820     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
    821         didFailPostMessageImpl(*instrumentingAgents, timer);
    822 }
    823 
    824 inline void InspectorInstrumentation::willDispatchPostMessage(Frame& frame, TimerBase& timer)
    825 {
    826     FAST_RETURN_IF_NO_FRONTENDS(void());
    827     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
    828         willDispatchPostMessageImpl(*instrumentingAgents, timer);
    829 }
    830 
    831 inline void InspectorInstrumentation::didDispatchPostMessage(Frame& frame, TimerBase& timer)
    832 {
    833     FAST_RETURN_IF_NO_FRONTENDS(void());
    834     if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
    835         didDispatchPostMessageImpl(*instrumentingAgents, timer);
     812inline int InspectorInstrumentation::willPostMessage(Frame& frame)
     813{
     814    FAST_RETURN_IF_NO_FRONTENDS(0);
     815    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
     816        return willPostMessageImpl(*instrumentingAgents);
     817    return 0;
     818}
     819
     820inline void InspectorInstrumentation::didPostMessage(Frame& frame, int postMessageIdentifier, JSC::JSGlobalObject& state)
     821{
     822    FAST_RETURN_IF_NO_FRONTENDS(void());
     823    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
     824        didPostMessageImpl(*instrumentingAgents, postMessageIdentifier, state);
     825}
     826
     827inline void InspectorInstrumentation::didFailPostMessage(Frame& frame, int postMessageIdentifier)
     828{
     829    FAST_RETURN_IF_NO_FRONTENDS(void());
     830    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
     831        didFailPostMessageImpl(*instrumentingAgents, postMessageIdentifier);
     832}
     833
     834inline void InspectorInstrumentation::willDispatchPostMessage(Frame& frame, int postMessageIdentifier)
     835{
     836    FAST_RETURN_IF_NO_FRONTENDS(void());
     837    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
     838        willDispatchPostMessageImpl(*instrumentingAgents, postMessageIdentifier);
     839}
     840
     841inline void InspectorInstrumentation::didDispatchPostMessage(Frame& frame, int postMessageIdentifier)
     842{
     843    FAST_RETURN_IF_NO_FRONTENDS(void());
     844    if (InstrumentingAgents* instrumentingAgents = instrumentingAgentsForFrame(frame))
     845        didDispatchPostMessageImpl(*instrumentingAgents, postMessageIdentifier);
    836846}
    837847
  • trunk/Source/WebCore/inspector/agents/WebDebuggerAgent.cpp

    r251425 r258850  
    113113}
    114114
    115 void WebDebuggerAgent::didPostMessage(const TimerBase& timer, JSC::JSGlobalObject& state)
     115int WebDebuggerAgent::willPostMessage()
     116{
     117    if (!breakpointsActive())
     118        return 0;
     119
     120    auto postMessageIdentifier = m_nextPostMessageIdentifier++;
     121    m_postMessageTasks.add(postMessageIdentifier);
     122    return postMessageIdentifier;
     123}
     124
     125void WebDebuggerAgent::didPostMessage(int postMessageIdentifier, JSC::JSGlobalObject& state)
    116126{
    117127    if (!breakpointsActive())
    118128        return;
    119129
    120     if (m_postMessageTimers.contains(&timer))
     130    if (!postMessageIdentifier || !m_postMessageTasks.contains(postMessageIdentifier))
    121131        return;
    122 
    123     int postMessageIdentifier = m_nextPostMessageIdentifier++;
    124     m_postMessageTimers.set(&timer, postMessageIdentifier);
    125132
    126133    didScheduleAsyncCall(&state, InspectorDebuggerAgent::AsyncCallType::PostMessage, postMessageIdentifier, true);
    127134}
    128135
    129 void WebDebuggerAgent::didFailPostMessage(const TimerBase& timer)
     136void WebDebuggerAgent::didFailPostMessage(int postMessageIdentifier)
    130137{
    131     auto it = m_postMessageTimers.find(&timer);
    132     if (it == m_postMessageTimers.end())
     138    if (!postMessageIdentifier)
    133139        return;
    134140
    135     didCancelAsyncCall(InspectorDebuggerAgent::AsyncCallType::PostMessage, it->value);
     141    auto it = m_postMessageTasks.find(postMessageIdentifier);
     142    if (it == m_postMessageTasks.end())
     143        return;
    136144
    137     m_postMessageTimers.remove(it);
     145    didCancelAsyncCall(InspectorDebuggerAgent::AsyncCallType::PostMessage, postMessageIdentifier);
     146
     147    m_postMessageTasks.remove(it);
    138148}
    139149
    140 void WebDebuggerAgent::willDispatchPostMessage(const TimerBase& timer)
     150void WebDebuggerAgent::willDispatchPostMessage(int postMessageIdentifier)
    141151{
    142     auto it = m_postMessageTimers.find(&timer);
    143     if (it == m_postMessageTimers.end())
     152    if (!postMessageIdentifier || !m_postMessageTasks.contains(postMessageIdentifier))
    144153        return;
    145154
    146     willDispatchAsyncCall(InspectorDebuggerAgent::AsyncCallType::PostMessage, it->value);
     155    willDispatchAsyncCall(InspectorDebuggerAgent::AsyncCallType::PostMessage, postMessageIdentifier);
    147156}
    148157
    149 void WebDebuggerAgent::didDispatchPostMessage(const TimerBase& timer)
     158void WebDebuggerAgent::didDispatchPostMessage(int postMessageIdentifier)
    150159{
    151     auto it = m_postMessageTimers.find(&timer);
    152     if (it == m_postMessageTimers.end())
     160    if (!postMessageIdentifier)
     161        return;
     162
     163    auto it = m_postMessageTasks.find(postMessageIdentifier);
     164    if (it == m_postMessageTasks.end())
    153165        return;
    154166
    155167    didDispatchAsyncCall();
    156168
    157     m_postMessageTimers.remove(it);
     169    m_postMessageTasks.remove(it);
    158170}
    159171
     
    161173{
    162174    m_registeredEventListeners.clear();
    163     m_postMessageTimers.clear();
     175    m_postMessageTasks.clear();
    164176    m_nextEventListenerIdentifier = 1;
    165177    m_nextPostMessageIdentifier = 1;
  • trunk/Source/WebCore/inspector/agents/WebDebuggerAgent.h

    r251425 r258850  
    4949    void willRemoveEventListener(EventTarget&, const AtomString& eventType, EventListener&, bool capture);
    5050    void willHandleEvent(const RegisteredEventListener&);
    51     void didPostMessage(const TimerBase&, JSC::JSGlobalObject&);
    52     void didFailPostMessage(const TimerBase&);
    53     void willDispatchPostMessage(const TimerBase&);
    54     void didDispatchPostMessage(const TimerBase&);
     51    int willPostMessage();
     52    void didPostMessage(int postMessageIdentifier, JSC::JSGlobalObject&);
     53    void didFailPostMessage(int postMessageIdentifier);
     54    void willDispatchPostMessage(int postMessageIdentifier);
     55    void didDispatchPostMessage(int postMessageIdentifier);
    5556
    5657protected:
     
    6566private:
    6667    HashMap<const RegisteredEventListener*, int> m_registeredEventListeners;
    67     HashMap<const TimerBase*, int> m_postMessageTimers;
     68    HashSet<int> m_postMessageTasks;
    6869    int m_nextEventListenerIdentifier { 1 };
    6970    int m_nextPostMessageIdentifier { 1 };
  • trunk/Source/WebCore/page/DOMWindow.cpp

    r257767 r258850  
    165165WTF_MAKE_ISO_ALLOCATED_IMPL(DOMWindow);
    166166
    167 class PostMessageTimer : public TimerBase {
    168 public:
    169     PostMessageTimer(DOMWindow& window, MessageWithMessagePorts&& message, const String& sourceOrigin, RefPtr<WindowProxy>&& source, RefPtr<SecurityOrigin>&& targetOrigin, RefPtr<ScriptCallStack>&& stackTrace)
    170         : m_window(window)
    171         , m_message(WTFMove(message))
    172         , m_origin(sourceOrigin)
    173         , m_source(source)
    174         , m_targetOrigin(WTFMove(targetOrigin))
    175         , m_stackTrace(stackTrace)
    176         , m_userGestureToForward(UserGestureIndicator::currentUserGesture())
    177     {
    178     }
    179 
    180     Ref<MessageEvent> event(ScriptExecutionContext& context)
    181     {
    182         return MessageEvent::create(MessagePort::entanglePorts(context, WTFMove(m_message.transferredPorts)), m_message.message.releaseNonNull(), m_origin, { }, m_source ? makeOptional(MessageEventSource(WTFMove(m_source))) : WTF::nullopt);
    183     }
    184 
    185     SecurityOrigin* targetOrigin() const { return m_targetOrigin.get(); }
    186     ScriptCallStack* stackTrace() const { return m_stackTrace.get(); }
    187 
    188 private:
    189     void fired() override
    190     {
    191         // This object gets deleted when std::unique_ptr falls out of scope..
    192         std::unique_ptr<PostMessageTimer> timer(this);
    193        
    194         UserGestureIndicator userGestureIndicator(m_userGestureToForward);
    195         m_window->postMessageTimerFired(*timer);
    196     }
    197 
    198     Ref<DOMWindow> m_window;
    199     MessageWithMessagePorts m_message;
    200     String m_origin;
    201     RefPtr<WindowProxy> m_source;
    202     RefPtr<SecurityOrigin> m_targetOrigin;
    203     RefPtr<ScriptCallStack> m_stackTrace;
    204     RefPtr<UserGestureToken> m_userGestureToForward;
    205 };
    206 
    207167typedef HashCountedSet<DOMWindow*> DOMWindowSet;
    208168
     
    931891        stackTrace = createScriptCallStack(JSExecState::currentState());
    932892
     893    auto postMessageIdentifier = InspectorInstrumentation::willPostMessage(*frame());
     894
    933895    MessageWithMessagePorts message { messageData.releaseReturnValue(), disentangledPorts.releaseReturnValue() };
    934896
    935897    // Schedule the message.
    936898    RefPtr<WindowProxy> incumbentWindowProxy = incumbentWindow.frame() ? &incumbentWindow.frame()->windowProxy() : nullptr;
    937     auto* timer = new PostMessageTimer(*this, WTFMove(message), sourceOrigin, WTFMove(incumbentWindowProxy), WTFMove(target), WTFMove(stackTrace));
    938     timer->startOneShot(0_s);
    939 
    940     InspectorInstrumentation::didPostMessage(*frame(), *timer, lexicalGlobalObject);
     899    auto userGestureToForward = UserGestureIndicator::currentUserGesture();
     900
     901    document()->eventLoop().queueTask(TaskSource::PostedMessageQueue, [this, protectedThis = makeRef(*this), message = WTFMove(message), incumbentWindowProxy = WTFMove(incumbentWindowProxy), sourceOrigin = WTFMove(sourceOrigin), userGestureToForward = WTFMove(userGestureToForward), postMessageIdentifier, stackTrace = WTFMove(stackTrace), targetOrigin = WTFMove(target)]() mutable {
     902        if (!isCurrentlyDisplayedInFrame())
     903            return;
     904
     905        Ref<Frame> frame = *this->frame();
     906        if (targetOrigin) {
     907            // Check target origin now since the target document may have changed since the timer was scheduled.
     908            if (!targetOrigin->isSameSchemeHostPort(document()->securityOrigin())) {
     909                if (auto* pageConsole = console()) {
     910                    String message = makeString("Unable to post message to ", targetOrigin->toString(), ". Recipient has origin ", document()->securityOrigin().toString(), ".\n");
     911                    if (stackTrace)
     912                        pageConsole->addMessage(MessageSource::Security, MessageLevel::Error, message, *stackTrace);
     913                    else
     914                        pageConsole->addMessage(MessageSource::Security, MessageLevel::Error, message);
     915                }
     916
     917                InspectorInstrumentation::didFailPostMessage(frame, postMessageIdentifier);
     918                return;
     919            }
     920        }
     921
     922        UserGestureIndicator userGestureIndicator(userGestureToForward);
     923        InspectorInstrumentation::willDispatchPostMessage(frame, postMessageIdentifier);
     924
     925        auto event = MessageEvent::create(MessagePort::entanglePorts(*document(), WTFMove(message.transferredPorts)), message.message.releaseNonNull(), sourceOrigin, { }, incumbentWindowProxy ? makeOptional(MessageEventSource(WTFMove(incumbentWindowProxy))) : WTF::nullopt);
     926        dispatchEvent(event);
     927
     928        InspectorInstrumentation::didDispatchPostMessage(frame, postMessageIdentifier);
     929    });
     930
     931    InspectorInstrumentation::didPostMessage(*frame(), postMessageIdentifier, lexicalGlobalObject);
    941932
    942933    return { };
    943 }
    944 
    945 void DOMWindow::postMessageTimerFired(PostMessageTimer& timer)
    946 {
    947     if (!document() || !isCurrentlyDisplayedInFrame())
    948         return;
    949 
    950     Ref<Frame> frame = *this->frame();
    951     if (auto* intendedTargetOrigin = timer.targetOrigin()) {
    952         // Check target origin now since the target document may have changed since the timer was scheduled.
    953         if (!intendedTargetOrigin->isSameSchemeHostPort(document()->securityOrigin())) {
    954             if (auto* pageConsole = console()) {
    955                 String message = makeString("Unable to post message to ", intendedTargetOrigin->toString(), ". Recipient has origin ", document()->securityOrigin().toString(), ".\n");
    956                 if (timer.stackTrace())
    957                     pageConsole->addMessage(MessageSource::Security, MessageLevel::Error, message, *timer.stackTrace());
    958                 else
    959                     pageConsole->addMessage(MessageSource::Security, MessageLevel::Error, message);
    960             }
    961 
    962             InspectorInstrumentation::didFailPostMessage(frame, timer);
    963             return;
    964         }
    965     }
    966 
    967     InspectorInstrumentation::willDispatchPostMessage(frame, timer);
    968 
    969     dispatchEvent(timer.event(*document()));
    970 
    971     InspectorInstrumentation::didDispatchPostMessage(frame, timer);
    972934}
    973935
  • trunk/Source/WebCore/page/DOMWindow.h

    r255668 r258850  
    7676class PageConsoleClient;
    7777class Performance;
    78 class PostMessageTimer;
    7978class RequestAnimationFrameCallback;
    8079class RequestIdleCallback;
     
    276275        return postMessage(globalObject, incumbentWindow, message, WindowPostMessageOptions { WTFMove(targetOrigin), WTFMove(transfer) });
    277276    }
    278 
    279     void postMessageTimerFired(PostMessageTimer&);
    280277
    281278    void languagesChanged();
Note: See TracChangeset for help on using the changeset viewer.