PageScriptDebugServer.cpp [plain text]
#include "config.h"
#include "PageScriptDebugServer.h"
#include "Document.h"
#include "EventLoop.h"
#include "FrameView.h"
#include "JSDOMWindowCustom.h"
#include "MainFrame.h"
#include "Page.h"
#include "PageGroup.h"
#include "PluginViewBase.h"
#include "ScriptController.h"
#include "Timer.h"
#include <runtime/JSLock.h>
#include <wtf/MainThread.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(IOS)
#include "JSDOMWindowBase.h"
#include "WebCoreThreadInternal.h"
#endif
using namespace JSC;
using namespace Inspector;
namespace WebCore {
PageScriptDebugServer::PageScriptDebugServer(Page& page)
: ScriptDebugServer(false)
, m_page(page)
{
}
void PageScriptDebugServer::addListener(ScriptDebugListener* listener)
{
if (!listener)
return;
bool wasEmpty = m_listeners.isEmpty();
m_listeners.add(listener);
if (wasEmpty) {
m_page.setDebugger(this);
recompileAllJSFunctions();
}
}
void PageScriptDebugServer::removeListener(ScriptDebugListener* listener, bool isBeingDestroyed)
{
if (!listener)
return;
m_listeners.remove(listener);
if (m_listeners.isEmpty()) {
m_page.setDebugger(nullptr);
if (!isBeingDestroyed)
recompileAllJSFunctions();
}
}
void PageScriptDebugServer::recompileAllJSFunctions()
{
JSLockHolder lock(JSDOMWindow::commonVM());
Debugger::recompileAllJSFunctions(&JSDOMWindow::commonVM());
}
void PageScriptDebugServer::didPause(JSGlobalObject*)
{
setJavaScriptPaused(m_page.group(), true);
}
void PageScriptDebugServer::didContinue(JSGlobalObject*)
{
setJavaScriptPaused(m_page.group(), false);
}
void PageScriptDebugServer::runEventLoopWhilePaused()
{
#if PLATFORM(IOS)
if (WebThreadIsEnabled()) {
ASSERT(WebThreadIsLockedOrDisabled());
JSC::JSLock::DropAllLocks dropAllLocks(WebCore::JSDOMWindowBase::commonVM());
WebRunLoopEnableNested();
runEventLoopWhilePausedInternal();
WebRunLoopDisableNested();
ASSERT(WebThreadIsLockedOrDisabled());
return;
}
#endif
runEventLoopWhilePausedInternal();
}
void PageScriptDebugServer::runEventLoopWhilePausedInternal()
{
TimerBase::fireTimersInNestedEventLoop();
EventLoop loop;
while (!m_doneProcessingDebuggerEvents && !loop.ended())
loop.cycle();
}
bool PageScriptDebugServer::isContentScript(ExecState* exec) const
{
return ¤tWorld(exec) != &mainThreadNormalWorld();
}
void PageScriptDebugServer::reportException(ExecState* exec, Exception* exception) const
{
WebCore::reportException(exec, exception);
}
void PageScriptDebugServer::setJavaScriptPaused(const PageGroup& pageGroup, bool paused)
{
setMainThreadCallbacksPaused(paused);
const HashSet<Page*>& pages = pageGroup.pages();
HashSet<Page*>::const_iterator end = pages.end();
for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it)
setJavaScriptPaused(*it, paused);
}
void PageScriptDebugServer::setJavaScriptPaused(Page* page, bool paused)
{
ASSERT_ARG(page, page);
page->setDefersLoading(paused);
for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext())
setJavaScriptPaused(frame, paused);
}
void PageScriptDebugServer::setJavaScriptPaused(Frame* frame, bool paused)
{
ASSERT_ARG(frame, frame);
if (!frame->script().canExecuteScripts(NotAboutToExecuteScript))
return;
frame->script().setPaused(paused);
Document* document = frame->document();
if (paused) {
document->suspendScriptedAnimationControllerCallbacks();
document->suspendActiveDOMObjects(ActiveDOMObject::JavaScriptDebuggerPaused);
} else {
document->resumeActiveDOMObjects(ActiveDOMObject::JavaScriptDebuggerPaused);
document->resumeScriptedAnimationControllerCallbacks();
}
setJavaScriptPaused(frame->view(), paused);
}
void PageScriptDebugServer::setJavaScriptPaused(FrameView* view, bool paused)
{
if (!view)
return;
for (auto& child : view->children()) {
if (!is<PluginViewBase>(*child))
continue;
downcast<PluginViewBase>(*child).setJavaScriptPaused(paused);
}
}
}