[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[WebKit-https.git] / Source / WebKit2 / UIProcess / qt / QtWebContext.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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 program 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "QtWebContext.h"
23
24 #include "MutableArray.h"
25 #include "QtDownloadManager.h"
26 #include "QtWebIconDatabaseClient.h"
27 #include "WKAPICast.h"
28 #include "WebContext.h"
29 #include "WebInspectorServer.h"
30 #include "WebPageProxy.h"
31 #include <WKArray.h>
32 #include <WKPage.h>
33 #include <WKString.h>
34 #include <WKType.h>
35
36 namespace WebKit {
37
38 static uint64_t generateContextID()
39 {
40     static uint64_t uniqueContextID = 1;
41     return uniqueContextID++;
42 }
43
44 static HashMap<uint64_t, QtWebContext*> contextMap;
45
46 QtWebContext* QtWebContext::s_defaultContext = 0;
47
48 static void initInspectorServer()
49 {
50     QString inspectorEnv = QString::fromUtf8(qgetenv("QTWEBKIT_INSPECTOR_SERVER"));
51     if (!inspectorEnv.isEmpty()) {
52         QString bindAddress = QLatin1String("127.0.0.1");
53         QString portStr = inspectorEnv;
54         int port = 0;
55
56         int portColonPos = inspectorEnv.lastIndexOf(':');
57         if (portColonPos != -1) {
58             portStr = inspectorEnv.mid(portColonPos + 1);
59             bindAddress = inspectorEnv.mid(0, portColonPos);
60         }
61
62         bool ok = false;
63         port = portStr.toInt(&ok);
64         if (!ok) {
65             qWarning("Non numeric port for the inspector server \"%s\". Examples of valid input: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's interface).", qPrintable(portStr));
66             return;
67         }
68
69         bool success = WebInspectorServer::shared().listen(bindAddress, port);
70         if (success) {
71             QString inspectorServerUrl = QString::fromLatin1("http://%1:%2").arg(bindAddress).arg(port);
72             qWarning("Inspector server started successfully. Try pointing a WebKit browser to %s", qPrintable(inspectorServerUrl));
73         } else
74             qWarning("Couldn't start the inspector server on bind address \"%s\" and port \"%d\". In case of invalid input, try something like: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's interface).", qPrintable(bindAddress), port);
75     }
76 }
77
78 static void globalInitialization()
79 {
80     static bool initialized = false;
81     if (initialized)
82         return;
83
84     initInspectorServer();
85     initialized = true;
86 }
87
88 QtWebContext::QtWebContext(WebContext* context)
89     : m_contextID(generateContextID())
90     , m_context(context)
91     , m_downloadManager(adoptPtr(new QtDownloadManager(context)))
92     , m_iconDatabase(adoptPtr(new QtWebIconDatabaseClient(this)))
93 {
94     contextMap.set(m_contextID, this);
95 }
96
97 QtWebContext::~QtWebContext()
98 {
99     if (s_defaultContext == this)
100         s_defaultContext = 0;
101     contextMap.remove(m_contextID);
102 }
103
104 // Used only by WebKitTestRunner. It avoids calling initialize(), so that we don't register any clients.
105 PassRefPtr<QtWebContext> QtWebContext::create(WebContext* context)
106 {
107     globalInitialization();
108     return adoptRef(new QtWebContext(context));
109 }
110
111 PassRefPtr<QtWebContext> QtWebContext::defaultContext()
112 {
113     if (s_defaultContext)
114         return PassRefPtr<QtWebContext>(s_defaultContext);
115
116     RefPtr<WebContext> context = WebContext::sharedProcessContext();
117     RefPtr<QtWebContext> defaultContext = QtWebContext::create(context.get());
118     s_defaultContext = defaultContext.get();
119     // Make sure that this doesn't get called in WebKitTestRunner (defaultContext isn't used there).
120     defaultContext->initializeContextInjectedBundleClient();
121
122     return defaultContext.release();
123 }
124
125 PassRefPtr<WebPageProxy> QtWebContext::createWebPage(PageClient* client, WebPageGroup* pageGroup)
126 {
127     return m_context->createWebPage(client, pageGroup);
128 }
129
130 void QtWebContext::setNavigatorQtObjectEnabled(WebPageProxy* webPageProxy, bool enabled)
131 {
132     static String messageName("SetNavigatorQtObjectEnabled");
133     RefPtr<MutableArray> body = MutableArray::create();
134     body->append(webPageProxy);
135     RefPtr<WebBoolean> webEnabled = WebBoolean::create(enabled);
136     body->append(webEnabled.get());
137     m_context->postMessageToInjectedBundle(messageName, body.get());
138 }
139
140 void QtWebContext::postMessageToNavigatorQtObject(WebPageProxy* webPageProxy, const QString& message)
141 {
142     static String messageName("MessageToNavigatorQtObject");
143     RefPtr<MutableArray> body = MutableArray::create();
144     body->append(webPageProxy);
145     RefPtr<WebString> contents = WebString::create(String(message));
146     body->append(contents.get());
147     m_context->postMessageToInjectedBundle(messageName, body.get());
148 }
149
150 QtWebContext* QtWebContext::contextByID(uint64_t id)
151 {
152     return contextMap.get(id);
153 }
154
155 void QtWebContext::initializeContextInjectedBundleClient()
156 {
157     WKContextInjectedBundleClient injectedBundleClient;
158     memset(&injectedBundleClient, 0, sizeof(WKContextInjectedBundleClient));
159     injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion;
160     injectedBundleClient.clientInfo = this;
161     injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
162     WKContextSetInjectedBundleClient(toAPI(m_context.get()), &injectedBundleClient);
163 }
164
165 static QtWebContext* toQtWebContext(const void* clientInfo)
166 {
167     ASSERT(clientInfo);
168     return reinterpret_cast<QtWebContext*>(const_cast<void*>(clientInfo));
169 }
170
171 void QtWebContext::didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
172 {
173     toQtWebContext(clientInfo)->didReceiveMessageFromInjectedBundle(messageName, messageBody);
174 }
175
176 void QtWebContext::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
177 {
178     if (!WKStringIsEqualToUTF8CString(messageName, "MessageFromNavigatorQtObject"))
179         return;
180
181     ASSERT(messageBody);
182     ASSERT(WKGetTypeID(messageBody) == WKArrayGetTypeID());
183
184     WKArrayRef body = static_cast<WKArrayRef>(messageBody);
185     ASSERT(WKArrayGetSize(body) == 2);
186     ASSERT(WKGetTypeID(WKArrayGetItemAtIndex(body, 0)) == WKPageGetTypeID());
187     ASSERT(WKGetTypeID(WKArrayGetItemAtIndex(body, 1)) == WKStringGetTypeID());
188
189     WKPageRef page = static_cast<WKPageRef>(WKArrayGetItemAtIndex(body, 0));
190     WKStringRef str = static_cast<WKStringRef>(WKArrayGetItemAtIndex(body, 1));
191
192     toImpl(page)->didReceiveMessageFromNavigatorQtObject(toImpl(str)->string());
193 }
194
195 } // namespace WebKit
196