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

« back to all changes in this revision

Viewing changes to Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.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) 2010, 2011, 2012 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 "InjectedBundle.h"
 
28
 
 
29
#include "ActivateFonts.h"
 
30
#include "InjectedBundlePage.h"
 
31
#include "StringFunctions.h"
 
32
#include <WebKit2/WKBundle.h>
 
33
#include <WebKit2/WKBundlePage.h>
 
34
#include <WebKit2/WKBundlePagePrivate.h>
 
35
#include <WebKit2/WKBundlePrivate.h>
 
36
#include <WebKit2/WKRetainPtr.h>
 
37
#include <WebKit2/WebKit2_C.h>
 
38
#include <wtf/PassOwnPtr.h>
 
39
#include <wtf/text/CString.h>
 
40
#include <wtf/text/StringBuilder.h>
 
41
#include <wtf/Vector.h>
 
42
 
 
43
namespace WTR {
 
44
 
 
45
InjectedBundle& InjectedBundle::shared()
 
46
{
 
47
    static InjectedBundle& shared = *new InjectedBundle;
 
48
    return shared;
 
49
}
 
50
 
 
51
InjectedBundle::InjectedBundle()
 
52
    : m_bundle(0)
 
53
    , m_topLoadingFrame(0)
 
54
    , m_state(Idle)
 
55
    , m_dumpPixels(false)
 
56
    , m_useWaitToDumpWatchdogTimer(true)
 
57
    , m_useWorkQueue(false)
 
58
{
 
59
}
 
60
 
 
61
void InjectedBundle::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
 
62
{
 
63
    static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didCreatePage(page);
 
64
}
 
65
 
 
66
void InjectedBundle::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
 
67
{
 
68
    static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->willDestroyPage(page);
 
69
}
 
70
 
 
71
void InjectedBundle::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
 
72
{
 
73
    static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didInitializePageGroup(pageGroup);
 
74
}
 
75
 
 
76
void InjectedBundle::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
 
77
{
 
78
    static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didReceiveMessage(messageName, messageBody);
 
79
}
 
80
 
 
81
void InjectedBundle::didReceiveMessageToPage(WKBundleRef bundle, WKBundlePageRef page, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
 
82
{
 
83
    static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didReceiveMessageToPage(page, messageName, messageBody);
 
84
}
 
85
 
 
86
void InjectedBundle::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
 
87
{
 
88
    m_bundle = bundle;
 
89
    m_stringBuilder = WTF::adoptPtr(new WTF::StringBuilder());
 
90
 
 
91
    WKBundleClient client = {
 
92
        kWKBundleClientCurrentVersion,
 
93
        this,
 
94
        didCreatePage,
 
95
        willDestroyPage,
 
96
        didInitializePageGroup,
 
97
        didReceiveMessage,
 
98
        didReceiveMessageToPage
 
99
    };
 
100
    WKBundleSetClient(m_bundle, &client);
 
101
 
 
102
    platformInitialize(initializationUserData);
 
103
 
 
104
    activateFonts();
 
105
    WKBundleActivateMacFontAscentHack(m_bundle);
 
106
 
 
107
    // FIXME: We'd like to start with a clean state for every test, but this function can't be used more than once yet.
 
108
    WKBundleSwitchNetworkLoaderToNewTestingSession(m_bundle);
 
109
}
 
110
 
 
111
void InjectedBundle::didCreatePage(WKBundlePageRef page)
 
112
{
 
113
    m_pages.append(adoptPtr(new InjectedBundlePage(page)));
 
114
}
 
115
 
 
116
void InjectedBundle::willDestroyPage(WKBundlePageRef page)
 
117
{
 
118
    size_t size = m_pages.size();
 
119
    for (size_t i = 0; i < size; ++i) {
 
120
        if (m_pages[i]->page() == page) {
 
121
            m_pages.remove(i);
 
122
            break;
 
123
        }
 
124
    }
 
125
}
 
126
 
 
127
void InjectedBundle::didInitializePageGroup(WKBundlePageGroupRef pageGroup)
 
128
{
 
129
    m_pageGroup = pageGroup;
 
130
}
 
131
 
 
132
InjectedBundlePage* InjectedBundle::page() const
 
133
{
 
134
    // It might be better to have the UI process send over a reference to the main
 
135
    // page instead of just assuming it's the first one.
 
136
    return m_pages[0].get();
 
137
}
 
138
 
 
139
void InjectedBundle::resetLocalSettings()
 
140
{
 
141
    setlocale(LC_ALL, "");
 
142
}
 
143
 
 
144
void InjectedBundle::didReceiveMessage(WKStringRef messageName, WKTypeRef messageBody)
 
145
{
 
146
    if (WKStringIsEqualToUTF8CString(messageName, "BeginTest")) {
 
147
        ASSERT(messageBody);
 
148
        ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
 
149
        WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
 
150
 
 
151
        WKRetainPtr<WKStringRef> dumpPixelsKey(AdoptWK, WKStringCreateWithUTF8CString("DumpPixels"));
 
152
        m_dumpPixels = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, dumpPixelsKey.get())));
 
153
 
 
154
        WKRetainPtr<WKStringRef> useWaitToDumpWatchdogTimerKey(AdoptWK, WKStringCreateWithUTF8CString("UseWaitToDumpWatchdogTimer"));
 
155
        m_useWaitToDumpWatchdogTimer = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, useWaitToDumpWatchdogTimerKey.get())));
 
