InspectorFrontendClientLocal.cpp [plain text]
#include "config.h"
#include "InspectorFrontendClientLocal.h"
#if ENABLE(INSPECTOR)
#include "Chrome.h"
#include "FloatRect.h"
#include "Frame.h"
#include "FrameView.h"
#include "InspectorController.h"
#include "InspectorFrontend.h"
#include "InspectorFrontendHost.h"
#include "Page.h"
#include "PlatformString.h"
#include "ScriptFunctionCall.h"
#include "ScriptObject.h"
namespace WebCore {
static const char* const inspectorAttachedHeightName = "inspectorAttachedHeight";
static const unsigned defaultAttachedHeight = 300;
static const float minimumAttachedHeight = 250.0f;
static const float maximumAttachedHeightRatio = 0.75f;
InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController* inspectorController, Page* frontendPage)
: m_inspectorController(inspectorController)
, m_frontendPage(frontendPage)
, m_frontendScriptState(0)
{
}
InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
{
if (m_frontendHost)
m_frontendHost->disconnectClient();
m_frontendScriptState = 0;
m_frontendPage = 0;
m_inspectorController = 0;
}
void InspectorFrontendClientLocal::windowObjectCleared()
{
m_frontendPage->mainFrame()->document()->securityOrigin()->grantUniversalAccess();
m_frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
ScriptGlobalObject::set(m_frontendScriptState, "InspectorBackend", m_inspectorController->inspectorBackend());
m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
ScriptGlobalObject::set(m_frontendScriptState, "InspectorFrontendHost", m_frontendHost.get());
}
void InspectorFrontendClientLocal::frontendLoaded()
{
bringToFront();
ASSERT(m_frontendScriptState);
ScriptObject webInspectorObj;
if (!ScriptGlobalObject::get(m_frontendScriptState, "WebInspector", webInspectorObj)) {
ASSERT_NOT_REACHED();
return;
}
m_inspectorController->setFrontend(new InspectorFrontend(webInspectorObj));
}
void InspectorFrontendClientLocal::requestAttachWindow()
{
if (!canAttachWindow())
return;
attachWindow();
setAttachedWindow(true);
}
void InspectorFrontendClientLocal::requestDetachWindow()
{
detachWindow();
setAttachedWindow(false);
}
bool InspectorFrontendClientLocal::canAttachWindow()
{
unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
return minimumAttachedHeight <= inspectedPageHeight * maximumAttachedHeightRatio;
}
void InspectorFrontendClientLocal::changeAttachedWindowHeight(unsigned height)
{
unsigned totalHeight = m_frontendPage->mainFrame()->view()->visibleHeight() + m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
unsigned attachedHeight = constrainedAttachedWindowHeight(height, totalHeight);
m_inspectorController->setSetting(inspectorAttachedHeightName, String::number(attachedHeight));
setAttachedWindowHeight(attachedHeight);
}
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(bool attached)
{
ScriptObject webInspectorObj;
if (!ScriptGlobalObject::get(m_frontendScriptState, "WebInspector", webInspectorObj)) {
ASSERT_NOT_REACHED();
return;
}
ScriptFunctionCall function(webInspectorObj, "dispatch");
function.appendArgument("setAttachedWindow");
function.appendArgument(attached);
function.call();
}
void InspectorFrontendClientLocal::restoreAttachedWindowHeight()
{
unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
String attachedHeight = m_inspectorController->setting(inspectorAttachedHeightName);
bool success = true;
int height = attachedHeight.toInt(&success);
unsigned preferredHeight = success ? height : defaultAttachedHeight;
setAttachedWindowHeight(constrainedAttachedWindowHeight(preferredHeight, inspectedPageHeight));
}
unsigned InspectorFrontendClientLocal::constrainedAttachedWindowHeight(unsigned preferredHeight, unsigned totalWindowHeight)
{
using namespace std;
return roundf(max(minimumAttachedHeight, min<float>(preferredHeight, totalWindowHeight * maximumAttachedHeightRatio)));
}
}
#endif