1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebInspectorProxy.h"
#if ENABLE(INSPECTOR)
#include "WebKitBundle.h"
#include "WebPageProxy.h"
#include "WebProcessProxy.h"
#include "WebView.h"
#include <WebCore/InspectorFrontendClientLocal.h>
#include <WebCore/NotImplemented.h>
#include <WebCore/WebCoreInstanceHandle.h>
#include <WebCore/WindowMessageBroadcaster.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RetainPtr.h>
#include <wtf/text/StringConcatenate.h>
#include <wtf/text/WTFString.h>
using namespace WebCore;
namespace WebKit {
static const LPCWSTR kWebKit2InspectorWindowClassName = L"WebKit2InspectorWindowClass";
bool WebInspectorProxy::registerInspectorViewWindowClass()
{
static bool haveRegisteredWindowClass = false;
if (haveRegisteredWindowClass)
return true;
haveRegisteredWindowClass = true;
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_DBLCLKS;
wcex.lpfnWndProc = WebInspectorProxy::InspectorViewWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(WebInspectorProxy*);
wcex.hInstance = instanceHandle();
wcex.hIcon = 0;
wcex.hCursor = ::LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = kWebKit2InspectorWindowClassName;
wcex.hIconSm = 0;
return !!::RegisterClassEx(&wcex);
}
LRESULT CALLBACK WebInspectorProxy::InspectorViewWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LONG_PTR longPtr = ::GetWindowLongPtr(hWnd, 0);
if (WebInspectorProxy* inspectorView = reinterpret_cast<WebInspectorProxy*>(longPtr))
return inspectorView->wndProc(hWnd, message, wParam, lParam);
if (message == WM_CREATE) {
LPCREATESTRUCT createStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
// Associate the WebInspectorProxy with the window.
::SetWindowLongPtr(hWnd, 0, (LONG_PTR)createStruct->lpCreateParams);
return 0;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
void WebInspectorProxy::windowReceivedMessage(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
{
ASSERT(m_isAttached);
switch (msg) {
case WM_WINDOWPOSCHANGING:
onWebViewWindowPosChangingEvent(wParam, lParam);
break;
default:
break;
}
}
LRESULT WebInspectorProxy::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
bool handled = true;
switch (message) {
case WM_SIZE:
lResult = onSizeEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_GETMINMAXINFO:
lResult = onMinMaxInfoEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_SETFOCUS:
lResult = onSetFocusEvent(hWnd, message, wParam, lParam, handled);
break;
case WM_CLOSE:
lResult = onCloseEvent(hWnd, message, wParam, lParam, handled);
break;
default:
handled = false;
break;
}
if (!handled)
lResult = ::DefWindowProc(hWnd, message, wParam, lParam);
return lResult;
}
LRESULT WebInspectorProxy::onSizeEvent(HWND, UINT, WPARAM, LPARAM, bool&)
{
RECT rect;
::GetClientRect(m_inspectorWindow, &rect);
::SetWindowPos(m_inspectorView->window(), 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER);
return 0;
}
LRESULT WebInspectorProxy::onSetFocusEvent(HWND, UINT, WPARAM, LPARAM lParam, bool&)
{
::SetFocus(m_inspectorView->window());
return 0;
}
LRESULT WebInspectorProxy::onMinMaxInfoEvent(HWND, UINT, WPARAM, LPARAM lParam, bool&)
{
MINMAXINFO* info = reinterpret_cast<MINMAXINFO*>(lParam);
POINT size = {minimumWindowWidth, minimumWindowHeight};
info->ptMinTrackSize = size;
return 0;
}
LRESULT WebInspectorProxy::onCloseEvent(HWND, UINT, WPARAM, LPARAM, bool&)
{
::ShowWindow(m_inspectorWindow, SW_HIDE);
close();
return 0;
}
void WebInspectorProxy::onWebViewWindowPosChangingEvent(WPARAM wParam, LPARAM lParam)
{
WINDOWPOS* windowPos = reinterpret_cast<WINDOWPOS*>(lParam);
if (windowPos->flags & SWP_NOSIZE)
return;
HWND inspectorWindow = m_inspectorView->window();
RECT inspectorRect;
::GetClientRect(inspectorWindow, &inspectorRect);
unsigned inspectorHeight = inspectorRect.bottom - inspectorRect.top;
RECT parentRect;
::GetClientRect(::GetParent(inspectorWindow), &parentRect);
inspectorHeight = InspectorFrontendClientLocal::constrainedAttachedWindowHeight(inspectorHeight, parentRect.bottom - parentRect.top);
windowPos->cy -= inspectorHeight;
::SetWindowPos(inspectorWindow, 0, windowPos->x, windowPos->y + windowPos->cy, windowPos->cx, inspectorHeight, SWP_NOZORDER);
}
WebPageProxy* WebInspectorProxy::platformCreateInspectorPage()
{
ASSERT(!m_inspectorView);
ASSERT(!m_inspectorWindow);
RECT initialRect = { 0, 0, initialWindowWidth, initialWindowHeight };
m_inspectorView = WebView::create(initialRect, m_page->process()->context(), inspectorPageGroup(), 0);
return m_inspectorView->page();
}
void WebInspectorProxy::platformOpen()
{
registerInspectorViewWindowClass();
m_inspectorWindow = ::CreateWindowEx(0, kWebKit2InspectorWindowClassName, 0, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, initialWindowWidth, initialWindowHeight, 0, 0, instanceHandle(), this);
ASSERT(::IsWindow(m_inspectorWindow));
m_inspectorView->setParentWindow(m_inspectorWindow);
if (m_isAttached)
platformAttach();
else
::ShowWindow(m_inspectorWindow, SW_SHOW);
}
void WebInspectorProxy::platformDidClose()
{
ASSERT(!m_isAttached);
ASSERT(!m_isVisible || m_inspectorWindow);
ASSERT(!m_isVisible || m_inspectorView);
if (m_inspectorWindow) {
ASSERT(::IsWindow(m_inspectorWindow));
::DestroyWindow(m_inspectorWindow);
}
m_inspectorWindow = 0;
m_inspectorView = 0;
}
void WebInspectorProxy::platformBringToFront()
{
// FIXME: this will not bring a background tab in Safari to the front, only its window.
HWND parentWindow = m_isAttached ? ::GetAncestor(m_page->nativeWindow(), GA_ROOT) : m_inspectorWindow;
if (!parentWindow)
return;
ASSERT(::IsWindow(parentWindow));
::SetWindowPos(parentWindow, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
}
bool WebInspectorProxy::platformIsFront()
{
notImplemented();
return false;
}
void WebInspectorProxy::platformAttachAvailabilityChanged(bool)
{
notImplemented();
}
void WebInspectorProxy::platformInspectedURLChanged(const String& urlString)
{
// FIXME: this should be made localizable once WebKit2 supports it. <rdar://problem/8728860>
String title = makeString("Web Inspector ", static_cast<UChar>(0x2014), ' ', urlString);
::SetWindowTextW(m_inspectorWindow, title.charactersWithNullTermination());
}
unsigned WebInspectorProxy::platformInspectedWindowHeight()
{
HWND inspectedWindow = m_page->nativeWindow();
RECT inspectedWindowRect;
::GetWindowRect(inspectedWindow, &inspectedWindowRect);
return static_cast<unsigned>(inspectedWindowRect.bottom - inspectedWindowRect.top);
}
void WebInspectorProxy::platformAttach()
{
HWND webViewWindow = m_page->nativeWindow();
HWND parentWindow = ::GetParent(webViewWindow);
WindowMessageBroadcaster::addListener(webViewWindow, this);
m_inspectorView->setParentWindow(parentWindow);
::ShowWindow(m_inspectorWindow, SW_HIDE);
::PostMessage(parentWindow, WM_SIZE, 0, 0);
}
void WebInspectorProxy::platformDetach()
{
HWND webViewWindow = m_page->nativeWindow();
WindowMessageBroadcaster::removeListener(webViewWindow, this);
m_inspectorView->setParentWindow(m_inspectorWindow);
if (m_isVisible)
::ShowWindow(m_inspectorWindow, SW_SHOW);
// Send the detached inspector window and the WebView's parent window WM_SIZE messages
// to have them re-layout correctly.
::PostMessage(m_inspectorWindow, WM_SIZE, 0, 0);
::PostMessage(::GetParent(webViewWindow), WM_SIZE, 0, 0);
}
void WebInspectorProxy::platformSetAttachedWindowHeight(unsigned height)
{
if (!m_isAttached)
return;
HWND inspectedWindow = m_page->nativeWindow();
HWND parentWindow = ::GetParent(inspectedWindow);
RECT parentWindowRect;
::GetWindowRect(parentWindow, &parentWindowRect);
RECT inspectedWindowRect;
::GetWindowRect(inspectedWindow, &inspectedWindowRect);
int totalHeight = parentWindowRect.bottom - parentWindowRect.top;
int webViewWidth = inspectedWindowRect.right - inspectedWindowRect.left;
POINT inspectedWindowOrigin = { inspectedWindowRect.left, inspectedWindowRect.top };
::ScreenToClient(parentWindow, &inspectedWindowOrigin);
HWND inspectorWindow = m_inspectorView->window();
::SetWindowPos(inspectorWindow, 0, inspectedWindowOrigin.x, totalHeight - height, webViewWidth, height, SWP_NOZORDER);
// We want to set the inspected web view height to the totalHeight, because the height adjustment
// of the inspected WebView happens in onWindowPosChanging, not here.
::SetWindowPos(inspectedWindow, 0, inspectedWindowOrigin.x, inspectedWindowOrigin.y, webViewWidth, totalHeight, SWP_NOZORDER);
::RedrawWindow(inspectorWindow, 0, 0, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
::RedrawWindow(inspectedWindow, 0, 0, RDW_INVALIDATE | RDW_ALLCHILDREN | RDW_UPDATENOW);
}
String WebInspectorProxy::inspectorPageURL() const
{
RetainPtr<CFURLRef> htmlURLRef(AdoptCF, CFBundleCopyResourceURL(webKitBundle(), CFSTR("inspector"), CFSTR("html"), CFSTR("inspector")));
if (!htmlURLRef)
return String();
return String(CFURLGetString(htmlURLRef.get()));
}
String WebInspectorProxy::inspectorBaseURL() const
{
// Web Inspector uses localized strings, so it's not contained within inspector directory.
RetainPtr<CFURLRef> htmlURLRef(AdoptCF, CFBundleCopyResourcesDirectoryURL(webKitBundle()));
if (!htmlURLRef)
return String();
return String(CFURLGetString(htmlURLRef.get()));
}
} // namespace WebKit
#endif // ENABLE(INSPECTOR)
|