156
 
 
157
        WKRetainPtr<WKStringRef> ackMessageName(AdoptWK, WKStringCreateWithUTF8CString("Ack"));
 
158
        WKRetainPtr<WKStringRef> ackMessageBody(AdoptWK, WKStringCreateWithUTF8CString("BeginTest"));
 
159
        WKBundlePostMessage(m_bundle, ackMessageName.get(), ackMessageBody.get());
 
160
 
 
161
        beginTesting(messageBodyDictionary);
 
162
        return;
 
163
    } else if (WKStringIsEqualToUTF8CString(messageName, "Reset")) {
 
164
        ASSERT(messageBody);
 
165
        ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
 
166
        WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
 
167
 
 
168
        WKRetainPtr<WKStringRef> shouldGCKey(AdoptWK, WKStringCreateWithUTF8CString("ShouldGC"));
 
169
        bool shouldGC = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, shouldGCKey.get())));
 
170
 
 
171
        if (shouldGC)
 
172
            WKBundleGarbageCollectJavaScriptObjects(m_bundle);
 
173
 
 
174
        m_state = Idle;
 
175
        m_dumpPixels = false;
 
176
 
 
177
        resetLocalSettings();
 
178
        m_testRunner->removeAllWebNotificationPermissions();
 
179
 
 
180
        return;
 
181
    }
 
182
    if (WKStringIsEqualToUTF8CString(messageName, "CallAddChromeInputFieldCallback")) {
 
183
        m_testRunner->callAddChromeInputFieldCallback();
 
184
        return;
 
185
    }
 
186
    if (WKStringIsEqualToUTF8CString(messageName, "CallRemoveChromeInputFieldCallback")) {
 
187
        m_testRunner->callRemoveChromeInputFieldCallback();
 
188
        return;
 
189
    }
 
190
    if (WKStringIsEqualToUTF8CString(messageName, "CallFocusWebViewCallback")) {
 
191
        m_testRunner->callFocusWebViewCallback();
 
192
        return;
 
193
    }
 
194
    if (WKStringIsEqualToUTF8CString(messageName, "CallSetBackingScaleFactorCallback")) {
 
195
        m_testRunner->callSetBackingScaleFactorCallback();
 
196
        return;
 
197
    }   
 
198
    if (WKStringIsEqualToUTF8CString(messageName, "WorkQueueProcessedCallback")) {
 
199
        if (!topLoadingFrame() && !m_testRunner->waitToDump())
 
200
            page()->dump();
 
201
        return;
 
202
    }
 
203
 
 
204
    WKRetainPtr<WKStringRef> errorMessageName(AdoptWK, WKStringCreateWithUTF8CString("Error"));
 
