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
|
/*
* Copyright (C) 2012 Research In Motion Limited. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "PagePopupBlackBerry.h"
#include "DocumentLoader.h"
#include "EmptyClients.h"
#include "FrameView.h"
#include "JSDOMBinding.h"
#include "JSDOMWindowBase.h"
#include "JSObject.h"
#include "JSRetainPtr.h"
#include "Page.h"
#include "PageGroup.h"
#include "PagePopupClient.h"
#include "PlatformMouseEvent.h"
#include "Settings.h"
#include "WebPage.h"
#include "WebPage_p.h"
#include <JavaScriptCore/API/JSCallbackObject.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSStringRef.h>
#include <JavaScriptCore/JSValueRef.h>
// Fixme: should get the height from runtime.
#define URL_BAR_HEIGHT 70
#define PADDING 80
using namespace BlackBerry::Platform::Graphics;
using namespace BlackBerry::WebKit;
namespace WebCore {
PagePopupBlackBerry::PagePopupBlackBerry(BlackBerry::WebKit::WebPagePrivate* webPage, PagePopupClient* client, const IntRect& rect)
: m_webPagePrivate(webPage)
, m_client(adoptPtr(client))
, m_sharedClientPointer(adoptRef(new PagePopupBlackBerry::SharedClientPointer(client)))
{
m_rect = IntRect(rect.x(), rect.y() - URL_BAR_HEIGHT, client->contentSize().width(), client->contentSize().height());
}
PagePopupBlackBerry::~PagePopupBlackBerry()
{
ASSERT(!m_sharedClientPointer->get());
}
bool PagePopupBlackBerry::sendCreatePopupWebViewRequest()
{
return m_webPagePrivate->client()->createPopupWebView(m_rect);
}
bool PagePopupBlackBerry::init(WebPage* webpage)
{
generateHTML(webpage);
installDOMFunction(webpage->d->mainFrame());
return true;
}
void PagePopupBlackBerry::generateHTML(WebPage* webpage)
{
DocumentWriter* writer = webpage->d->mainFrame()->loader()->activeDocumentLoader()->writer();
writer->setMIMEType("text/html");
writer->begin(KURL());
// All the popups have the same html head and the page content should be non-zoomable.
StringBuilder source;
// FIXME: the hardcoding padding will be removed soon.
int screenWidth = webpage->d->screenSize().width() - PADDING;
source.appendLiteral("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n");
source.append("<meta name=\"viewport\" content=\"width=" + String::number(screenWidth));
source.appendLiteral(", user-scalable=no\" />\n");
writer->addData(source.toString().utf8().data(), source.toString().utf8().length());
m_client->writeDocument(*writer);
writer->end();
}
static JSValueRef setValueAndClosePopupCallback(JSContextRef context,
JSObjectRef, JSObjectRef, size_t argumentCount,
const JSValueRef arguments[], JSValueRef*)
{
JSValueRef jsRetVal = JSValueMakeUndefined(context);
if (argumentCount <= 0)
return jsRetVal;
JSStringRef string = JSValueToStringCopy(context, arguments[0], 0);
size_t sizeUTF8 = JSStringGetMaximumUTF8CStringSize(string);
Vector<char> strArgs(sizeUTF8 + 1);
strArgs[sizeUTF8] = 0;
JSStringGetUTF8CString(string, strArgs.data(), sizeUTF8);
JSStringRelease(string);
JSObjectRef popUpObject = JSValueToObject(context,
arguments[argumentCount - 1], 0);
PagePopupBlackBerry::SharedClientPointer* client = reinterpret_cast<PagePopupBlackBerry::SharedClientPointer*>(JSObjectGetPrivate(popUpObject));
// Check the weak pointer as the owner page may have destroyed the popup.
if (client->get())
client->get()->setValueAndClosePopup(0, strArgs.data());
return jsRetVal;
}
static void popUpExtensionInitialize(JSContextRef context, JSObjectRef object)
{
UNUSED_PARAM(context);
UNUSED_PARAM(object);
}
static void popUpExtensionFinalize(JSObjectRef object)
{
// Clear the reference. See installDOMFunction().
PagePopupBlackBerry::SharedClientPointer* client = reinterpret_cast<PagePopupBlackBerry::SharedClientPointer*>(JSObjectGetPrivate(object));
client->deref();
}
static JSStaticFunction popUpExtensionStaticFunctions[] =
{
{ 0, 0, 0 },
{ 0, 0, 0 }
};
static JSStaticValue popUpExtensionStaticValues[] =
{
{ 0, 0, 0, 0 }
};
void PagePopupBlackBerry::installDOMFunction(Frame* frame)
{
JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld());
ASSERT(window);
JSC::ExecState* exec = window->globalExec();
ASSERT(exec);
JSC::JSLockHolder lock(exec);
JSContextRef context = ::toRef(exec);
JSObjectRef globalObject = JSContextGetGlobalObject(context);
JSStringRef functionName = JSStringCreateWithUTF8CString(
"setValueAndClosePopup");
JSObjectRef function = JSObjectMakeFunctionWithCallback(context,
functionName, setValueAndClosePopupCallback);
JSObjectSetProperty(context, globalObject, functionName, function,
kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
// Register client into DOM
JSClassDefinition definition = kJSClassDefinitionEmpty;
definition.staticValues = popUpExtensionStaticValues;
definition.staticFunctions = popUpExtensionStaticFunctions;
definition.initialize = popUpExtensionInitialize;
definition.finalize = popUpExtensionFinalize;
JSClassRef clientClass = JSClassCreate(&definition);
JSObjectRef clientClassObject = JSObjectMake(context, clientClass, 0);
// Add a reference. See popUpExtensionFinalize.
m_sharedClientPointer->ref();
JSObjectSetPrivate(clientClassObject, m_sharedClientPointer.get());
String name("popUp");
JSC::PutPropertySlot slot;
window->put(window, exec, JSC::Identifier(exec, name),
toJS(clientClassObject), slot);
JSClassRelease(clientClass);
}
void PagePopupBlackBerry::closePopup()
{
// Prevent the popup page from accessing the client.
m_sharedClientPointer->clear();
m_client->didClosePopup();
m_webPagePrivate->client()->closePopupWebView();
m_webPagePrivate->m_webPage->popupClosed();
}
}
|