InspectorFrontendClientLocal.cpp [plain text]
#include "config.h"
#include "InspectorFrontendClientLocal.h"
#include "Chrome.h"
#include "DOMWrapperWorld.h"
#include "Document.h"
#include "FloatRect.h"
#include "FrameLoadRequest.h"
#include "FrameLoader.h"
#include "FrameView.h"
#include "InspectorController.h"
#include "InspectorFrontendHost.h"
#include "InspectorPageAgent.h"
#include "MainFrame.h"
#include "Page.h"
#include "ScriptController.h"
#include "ScriptGlobalObject.h"
#include "ScriptState.h"
#include "Settings.h"
#include "Timer.h"
#include "UserGestureIndicator.h"
#include "WindowFeatures.h"
#include <bindings/ScriptValue.h>
#include <inspector/InspectorBackendDispatchers.h>
#include <wtf/Deque.h>
#include <wtf/text/CString.h>
using namespace Inspector;
namespace WebCore {
static const char* inspectorAttachedHeightSetting = "inspectorAttachedHeight";
static const unsigned defaultAttachedHeight = 300;
static const float minimumAttachedHeight = 250.0f;
static const float maximumAttachedHeightRatio = 0.75f;
static const float minimumAttachedWidth = 500.0f;
static const float minimumAttachedInspectedWidth = 320.0f;
class InspectorBackendDispatchTask : public RefCounted<InspectorBackendDispatchTask> {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<InspectorBackendDispatchTask> create(InspectorController* inspectedPageController)
{
return adoptRef(*new InspectorBackendDispatchTask(inspectedPageController));
}
void dispatch(const String& message)
{
ASSERT_ARG(message, !message.isEmpty());
m_messages.append(message);
if (!m_timer.isActive())
m_timer.startOneShot(0);
}
void reset()
{
m_messages.clear();
m_timer.stop();
m_inspectedPageController = nullptr;
}
void timerFired()
{
ASSERT(m_inspectedPageController);
Ref<InspectorBackendDispatchTask> protectedThis(*this);
if (!m_messages.isEmpty())
m_inspectedPageController->dispatchMessageFromFrontend(m_messages.takeFirst());
if (!m_messages.isEmpty() && m_inspectedPageController)
m_timer.startOneShot(0);
}
private:
InspectorBackendDispatchTask(InspectorController* inspectedPageController)
: m_inspectedPageController(inspectedPageController)
, m_timer(*this, &InspectorBackendDispatchTask::timerFired)
{
ASSERT_ARG(inspectedPageController, inspectedPageController);
}
InspectorController* m_inspectedPageController { nullptr };
Timer m_timer;
Deque<String> m_messages;
};
String InspectorFrontendClientLocal::Settings::getProperty(const String&)
{
return String();
}
void InspectorFrontendClientLocal::Settings::setProperty(const String&, const String&)
{
}
InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController* inspectedPageController, Page* frontendPage, std::unique_ptr<Settings> settings)
: m_inspectedPageController(inspectedPageController)
, m_frontendPage(frontendPage)
, m_settings(WTFMove(settings))
, m_dockSide(DockSide::Undocked)
, m_dispatchTask(InspectorBackendDispatchTask::create(inspectedPageController))
{
m_frontendPage->settings().setAllowFileAccessFromFileURLs(true);
}
InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
{
if (m_frontendHost)
m_frontendHost->disconnectClient();
m_frontendPage = nullptr;
m_inspectedPageController = nullptr;
m_dispatchTask->reset();
}
void InspectorFrontendClientLocal::windowObjectCleared()
{
if (m_frontendHost)
m_frontendHost->disconnectClient();
m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
ScriptGlobalObject::set(*execStateFromPage(debuggerWorld(), m_frontendPage), "InspectorFrontendHost", *m_frontendHost);
}
void InspectorFrontendClientLocal::frontendLoaded()
{
setDockingUnavailable(!canAttachWindow());
bringToFront();
m_frontendLoaded = true;
for (auto& evaluate : m_evaluateOnLoad)
evaluateOnLoad(evaluate);
m_evaluateOnLoad.clear();
}
void InspectorFrontendClientLocal::requestSetDockSide(DockSide dockSide)
{
if (dockSide == DockSide::Undocked) {
detachWindow();
setAttachedWindow(dockSide);
} else if (canAttachWindow()) {
attachWindow(dockSide);
setAttachedWindow(dockSide);
}
}
bool InspectorFrontendClientLocal::canAttachWindow()
{
bool isInspectorPage = m_inspectedPageController->inspectionLevel() > 0;
if (isInspectorPage)
return false;
if (m_dockSide != DockSide::Undocked)
return true;
unsigned inspectedPageHeight = m_inspectedPageController->inspectedPage().mainFrame().view()->visibleHeight();
unsigned inspectedPageWidth = m_inspectedPageController->inspectedPage().mainFrame().view()->visibleWidth();
unsigned maximumAttachedHeight = inspectedPageHeight * maximumAttachedHeightRatio;
return minimumAttachedHeight <= maximumAttachedHeight && minimumAttachedWidth <= inspectedPageWidth;
}
void InspectorFrontendClientLocal::setDockingUnavailable(bool unavailable)
{
evaluateOnLoad(String::format("[\"setDockingUnavailable\", %s]", unavailable ? "true" : "false"));
}
void InspectorFrontendClientLocal::changeAttachedWindowHeight(unsigned height)
{
unsigned totalHeight = m_frontendPage->mainFrame().view()->visibleHeight() + m_inspectedPageController->inspectedPage().mainFrame().view()->visibleHeight();
unsigned attachedHeight = constrainedAttachedWindowHeight(height, totalHeight);
m_settings->setProperty(inspectorAttachedHeightSetting, String::number(attachedHeight));
setAttachedWindowHeight(attachedHeight);
}
void InspectorFrontendClientLocal::changeAttachedWindowWidth(unsigned width)
{
unsigned totalWidth = m_frontendPage->mainFrame().view()->visibleWidth() + m_inspectedPageController->inspectedPage().mainFrame().view()->visibleWidth();
unsigned attachedWidth = constrainedAttachedWindowWidth(width, totalWidth);
setAttachedWindowWidth(attachedWidth);
}
void InspectorFrontendClientLocal::openInNewTab(const String& url)
{
UserGestureIndicator indicator(ProcessingUserGesture);
Frame& mainFrame = m_inspectedPageController->inspectedPage().mainFrame();
FrameLoadRequest request(mainFrame.document()->securityOrigin(), ResourceRequest(), "_blank", LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, ReplaceDocumentIfJavaScriptURL, ShouldOpenExternalURLsPolicy::ShouldNotAllow);
bool created;
WindowFeatures windowFeatures;
RefPtr<Frame> frame = WebCore::createWindow(mainFrame, mainFrame, request, windowFeatures, created);
if (!frame)
return;
frame->loader().setOpener(&mainFrame);
frame->page()->setOpenedByDOM();
ResourceRequest resourceRequest(frame->document()->completeURL(url));
FrameLoadRequest frameRequest(mainFrame.document()->securityOrigin(), resourceRequest, "_self", LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, ReplaceDocumentIfJavaScriptURL, ShouldOpenExternalURLsPolicy::ShouldNotAllow);
frame->loader().changeLocation(frameRequest);
}
void InspectorFrontendClientLocal::moveWindowBy(float x, float y)
{
FloatRect frameRect = m_frontendPage->chrome().windowRect();
frameRect.move(x, y);
m_frontendPage->chrome().setWindowRect(frameRect);
}
void InspectorFrontendClientLocal::setAttachedWindow(DockSide dockSide)
{
const char* side = "undocked";
switch (dockSide) {
case DockSide::Undocked:
side = "undocked";
break;
case DockSide::Right:
side = "right";
break;
case DockSide::Bottom:
side = "bottom";
break;
}
m_dockSide = dockSide;
evaluateOnLoad(String::format("[\"setDockSide\", \"%s\"]", side));
}
void InspectorFrontendClientLocal::restoreAttachedWindowHeight()
{
unsigned inspectedPageHeight = m_inspectedPageController->inspectedPage().mainFrame().view()->visibleHeight();
String value = m_settings->getProperty(inspectorAttachedHeightSetting);
unsigned preferredHeight = value.isEmpty() ? defaultAttachedHeight : value.toUInt();
setAttachedWindowHeight(constrainedAttachedWindowHeight(preferredHeight, inspectedPageHeight));
}
bool InspectorFrontendClientLocal::isDebuggingEnabled()
{
if (m_frontendLoaded)
return evaluateAsBoolean("[\"isDebuggingEnabled\"]");
return false;
}
void InspectorFrontendClientLocal::setDebuggingEnabled(bool enabled)
{
evaluateOnLoad(String::format("[\"setDebuggingEnabled\", %s]", enabled ? "true" : "false"));
}
bool InspectorFrontendClientLocal::isTimelineProfilingEnabled()
{
if (m_frontendLoaded)
return evaluateAsBoolean("[\"isTimelineProfilingEnabled\"]");
return false;
}
void InspectorFrontendClientLocal::setTimelineProfilingEnabled(bool enabled)
{
evaluateOnLoad(String::format("[\"setTimelineProfilingEnabled\", %s]", enabled ? "true" : "false"));
}
bool InspectorFrontendClientLocal::isProfilingJavaScript()
{
if (m_frontendLoaded)
return evaluateAsBoolean("[\"isProfilingJavaScript\"]");
return false;
}
void InspectorFrontendClientLocal::startProfilingJavaScript()
{
evaluateOnLoad("[\"startProfilingJavaScript\"]");
}
void InspectorFrontendClientLocal::stopProfilingJavaScript()
{
evaluateOnLoad("[\"stopProfilingJavaScript\"]");
}
void InspectorFrontendClientLocal::showConsole()
{
evaluateOnLoad("[\"showConsole\"]");
}
void InspectorFrontendClientLocal::showResources()
{
evaluateOnLoad("[\"showResources\"]");
}
void InspectorFrontendClientLocal::showMainResourceForFrame(Frame* frame)
{
String frameId = m_inspectedPageController->pageAgent()->frameId(frame);
evaluateOnLoad(String::format("[\"showMainResourceForFrame\", \"%s\"]", frameId.ascii().data()));
}
unsigned InspectorFrontendClientLocal::constrainedAttachedWindowHeight(unsigned preferredHeight, unsigned totalWindowHeight)
{
return roundf(std::max(minimumAttachedHeight, std::min<float>(preferredHeight, totalWindowHeight * maximumAttachedHeightRatio)));
}
unsigned InspectorFrontendClientLocal::constrainedAttachedWindowWidth(unsigned preferredWidth, unsigned totalWindowWidth)
{
return roundf(std::max(minimumAttachedWidth, std::min<float>(preferredWidth, totalWindowWidth - minimumAttachedInspectedWidth)));
}
void InspectorFrontendClientLocal::sendMessageToBackend(const String& message)
{
m_dispatchTask->dispatch(message);
}
bool InspectorFrontendClientLocal::isUnderTest()
{
return m_inspectedPageController->isUnderTest();
}
unsigned InspectorFrontendClientLocal::inspectionLevel() const
{
return m_inspectedPageController->inspectionLevel() + 1;
}
bool InspectorFrontendClientLocal::evaluateAsBoolean(const String& expression)
{
auto& state = *mainWorldExecState(&m_frontendPage->mainFrame());
return m_frontendPage->mainFrame().script().executeScript(expression).toWTFString(&state) == "true";
}
void InspectorFrontendClientLocal::evaluateOnLoad(const String& expression)
{
if (m_frontendLoaded)
m_frontendPage->mainFrame().script().executeScript("if (InspectorFrontendAPI) InspectorFrontendAPI.dispatch(" + expression + ")");
else
m_evaluateOnLoad.append(expression);
}
Page* InspectorFrontendClientLocal::inspectedPage() const
{
if (!m_inspectedPageController)
return nullptr;
return &m_inspectedPageController->inspectedPage();
}
}