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