205
    WKRetainPtr<WKStringRef> errorMessageBody(AdoptWK, WKStringCreateWithUTF8CString("Unknown"));
 
206
    WKBundlePostMessage(m_bundle, errorMessageName.get(), errorMessageBody.get());
 
207
}
 
208
 
 
209
void InjectedBundle::didReceiveMessageToPage(WKBundlePageRef page, WKStringRef messageName, WKTypeRef messageBody)
 
210
{
 
211
    WKRetainPtr<WKStringRef> errorMessageName(AdoptWK, WKStringCreateWithUTF8CString("Error"));
 
212
    WKRetainPtr<WKStringRef> errorMessageBody(AdoptWK, WKStringCreateWithUTF8CString("Unknown"));
 
213
    WKBundlePostMessage(m_bundle, errorMessageName.get(), errorMessageBody.get());
 
214
}
 
215
 
 
216
bool InjectedBundle::booleanForKey(WKDictionaryRef dictionary, const char* key)
 
217
{
 
218
    WKRetainPtr<WKStringRef> wkKey(AdoptWK, WKStringCreateWithUTF8CString(key));
 
219
    WKTypeRef value = WKDictionaryGetItemForKey(dictionary, wkKey.get());
 
220
    if (WKGetTypeID(value) != WKBooleanGetTypeID()) {
 
221
        stringBuilder()->appendLiteral("Boolean value for key \"");
 
222
        stringBuilder()->append(key);
 
223
        stringBuilder()->appendLiteral("\" not found in dictionary\n");
 
224
        return false;
 
225
    }
 
226
    return WKBooleanGetValue(static_cast<WKBooleanRef>(value));
 
227
}
 
228
 
 
229
void InjectedBundle::beginTesting(WKDictionaryRef settings)
 
230
{
 
231
    m_state = Testing;
 
232
 
 
233
    m_pixelResult.clear();
 
234
    m_repaintRects.clear();
 
235
    m_stringBuilder->clear();
 
236
 
 
237
    m_testRunner = TestRunner::create();
 
238
    m_gcController = GCController::create();
 
239
    m_eventSendingController = EventSendingController::create();
 
240
    m_textInputController = TextInputController::create();
 
241
    m_accessibilityController = AccessibilityController::create();
 
242
 
 
243
    WKBundleSetShouldTrackVisitedLinks(m_bundle, false);
 
244
    WKBundleRemoveAllVisitedLinks(m_bundle);
 
245
    WKBundleSetAllowUniversalAccessFromFileURLs(m_bundle, m_pageGroup, true);
 
246
    WKBundleSetJavaScriptCanAccessClipboard(m_bundle, m_pageGroup, true);
 
247
    WKBundleSetPrivateBrowsingEnabled(m_bundle, m_pageGroup, false);
 
248
    WKBundleSetAuthorAndUserStylesEnabled(m_bundle, m_pageGroup, true);
 
249
    WKBundleSetFrameFlatteningEnabled(m_bundle, m_pageGroup, false);
 
250
    WKBundleSetMinimumLogicalFontSize(m_bundle, m_pageGroup, 9);
 
251
    WKBundleSetMinimumTimerInterval(m_bundle, m_pageGroup, 0.010); // 10 milliseconds (DOMTimer::s_minDefaultTimerInterval)
 
252
    WKBundleSetSpatialNavigationEnabled(m_bundle, m_pageGroup, false);
 
253
    WKBundleSetAllowFileAccessFromFileURLs(m_bundle, m_pageGroup, true);
 
254
    WKBundleSetPluginsEnabled(m_bundle, m_pageGroup, true);
 
255
    WKBundleSetPopupBlockingEnabled(m_bundle, m_pageGroup, false);
 
256
    WKBundleSetAlwaysAcceptCookies(m_bundle, false);
 
257
    WKBundleSetSerialLoadingEnabled(m_bundle, false);
 
258
    WKBundleSetShadowDOMEnabled(m_bundle, true);
 
259
    WKBundleSetCacheModel(m_bundle, 1 /*CacheModelDocumentBrowser*/);
 
260
 
 
261
    WKBundleRemoveAllUserContent(m_bundle, m_pageGroup);
 
262
 
 
263
    m_testRunner->setShouldDumpFrameLoadCallbacks(booleanForKey(settings, "DumpFrameLoadDelegates"));
 
264
    m_testRunner->setUserStyleSheetEnabled(false);
 
265
    m_testRunner->setXSSAuditorEnabled(false);
 
266
    m_testRunner->setCloseRemainingWindowsWhenComplete(false);
 
267
    m_testRunner->setAcceptsEditing(true);
 
268
    m_testRunner->setTabKeyCyclesThroughElements(true);
 
269
 
 
270
    page()->prepare();
 
271
 
 
272
    WKBundleClearAllDatabases(m_bundle);
 
273
    WKBundleClearApplicationCache(m_bundle);
 
274
    WKBundleResetOriginAccessWhitelists(m_bundle);
 
275
 
 
276
    // [WK2] REGRESSION(r128623): It made layout tests extremely slow
 
277
    // https://bugs.webkit.org/show_bug.cgi?id=96862
 
278
    // WKBundleSetDatabaseQuota(m_bundle, 5 * 1024 * 1024);
 
279
}
 
