From 8fc2a37a307e5042353657141f0c751a302be9f0 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 17 Jun 2019 17:44:45 +1000 Subject: [PATCH] JavascriptCallback - Hack to work around upstream issue JavascriptCallback's are limited to only working in the main frame Frame ids aren't the same between processes, so makes it almost impossible to handle callbacks in sub frames https://bitbucket.org/chromiumembedded/cef/issues/2687/cefframe-getidentifier-differs-between https://github.com/cefsharp/CefSharp/issues/2743#issuecomment-502566136 This should be reverted after the problem has been resolved. --- .../JavascriptCallbackRegistry.cpp | 18 +++++++++++++-- .../Internals/JavascriptCallbackProxy.cpp | 23 ++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CefSharp.BrowserSubprocess.Core/JavascriptCallbackRegistry.cpp b/CefSharp.BrowserSubprocess.Core/JavascriptCallbackRegistry.cpp index e8fa646943..70feb6c871 100644 --- a/CefSharp.BrowserSubprocess.Core/JavascriptCallbackRegistry.cpp +++ b/CefSharp.BrowserSubprocess.Core/JavascriptCallbackRegistry.cpp @@ -20,7 +20,21 @@ namespace CefSharp auto result = gcnew JavascriptCallback(); result->Id = newId; result->BrowserId = _browserId; - result->FrameId = context->GetFrame()->GetIdentifier(); + result->FrameId = -1; + + auto frame = context->GetFrame(); + + if (frame.get() && frame->IsValid()) + { + //Issue https://bitbucket.org/chromiumembedded/cef/issues/2687/cefframe-getidentifier-differs-between + //prevents callbacks from working properly + //As a hack to get callbacks working we'll only return an Id for the main frame + //https://github.com/cefsharp/CefSharp/issues/2743#issuecomment-502566136 + if (frame->IsMain()) + { + result->FrameId = frame->GetIdentifier(); + } + } return result; } @@ -41,4 +55,4 @@ namespace CefSharp } } -} \ No newline at end of file +} diff --git a/CefSharp.Core/Internals/JavascriptCallbackProxy.cpp b/CefSharp.Core/Internals/JavascriptCallbackProxy.cpp index 40d4badb68..7f16dc1da2 100644 --- a/CefSharp.Core/Internals/JavascriptCallbackProxy.cpp +++ b/CefSharp.Core/Internals/JavascriptCallbackProxy.cpp @@ -41,7 +41,16 @@ namespace CefSharp } argList->SetList(2, paramList); - auto frame = browserWrapper->Browser->GetFrame(_callback->FrameId); + if (_callback->FrameId < 0) + { + auto invalidFrameResponse = gcnew JavascriptResponse(); + invalidFrameResponse->Success = false; + invalidFrameResponse->Message = "Javascript callbacks are only supported via the main frame until an upstream issue is resolved, see https://github.com/cefsharp/CefSharp/issues/2743#issuecomment-502566136"; + + return Task::FromResult(invalidFrameResponse); + } + + auto frame = browserWrapper->Browser->GetMainFrame(); if (frame.get() && frame->IsValid()) { @@ -55,7 +64,7 @@ namespace CefSharp invalidFrameResponse->Success = false; invalidFrameResponse->Message = "Frame with Id:" + _callback->FrameId + " is no longer valid."; - Task::FromResult(invalidFrameResponse); + return Task::FromResult(invalidFrameResponse); } } @@ -106,8 +115,16 @@ namespace CefSharp return false; } + //https://github.com/cefsharp/CefSharp/issues/2743#issuecomment-502566136 + if (_callback->FrameId < 0) + { + //We're only able to support callbacks in the main frame until the above issue + //is resolved. + return false; + } + //If the frame Id is still valid then we can attemp to execute the callback - auto frame = browser->GetFrame(_callback->FrameId); + auto frame = browser->MainFrame; if (frame == nullptr) { return false;