5e9b44133ce94bc402e08fb9160fd0800c54e6ac
[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 "InspectorClient.h"
42 #include "InspectorController.h"
43 #include "InspectorFrontendHost.h"
44 #include "Page.h"
45 #include "PlatformString.h"
46 #include "ScriptFunctionCall.h"
47 #include "ScriptObject.h"
48
49 namespace WebCore {
50
51 static const char* inspectorAttachedHeightSetting = "inspectorAttachedHeight";
52 static const unsigned defaultAttachedHeight = 300;
53 static const float minimumAttachedHeight = 250.0f;
54 static const float maximumAttachedHeightRatio = 0.75f;
55
56 InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController* inspectorController, Page* frontendPage)
57     : m_inspectorController(inspectorController)
58     , m_frontendPage(frontendPage)
59     , m_frontendScriptState(0)
60 {
61 }
62
63 InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
64 {
65     if (m_frontendHost)
66         m_frontendHost->disconnectClient();
67     m_frontendScriptState = 0;
68     m_frontendPage = 0;
69     m_inspectorController = 0;
70 }
71     
72 void InspectorFrontendClientLocal::windowObjectCleared()
73 {
74     // FIXME: don't keep reference to the script state
75     m_frontendScriptState = scriptStateFromPage(debuggerWorld(), m_frontendPage);
76     m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
77     ScriptGlobalObject::set(m_frontendScriptState, "InspectorFrontendHost", m_frontendHost.get());
78 }
79
80 void InspectorFrontendClientLocal::frontendLoaded()
81 {
82     bringToFront();
83     m_inspectorController->connectFrontend();
84 }
85
86 void InspectorFrontendClientLocal::requestAttachWindow()
87 {
88     if (!canAttachWindow())
89         return;
90     attachWindow();
91     setAttachedWindow(true);
92 }
93
94 void InspectorFrontendClientLocal::requestDetachWindow()
95 {
96     detachWindow();
97     setAttachedWindow(false);
98 }
99
100 bool InspectorFrontendClientLocal::canAttachWindow()
101 {
102     unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
103
104     // Don't allow the attach if the window would be too small to accommodate the minimum inspector height.
105     return minimumAttachedHeight <= inspectedPageHeight * maximumAttachedHeightRatio;
106 }
107
108 void InspectorFrontendClientLocal::changeAttachedWindowHeight(unsigned height)
109 {
110     unsigned totalHeight = m_frontendPage->mainFrame()->view()->visibleHeight() + m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
111     unsigned attachedHeight = constrainedAttachedWindowHeight(height, totalHeight);
112     m_inspectorController->inspectorClient()->storeSetting(inspectorAttachedHeightSetting, String::number(attachedHeight));
113     setAttachedWindowHeight(attachedHeight);
114 }
115
116 void InspectorFrontendClientLocal::moveWindowBy(float x, float y)
117 {
118     FloatRect frameRect = m_frontendPage->chrome()->windowRect();
119     frameRect.move(x, y);
120     m_frontendPage->chrome()->setWindowRect(frameRect);
121 }
122
123 void InspectorFrontendClientLocal::setAttachedWindow(bool attached)
124 {
125     ScriptObject webInspectorObj;
126     if (!ScriptGlobalObject::get(m_frontendScriptState, "WebInspector", webInspectorObj)) {
127         ASSERT_NOT_REACHED();
128         return;
129     }
130     ScriptFunctionCall function(webInspectorObj, "setAttachedWindow");
131     function.appendArgument(attached);
132     function.call();
133 }
134
135 void InspectorFrontendClientLocal::restoreAttachedWindowHeight()
136 {
137     unsigned inspectedPageHeight = m_inspectorController->inspectedPage()->mainFrame()->view()->visibleHeight();
138     String value;
139     m_inspectorController->inspectorClient()->populateSetting(inspectorAttachedHeightSetting, &value);
140     unsigned preferredHeight = value.isEmpty() ? defaultAttachedHeight : value.toUInt();
141     
142     // This call might not go through (if the window starts out detached), but if the window is initially created attached,
143     // InspectorController::attachWindow is never called, so we need to make sure to set the attachedWindowHeight.
144     // FIXME: Clean up code so we only have to call setAttachedWindowHeight in InspectorController::attachWindow
145     setAttachedWindowHeight(constrainedAttachedWindowHeight(preferredHeight, inspectedPageHeight));
146 }
147
148 unsigned InspectorFrontendClientLocal::constrainedAttachedWindowHeight(unsigned preferredHeight, unsigned totalWindowHeight)
149 {
150     using namespace std;
151     return roundf(max(minimumAttachedHeight, min<float>(preferredHeight, totalWindowHeight * maximumAttachedHeightRatio)));
152 }
153
154 void InspectorFrontendClientLocal::sendMessageToBackend(const String& message)
155 {
156     m_inspectorController->inspectorBackendDispatcher()->dispatch(message);
157 }
158
159 } // namespace WebCore
160
161 #endif