280
 
 
281
void InjectedBundle::done()
 
282
{
 
283
    m_state = Stopping;
 
284
 
 
285
    m_useWorkQueue = false;
 
286
 
 
287
    page()->stopLoading();
 
288
    setTopLoadingFrame(0);
 
289
 
 
290
    m_accessibilityController->resetToConsistentState();
 
291
 
 
292
    WKRetainPtr<WKStringRef> doneMessageName(AdoptWK, WKStringCreateWithUTF8CString("Done"));
 
293
    WKRetainPtr<WKMutableDictionaryRef> doneMessageBody(AdoptWK, WKMutableDictionaryCreate());
 
294
 
 
295
    WKRetainPtr<WKStringRef> textOutputKey(AdoptWK, WKStringCreateWithUTF8CString("TextOutput"));
 
296
    WKRetainPtr<WKStringRef> textOutput(AdoptWK, WKStringCreateWithUTF8CString(m_stringBuilder->toString().utf8().data()));
 
297
    WKDictionaryAddItem(doneMessageBody.get(), textOutputKey.get(), textOutput.get());
 
298
    
 
299
    WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
 
300
    WKDictionaryAddItem(doneMessageBody.get(), pixelResultKey.get(), m_pixelResult.get());
 
301
 
 
302
    WKRetainPtr<WKStringRef> repaintRectsKey = adoptWK(WKStringCreateWithUTF8CString("RepaintRects"));
 
303
    WKDictionaryAddItem(doneMessageBody.get(), repaintRectsKey.get(), m_repaintRects.get());
 
304
 
 
305
    WKBundlePostMessage(m_bundle, doneMessageName.get(), doneMessageBody.get());
 
306
 
 
307
    closeOtherPages();
 
308
 
 
309
    page()->resetAfterTest();
 
310
 
 
311
    m_state = Idle;
 
312
}
 
313
 
 
314
void InjectedBundle::closeOtherPages()
 
315
{
 
316
    Vector<WKBundlePageRef> pagesToClose;
 
317
    size_t size = m_pages.size();
 
318
    for (size_t i = 1; i < size; ++i)
 
319
        pagesToClose.append(m_pages[i]->page());
 
320
    size = pagesToClose.size();
 
321
    for (size_t i = 0; i < size; ++i)
 
322
        WKBundlePageClose(pagesToClose[i]);
 
323
}
 
324
 
 
325
void InjectedBundle::dumpBackForwardListsForAllPages()
 
326
{
 
327
    size_t size = m_pages.size();
 
328
    for (size_t i = 0; i < size; ++i)
 
329
        m_pages[i]->dumpBackForwardList();
 
330
}
 
331
    
 
332
void InjectedBundle::postNewBeforeUnloadReturnValue(bool value)
 
333
{
 
334
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("BeforeUnloadReturnValue"));
 
335
    WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(value));
 
336
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
337
}
 
