2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "InjectedBundle.h"
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.h>
38 #include <wtf/PassOwnPtr.h>
39 #include <wtf/Vector.h>
43 InjectedBundle& InjectedBundle::shared()
45 static InjectedBundle& shared = *new InjectedBundle;
49 InjectedBundle::InjectedBundle()
51 , m_topLoadingFrame(0)
57 void InjectedBundle::didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
59 static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didCreatePage(page);
62 void InjectedBundle::willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
64 static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->willDestroyPage(page);
67 void InjectedBundle::didInitializePageGroup(WKBundleRef bundle, WKBundlePageGroupRef pageGroup, const void* clientInfo)
69 static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didInitializePageGroup(pageGroup);
72 void InjectedBundle::didReceiveMessage(WKBundleRef bundle, WKStringRef messageName, WKTypeRef messageBody, const void *clientInfo)
74 static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didReceiveMessage(messageName, messageBody);
77 void InjectedBundle::initialize(WKBundleRef bundle, WKTypeRef initializationUserData)
81 WKBundleClient client = {
82 kWKBundleClientCurrentVersion,
86 didInitializePageGroup,
89 WKBundleSetClient(m_bundle, &client);
91 platformInitialize(initializationUserData);
94 WKBundleActivateMacFontAscentHack(m_bundle);
97 void InjectedBundle::didCreatePage(WKBundlePageRef page)
99 m_pages.append(adoptPtr(new InjectedBundlePage(page)));
102 void InjectedBundle::willDestroyPage(WKBundlePageRef page)
104 size_t size = m_pages.size();
105 for (size_t i = 0; i < size; ++i) {
106 if (m_pages[i]->page() == page) {
113 void InjectedBundle::didInitializePageGroup(WKBundlePageGroupRef pageGroup)
115 m_pageGroup = pageGroup;
118 InjectedBundlePage* InjectedBundle::page() const
120 // It might be better to have the UI process send over a reference to the main
121 // page instead of just assuming it's the first one.
122 return m_pages[0].get();
125 void InjectedBundle::resetLocalSettings()
127 setlocale(LC_ALL, "");
130 void InjectedBundle::didReceiveMessage(WKStringRef messageName, WKTypeRef messageBody)
132 if (WKStringIsEqualToUTF8CString(messageName, "BeginTest")) {
134 ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
135 m_dumpPixels = WKBooleanGetValue(static_cast<WKBooleanRef>(messageBody));
137 WKRetainPtr<WKStringRef> ackMessageName(AdoptWK, WKStringCreateWithUTF8CString("Ack"));
138 WKRetainPtr<WKStringRef> ackMessageBody(AdoptWK, WKStringCreateWithUTF8CString("BeginTest"));
139 WKBundlePostMessage(m_bundle, ackMessageName.get(), ackMessageBody.get());
143 } else if (WKStringIsEqualToUTF8CString(messageName, "Reset")) {
145 ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
146 WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
148 WKRetainPtr<WKStringRef> shouldGCKey(AdoptWK, WKStringCreateWithUTF8CString("ShouldGC"));
149 bool shouldGC = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, shouldGCKey.get())));
152 WKBundleGarbageCollectJavaScriptObjects(m_bundle);
155 m_dumpPixels = false;
157 resetLocalSettings();
162 WKRetainPtr<WKStringRef> errorMessageName(AdoptWK, WKStringCreateWithUTF8CString("Error"));
163 WKRetainPtr<WKStringRef> errorMessageBody(AdoptWK, WKStringCreateWithUTF8CString("Unknown"));
164 WKBundlePostMessage(m_bundle, errorMessageName.get(), errorMessageBody.get());
167 void InjectedBundle::beginTesting()
171 m_outputStream.str("");
172 m_pixelResult.clear();
174 m_layoutTestController = LayoutTestController::create();
175 m_gcController = GCController::create();
176 m_eventSendingController = EventSendingController::create();
178 WKBundleSetShouldTrackVisitedLinks(m_bundle, false);
179 WKBundleRemoveAllVisitedLinks(m_bundle);
180 WKBundleSetAllowUniversalAccessFromFileURLs(m_bundle, m_pageGroup, true);
181 WKBundleSetJavaScriptCanAccessClipboard(m_bundle, m_pageGroup, true);
182 WKBundleSetPrivateBrowsingEnabled(m_bundle, m_pageGroup, false);
183 WKBundleSwitchNetworkLoaderToNewTestingSession(m_bundle);
184 WKBundleSetAuthorAndUserStylesEnabled(m_bundle, m_pageGroup, true);
185 WKBundleSetFrameFlatteningEnabled(m_bundle, m_pageGroup, false);
187 WKBundleRemoveAllUserContent(m_bundle, m_pageGroup);
191 WKBundleClearAllDatabases(m_bundle);
192 WKBundleResetOriginAccessWhitelists(m_bundle);
195 void InjectedBundle::done()
199 page()->stopLoading();
200 setTopLoadingFrame(0);
202 WKRetainPtr<WKStringRef> doneMessageName(AdoptWK, WKStringCreateWithUTF8CString("Done"));
203 WKRetainPtr<WKMutableDictionaryRef> doneMessageBody(AdoptWK, WKMutableDictionaryCreate());
205 WKRetainPtr<WKStringRef> textOutputKey(AdoptWK, WKStringCreateWithUTF8CString("TextOutput"));
206 WKRetainPtr<WKStringRef> textOutput(AdoptWK, WKStringCreateWithUTF8CString(m_outputStream.str().c_str()));
207 WKDictionaryAddItem(doneMessageBody.get(), textOutputKey.get(), textOutput.get());
209 WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
210 WKDictionaryAddItem(doneMessageBody.get(), pixelResultKey.get(), m_pixelResult.get());
212 WKBundlePostMessage(m_bundle, doneMessageName.get(), doneMessageBody.get());
219 void InjectedBundle::closeOtherPages()
221 Vector<WKBundlePageRef> pagesToClose;
222 size_t size = m_pages.size();
223 for (size_t i = 1; i < size; ++i)
224 pagesToClose.append(m_pages[i]->page());
225 size = pagesToClose.size();
226 for (size_t i = 0; i < size; ++i)
227 WKBundlePageClose(pagesToClose[i]);
230 void InjectedBundle::dumpBackForwardListsForAllPages()
232 size_t size = m_pages.size();
233 for (size_t i = 0; i < size; ++i)
234 m_pages[i]->dumpBackForwardList();
237 void InjectedBundle::postNewBeforeUnloadReturnValue(bool value)
239 WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("BeforeUnloadReturnValue"));
240 WKRetainPtr<WKBooleanRef> messageBody(AdoptWK, WKBooleanCreate(value));
241 WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());