2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "InspectorFrontendClientLocal.h"
37 #include "FloatRect.h"
39 #include "FrameView.h"
40 #include "InspectorBackendDispatcher.h"
41 #include "InspectorController.h"
42 #include "InspectorFrontendHost.h"
44 #include "PlatformString.h"
45 #include "ScriptFunctionCall.h"
46 #include "ScriptObject.h"
50 static const char* inspectorAttachedHeightSetting = "inspectorAttachedHeight";
51 static const unsigned defaultAttachedHeight = 300;
52 static const float minimumAttachedHeight = 250.0f;
53 static const float maximumAttachedHeightRatio = 0.75f;
55 String InspectorFrontendClientLocal::Settings::getProperty(const String&)
60 void InspectorFrontendClientLocal::Settings::setProperty(const String&, const String&)
64 InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController* inspectorController, Page* frontendPage, PassOwnPtr<Settings> settings)
65 : m_inspectorController(inspectorController)
66 , m_frontendPage(frontendPage)
67 , m_frontendScriptState(0)
68 , m_settings(settings)
72 InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
75 m_frontendHost->disconnectClient();
76 m_frontendScriptState = 0;
78 m_inspectorController = 0;
81 void InspectorFrontendClientLocal::windowObjectCleared()
83 // FIXME: don't keep reference to the script state
84 m_frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
85 m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
86 ScriptGlobalObject::set(m_frontendScriptState, "InspectorFrontendHost", m_frontendHost.get());
89 void InspectorFrontendClientLocal::frontendLoaded()
92 m_inspectorController->connectFrontend();
95 void InspectorFrontendClientLocal::requestAttachWindow()
97 if (!canAttachWindow())
100 setAttachedWindow(true);
103 void InspectorFrontendClientLocal::requestDetachWindow()
106 setAttachedWindow(false);
109 bool InspectorFrontendClientLocal::canAttachWindow()
111 unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
113 // Don't allow the attach if the window would be too small to accommodate the minimum inspector height.
114 return minimumAttachedHeight <= inspectedPageHeight * maximumAttachedHeightRatio;
117 void InspectorFrontendClientLocal::changeAttachedWindowHeight(unsigned height)
119 unsigned totalHeight = m_frontendPage->mainFrame()->view()->visibleHeight() + m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
120 unsigned attachedHeight = constrainedAttachedWindowHeight(height, totalHeight);
121 m_settings->setProperty(inspectorAttachedHeightSetting, String::number(attachedHeight));
122 setAttachedWindowHeight(attachedHeight);
125 void InspectorFrontendClientLocal::moveWindowBy(float x, float y)
127 FloatRect frameRect = m_frontendPage->chrome()->windowRect();
128 frameRect.move(x, y);
129 m_frontendPage->chrome()->setWindowRect(frameRect);
132 void InspectorFrontendClientLocal::setAttachedWindow(bool attached)
134 ScriptObject webInspectorObj;
135 if (!ScriptGlobalObject::get(m_frontendScriptState, "WebInspector", webInspectorObj)) {
136 ASSERT_NOT_REACHED();
139 ScriptFunctionCall function(webInspectorObj, "setAttachedWindow");
140 function.appendArgument(attached);
144 void InspectorFrontendClientLocal::restoreAttachedWindowHeight()
146 unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
147 String value = m_settings->getProperty(inspectorAttachedHeightSetting);
148 unsigned preferredHeight = value.isEmpty() ? defaultAttachedHeight : value.toUInt();
150 // This call might not go through (if the window starts out detached), but if the window is initially created attached,
151 // InspectorController::attachWindow is never called, so we need to make sure to set the attachedWindowHeight.
152 // FIXME: Clean up code so we only have to call setAttachedWindowHeight in InspectorController::attachWindow
153 setAttachedWindowHeight(constrainedAttachedWindowHeight(preferredHeight, inspectedPageHeight));
156 unsigned InspectorFrontendClientLocal::constrainedAttachedWindowHeight(unsigned preferredHeight, unsigned totalWindowHeight)
159 return roundf(max(minimumAttachedHeight, min<float>(preferredHeight, totalWindowHeight * maximumAttachedHeightRatio)));
162 void InspectorFrontendClientLocal::sendMessageToBackend(const String& message)
164 m_inspectorController->inspectorBackendDispatcher()->dispatch(message);
167 } // namespace WebCore