833057d8659b653f7f8183843ff1374b6c92c517
[WebKit-https.git] / Source / WebCore / inspector / InspectorFrontendClientLocal.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "InspectorFrontendClientLocal.h"
33
34 #if ENABLE(INSPECTOR)
35
36 #include "Chrome.h"
37 #include "FloatRect.h"
38 #include "Frame.h"
39 #include "FrameView.h"
40 #include "InspectorBackendDispatcher.h"
41 #include "InspectorController.h"
42 #include "InspectorFrontendHost.h"
43 #include "Page.h"
44 #include "PlatformString.h"
45 #include "ScriptFunctionCall.h"
46 #include "ScriptObject.h"
47
48 namespace WebCore {
49
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;
54
55 String InspectorFrontendClientLocal::Settings::getProperty(const String&)
56 {
57     return String();
58 }
59
60 void InspectorFrontendClientLocal::Settings::setProperty(const String&, const String&)
61 {
62 }
63
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)
69 {
70 }
71
72 InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
73 {
74     if (m_frontendHost)
75         m_frontendHost->disconnectClient();
76     m_frontendScriptState = 0;
77     m_frontendPage = 0;
78     m_inspectorController = 0;
79 }
80     
81 void InspectorFrontendClientLocal::windowObjectCleared()
82 {
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());
87 }
88
89 void InspectorFrontendClientLocal::frontendLoaded()
90 {
91     bringToFront();
92     m_inspectorController->connectFrontend();
93 }
94
95 void InspectorFrontendClientLocal::requestAttachWindow()
96 {
97     if (!canAttachWindow())
98         return;
99     attachWindow();
100     setAttachedWindow(true);
101 }
102
103 void InspectorFrontendClientLocal::requestDetachWindow()
104 {
105     detachWindow();
106     setAttachedWindow(false);
107 }
108
109 bool InspectorFrontendClientLocal::canAttachWindow()
110 {
111     unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
112
113     // Don't allow the attach if the window would be too small to accommodate the minimum inspector height.
114     return minimumAttachedHeight <= inspectedPageHeight * maximumAttachedHeightRatio;
115 }
116
117 void InspectorFrontendClientLocal::changeAttachedWindowHeight(unsigned height)
118 {
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);
123 }
124
125 void InspectorFrontendClientLocal::moveWindowBy(float x, float y)
126 {
127     FloatRect frameRect = m_frontendPage->chrome()->windowRect();
128     frameRect.move(x, y);
129     m_frontendPage->chrome()->setWindowRect(frameRect);
130 }
131
132 void InspectorFrontendClientLocal::setAttachedWindow(bool attached)
133 {
134     ScriptObject webInspectorObj;
135     if (!ScriptGlobalObject::get(m_frontendScriptState, "WebInspector", webInspectorObj)) {
136         ASSERT_NOT_REACHED();
137         return;
138     }
139     ScriptFunctionCall function(webInspectorObj, "setAttachedWindow");
140     function.appendArgument(attached);
141     function.call();
142 }
143
144 void InspectorFrontendClientLocal::restoreAttachedWindowHeight()
145 {
146     unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
147     String value = m_settings->getProperty(inspectorAttachedHeightSetting);
148     unsigned preferredHeight = value.isEmpty() ? defaultAttachedHeight : value.toUInt();
149     
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));
154 }
155
156 unsigned InspectorFrontendClientLocal::constrainedAttachedWindowHeight(unsigned preferredHeight, unsigned totalWindowHeight)
157 {
158     using namespace std;
159     return roundf(max(minimumAttachedHeight, min<float>(preferredHeight, totalWindowHeight * maximumAttachedHeightRatio)));
160 }
161
162 void InspectorFrontendClientLocal::sendMessageToBackend(const String& message)
163 {
164     m_inspectorController->dispatchMessageFromFrontend(message);
165 }
166
167 } // namespace WebCore
168
169 #endif