338
 
 
339
void InjectedBundle::postAddChromeInputField()
 
340
{
 
341
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("AddChromeInputField"));
 
342
    WKBundlePostMessage(m_bundle, messageName.get(), 0);
 
343
}
 
344
 
 
345
void InjectedBundle::postRemoveChromeInputField()
 
346
{
 
347
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("RemoveChromeInputField"));
 
348
    WKBundlePostMessage(m_bundle, messageName.get(), 0);
 
349
}
 
350
 
 
351
void InjectedBundle::postFocusWebView()
 
352
{
 
353
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("FocusWebView"));
 
354
    WKBundlePostMessage(m_bundle, messageName.get(), 0);
 
355
}
 
356
 
 
357
void InjectedBundle::postSetBackingScaleFactor(double backingScaleFactor)
 
358
{
 
359
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetBackingScaleFactor"));
 
360
    WKRetainPtr<WKDoubleRef> messageBody(AdoptWK, WKDoubleCreate(backingScaleFactor));
 
361
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
362
}
 
363
 
 
364
void InjectedBundle::postSetWindowIsKey(bool isKey)
 
365
{
 
366
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetWindowIsKey"));
 
367
    WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(isKey));
 
368
    WKBundlePostSynchronousMessage(m_bundle, messageName.get(), messageBody.get(), 0);
 
369
}
 
370
 
 
371
void InjectedBundle::postSimulateWebNotificationClick(uint64_t notificationID)
 
372
{
 
373
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SimulateWebNotificationClick"));
 
374
    WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(notificationID));
 
375
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
376
}
 
377
 
 
378
void InjectedBundle::setGeolocationPermission(bool enabled)
 
379
{
 
380
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetGeolocationPermission"));
 
381
    WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(enabled));
 
382
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
383
}
 
384
 
 
385
void InjectedBundle::setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
 
386
{
 
387
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetMockGeolocationPosition"));
 
388
 
 
389
    WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
 
390
 
 
391
    WKRetainPtr<WKStringRef> latitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("latitude"));
 
392
    WKRetainPtr<WKDoubleRef> latitudeWK(AdoptWK, WKDoubleCreate(latitude));
 
393
    WKDictionaryAddItem(messageBody.get(), latitudeKeyWK.get(), latitudeWK.get());
 
394
 
 
395
    WKRetainPtr<WKStringRef> longitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("longitude"));
 
396
    WKRetainPtr<WKDoubleRef> longitudeWK(AdoptWK, WKDoubleCreate(longitude));
 
397
    WKDictionaryAddItem(messageBody.get(), longitudeKeyWK.get(), longitudeWK.get());
 
398
 
 
399
    WKRetainPtr<WKStringRef> accuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("accuracy"));
 
400
    WKRetainPtr<WKDoubleRef> accuracyWK(AdoptWK, WKDoubleCreate(accuracy));
 
401
    WKDictionaryAddItem(messageBody.get(), accuracyKeyWK.get(), accuracyWK.get());
 
402
 
 
403
    WKRetainPtr<WKStringRef> providesAltitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitude"));
 
404
    WKRetainPtr<WKBooleanRef> providesAltitudeWK(AdoptWK, WKBooleanCreate(providesAltitude));
 
405
    WKDictionaryAddItem(messageBody.get(), providesAltitudeKeyWK.get(), providesAltitudeWK.get());
 
406
 
 
407
    WKRetainPtr<WKStringRef> altitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitude"));
 
408
    WKRetainPtr<WKDoubleRef> altitudeWK(AdoptWK, WKDoubleCreate(altitude));
 
409
    WKDictionaryAddItem(messageBody.get(), altitudeKeyWK.get(), altitudeWK.get());
 
410
 
 
411
    WKRetainPtr<WKStringRef> providesAltitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitudeAccuracy"));
 
412
    WKRetainPtr<WKBooleanRef> providesAltitudeAccuracyWK(AdoptWK, WKBooleanCreate(providesAltitudeAccuracy));
 
