2
* Copyright (C) 2012 Research In Motion Limited. All rights reserved.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "PagePopupBlackBerry.h"
22
#include "DocumentLoader.h"
23
#include "EmptyClients.h"
24
#include "FrameView.h"
25
#include "JSDOMBinding.h"
26
#include "JSDOMWindowBase.h"
28
#include "JSRetainPtr.h"
30
#include "PageGroup.h"
31
#include "PagePopupClient.h"
32
#include "PlatformMouseEvent.h"
35
#include "WebPage_p.h"
37
#include <JavaScriptCore/API/JSCallbackObject.h>
38
#include <JavaScriptCore/JSObjectRef.h>
39
#include <JavaScriptCore/JSStringRef.h>
40
#include <JavaScriptCore/JSValueRef.h>
42
// Fixme: should get the height from runtime.
43
#define URL_BAR_HEIGHT 70
46
using namespace BlackBerry::Platform::Graphics;
47
using namespace BlackBerry::WebKit;
50
PagePopupBlackBerry::PagePopupBlackBerry(BlackBerry::WebKit::WebPagePrivate* webPage, PagePopupClient* client, const IntRect& rect)
51
: m_webPagePrivate(webPage)
52
, m_client(adoptPtr(client))
53
, m_sharedClientPointer(adoptRef(new PagePopupBlackBerry::SharedClientPointer(client)))
55
m_rect = IntRect(rect.x(), rect.y() - URL_BAR_HEIGHT, client->contentSize().width(), client->contentSize().height());
58
PagePopupBlackBerry::~PagePopupBlackBerry()
60
ASSERT(!m_sharedClientPointer->get());
63
bool PagePopupBlackBerry::sendCreatePopupWebViewRequest()
65
return m_webPagePrivate->client()->createPopupWebView(m_rect);
68
bool PagePopupBlackBerry::init(WebPage* webpage)
70
generateHTML(webpage);
72
installDOMFunction(webpage->d->mainFrame());
77
void PagePopupBlackBerry::generateHTML(WebPage* webpage)
79
DocumentWriter* writer = webpage->d->mainFrame()->loader()->activeDocumentLoader()->writer();
80
writer->setMIMEType("text/html");
81
writer->begin(KURL());
83
// All the popups have the same html head and the page content should be non-zoomable.
85
// FIXME: the hardcoding padding will be removed soon.
86
int screenWidth = webpage->d->screenSize().width() - PADDING;
87
source.appendLiteral("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n");
88
source.append("<meta name=\"viewport\" content=\"width=" + String::number(screenWidth));
89
source.appendLiteral(", user-scalable=no\" />\n");
90
writer->addData(source.toString().utf8().data(), source.toString().utf8().length());
92
m_client->writeDocument(*writer);
96
static JSValueRef setValueAndClosePopupCallback(JSContextRef context,
97
JSObjectRef, JSObjectRef, size_t argumentCount,
98
const JSValueRef arguments[], JSValueRef*)
100
JSValueRef jsRetVal = JSValueMakeUndefined(context);
101
if (argumentCount <= 0)
104
JSStringRef string = JSValueToStringCopy(context, arguments[0], 0);
105
size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string);
106
Vector<char> strArgs(sizeUTF8 + 1);
107
strArgs[sizeUTF8] = 0;
108
JSStringGetUTF8CString(string, strArgs.data(), sizeUTF8);
109
JSStringRelease(string);
110
JSObjectRef popUpObject = JSValueToObject(context,
111
arguments[argumentCount - 1], 0);
112
PagePopupBlackBerry::SharedClientPointer* client = reinterpret_cast<PagePopupBlackBerry::SharedClientPointer*>(JSObjectGetPrivate(popUpObject));
114
// Check the weak pointer as the owner page may have destroyed the popup.
116
client->get()->setValueAndClosePopup(0, strArgs.data());
121
static void popUpExtensionInitialize(JSContextRef context, JSObjectRef object)
123
UNUSED_PARAM(context);
124
UNUSED_PARAM(object);
127
static void popUpExtensionFinalize(JSObjectRef object)
129
// Clear the reference. See installDOMFunction().
130
PagePopupBlackBerry::SharedClientPointer* client = reinterpret_cast<PagePopupBlackBerry::SharedClientPointer*>(JSObjectGetPrivate(object));
134
static JSStaticFunction popUpExtensionStaticFunctions[] =
140
static JSStaticValue popUpExtensionStaticValues[] =
145
void PagePopupBlackBerry::installDOMFunction(Frame* frame)
147
JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld());
150
JSC::ExecState* exec = window->globalExec();
152
JSC::JSLockHolder lock(exec);
154
JSContextRef context = ::toRef(exec);
155
JSObjectRef globalObject = JSContextGetGlobalObject(context);
156
JSStringRef functionName = JSStringCreateWithUTF8CString(
157
"setValueAndClosePopup");
158
JSObjectRef function = JSObjectMakeFunctionWithCallback(context,
159
functionName, setValueAndClosePopupCallback);
160
JSObjectSetProperty(context, globalObject, functionName, function,
161
kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
163
// Register client into DOM
164
JSClassDefinition definition = kJSClassDefinitionEmpty;
165
definition.staticValues = popUpExtensionStaticValues;
166
definition.staticFunctions = popUpExtensionStaticFunctions;
167
definition.initialize = popUpExtensionInitialize;
168
definition.finalize = popUpExtensionFinalize;
169
JSClassRef clientClass = JSClassCreate(&definition);
171
JSObjectRef clientClassObject = JSObjectMake(context, clientClass, 0);
173
// Add a reference. See popUpExtensionFinalize.
174
m_sharedClientPointer->ref();
175
JSObjectSetPrivate(clientClassObject, m_sharedClientPointer.get());
177
String name("popUp");
179
JSC::PutPropertySlot slot;
180
window->put(window, exec, JSC::Identifier(exec, name),
181
toJS(clientClassObject), slot);
183
JSClassRelease(clientClass);
186
void PagePopupBlackBerry::closePopup()
188
// Prevent the popup page from accessing the client.
189
m_sharedClientPointer->clear();
191
m_client->didClosePopup();
192
m_webPagePrivate->client()->closePopupWebView();
193
m_webPagePrivate->m_webPage->popupClosed();