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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* Copyright (C) 2012 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "InjectedBundleTest.h"
#include <WebKit2/WKBundleDOMWindowExtension.h>
#include <WebKit2/WKBundleFrame.h>
#include <WebKit2/WKBundlePage.h>
#include <WebKit2/WKBundlePageGroup.h>
#include <WebKit2/WKBundlePrivate.h>
#include <WebKit2/WKBundleScriptWorld.h>
#include <WebKit2/WKRetainPtr.h>
#include <wtf/HashMap.h>
#include <assert.h>
namespace TestWebKitAPI {
static void didFinishLoadForFrameCallback(WKBundlePageRef, WKBundleFrameRef, WKTypeRef*, const void* clientInfo);
static void globalObjectIsAvailableForFrameCallback(WKBundlePageRef, WKBundleFrameRef, WKBundleScriptWorldRef, const void* clientInfo);
static void willDisconnectDOMWindowExtensionFromGlobalObjectCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef, const void* clientInfo);
static void didReconnectDOMWindowExtensionToGlobalObjectCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef, const void* clientInfo);
static void willDestroyGlobalObjectForDOMWindowExtensionCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef, const void* clientInfo);
enum ExtensionState {
Uncreated = 0, Connected, Disconnected, Destroyed, Removed
};
const char* states[5] = {
"Uncreated",
"Connected",
"Disconnected",
"Destroyed",
"Removed"
};
typedef struct {
const char* name;
ExtensionState state;
} ExtensionRecord;
class DOMWindowExtensionNoCache : public InjectedBundleTest {
public:
DOMWindowExtensionNoCache(const std::string& identifier);
virtual void initialize(WKBundleRef, WKTypeRef userData);
virtual void didCreatePage(WKBundleRef, WKBundlePageRef);
virtual void willDestroyPage(WKBundleRef, WKBundlePageRef);
void globalObjectIsAvailableForFrame(WKBundleFrameRef, WKBundleScriptWorldRef);
void willDisconnectDOMWindowExtensionFromGlobalObject(WKBundleDOMWindowExtensionRef);
void didReconnectDOMWindowExtensionToGlobalObject(WKBundleDOMWindowExtensionRef);
void willDestroyGlobalObjectForDOMWindowExtension(WKBundleDOMWindowExtensionRef);
void frameLoadFinished(WKBundleFrameRef);
private:
void updateExtensionStateRecord(WKBundleDOMWindowExtensionRef, ExtensionState);
void sendExtensionStateMessage();
void sendBundleMessage(const char*);
WKBundlePageGroupRef m_pageGroup;
WKBundleRef m_bundle;
ExtensionRecord m_extensionRecords[10];
HashMap<WKBundleDOMWindowExtensionRef, int> m_extensionToRecordMap;
int m_numberMainFrameLoads;
};
static InjectedBundleTest::Register<DOMWindowExtensionNoCache> registrar("DOMWindowExtensionNoCache");
DOMWindowExtensionNoCache::DOMWindowExtensionNoCache(const std::string& identifier)
: InjectedBundleTest(identifier)
, m_numberMainFrameLoads(0)
{
m_extensionRecords[0].name = "First page, main frame, standard world";
m_extensionRecords[1].name = "First page, main frame, non-standard world";
m_extensionRecords[2].name = "First page, subframe, standard world";
m_extensionRecords[3].name = "First page, subframe, non-standard world";
m_extensionRecords[4].name = "Second page, main frame, standard world";
m_extensionRecords[5].name = "Second page, main frame, non-standard world";
m_extensionRecords[6].name = "First page, main frame, standard world";
m_extensionRecords[7].name = "First page, main frame, non-standard world";
m_extensionRecords[8].name = "First page, subframe, standard world";
m_extensionRecords[9].name = "First page, subframe, non-standard world";
for (size_t i = 0; i < 10; ++i)
m_extensionRecords[i].state = Uncreated;
}
void DOMWindowExtensionNoCache::frameLoadFinished(WKBundleFrameRef frame)
{
bool mainFrame = !WKBundleFrameGetParentFrame(frame);
if (mainFrame)
m_numberMainFrameLoads++;
char body[16384];
sprintf(body, "%s finished loading", mainFrame ? "Main frame" : "Subframe");
// Only consider load finished for the main frame
const char* name = mainFrame ? "DidFinishLoadForMainFrame" : "DidFinishLoadForFrame";
WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString(name));
WKRetainPtr<WKStringRef> messageBody = adoptWK(WKStringCreateWithUTF8CString(body));
WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
sendExtensionStateMessage();
}
void DOMWindowExtensionNoCache::sendExtensionStateMessage()
{
char body[16384];
sprintf(body, "Extension states:\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s\n%s - %s",
m_extensionRecords[0].name, states[m_extensionRecords[0].state],
m_extensionRecords[1].name, states[m_extensionRecords[1].state],
m_extensionRecords[2].name, states[m_extensionRecords[2].state],
m_extensionRecords[3].name, states[m_extensionRecords[3].state],
m_extensionRecords[4].name, states[m_extensionRecords[4].state],
m_extensionRecords[5].name, states[m_extensionRecords[5].state],
m_extensionRecords[6].name, states[m_extensionRecords[6].state],
m_extensionRecords[7].name, states[m_extensionRecords[7].state],
m_extensionRecords[8].name, states[m_extensionRecords[8].state],
m_extensionRecords[9].name, states[m_extensionRecords[9].state]);
WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("ExtensionStates"));
WKRetainPtr<WKStringRef> messageBody = adoptWK(WKStringCreateWithUTF8CString(body));
WKBundlePostMessage(m_bundle, messageName.get(), messageBody.get());
}
void DOMWindowExtensionNoCache::initialize(WKBundleRef bundle, WKTypeRef userData)
{
assert(WKGetTypeID(userData) == WKBundlePageGroupGetTypeID());
WKBundlePageGroupRef pageGroup = static_cast<WKBundlePageGroupRef>(userData);
WKRetainPtr<WKStringRef> source(AdoptWK, WKStringCreateWithUTF8CString("alert('Unimportant alert');"));
WKBundleAddUserScript(bundle, pageGroup, WKBundleScriptWorldCreateWorld(), source.get(), 0, 0, 0, kWKInjectAtDocumentStart, kWKInjectInAllFrames);
}
void DOMWindowExtensionNoCache::didCreatePage(WKBundleRef bundle, WKBundlePageRef page)
{
m_bundle = bundle;
WKBundlePageLoaderClient pageLoaderClient;
memset(&pageLoaderClient, 0, sizeof(pageLoaderClient));
pageLoaderClient.version = kWKBundlePageLoaderClientCurrentVersion;
pageLoaderClient.clientInfo = this;
pageLoaderClient.didFinishLoadForFrame = didFinishLoadForFrameCallback;
pageLoaderClient.globalObjectIsAvailableForFrame = globalObjectIsAvailableForFrameCallback;
pageLoaderClient.willDisconnectDOMWindowExtensionFromGlobalObject = willDisconnectDOMWindowExtensionFromGlobalObjectCallback;
pageLoaderClient.didReconnectDOMWindowExtensionToGlobalObject = didReconnectDOMWindowExtensionToGlobalObjectCallback;
pageLoaderClient.willDestroyGlobalObjectForDOMWindowExtension = willDestroyGlobalObjectForDOMWindowExtensionCallback;
WKBundlePageSetPageLoaderClient(page, &pageLoaderClient);
}
void DOMWindowExtensionNoCache::willDestroyPage(WKBundleRef, WKBundlePageRef)
{
HashMap<WKBundleDOMWindowExtensionRef, int>::iterator it = m_extensionToRecordMap.begin();
HashMap<WKBundleDOMWindowExtensionRef, int>::iterator end = m_extensionToRecordMap.end();
for (; it != end; ++it) {
updateExtensionStateRecord(it->first, Removed);
WKRelease(it->first);
}
m_extensionToRecordMap.clear();
sendExtensionStateMessage();
sendBundleMessage("TestComplete");
}
void DOMWindowExtensionNoCache::updateExtensionStateRecord(WKBundleDOMWindowExtensionRef extension, ExtensionState state)
{
int index = m_extensionToRecordMap.get(extension);
m_extensionRecords[index].state = state;
}
void DOMWindowExtensionNoCache::sendBundleMessage(const char* message)
{
WKRetainPtr<WKStringRef> wkMessage = adoptWK(WKStringCreateWithUTF8CString(message));
WKBundlePostMessage(m_bundle, wkMessage.get(), wkMessage.get());
}
void DOMWindowExtensionNoCache::globalObjectIsAvailableForFrame(WKBundleFrameRef frame, WKBundleScriptWorldRef world)
{
WKBundleDOMWindowExtensionRef extension = WKBundleDOMWindowExtensionCreate(frame, world);
int index;
bool standard;
standard = world == WKBundleScriptWorldNormalWorld();
bool mainFrame = !WKBundleFrameGetParentFrame(frame);
switch (m_numberMainFrameLoads) {
case 0:
index = mainFrame ? (standard ? 0 : 1) : (standard ? 2 : 3);
break;
case 1:
index = standard ? 4 : 5;
break;
case 2:
index = mainFrame ? (standard ? 6 : 7) : (standard ? 8 : 9);
break;
default:
ASSERT_NOT_REACHED();
break;
}
m_extensionToRecordMap.set(extension, index);
updateExtensionStateRecord(extension, Connected);
sendBundleMessage("GlobalObjectIsAvailableForFrame called");
}
void DOMWindowExtensionNoCache::willDisconnectDOMWindowExtensionFromGlobalObject(WKBundleDOMWindowExtensionRef extension)
{
// No items should be going into a 0-capacity page cache.
ASSERT_NOT_REACHED();
}
void DOMWindowExtensionNoCache::didReconnectDOMWindowExtensionToGlobalObject(WKBundleDOMWindowExtensionRef)
{
// No items should be coming out of a 0-capacity page cache.
ASSERT_NOT_REACHED();
}
void DOMWindowExtensionNoCache::willDestroyGlobalObjectForDOMWindowExtension(WKBundleDOMWindowExtensionRef extension)
{
sendBundleMessage("WillDestroyDOMWindowExtensionToGlobalObject called");
updateExtensionStateRecord(extension, Destroyed);
m_extensionToRecordMap.remove(extension);
WKRelease(extension);
}
static void didFinishLoadForFrameCallback(WKBundlePageRef, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
{
((DOMWindowExtensionNoCache*)clientInfo)->frameLoadFinished(frame);
}
static void globalObjectIsAvailableForFrameCallback(WKBundlePageRef, WKBundleFrameRef frame, WKBundleScriptWorldRef world, const void* clientInfo)
{
((DOMWindowExtensionNoCache*)clientInfo)->globalObjectIsAvailableForFrame(frame, world);
}
static void willDisconnectDOMWindowExtensionFromGlobalObjectCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef extension, const void* clientInfo)
{
((DOMWindowExtensionNoCache*)clientInfo)->willDisconnectDOMWindowExtensionFromGlobalObject(extension);
}
static void didReconnectDOMWindowExtensionToGlobalObjectCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef extension, const void* clientInfo)
{
((DOMWindowExtensionNoCache*)clientInfo)->didReconnectDOMWindowExtensionToGlobalObject(extension);
}
static void willDestroyGlobalObjectForDOMWindowExtensionCallback(WKBundlePageRef, WKBundleDOMWindowExtensionRef extension , const void* clientInfo)
{
((DOMWindowExtensionNoCache*)clientInfo)->willDestroyGlobalObjectForDOMWindowExtension(extension);
}
} // namespace TestWebKitAPI
|