413
    WKDictionaryAddItem(messageBody.get(), providesAltitudeAccuracyKeyWK.get(), providesAltitudeAccuracyWK.get());
 
414
 
 
415
    WKRetainPtr<WKStringRef> altitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitudeAccuracy"));
 
416
    WKRetainPtr<WKDoubleRef> altitudeAccuracyWK(AdoptWK, WKDoubleCreate(altitudeAccuracy));
 
417
    WKDictionaryAddItem(messageBody.get(), altitudeAccuracyKeyWK.get(), altitudeAccuracyWK.get());
 
418
 
 
419
    WKRetainPtr<WKStringRef> providesHeadingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesHeading"));
 
420
    WKRetainPtr<WKBooleanRef> providesHeadingWK(AdoptWK, WKBooleanCreate(providesHeading));
 
421
    WKDictionaryAddItem(messageBody.get(), providesHeadingKeyWK.get(), providesHeadingWK.get());
 
422
 
 
423
    WKRetainPtr<WKStringRef> headingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("heading"));
 
424
    WKRetainPtr<WKDoubleRef> headingWK(AdoptWK, WKDoubleCreate(heading));
 
425
    WKDictionaryAddItem(messageBody.get(), headingKeyWK.get(), headingWK.get());
 
426
 
 
427
    WKRetainPtr<WKStringRef> providesSpeedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesSpeed"));
 
428
    WKRetainPtr<WKBooleanRef> providesSpeedWK(AdoptWK, WKBooleanCreate(providesSpeed));
 
429
    WKDictionaryAddItem(messageBody.get(), providesSpeedKeyWK.get(), providesSpeedWK.get());
 
430
 
 
431
    WKRetainPtr<WKStringRef> speedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("speed"));
 
432
    WKRetainPtr<WKDoubleRef> speedWK(AdoptWK, WKDoubleCreate(speed));
 
433
    WKDictionaryAddItem(messageBody.get(), speedKeyWK.get(), speedWK.get());
 
434
 
 
435
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
436
}
 
437
 
 
438
void InjectedBundle::setMockGeolocationPositionUnavailableError(WKStringRef errorMessage)
 
439
{
 
440
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetMockGeolocationPositionUnavailableError"));
 
441
    WKBundlePostMessage(m_bundle, messageName.get(), errorMessage);
 
442
}
 
443
 
 
444
void InjectedBundle::setCustomPolicyDelegate(bool enabled, bool permissive)
 
445
{
 
446
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("SetCustomPolicyDelegate"));
 
447
 
 
448
    WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
 
449
 
 
450
    WKRetainPtr<WKStringRef> enabledKeyWK(AdoptWK, WKStringCreateWithUTF8CString("enabled"));
 
451
    WKRetainPtr<WKBooleanRef> enabledWK(AdoptWK, WKBooleanCreate(enabled));
 
452
    WKDictionaryAddItem(messageBody.get(), enabledKeyWK.get(), enabledWK.get());
 
453
 
 
454
    WKRetainPtr<WKStringRef> permissiveKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permissive"));
 
455
    WKRetainPtr<WKBooleanRef> permissiveWK(AdoptWK, WKBooleanCreate(permissive));
 
456
    WKDictionaryAddItem(messageBody.get(), permissiveKeyWK.get(), permissiveWK.get());
 
457
 
 
458
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
459
}
 
460
 
 
461
bool InjectedBundle::shouldProcessWorkQueue() const
 
462
{
 
463
    if (!m_useWorkQueue)
 
464
        return false;
 
465
 
 
466
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("IsWorkQueueEmpty"));
 
467
    WKTypeRef resultToPass = 0;
 
468
    WKBundlePostSynchronousMessage(m_bundle, messageName.get(), 0, &resultToPass);
 
469
    WKRetainPtr<WKBooleanRef> isEmpty(AdoptWK, static_cast<WKBooleanRef>(resultToPass));
 
470
 
 
471
    return !WKBooleanGetValue(isEmpty.get());
 
472
}
 
473
 
 
474
void InjectedBundle::processWorkQueue()
 
475
{
 
476
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("ProcessWorkQueue"));
 
