1/*
2 * Copyright (C) 2010-2016 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#pragma once
27
28#include "EventSendingController.h"
29#include "GCController.h"
30#include "TestRunner.h"
31#include "TextInputController.h"
32#include <WebKit/WKBase.h>
33#include <WebKit/WKRetainPtr.h>
34#include <sstream>
35#include <wtf/Forward.h>
36#include <wtf/RefPtr.h>
37#include <wtf/Vector.h>
38
39#if HAVE(ACCESSIBILITY)
40#include "AccessibilityController.h"
41#endif
42
43namespace WTR {
44
45class InjectedBundlePage;
46
47class InjectedBundle {
48public:
49 static InjectedBundle& singleton();
50
51 // Initialize the InjectedBundle.
52 void initialize(WKBundleRef, WKTypeRef initializationUserData);
53
54 WKBundleRef bundle() const { return m_bundle; }
55 WKBundlePageGroupRef pageGroup() const { return m_pageGroup; }
56
57 TestRunner* testRunner() { return m_testRunner.get(); }
58 GCController* gcController() { return m_gcController.get(); }
59 EventSendingController* eventSendingController() { return m_eventSendingController.get(); }
60 TextInputController* textInputController() { return m_textInputController.get(); }
61#if HAVE(ACCESSIBILITY)
62 AccessibilityController* accessibilityController() { return m_accessibilityController.get(); }
63#endif
64
65 InjectedBundlePage* page() const;
66 size_t pageCount() const { return m_pages.size(); }
67 void closeOtherPages();
68
69 void dumpBackForwardListsForAllPages(StringBuilder&);
70
71 void done();
72 void setAudioResult(WKDataRef audioData) { m_audioResult = audioData; }
73 void setPixelResult(WKImageRef image) { m_pixelResult = image; m_pixelResultIsPending = false; }
74 void setPixelResultIsPending(bool isPending) { m_pixelResultIsPending = isPending; }
75 void setRepaintRects(WKArrayRef rects) { m_repaintRects = rects; }
76
77 bool isTestRunning() { return m_state == Testing; }
78
79 WKBundleFrameRef topLoadingFrame() { return m_topLoadingFrame; }
80 void setTopLoadingFrame(WKBundleFrameRef frame) { m_topLoadingFrame = frame; }
81
82 bool shouldDumpPixels() const { return m_dumpPixels; }
83 bool dumpJSConsoleLogInStdErr() const { return m_dumpJSConsoleLogInStdErr; };
84
85 void outputText(const String&);
86 void dumpToStdErr(const String&);
87 void postNewBeforeUnloadReturnValue(bool);
88 void postAddChromeInputField();
89 void postRemoveChromeInputField();
90 void postFocusWebView();
91 void postSetBackingScaleFactor(double);
92 void postSetWindowIsKey(bool);
93 void postSetViewSize(double width, double height);
94 void postSimulateWebNotificationClick(uint64_t notificationID);
95 void postSetAddsVisitedLinks(bool);
96
97 // Geolocation.
98 void setGeolocationPermission(bool);
99 void setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, bool providesFloorLevel, double floorLevel);
100 void setMockGeolocationPositionUnavailableError(WKStringRef errorMessage);
101 bool isGeolocationProviderActive() const;
102
103 // MediaStream.
104 void setUserMediaPermission(bool);
105 void resetUserMediaPermission();
106 void setUserMediaPersistentPermissionForOrigin(bool permission, WKStringRef origin, WKStringRef parentOrigin);
107 unsigned userMediaPermissionRequestCountForOrigin(WKStringRef origin, WKStringRef parentOrigin) const;
108 void resetUserMediaPermissionRequestCountForOrigin(WKStringRef origin, WKStringRef parentOrigin);
109
110 // Policy delegate.
111 void setCustomPolicyDelegate(bool enabled, bool permissive);
112
113 // Page Visibility.
114 void setHidden(bool);
115
116 // Cache.
117 void setCacheModel(int);
118
119 // Work queue.
120 bool shouldProcessWorkQueue() const;
121 void processWorkQueue();
122 void queueBackNavigation(unsigned howFarBackward);
123 void queueForwardNavigation(unsigned howFarForward);
124 void queueLoad(WKStringRef url, WKStringRef target, bool shouldOpenExternalURLs = false);
125 void queueLoadHTMLString(WKStringRef content, WKStringRef baseURL = 0, WKStringRef unreachableURL = 0);
126 void queueReload();
127 void queueLoadingScript(WKStringRef script);
128 void queueNonLoadingScript(WKStringRef script);
129
130 bool isAllowedHost(WKStringRef);
131
132 unsigned imageCountInGeneralPasteboard() const;
133
134 void setAllowsAnySSLCertificate(bool);
135
136 void statisticsNotifyObserver();
137
138 void textDidChangeInTextField();
139 void textFieldDidBeginEditing();
140 void textFieldDidEndEditing();
141
142 void reportLiveDocuments(WKBundlePageRef);
143
144 void resetUserScriptInjectedCount() { m_userScriptInjectedCount = 0; }
145 void increaseUserScriptInjectedCount() { ++m_userScriptInjectedCount; }
146 size_t userScriptInjectedCount() const { return m_userScriptInjectedCount; }
147
148private:
149 InjectedBundle() = default;
150 ~InjectedBundle();
151
152 static void didCreatePage(WKBundleRef, WKBundlePageRef, const void* clientInfo);
153 static void willDestroyPage(WKBundleRef, WKBundlePageRef, const void* clientInfo);
154 static void didInitializePageGroup(WKBundleRef, WKBundlePageGroupRef, const void* clientInfo);
155 static void didReceiveMessage(WKBundleRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo);
156 static void didReceiveMessageToPage(WKBundleRef, WKBundlePageRef, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo);
157
158 void didCreatePage(WKBundlePageRef);
159 void willDestroyPage(WKBundlePageRef);
160 void didInitializePageGroup(WKBundlePageGroupRef);
161 void didReceiveMessage(WKStringRef messageName, WKTypeRef messageBody);
162 void didReceiveMessageToPage(WKBundlePageRef, WKStringRef messageName, WKTypeRef messageBody);
163
164 void setUpInjectedBundleClients(WKBundlePageRef);
165
166 void platformInitialize(WKTypeRef initializationUserData);
167 void resetLocalSettings();
168
169 enum class BegingTestingMode { New, Resume };
170 void beginTesting(WKDictionaryRef initialSettings, BegingTestingMode);
171
172 bool booleanForKey(WKDictionaryRef, const char* key);
173 String stringForKey(WKDictionaryRef, const char* key);
174
175 WKBundleRef m_bundle { nullptr };
176 WKBundlePageGroupRef m_pageGroup { nullptr };
177 Vector<std::unique_ptr<InjectedBundlePage>> m_pages;
178
179#if HAVE(ACCESSIBILITY)
180 RefPtr<AccessibilityController> m_accessibilityController;
181#endif
182 RefPtr<TestRunner> m_testRunner;
183 RefPtr<GCController> m_gcController;
184 RefPtr<EventSendingController> m_eventSendingController;
185 RefPtr<TextInputController> m_textInputController;
186
187 WKBundleFrameRef m_topLoadingFrame { nullptr };
188
189 enum State {
190 Idle,
191 Testing,
192 Stopping
193 };
194 State m_state { Idle };
195
196 bool m_dumpPixels { false };
197 bool m_useWorkQueue { false };
198 bool m_pixelResultIsPending { false };
199 bool m_dumpJSConsoleLogInStdErr { false };
200
201 WTF::Seconds m_timeout;
202
203 WKRetainPtr<WKDataRef> m_audioResult;
204 WKRetainPtr<WKImageRef> m_pixelResult;
205 WKRetainPtr<WKArrayRef> m_repaintRects;
206
207 Vector<String> m_allowedHosts;
208
209 size_t m_userScriptInjectedCount { 0 };
210};
211
212} // namespace WTR
213