initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / win / WebPageProxyWin.cpp
1 /*
2 * Copyright (C) 2010 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebPageProxy.h"
28
29 #include "PageClient.h"
30 #include "WebDragSource.h"
31 #include "WebPageMessages.h"
32 #include "WebPopupMenuProxyWin.h"
33 #include "WebProcessProxy.h"
34
35 #include "resource.h"
36 #include <WebCore/BitmapInfo.h>
37 #include <WebCore/COMPtr.h>
38 #include <WebCore/ClipboardUtilitiesWin.h>
39 #include <WebCore/SystemInfo.h>
40 #include <WebCore/WCDataObject.h>
41 #include <WebCore/WebCoreInstanceHandle.h>
42 #include <tchar.h>
43 #include <wtf/StdLibExtras.h>
44 #include <wtf/text/StringConcatenate.h>
45
46 using namespace WebCore;
47
48 namespace WebKit {
49
50 static String userVisibleWebKitVersionString()
51 {
52     LPWSTR buildNumberStringPtr;
53     if (!::LoadStringW(instanceHandle(), BUILD_NUMBER, reinterpret_cast<LPWSTR>(&buildNumberStringPtr), 0) || !buildNumberStringPtr)
54         return "534+";
55
56     return buildNumberStringPtr;
57 }
58
59 String WebPageProxy::standardUserAgent(const String& applicationNameForUserAgent)
60 {
61     DEFINE_STATIC_LOCAL(String, osVersion, (windowsVersionForUAString()));
62     DEFINE_STATIC_LOCAL(String, webKitVersion, (userVisibleWebKitVersionString()));
63
64     return makeString("Mozilla/5.0 (", osVersion, ") AppleWebKit/", webKitVersion, " (KHTML, like Gecko)", applicationNameForUserAgent.isEmpty() ? "" : " ", applicationNameForUserAgent);
65 }
66
67 void WebPageProxy::setPopupMenuSelectedIndex(int32_t selectedIndex)
68 {
69     if (!m_activePopupMenu)
70         return;
71
72     static_cast<WebPopupMenuProxyWin*>(m_activePopupMenu.get())->setFocusedIndex(selectedIndex);
73 }
74
75 HWND WebPageProxy::nativeWindow() const
76 {
77     return m_pageClient->nativeWindow();
78 }
79
80 void WebPageProxy::scheduleChildWindowGeometryUpdate(const WindowGeometry& geometry)
81 {
82     m_pageClient->scheduleChildWindowGeometryUpdate(geometry);
83 }
84
85 void WebPageProxy::didInstallOrUninstallPageOverlay(bool didInstall)
86 {
87     m_pageClient->didInstallOrUninstallPageOverlay(didInstall);
88 }
89
90 IntRect WebPageProxy::firstRectForCharacterInSelectedRange(int characterPosition)
91 {
92     IntRect resultRect;
93     process()->sendSync(Messages::WebPage::FirstRectForCharacterInSelectedRange(characterPosition), Messages::WebPage::FirstRectForCharacterInSelectedRange::Reply(resultRect), m_pageID);
94     return resultRect;
95 }
96
97 String WebPageProxy::getSelectedText()
98 {
99     String text;
100     process()->sendSync(Messages::WebPage::GetSelectedText(), Messages::WebPage::GetSelectedText::Reply(text), m_pageID);
101     return text;
102 }
103
104 bool WebPageProxy::gestureWillBegin(const IntPoint& point)
105 {
106     bool canBeginPanning = false;
107     process()->sendSync(Messages::WebPage::GestureWillBegin(point), Messages::WebPage::GestureWillBegin::Reply(canBeginPanning), m_pageID);
108     return canBeginPanning;
109 }
110
111 void WebPageProxy::gestureDidScroll(const IntSize& size)
112 {
113     process()->send(Messages::WebPage::GestureDidScroll(size), m_pageID);
114 }
115
116 void WebPageProxy::gestureDidEnd()
117 {
118     process()->send(Messages::WebPage::GestureDidEnd(), m_pageID);
119 }
120
121 void WebPageProxy::setGestureReachedScrollingLimit(bool limitReached)
122 {
123     m_pageClient->setGestureReachedScrollingLimit(limitReached);
124 }
125
126 void WebPageProxy::startDragDrop(const IntPoint& imageOrigin, const IntPoint& dragPoint, uint64_t okEffect, const HashMap<UINT, Vector<String> >& dataMap, uint64_t fileSize, const String& pathname, const SharedMemory::Handle& fileContentHandle, const IntSize& dragImageSize, const SharedMemory::Handle& dragImageHandle, bool isLinkDrag)
127 {
128     COMPtr<WCDataObject> dataObject;
129     WCDataObject::createInstance(&dataObject, dataMap);
130
131     if (fileSize) {
132         RefPtr<SharedMemory> fileContentBuffer = SharedMemory::create(fileContentHandle, SharedMemory::ReadOnly);
133         setFileDescriptorData(dataObject.get(), fileSize, pathname);
134         setFileContentData(dataObject.get(), fileSize, fileContentBuffer->data());
135     }
136
137     RefPtr<SharedMemory> memoryBuffer = SharedMemory::create(dragImageHandle, SharedMemory::ReadOnly);
138     if (!memoryBuffer)
139         return;
140
141     RefPtr<WebDragSource> source = WebDragSource::createInstance();
142     if (!source)
143         return;
144
145     COMPtr<IDragSourceHelper> helper;
146     if (FAILED(::CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER, IID_IDragSourceHelper, reinterpret_cast<LPVOID*>(&helper))))
147         return;
148
149     BitmapInfo bitmapInfo = BitmapInfo::create(dragImageSize);
150     void* bits;
151     OwnPtr<HBITMAP> hbmp = adoptPtr(::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &bits, 0, 0));
152     memcpy(bits, memoryBuffer->data(), memoryBuffer->size());
153
154     SHDRAGIMAGE sdi;
155     sdi.sizeDragImage.cx = bitmapInfo.bmiHeader.biWidth;
156     sdi.sizeDragImage.cy = bitmapInfo.bmiHeader.biHeight;
157     sdi.crColorKey = 0xffffffff;
158     sdi.hbmpDragImage = hbmp.leakPtr();
159     sdi.ptOffset.x = dragPoint.x() - imageOrigin.x();
160     sdi.ptOffset.y = dragPoint.y() - imageOrigin.y();
161     if (isLinkDrag)
162         sdi.ptOffset.y = bitmapInfo.bmiHeader.biHeight - sdi.ptOffset.y;
163
164     helper->InitializeFromBitmap(&sdi, dataObject.get());
165
166     DWORD effect = DROPEFFECT_NONE;
167
168     DragOperation operation = DragOperationNone;
169     if (::DoDragDrop(dataObject.get(), source.get(), okEffect, &effect) == DRAGDROP_S_DROP) {
170         if (effect & DROPEFFECT_COPY)
171             operation = DragOperationCopy;
172         else if (effect & DROPEFFECT_LINK)
173             operation = DragOperationLink;
174         else if (effect & DROPEFFECT_MOVE)
175             operation = DragOperationMove;
176     }
177     POINT globalPoint;
178     ::GetCursorPos(&globalPoint);
179     POINT localPoint = globalPoint;
180     ::ScreenToClient(m_pageClient->nativeWindow(), &localPoint);
181
182     dragEnded(localPoint, globalPoint, operation);
183 }
184
185 void WebPageProxy::didChangeCompositionSelection(bool hasComposition)
186 {
187     m_pageClient->compositionSelectionChanged(hasComposition);
188 }
189
190 void WebPageProxy::confirmComposition(const String& compositionString)
191 {
192     process()->send(Messages::WebPage::ConfirmComposition(compositionString), m_pageID);
193 }
194
195 void WebPageProxy::setComposition(const String& compositionString, Vector<CompositionUnderline>& underlines, int cursorPosition)
196 {
197     process()->send(Messages::WebPage::SetComposition(compositionString, underlines, cursorPosition), m_pageID);
198 }
199
200
201 } // namespace WebKit