477
    WKBundlePostMessage(m_bundle, messageName.get(), 0);
 
478
}
 
479
 
 
480
void InjectedBundle::queueBackNavigation(unsigned howFarBackward)
 
481
{
 
482
    m_useWorkQueue = true;
 
483
 
 
484
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueBackNavigation"));
 
485
    WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(howFarBackward));
 
486
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
487
}
 
488
 
 
489
void InjectedBundle::queueForwardNavigation(unsigned howFarForward)
 
490
{
 
491
    m_useWorkQueue = true;
 
492
 
 
493
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueForwardNavigation"));
 
494
    WKRetainPtr<WKUInt64Ref> messageBody(AdoptWK, WKUInt64Create(howFarForward));
 
495
    WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
 
496
}
 
497
 
 
498
void InjectedBundle::queueLoad(WKStringRef url, WKStringRef target)
 
499
{
 
500
    m_useWorkQueue = true;
 
501
 
 
502
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoad"));
 
503
 
 
504
    WKRetainPtr<WKMutableDictionaryRef> loadData(AdoptWK, WKMutableDictionaryCreate());
 
505
 
 
506
    WKRetainPtr<WKStringRef> urlKey(AdoptWK, WKStringCreateWithUTF8CString("url"));
 
507
    WKDictionaryAddItem(loadData.get(), urlKey.get(), url);
 
508
 
 
509
    WKRetainPtr<WKStringRef> targetKey(AdoptWK, WKStringCreateWithUTF8CString("target"));
 
510
    WKDictionaryAddItem(loadData.get(), targetKey.get(), target);
 
511
 
 
512
    WKBundlePostMessage(m_bundle, messageName.get(), loadData.get());
 
513
}
 
514
 
 
515
void InjectedBundle::queueLoadHTMLString(WKStringRef content, WKStringRef baseURL, WKStringRef unreachableURL)
 
516
{
 
517
    m_useWorkQueue = true;
 
518
 
 
519
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoadHTMLString"));
 
520
 
 
521
    WKRetainPtr<WKMutableDictionaryRef> loadData(AdoptWK, WKMutableDictionaryCreate());
 
522
 
 
523
    WKRetainPtr<WKStringRef> contentKey(AdoptWK, WKStringCreateWithUTF8CString("content"));
 
524
    WKDictionaryAddItem(loadData.get(), contentKey.get(), content);
 
525
 
 
526
    if (baseURL) {
 
527
        WKRetainPtr<WKStringRef> baseURLKey(AdoptWK, WKStringCreateWithUTF8CString("baseURL"));
 
528
        WKDictionaryAddItem(loadData.get(), baseURLKey.get(), baseURL);
 
529
    }
 
530
 
 
531
    if (unreachableURL) {
 
532
        WKRetainPtr<WKStringRef> unreachableURLKey(AdoptWK, WKStringCreateWithUTF8CString("unreachableURL"));
 
533
        WKDictionaryAddItem(loadData.get(), unreachableURLKey.get(), unreachableURL);
 
534
    }
 
535
 
 
536
    WKBundlePostMessage(m_bundle, messageName.get(), loadData.get());
 
537
}
 
538
 
 
539
void InjectedBundle::queueReload()
 
540
{
 
541
    m_useWorkQueue = true;
 
542
 
 
543
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueReload"));
 
544
    WKBundlePostMessage(m_bundle, messageName.get(), 0);
 
545
}
 
546
 
 
547
void InjectedBundle::queueLoadingScript(WKStringRef script)
 
548
{
 
549
    m_useWorkQueue = true;
 
550
 
 
551
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueLoadingScript"));
 
552
    WKBundlePostMessage(m_bundle, messageName.get(), script);
 
553
}
 
554
 
 
555
void InjectedBundle::queueNonLoadingScript(WKStringRef script)
 
556
{
 
557
    m_useWorkQueue = true;
 
558
 
 
559
    WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("QueueNonLoadingScript"));
 
560
    WKBundlePostMessage(m_bundle, messageName.get(), script);
 
561
}
 
562
 
 
563
} // namespace WTR