~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit/blackberry/WebCoreSupport/PagePopupBlackBerry.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 */
 
18
#include "config.h"
 
19
 
 
20
#include "PagePopupBlackBerry.h"
 
21
 
 
22
#include "DocumentLoader.h"
 
23
#include "EmptyClients.h"
 
24
#include "FrameView.h"
 
25
#include "JSDOMBinding.h"
 
26
#include "JSDOMWindowBase.h"
 
27
#include "JSObject.h"
 
28
#include "JSRetainPtr.h"
 
29
#include "Page.h"
 
30
#include "PageGroup.h"
 
31
#include "PagePopupClient.h"
 
32
#include "PlatformMouseEvent.h"
 
33
#include "Settings.h"
 
34
#include "WebPage.h"
 
35
#include "WebPage_p.h"
 
36
 
 
37
#include <JavaScriptCore/API/JSCallbackObject.h>
 
38
#include <JavaScriptCore/JSObjectRef.h>
 
39
#include <JavaScriptCore/JSStringRef.h>
 
40
#include <JavaScriptCore/JSValueRef.h>
 
41
 
 
42
// Fixme: should get the height from runtime.
 
43
#define URL_BAR_HEIGHT 70
 
44
#define PADDING 80
 
45
 
 
46
using namespace BlackBerry::Platform::Graphics;
 
47
using namespace BlackBerry::WebKit;
 
48
namespace WebCore {
 
49
 
 
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)))
 
54
{
 
55
    m_rect = IntRect(rect.x(), rect.y() - URL_BAR_HEIGHT, client->contentSize().width(), client->contentSize().height());
 
56
}
 
57
 
 
58
PagePopupBlackBerry::~PagePopupBlackBerry()
 
59
{
 
60
    ASSERT(!m_sharedClientPointer->get());
 
61
}
 
62
 
 
63
bool PagePopupBlackBerry::sendCreatePopupWebViewRequest()
 
64
{
 
65
    return m_webPagePrivate->client()->createPopupWebView(m_rect);
 
66
}
 
67
 
 
68
bool PagePopupBlackBerry::init(WebPage* webpage)
 
69
{
 
70
    generateHTML(webpage);
 
71
 
 
72
    installDOMFunction(webpage->d->mainFrame());
 
73
 
 
74
    return true;
 
75
}
 
76
 
 
77
void PagePopupBlackBerry::generateHTML(WebPage* webpage)
 
78
{
 
79
    DocumentWriter* writer = webpage->d->mainFrame()->loader()->activeDocumentLoader()->writer();
 
80
    writer->setMIMEType("text/html");
 
81
    writer->begin(KURL());
 
82
 
 
83
    // All the popups have the same html head and the page content should be non-zoomable.
 
84
    StringBuilder source;
 
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());
 
91
 
 
92
    m_client->writeDocument(*writer);
 
93
    writer->end();
 
94
}
 
95
 
 
96
static JSValueRef setValueAndClosePopupCallback(JSContextRef context,
 
97
        JSObjectRef, JSObjectRef, size_t argumentCount,
 
98
        const JSValueRef arguments[], JSValueRef*)
 
99
{
 
100
    JSValueRef jsRetVal = JSValueMakeUndefined(context);
 
101
    if (argumentCount <= 0)
 
102
        return jsRetVal;
 
103
 
 
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));
 
113
 
 
114
    // Check the weak pointer as the owner page may have destroyed the popup.
 
115
    if (client->get())
 
116
        client->get()->setValueAndClosePopup(0, strArgs.data());
 
117
 
 
118
    return jsRetVal;
 
119
}
 
120
 
 
121
static void popUpExtensionInitialize(JSContextRef context, JSObjectRef object)
 
122
{
 
123
    UNUSED_PARAM(context);
 
124
    UNUSED_PARAM(object);
 
125
}
 
126
 
 
127
static void popUpExtensionFinalize(JSObjectRef object)
 
128
{
 
129
    // Clear the reference. See installDOMFunction().
 
130
    PagePopupBlackBerry::SharedClientPointer* client = reinterpret_cast<PagePopupBlackBerry::SharedClientPointer*>(JSObjectGetPrivate(object));
 
131
    client->deref();
 
132
}
 
133
 
 
134
static JSStaticFunction popUpExtensionStaticFunctions[] =
 
135
{
 
136
{ 0, 0, 0 },
 
137
{ 0, 0, 0 }
 
138
};
 
139
 
 
140
static JSStaticValue popUpExtensionStaticValues[] =
 
141
{
 
142
{ 0, 0, 0, 0 }
 
143
};
 
144
 
 
145
void PagePopupBlackBerry::installDOMFunction(Frame* frame)
 
146
{
 
147
    JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld());
 
148
    ASSERT(window);
 
149
 
 
150
    JSC::ExecState* exec = window->globalExec();
 
151
    ASSERT(exec);
 
152
    JSC::JSLockHolder lock(exec);
 
153
 
 
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);
 
162
 
 
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);
 
170
 
 
171
    JSObjectRef clientClassObject = JSObjectMake(context, clientClass, 0);
 
172
 
 
173
    // Add a reference. See popUpExtensionFinalize.
 
174
    m_sharedClientPointer->ref();
 
175
    JSObjectSetPrivate(clientClassObject, m_sharedClientPointer.get());
 
176
 
 
177
    String name("popUp");
 
178
 
 
179
    JSC::PutPropertySlot slot;
 
180
    window->put(window, exec, JSC::Identifier(exec, name),
 
181
            toJS(clientClassObject), slot);
 
182
 
 
183
    JSClassRelease(clientClass);
 
184
}
 
185
 
 
186
void PagePopupBlackBerry::closePopup()
 
187
{
 
188
    // Prevent the popup page from accessing the client.
 
189
    m_sharedClientPointer->clear();
 
190
 
 
191
    m_client->didClosePopup();
 
192
    m_webPagePrivate->client()->closePopupWebView();
 
193
    m_webPagePrivate->m_webPage->popupClosed();
 
194
}
 
195
 
 
196
}
 
197