InjectedBundlePage.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  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. #include "config.h"
  26. #include "InjectedBundlePage.h"
  27. #include "InjectedBundle.h"
  28. #include "StringFunctions.h"
  29. #include "WebCoreTestSupport.h"
  30. #include <cmath>
  31. #include <JavaScriptCore/JSRetainPtr.h>
  32. #include <WebKit2/WKArray.h>
  33. #include <WebKit2/WKBundle.h>
  34. #include <WebKit2/WKBundleBackForwardList.h>
  35. #include <WebKit2/WKBundleBackForwardListItem.h>
  36. #include <WebKit2/WKBundleFrame.h>
  37. #include <WebKit2/WKBundleFramePrivate.h>
  38. #include <WebKit2/WKBundleHitTestResult.h>
  39. #include <WebKit2/WKBundleNavigationAction.h>
  40. #include <WebKit2/WKBundleNodeHandlePrivate.h>
  41. #include <WebKit2/WKBundlePagePrivate.h>
  42. #include <WebKit2/WKBundlePrivate.h>
  43. #include <WebKit2/WKSecurityOrigin.h>
  44. #include <WebKit2/WKURLRequest.h>
  45. #include <wtf/HashMap.h>
  46. #include <wtf/text/CString.h>
  47. #include <wtf/text/StringBuilder.h>
  48. #if USE(CF) && !PLATFORM(QT)
  49. #include "WebArchiveDumpSupport.h"
  50. #endif
  51. #if PLATFORM(QT)
  52. #include "DumpRenderTreeSupportQt.h"
  53. #endif
  54. using namespace std;
  55. namespace WTR {
  56. static bool hasPrefix(const WTF::String& searchString, const WTF::String& prefix)
  57. {
  58. return searchString.length() >= prefix.length() && searchString.substring(0, prefix.length()) == prefix;
  59. }
  60. static JSValueRef propertyValue(JSContextRef context, JSObjectRef object, const char* propertyName)
  61. {
  62. if (!object)
  63. return 0;
  64. JSRetainPtr<JSStringRef> propertyNameString(Adopt, JSStringCreateWithUTF8CString(propertyName));
  65. return JSObjectGetProperty(context, object, propertyNameString.get(), 0);
  66. }
  67. static double propertyValueDouble(JSContextRef context, JSObjectRef object, const char* propertyName)
  68. {
  69. JSValueRef value = propertyValue(context, object, propertyName);
  70. if (!value)
  71. return 0;
  72. return JSValueToNumber(context, value, 0);
  73. }
  74. static int propertyValueInt(JSContextRef context, JSObjectRef object, const char* propertyName)
  75. {
  76. return static_cast<int>(propertyValueDouble(context, object, propertyName));
  77. }
  78. static double numericWindowPropertyValue(WKBundleFrameRef frame, const char* propertyName)
  79. {
  80. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  81. return propertyValueDouble(context, JSContextGetGlobalObject(context), propertyName);
  82. }
  83. static WTF::String dumpPath(JSGlobalContextRef context, JSObjectRef nodeValue)
  84. {
  85. JSValueRef nodeNameValue = propertyValue(context, nodeValue, "nodeName");
  86. JSRetainPtr<JSStringRef> jsStringNodeName(Adopt, JSValueToStringCopy(context, nodeNameValue, 0));
  87. WKRetainPtr<WKStringRef> nodeName = toWK(jsStringNodeName);
  88. JSValueRef parentNode = propertyValue(context, nodeValue, "parentNode");
  89. StringBuilder stringBuilder;
  90. stringBuilder.append(toWTFString(nodeName));
  91. if (parentNode && JSValueIsObject(context, parentNode)) {
  92. stringBuilder.appendLiteral(" > ");
  93. stringBuilder.append(dumpPath(context, (JSObjectRef)parentNode));
  94. }
  95. return stringBuilder.toString();
  96. }
  97. static WTF::String dumpPath(WKBundlePageRef page, WKBundleScriptWorldRef world, WKBundleNodeHandleRef node)
  98. {
  99. if (!node)
  100. return "(null)";
  101. WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
  102. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContextForWorld(frame, world);
  103. JSValueRef nodeValue = WKBundleFrameGetJavaScriptWrapperForNodeForWorld(frame, node, world);
  104. ASSERT(JSValueIsObject(context, nodeValue));
  105. JSObjectRef nodeObject = (JSObjectRef)nodeValue;
  106. return dumpPath(context, nodeObject);
  107. }
  108. static WTF::String rangeToStr(WKBundlePageRef page, WKBundleScriptWorldRef world, WKBundleRangeHandleRef rangeRef)
  109. {
  110. if (!rangeRef)
  111. return "(null)";
  112. WKBundleFrameRef frame = WKBundlePageGetMainFrame(page);
  113. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContextForWorld(frame, world);
  114. JSValueRef rangeValue = WKBundleFrameGetJavaScriptWrapperForRangeForWorld(frame, rangeRef, world);
  115. ASSERT(JSValueIsObject(context, rangeValue));
  116. JSObjectRef rangeObject = (JSObjectRef)rangeValue;
  117. JSValueRef startNodeValue = propertyValue(context, rangeObject, "startContainer");
  118. ASSERT(JSValueIsObject(context, startNodeValue));
  119. JSObjectRef startNodeObject = (JSObjectRef)startNodeValue;
  120. JSValueRef endNodeValue = propertyValue(context, rangeObject, "endContainer");
  121. ASSERT(JSValueIsObject(context, endNodeValue));
  122. JSObjectRef endNodeObject = (JSObjectRef)endNodeValue;
  123. int startOffset = propertyValueInt(context, rangeObject, "startOffset");
  124. int endOffset = propertyValueInt(context, rangeObject, "endOffset");
  125. StringBuilder stringBuilder;
  126. stringBuilder.appendLiteral("range from ");
  127. stringBuilder.appendNumber(startOffset);
  128. stringBuilder.appendLiteral(" of ");
  129. stringBuilder.append(dumpPath(context, startNodeObject));
  130. stringBuilder.appendLiteral(" to ");
  131. stringBuilder.appendNumber(endOffset);
  132. stringBuilder.appendLiteral(" of ");
  133. stringBuilder.append(dumpPath(context, endNodeObject));
  134. return stringBuilder.toString();
  135. }
  136. static WKRetainPtr<WKStringRef> NavigationTypeToString(WKFrameNavigationType type)
  137. {
  138. switch (type) {
  139. case kWKFrameNavigationTypeLinkClicked:
  140. return adoptWK(WKStringCreateWithUTF8CString("link clicked"));
  141. case kWKFrameNavigationTypeFormSubmitted:
  142. return adoptWK(WKStringCreateWithUTF8CString("form submitted"));
  143. case kWKFrameNavigationTypeBackForward:
  144. return adoptWK(WKStringCreateWithUTF8CString("back/forward"));
  145. case kWKFrameNavigationTypeReload:
  146. return adoptWK(WKStringCreateWithUTF8CString("reload"));
  147. case kWKFrameNavigationTypeFormResubmitted:
  148. return adoptWK(WKStringCreateWithUTF8CString("form resubmitted"));
  149. case kWKFrameNavigationTypeOther:
  150. return adoptWK(WKStringCreateWithUTF8CString("other"));
  151. }
  152. return adoptWK(WKStringCreateWithUTF8CString("illegal value"));
  153. }
  154. static WTF::String styleDecToStr(WKBundleCSSStyleDeclarationRef style)
  155. {
  156. // DumpRenderTree calls -[DOMCSSStyleDeclaration description], which just dumps class name and object address.
  157. // No existing tests actually hit this code path at the time of this writing, because WebCore doesn't call
  158. // the editing client if the styling operation source is CommandFromDOM or CommandFromDOMWithUserInterface.
  159. StringBuilder stringBuilder;
  160. stringBuilder.appendLiteral("<DOMCSSStyleDeclaration ADDRESS>");
  161. return stringBuilder.toString();
  162. }
  163. static WTF::String securityOriginToStr(WKSecurityOriginRef origin)
  164. {
  165. StringBuilder stringBuilder;
  166. stringBuilder.append('{');
  167. stringBuilder.append(toWTFString(adoptWK(WKSecurityOriginCopyProtocol(origin))));
  168. stringBuilder.appendLiteral(", ");
  169. stringBuilder.append(toWTFString(adoptWK(WKSecurityOriginCopyHost(origin))));
  170. stringBuilder.appendLiteral(", ");
  171. stringBuilder.appendNumber(WKSecurityOriginGetPort(origin));
  172. stringBuilder.append('}');
  173. return stringBuilder.toString();
  174. }
  175. static WTF::String frameToStr(WKBundleFrameRef frame)
  176. {
  177. WKRetainPtr<WKStringRef> name(AdoptWK, WKBundleFrameCopyName(frame));
  178. StringBuilder stringBuilder;
  179. if (WKBundleFrameIsMainFrame(frame)) {
  180. if (!WKStringIsEmpty(name.get())) {
  181. stringBuilder.appendLiteral("main frame \"");
  182. stringBuilder.append(toWTFString(name));
  183. stringBuilder.append('"');
  184. } else
  185. stringBuilder.appendLiteral("main frame");
  186. } else {
  187. if (!WKStringIsEmpty(name.get())) {
  188. stringBuilder.appendLiteral("frame \"");
  189. stringBuilder.append(toWTFString(name));
  190. stringBuilder.append('"');
  191. }
  192. else
  193. stringBuilder.appendLiteral("frame (anonymous)");
  194. }
  195. return stringBuilder.toString();
  196. }
  197. static inline bool isLocalFileScheme(WKStringRef scheme)
  198. {
  199. return WKStringIsEqualToUTF8CStringIgnoringCase(scheme, "file");
  200. }
  201. static const char divider = '/';
  202. static inline WTF::String pathSuitableForTestResult(WKURLRef fileUrl)
  203. {
  204. if (!fileUrl)
  205. return String();
  206. WKRetainPtr<WKStringRef> schemeString = adoptWK(WKURLCopyScheme(fileUrl));
  207. if (!isLocalFileScheme(schemeString.get()))
  208. return toWTFString(adoptWK(WKURLCopyString(fileUrl)));
  209. String pathString = toWTFString(adoptWK(WKURLCopyPath(fileUrl)));
  210. StringBuilder stringBuilder;
  211. // Remove the leading path from file urls.
  212. const size_t indexBaseName = pathString.reverseFind(divider);
  213. if (indexBaseName != notFound) {
  214. const size_t indexDirName = pathString.reverseFind(divider, indexBaseName - 1);
  215. if (indexDirName != notFound)
  216. stringBuilder.append(pathString.substring(indexDirName + 1, indexBaseName - indexDirName - 1));
  217. stringBuilder.append(divider);
  218. stringBuilder.append(pathString.substring(indexBaseName + 1)); // Filename.
  219. } else {
  220. stringBuilder.append(divider);
  221. stringBuilder.append(pathString); // Return "/pathString".
  222. }
  223. return stringBuilder.toString();
  224. }
  225. static inline WTF::String urlSuitableForTestResult(WKURLRef fileUrl)
  226. {
  227. if (!fileUrl)
  228. return String();
  229. WKRetainPtr<WKStringRef> schemeString = adoptWK(WKURLCopyScheme(fileUrl));
  230. if (!isLocalFileScheme(schemeString.get()))
  231. return toWTFString(adoptWK(WKURLCopyString(fileUrl)));
  232. WTF::String urlString = toWTFString(adoptWK(WKURLCopyString(fileUrl)));
  233. const size_t indexBaseName = urlString.reverseFind(divider);
  234. return (indexBaseName == notFound) ? urlString : urlString.substring(indexBaseName + 1);
  235. }
  236. static HashMap<uint64_t, String> assignedUrlsCache;
  237. static inline void dumpResourceURL(uint64_t identifier, StringBuilder& stringBuilder)
  238. {
  239. if (assignedUrlsCache.contains(identifier))
  240. stringBuilder.append(assignedUrlsCache.get(identifier));
  241. else
  242. stringBuilder.appendLiteral("<unknown>");
  243. }
  244. InjectedBundlePage::InjectedBundlePage(WKBundlePageRef page)
  245. : m_page(page)
  246. , m_world(AdoptWK, WKBundleScriptWorldCreateWorld())
  247. {
  248. WKBundlePageLoaderClient loaderClient = {
  249. kWKBundlePageLoaderClientCurrentVersion,
  250. this,
  251. didStartProvisionalLoadForFrame,
  252. didReceiveServerRedirectForProvisionalLoadForFrame,
  253. didFailProvisionalLoadWithErrorForFrame,
  254. didCommitLoadForFrame,
  255. didFinishDocumentLoadForFrame,
  256. didFinishLoadForFrame,
  257. didFailLoadWithErrorForFrame,
  258. didSameDocumentNavigationForFrame,
  259. didReceiveTitleForFrame,
  260. 0, // didFirstLayoutForFrame
  261. 0, // didFirstVisuallyNonEmptyLayoutForFrame
  262. 0, // didRemoveFrameFromHierarchy
  263. didDisplayInsecureContentForFrame,
  264. didRunInsecureContentForFrame,
  265. didClearWindowForFrame,
  266. didCancelClientRedirectForFrame,
  267. willPerformClientRedirectForFrame,
  268. didHandleOnloadEventsForFrame,
  269. 0, // didLayoutForFrame
  270. 0, // didNewFirstVisuallyNonEmptyLayoutForFrame
  271. didDetectXSSForFrame,
  272. 0, // shouldGoToBackForwardListItem
  273. 0, // didCreateGlobalObjectForFrame
  274. 0, // willDisconnectDOMWindowExtensionFromGlobalObject
  275. 0, // didReconnectDOMWindowExtensionToGlobalObject
  276. 0, // willDestroyGlobalObjectForDOMWindowExtension
  277. didFinishProgress, // didFinishProgress
  278. 0, // shouldForceUniversalAccessFromLocalURL
  279. 0, // didReceiveIntentForFrame
  280. 0, // registerIntentServiceForFrame
  281. 0, // didLayout
  282. 0, // featuresUsedInPage
  283. 0, // willLoadURLRequest
  284. 0, // willLoadDataRequest
  285. };
  286. WKBundlePageSetPageLoaderClient(m_page, &loaderClient);
  287. WKBundlePageResourceLoadClient resourceLoadClient = {
  288. kWKBundlePageResourceLoadClientCurrentVersion,
  289. this,
  290. didInitiateLoadForResource,
  291. willSendRequestForFrame,
  292. didReceiveResponseForResource,
  293. didReceiveContentLengthForResource,
  294. didFinishLoadForResource,
  295. didFailLoadForResource,
  296. shouldCacheResponse,
  297. 0 // shouldUseCredentialStorage
  298. };
  299. WKBundlePageSetResourceLoadClient(m_page, &resourceLoadClient);
  300. WKBundlePagePolicyClient policyClient = {
  301. kWKBundlePagePolicyClientCurrentVersion,
  302. this,
  303. decidePolicyForNavigationAction,
  304. decidePolicyForNewWindowAction,
  305. decidePolicyForResponse,
  306. unableToImplementPolicy
  307. };
  308. WKBundlePageSetPolicyClient(m_page, &policyClient);
  309. WKBundlePageUIClient uiClient = {
  310. kWKBundlePageUIClientCurrentVersion,
  311. this,
  312. willAddMessageToConsole,
  313. willSetStatusbarText,
  314. willRunJavaScriptAlert,
  315. willRunJavaScriptConfirm,
  316. willRunJavaScriptPrompt,
  317. 0, /*mouseDidMoveOverElement*/
  318. 0, /*pageDidScroll*/
  319. 0, /*paintCustomOverhangArea*/
  320. 0, /*shouldGenerateFileForUpload*/
  321. 0, /*generateFileForUpload*/
  322. 0, /*shouldRubberBandInDirection*/
  323. 0, /*statusBarIsVisible*/
  324. 0, /*menuBarIsVisible*/
  325. 0, /*toolbarsAreVisible*/
  326. didReachApplicationCacheOriginQuota,
  327. didExceedDatabaseQuota,
  328. 0, /*plugInStartLabelTitle*/
  329. 0, /*plugInStartLabelSubtitle*/
  330. 0, /*plugInExtraStyleSheet*/
  331. 0, /*plugInExtraScript*/
  332. };
  333. WKBundlePageSetUIClient(m_page, &uiClient);
  334. WKBundlePageEditorClient editorClient = {
  335. kWKBundlePageEditorClientCurrentVersion,
  336. this,
  337. shouldBeginEditing,
  338. shouldEndEditing,
  339. shouldInsertNode,
  340. shouldInsertText,
  341. shouldDeleteRange,
  342. shouldChangeSelectedRange,
  343. shouldApplyStyle,
  344. didBeginEditing,
  345. didEndEditing,
  346. didChange,
  347. didChangeSelection,
  348. 0, /* willWriteToPasteboard */
  349. 0, /* getPasteboardDataForRange */
  350. 0 /* didWriteToPasteboard */
  351. };
  352. WKBundlePageSetEditorClient(m_page, &editorClient);
  353. #if ENABLE(FULLSCREEN_API)
  354. WKBundlePageFullScreenClient fullScreenClient = {
  355. kWKBundlePageFullScreenClientCurrentVersion,
  356. this,
  357. supportsFullScreen,
  358. enterFullScreenForElement,
  359. exitFullScreenForElement,
  360. beganEnterFullScreen,
  361. beganExitFullScreen,
  362. closeFullScreen,
  363. };
  364. WKBundlePageSetFullScreenClient(m_page, &fullScreenClient);
  365. #endif
  366. }
  367. InjectedBundlePage::~InjectedBundlePage()
  368. {
  369. }
  370. void InjectedBundlePage::stopLoading()
  371. {
  372. WKBundlePageStopLoading(m_page);
  373. }
  374. void InjectedBundlePage::prepare()
  375. {
  376. WKBundlePageClearMainFrameName(m_page);
  377. WKBundlePageSetPageZoomFactor(m_page, 1);
  378. WKBundlePageSetTextZoomFactor(m_page, 1);
  379. WKPoint origin = { 0, 0 };
  380. WKBundlePageSetScaleAtOrigin(m_page, 1, origin);
  381. m_previousTestBackForwardListItem = adoptWK(WKBundleBackForwardListCopyItemAtIndex(WKBundlePageGetBackForwardList(m_page), 0));
  382. WKBundleFrameClearOpener(WKBundlePageGetMainFrame(m_page));
  383. WKBundlePageSetTracksRepaints(m_page, false);
  384. }
  385. void InjectedBundlePage::resetAfterTest()
  386. {
  387. WKBundleFrameRef frame = WKBundlePageGetMainFrame(m_page);
  388. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  389. #if PLATFORM(QT)
  390. DumpRenderTreeSupportQt::resetInternalsObject(context);
  391. #else
  392. WebCoreTestSupport::resetInternalsObject(context);
  393. #endif
  394. assignedUrlsCache.clear();
  395. }
  396. // Loader Client Callbacks
  397. // String output must be identical to -[WebFrame _drt_descriptionSuitableForTestResult].
  398. static void dumpFrameDescriptionSuitableForTestResult(WKBundleFrameRef frame, StringBuilder& stringBuilder)
  399. {
  400. WKRetainPtr<WKStringRef> name(AdoptWK, WKBundleFrameCopyName(frame));
  401. if (WKBundleFrameIsMainFrame(frame)) {
  402. if (WKStringIsEmpty(name.get())) {
  403. stringBuilder.appendLiteral("main frame");
  404. return;
  405. }
  406. stringBuilder.appendLiteral("main frame \"");
  407. stringBuilder.append(toWTFString(name));
  408. stringBuilder.append('"');
  409. return;
  410. }
  411. if (WKStringIsEmpty(name.get())) {
  412. stringBuilder.appendLiteral("frame (anonymous)");
  413. return;
  414. }
  415. stringBuilder.appendLiteral("frame \"");
  416. stringBuilder.append(toWTFString(name));
  417. stringBuilder.append('"');
  418. }
  419. static void dumpLoadEvent(WKBundleFrameRef frame, const char* eventName)
  420. {
  421. StringBuilder stringBuilder;
  422. dumpFrameDescriptionSuitableForTestResult(frame, stringBuilder);
  423. stringBuilder.appendLiteral(" - ");
  424. stringBuilder.append(eventName);
  425. stringBuilder.append('\n');
  426. InjectedBundle::shared().outputText(stringBuilder.toString());
  427. }
  428. static inline void dumpRequestDescriptionSuitableForTestResult(WKURLRequestRef request, StringBuilder& stringBuilder)
  429. {
  430. WKRetainPtr<WKURLRef> url = adoptWK(WKURLRequestCopyURL(request));
  431. WKRetainPtr<WKURLRef> firstParty = adoptWK(WKURLRequestCopyFirstPartyForCookies(request));
  432. WKRetainPtr<WKStringRef> httpMethod = adoptWK(WKURLRequestCopyHTTPMethod(request));
  433. stringBuilder.appendLiteral("<NSURLRequest URL ");
  434. stringBuilder.append(pathSuitableForTestResult(url.get()));
  435. stringBuilder.appendLiteral(", main document URL ");
  436. stringBuilder.append(urlSuitableForTestResult(firstParty.get()));
  437. stringBuilder.appendLiteral(", http method ");
  438. if (WKStringIsEmpty(httpMethod.get()))
  439. stringBuilder.appendLiteral("(none)");
  440. else
  441. stringBuilder.append(toWTFString(httpMethod));
  442. stringBuilder.append('>');
  443. }
  444. static inline void dumpResponseDescriptionSuitableForTestResult(WKURLResponseRef response, StringBuilder& stringBuilder)
  445. {
  446. WKRetainPtr<WKURLRef> url = adoptWK(WKURLResponseCopyURL(response));
  447. if (!url) {
  448. stringBuilder.appendLiteral("(null)");
  449. return;
  450. }
  451. stringBuilder.appendLiteral("<NSURLResponse ");
  452. stringBuilder.append(pathSuitableForTestResult(url.get()));
  453. stringBuilder.appendLiteral(", http status code ");
  454. stringBuilder.appendNumber(WKURLResponseHTTPStatusCode(response));
  455. stringBuilder.append('>');
  456. }
  457. static inline void dumpErrorDescriptionSuitableForTestResult(WKErrorRef error, StringBuilder& stringBuilder)
  458. {
  459. WKRetainPtr<WKStringRef> errorDomain = adoptWK(WKErrorCopyDomain(error));
  460. int errorCode = WKErrorGetErrorCode(error);
  461. // We need to do some error mapping here to match the test expectations (Mac error names are expected).
  462. if (WKStringIsEqualToUTF8CString(errorDomain.get(), "WebKitNetworkError")) {
  463. errorDomain = adoptWK(WKStringCreateWithUTF8CString("NSURLErrorDomain"));
  464. errorCode = -999;
  465. }
  466. if (WKStringIsEqualToUTF8CString(errorDomain.get(), "WebKitPolicyError"))
  467. errorDomain = adoptWK(WKStringCreateWithUTF8CString("WebKitErrorDomain"));
  468. stringBuilder.appendLiteral("<NSError domain ");
  469. stringBuilder.append(toWTFString(errorDomain));
  470. stringBuilder.appendLiteral(", code ");
  471. stringBuilder.appendNumber(errorCode);
  472. WKRetainPtr<WKURLRef> url = adoptWK(WKErrorCopyFailingURL(error));
  473. if (url.get()) {
  474. WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyString(url.get()));
  475. stringBuilder.appendLiteral(", failing URL \"");
  476. stringBuilder.append(toWTFString(urlString));
  477. stringBuilder.append('"');
  478. }
  479. stringBuilder.append('>');
  480. }
  481. void InjectedBundlePage::didStartProvisionalLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
  482. {
  483. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didStartProvisionalLoadForFrame(frame);
  484. }
  485. void InjectedBundlePage::didReceiveServerRedirectForProvisionalLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
  486. {
  487. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didReceiveServerRedirectForProvisionalLoadForFrame(frame);
  488. }
  489. void InjectedBundlePage::didFailProvisionalLoadWithErrorForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKErrorRef error, WKTypeRef*, const void *clientInfo)
  490. {
  491. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFailProvisionalLoadWithErrorForFrame(frame, error);
  492. }
  493. void InjectedBundlePage::didCommitLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
  494. {
  495. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didCommitLoadForFrame(frame);
  496. }
  497. void InjectedBundlePage::didFinishLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
  498. {
  499. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishLoadForFrame(frame);
  500. }
  501. void InjectedBundlePage::didFinishProgress(WKBundlePageRef, const void *clientInfo)
  502. {
  503. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishProgress();
  504. }
  505. void InjectedBundlePage::didFinishDocumentLoadForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
  506. {
  507. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishDocumentLoadForFrame(frame);
  508. }
  509. void InjectedBundlePage::didFailLoadWithErrorForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKErrorRef error, WKTypeRef*, const void *clientInfo)
  510. {
  511. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFailLoadWithErrorForFrame(frame, error);
  512. }
  513. void InjectedBundlePage::didReceiveTitleForFrame(WKBundlePageRef page, WKStringRef title, WKBundleFrameRef frame, WKTypeRef*, const void *clientInfo)
  514. {
  515. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didReceiveTitleForFrame(title, frame);
  516. }
  517. void InjectedBundlePage::didClearWindowForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKBundleScriptWorldRef world, const void *clientInfo)
  518. {
  519. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didClearWindowForFrame(frame, world);
  520. }
  521. void InjectedBundlePage::didCancelClientRedirectForFrame(WKBundlePageRef page, WKBundleFrameRef frame, const void* clientInfo)
  522. {
  523. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didCancelClientRedirectForFrame(frame);
  524. }
  525. void InjectedBundlePage::willPerformClientRedirectForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKURLRef url, double delay, double date, const void* clientInfo)
  526. {
  527. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willPerformClientRedirectForFrame(frame, url, delay, date);
  528. }
  529. void InjectedBundlePage::didSameDocumentNavigationForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKSameDocumentNavigationType type, WKTypeRef*, const void* clientInfo)
  530. {
  531. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didSameDocumentNavigationForFrame(frame, type);
  532. }
  533. void InjectedBundlePage::didHandleOnloadEventsForFrame(WKBundlePageRef page, WKBundleFrameRef frame, const void* clientInfo)
  534. {
  535. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didHandleOnloadEventsForFrame(frame);
  536. }
  537. void InjectedBundlePage::didDisplayInsecureContentForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
  538. {
  539. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didDisplayInsecureContentForFrame(frame);
  540. }
  541. void InjectedBundlePage::didDetectXSSForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
  542. {
  543. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didDetectXSSForFrame(frame);
  544. }
  545. void InjectedBundlePage::didRunInsecureContentForFrame(WKBundlePageRef page, WKBundleFrameRef frame, WKTypeRef*, const void* clientInfo)
  546. {
  547. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didRunInsecureContentForFrame(frame);
  548. }
  549. void InjectedBundlePage::didInitiateLoadForResource(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, WKURLRequestRef request, bool pageLoadIsProvisional, const void* clientInfo)
  550. {
  551. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didInitiateLoadForResource(page, frame, identifier, request, pageLoadIsProvisional);
  552. }
  553. WKURLRequestRef InjectedBundlePage::willSendRequestForFrame(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, WKURLRequestRef request, WKURLResponseRef redirectResponse, const void* clientInfo)
  554. {
  555. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willSendRequestForFrame(page, frame, identifier, request, redirectResponse);
  556. }
  557. void InjectedBundlePage::didReceiveResponseForResource(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, WKURLResponseRef response, const void* clientInfo)
  558. {
  559. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didReceiveResponseForResource(page, frame, identifier, response);
  560. }
  561. void InjectedBundlePage::didReceiveContentLengthForResource(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, uint64_t length, const void* clientInfo)
  562. {
  563. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didReceiveContentLengthForResource(page, frame, identifier, length);
  564. }
  565. void InjectedBundlePage::didFinishLoadForResource(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, const void* clientInfo)
  566. {
  567. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFinishLoadForResource(page, frame, identifier);
  568. }
  569. void InjectedBundlePage::didFailLoadForResource(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, WKErrorRef error, const void* clientInfo)
  570. {
  571. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didFailLoadForResource(page, frame, identifier, error);
  572. }
  573. bool InjectedBundlePage::shouldCacheResponse(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t identifier, const void* clientInfo)
  574. {
  575. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldCacheResponse(page, frame, identifier);
  576. }
  577. void InjectedBundlePage::didStartProvisionalLoadForFrame(WKBundleFrameRef frame)
  578. {
  579. if (!InjectedBundle::shared().isTestRunning())
  580. return;
  581. platformDidStartProvisionalLoadForFrame(frame);
  582. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  583. dumpLoadEvent(frame, "didStartProvisionalLoadForFrame");
  584. if (!InjectedBundle::shared().topLoadingFrame())
  585. InjectedBundle::shared().setTopLoadingFrame(frame);
  586. if (InjectedBundle::shared().testRunner()->shouldStopProvisionalFrameLoads())
  587. dumpLoadEvent(frame, "stopping load in didStartProvisionalLoadForFrame callback");
  588. }
  589. void InjectedBundlePage::didReceiveServerRedirectForProvisionalLoadForFrame(WKBundleFrameRef frame)
  590. {
  591. if (!InjectedBundle::shared().isTestRunning())
  592. return;
  593. if (!InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  594. return;
  595. dumpLoadEvent(frame, "didReceiveServerRedirectForProvisionalLoadForFrame");
  596. }
  597. void InjectedBundlePage::didFailProvisionalLoadWithErrorForFrame(WKBundleFrameRef frame, WKErrorRef)
  598. {
  599. if (!InjectedBundle::shared().isTestRunning())
  600. return;
  601. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  602. dumpLoadEvent(frame, "didFailProvisionalLoadWithError");
  603. frameDidChangeLocation(frame);
  604. }
  605. void InjectedBundlePage::didCommitLoadForFrame(WKBundleFrameRef frame)
  606. {
  607. if (!InjectedBundle::shared().isTestRunning())
  608. return;
  609. if (!InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  610. return;
  611. dumpLoadEvent(frame, "didCommitLoadForFrame");
  612. }
  613. void InjectedBundlePage::didFinishProgress()
  614. {
  615. if (!InjectedBundle::shared().isTestRunning())
  616. return;
  617. if (!InjectedBundle::shared().testRunner()->shouldDumpProgressFinishedCallback())
  618. return;
  619. InjectedBundle::shared().outputText("postProgressFinishedNotification\n");
  620. }
  621. enum FrameNamePolicy { ShouldNotIncludeFrameName, ShouldIncludeFrameName };
  622. static void dumpFrameScrollPosition(WKBundleFrameRef frame, StringBuilder& stringBuilder, FrameNamePolicy shouldIncludeFrameName = ShouldNotIncludeFrameName)
  623. {
  624. double x = numericWindowPropertyValue(frame, "pageXOffset");
  625. double y = numericWindowPropertyValue(frame, "pageYOffset");
  626. if (fabs(x) <= 0.00000001 && fabs(y) <= 0.00000001)
  627. return;
  628. if (shouldIncludeFrameName) {
  629. WKRetainPtr<WKStringRef> name(AdoptWK, WKBundleFrameCopyName(frame));
  630. stringBuilder.appendLiteral("frame '");
  631. stringBuilder.append(toWTFString(name));
  632. stringBuilder.appendLiteral("' ");
  633. }
  634. stringBuilder.appendLiteral("scrolled to ");
  635. stringBuilder.append(WTF::String::number(x));
  636. stringBuilder.append(',');
  637. stringBuilder.append(WTF::String::number(y));
  638. stringBuilder.append('\n');
  639. }
  640. static void dumpDescendantFrameScrollPositions(WKBundleFrameRef frame, StringBuilder& stringBuilder)
  641. {
  642. WKRetainPtr<WKArrayRef> childFrames(AdoptWK, WKBundleFrameCopyChildFrames(frame));
  643. size_t size = WKArrayGetSize(childFrames.get());
  644. for (size_t i = 0; i < size; ++i) {
  645. WKBundleFrameRef subframe = static_cast<WKBundleFrameRef>(WKArrayGetItemAtIndex(childFrames.get(), i));
  646. dumpFrameScrollPosition(subframe, stringBuilder, ShouldIncludeFrameName);
  647. dumpDescendantFrameScrollPositions(subframe, stringBuilder);
  648. }
  649. }
  650. void InjectedBundlePage::dumpAllFrameScrollPositions(StringBuilder& stringBuilder)
  651. {
  652. WKBundleFrameRef frame = WKBundlePageGetMainFrame(m_page);
  653. dumpFrameScrollPosition(frame, stringBuilder);
  654. dumpDescendantFrameScrollPositions(frame, stringBuilder);
  655. }
  656. static JSRetainPtr<JSStringRef> toJS(const char* string)
  657. {
  658. return JSRetainPtr<JSStringRef>(Adopt, JSStringCreateWithUTF8CString(string));
  659. }
  660. static bool hasDocumentElement(WKBundleFrameRef frame)
  661. {
  662. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame);
  663. JSObjectRef globalObject = JSContextGetGlobalObject(context);
  664. JSValueRef documentValue = JSObjectGetProperty(context, globalObject, toJS("document").get(), 0);
  665. if (!documentValue)
  666. return false;
  667. ASSERT(JSValueIsObject(context, documentValue));
  668. JSObjectRef document = JSValueToObject(context, documentValue, 0);
  669. JSValueRef documentElementValue = JSObjectGetProperty(context, document, toJS("documentElement").get(), 0);
  670. if (!documentElementValue)
  671. return false;
  672. return JSValueToBoolean(context, documentElementValue);
  673. }
  674. static void dumpFrameText(WKBundleFrameRef frame, StringBuilder& stringBuilder)
  675. {
  676. // If the frame doesn't have a document element, its inner text will be an empty string, so
  677. // we'll end up just appending a single newline below. But DumpRenderTree doesn't append
  678. // anything in this case, so we shouldn't either.
  679. if (!hasDocumentElement(frame))
  680. return;
  681. WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyInnerText(frame));
  682. stringBuilder.append(toWTFString(text));
  683. stringBuilder.append('\n');
  684. }
  685. static void dumpDescendantFramesText(WKBundleFrameRef frame, StringBuilder& stringBuilder)
  686. {
  687. WKRetainPtr<WKArrayRef> childFrames(AdoptWK, WKBundleFrameCopyChildFrames(frame));
  688. size_t size = WKArrayGetSize(childFrames.get());
  689. for (size_t i = 0; i < size; ++i) {
  690. WKBundleFrameRef subframe = static_cast<WKBundleFrameRef>(WKArrayGetItemAtIndex(childFrames.get(), i));
  691. WKRetainPtr<WKStringRef> subframeName(AdoptWK, WKBundleFrameCopyName(subframe));
  692. // DumpRenderTree ignores empty frames, so do the same thing here.
  693. if (!hasDocumentElement(subframe))
  694. continue;
  695. stringBuilder.appendLiteral("\n--------\nFrame: '");
  696. stringBuilder.append(toWTFString(subframeName));
  697. stringBuilder.appendLiteral("'\n--------\n");
  698. dumpFrameText(subframe, stringBuilder);
  699. dumpDescendantFramesText(subframe, stringBuilder);
  700. }
  701. }
  702. void InjectedBundlePage::dumpAllFramesText(StringBuilder& stringBuilder)
  703. {
  704. WKBundleFrameRef frame = WKBundlePageGetMainFrame(m_page);
  705. dumpFrameText(frame, stringBuilder);
  706. dumpDescendantFramesText(frame, stringBuilder);
  707. }
  708. void InjectedBundlePage::dumpDOMAsWebArchive(WKBundleFrameRef frame, StringBuilder& stringBuilder)
  709. {
  710. #if USE(CF) && !PLATFORM(QT)
  711. WKRetainPtr<WKDataRef> wkData = adoptWK(WKBundleFrameCopyWebArchive(frame));
  712. RetainPtr<CFDataRef> cfData = adoptCF(CFDataCreate(0, WKDataGetBytes(wkData.get()), WKDataGetSize(wkData.get())));
  713. RetainPtr<CFStringRef> cfString = adoptCF(createXMLStringFromWebArchiveData(cfData.get()));
  714. stringBuilder.append(cfString.get());
  715. #else
  716. UNUSED_PARAM(frame);
  717. UNUSED_PARAM(stringBuilder);
  718. #endif
  719. }
  720. void InjectedBundlePage::dump()
  721. {
  722. ASSERT(InjectedBundle::shared().isTestRunning());
  723. InjectedBundle::shared().testRunner()->invalidateWaitToDumpWatchdogTimer();
  724. // Force a paint before dumping. This matches DumpRenderTree on Windows. (DumpRenderTree on Mac
  725. // does this at a slightly different time.) See <http://webkit.org/b/55469> for details.
  726. WKBundlePageForceRepaint(m_page);
  727. WKBundleFrameRef frame = WKBundlePageGetMainFrame(m_page);
  728. WTF::String url = toWTFString(adoptWK(WKURLCopyString(adoptWK(WKBundleFrameCopyURL(frame)).get())));
  729. if (url.find("dumpAsText/") != WTF::notFound)
  730. InjectedBundle::shared().testRunner()->dumpAsText(false);
  731. StringBuilder stringBuilder;
  732. switch (InjectedBundle::shared().testRunner()->whatToDump()) {
  733. case TestRunner::RenderTree: {
  734. if (InjectedBundle::shared().testRunner()->isPrinting())
  735. stringBuilder.append(toWTFString(adoptWK(WKBundlePageCopyRenderTreeExternalRepresentationForPrinting(m_page)).get()));
  736. else
  737. stringBuilder.append(toWTFString(adoptWK(WKBundlePageCopyRenderTreeExternalRepresentation(m_page)).get()));
  738. break;
  739. }
  740. case TestRunner::MainFrameText:
  741. dumpFrameText(WKBundlePageGetMainFrame(m_page), stringBuilder);
  742. break;
  743. case TestRunner::AllFramesText:
  744. dumpAllFramesText(stringBuilder);
  745. break;
  746. case TestRunner::Audio:
  747. break;
  748. case TestRunner::DOMAsWebArchive:
  749. dumpDOMAsWebArchive(frame, stringBuilder);
  750. break;
  751. }
  752. if (InjectedBundle::shared().testRunner()->shouldDumpAllFrameScrollPositions())
  753. dumpAllFrameScrollPositions(stringBuilder);
  754. else if (InjectedBundle::shared().testRunner()->shouldDumpMainFrameScrollPosition())
  755. dumpFrameScrollPosition(WKBundlePageGetMainFrame(m_page), stringBuilder);
  756. if (InjectedBundle::shared().testRunner()->shouldDumpBackForwardListsForAllWindows())
  757. InjectedBundle::shared().dumpBackForwardListsForAllPages(stringBuilder);
  758. if (InjectedBundle::shared().shouldDumpPixels() && InjectedBundle::shared().testRunner()->shouldDumpPixels()) {
  759. WKSnapshotOptions options = kWKSnapshotOptionsShareable | kWKSnapshotOptionsInViewCoordinates;
  760. if (InjectedBundle::shared().testRunner()->shouldDumpSelectionRect())
  761. options |= kWKSnapshotOptionsPaintSelectionRectangle;
  762. InjectedBundle::shared().setPixelResult(adoptWK(WKBundlePageCreateSnapshotWithOptions(m_page, WKBundleFrameGetVisibleContentBounds(WKBundlePageGetMainFrame(m_page)), options)).get());
  763. if (WKBundlePageIsTrackingRepaints(m_page))
  764. InjectedBundle::shared().setRepaintRects(adoptWK(WKBundlePageCopyTrackedRepaintRects(m_page)).get());
  765. }
  766. InjectedBundle::shared().outputText(stringBuilder.toString());
  767. InjectedBundle::shared().done();
  768. }
  769. void InjectedBundlePage::didFinishLoadForFrame(WKBundleFrameRef frame)
  770. {
  771. if (!InjectedBundle::shared().isTestRunning())
  772. return;
  773. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  774. dumpLoadEvent(frame, "didFinishLoadForFrame");
  775. frameDidChangeLocation(frame, /*shouldDump*/ true);
  776. }
  777. void InjectedBundlePage::didFailLoadWithErrorForFrame(WKBundleFrameRef frame, WKErrorRef)
  778. {
  779. if (!InjectedBundle::shared().isTestRunning())
  780. return;
  781. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  782. dumpLoadEvent(frame, "didFailLoadWithError");
  783. frameDidChangeLocation(frame);
  784. }
  785. void InjectedBundlePage::didReceiveTitleForFrame(WKStringRef title, WKBundleFrameRef frame)
  786. {
  787. if (!InjectedBundle::shared().isTestRunning())
  788. return;
  789. StringBuilder stringBuilder;
  790. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks()) {
  791. dumpFrameDescriptionSuitableForTestResult(frame, stringBuilder);
  792. stringBuilder.appendLiteral(" - didReceiveTitle: ");
  793. stringBuilder.append(toWTFString(title));
  794. stringBuilder.append('\n');
  795. }
  796. if (InjectedBundle::shared().testRunner()->shouldDumpTitleChanges()) {
  797. stringBuilder.appendLiteral("TITLE CHANGED: '");
  798. stringBuilder.append(toWTFString(title));
  799. stringBuilder.appendLiteral("'\n");
  800. }
  801. InjectedBundle::shared().outputText(stringBuilder.toString());
  802. }
  803. void InjectedBundlePage::didClearWindowForFrame(WKBundleFrameRef frame, WKBundleScriptWorldRef world)
  804. {
  805. if (!InjectedBundle::shared().isTestRunning())
  806. return;
  807. JSGlobalContextRef context = WKBundleFrameGetJavaScriptContextForWorld(frame, world);
  808. JSObjectRef window = JSContextGetGlobalObject(context);
  809. if (WKBundleScriptWorldNormalWorld() != world) {
  810. JSObjectSetProperty(context, window, toJS("__worldID").get(), JSValueMakeNumber(context, TestRunner::worldIDForWorld(world)), kJSPropertyAttributeReadOnly, 0);
  811. return;
  812. }
  813. JSValueRef exception = 0;
  814. InjectedBundle::shared().testRunner()->makeWindowObject(context, window, &exception);
  815. InjectedBundle::shared().gcController()->makeWindowObject(context, window, &exception);
  816. InjectedBundle::shared().eventSendingController()->makeWindowObject(context, window, &exception);
  817. InjectedBundle::shared().textInputController()->makeWindowObject(context, window, &exception);
  818. InjectedBundle::shared().accessibilityController()->makeWindowObject(context, window, &exception);
  819. #if PLATFORM(MANX)
  820. InjectedBundle::shared().injectSceExtensions(frame, world);
  821. #endif
  822. #if PLATFORM(QT)
  823. DumpRenderTreeSupportQt::injectInternalsObject(context);
  824. #else
  825. WebCoreTestSupport::injectInternalsObject(context);
  826. #endif
  827. }
  828. void InjectedBundlePage::didCancelClientRedirectForFrame(WKBundleFrameRef frame)
  829. {
  830. if (!InjectedBundle::shared().isTestRunning())
  831. return;
  832. if (!InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  833. return;
  834. dumpLoadEvent(frame, "didCancelClientRedirectForFrame");
  835. }
  836. void InjectedBundlePage::willPerformClientRedirectForFrame(WKBundleFrameRef frame, WKURLRef url, double delay, double date)
  837. {
  838. if (!InjectedBundle::shared().isTestRunning())
  839. return;
  840. if (!InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  841. return;
  842. StringBuilder stringBuilder;
  843. dumpFrameDescriptionSuitableForTestResult(frame, stringBuilder);
  844. stringBuilder.appendLiteral(" - willPerformClientRedirectToURL: ");
  845. stringBuilder.append(pathSuitableForTestResult(url));
  846. stringBuilder.appendLiteral(" \n");
  847. InjectedBundle::shared().outputText(stringBuilder.toString());
  848. }
  849. void InjectedBundlePage::didSameDocumentNavigationForFrame(WKBundleFrameRef frame, WKSameDocumentNavigationType type)
  850. {
  851. }
  852. void InjectedBundlePage::didFinishDocumentLoadForFrame(WKBundleFrameRef frame)
  853. {
  854. if (!InjectedBundle::shared().isTestRunning())
  855. return;
  856. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  857. dumpLoadEvent(frame, "didFinishDocumentLoadForFrame");
  858. unsigned pendingFrameUnloadEvents = WKBundleFrameGetPendingUnloadCount(frame);
  859. if (pendingFrameUnloadEvents) {
  860. StringBuilder stringBuilder;
  861. stringBuilder.append(frameToStr(frame));
  862. stringBuilder.appendLiteral(" - has ");
  863. stringBuilder.appendNumber(pendingFrameUnloadEvents);
  864. stringBuilder.appendLiteral(" onunload handler(s)\n");
  865. InjectedBundle::shared().outputText(stringBuilder.toString());
  866. }
  867. }
  868. void InjectedBundlePage::didHandleOnloadEventsForFrame(WKBundleFrameRef frame)
  869. {
  870. if (!InjectedBundle::shared().isTestRunning())
  871. return;
  872. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  873. dumpLoadEvent(frame, "didHandleOnloadEventsForFrame");
  874. }
  875. void InjectedBundlePage::didDisplayInsecureContentForFrame(WKBundleFrameRef frame)
  876. {
  877. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  878. InjectedBundle::shared().outputText("didDisplayInsecureContent\n");
  879. }
  880. void InjectedBundlePage::didRunInsecureContentForFrame(WKBundleFrameRef frame)
  881. {
  882. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  883. InjectedBundle::shared().outputText("didRunInsecureContent\n");
  884. }
  885. void InjectedBundlePage::didDetectXSSForFrame(WKBundleFrameRef frame)
  886. {
  887. if (InjectedBundle::shared().testRunner()->shouldDumpFrameLoadCallbacks())
  888. InjectedBundle::shared().outputText("didDetectXSS\n");
  889. }
  890. void InjectedBundlePage::didInitiateLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, WKURLRequestRef request, bool)
  891. {
  892. if (!InjectedBundle::shared().isTestRunning())
  893. return;
  894. if (!InjectedBundle::shared().testRunner()->shouldDumpResourceLoadCallbacks())
  895. return;
  896. WKRetainPtr<WKURLRef> url = adoptWK(WKURLRequestCopyURL(request));
  897. assignedUrlsCache.add(identifier, pathSuitableForTestResult(url.get()));
  898. }
  899. // Resource Load Client Callbacks
  900. static inline bool isLocalHost(WKStringRef host)
  901. {
  902. return WKStringIsEqualToUTF8CString(host, "127.0.0.1") || WKStringIsEqualToUTF8CString(host, "localhost");
  903. }
  904. static inline bool isHTTPOrHTTPSScheme(WKStringRef scheme)
  905. {
  906. return WKStringIsEqualToUTF8CStringIgnoringCase(scheme, "http") || WKStringIsEqualToUTF8CStringIgnoringCase(scheme, "https");
  907. }
  908. WKURLRequestRef InjectedBundlePage::willSendRequestForFrame(WKBundlePageRef, WKBundleFrameRef frame, uint64_t identifier, WKURLRequestRef request, WKURLResponseRef response)
  909. {
  910. if (InjectedBundle::shared().isTestRunning()
  911. && InjectedBundle::shared().testRunner()->shouldDumpResourceLoadCallbacks()) {
  912. StringBuilder stringBuilder;
  913. dumpResourceURL(identifier, stringBuilder);
  914. stringBuilder.appendLiteral(" - willSendRequest ");
  915. dumpRequestDescriptionSuitableForTestResult(request, stringBuilder);
  916. stringBuilder.appendLiteral(" redirectResponse ");
  917. dumpResponseDescriptionSuitableForTestResult(response, stringBuilder);
  918. stringBuilder.append('\n');
  919. InjectedBundle::shared().outputText(stringBuilder.toString());
  920. }
  921. if (InjectedBundle::shared().isTestRunning() && InjectedBundle::shared().testRunner()->willSendRequestReturnsNull())
  922. return 0;
  923. WKRetainPtr<WKURLRef> redirectURL = adoptWK(WKURLResponseCopyURL(response));
  924. if (InjectedBundle::shared().isTestRunning() && InjectedBundle::shared().testRunner()->willSendRequestReturnsNullOnRedirect() && redirectURL) {
  925. InjectedBundle::shared().outputText("Returning null for this redirect\n");
  926. return 0;
  927. }
  928. WKRetainPtr<WKURLRef> url = adoptWK(WKURLRequestCopyURL(request));
  929. WKRetainPtr<WKStringRef> host = adoptWK(WKURLCopyHostName(url.get()));
  930. WKRetainPtr<WKStringRef> scheme = adoptWK(WKURLCopyScheme(url.get()));
  931. WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyString(url.get()));
  932. if (host && !WKStringIsEmpty(host.get())
  933. && isHTTPOrHTTPSScheme(scheme.get())
  934. && !WKStringIsEqualToUTF8CString(host.get(), "255.255.255.255") // Used in some tests that expect to get back an error.
  935. #if PLATFORM(MANX) && OS(ORBIS)
  936. && !strstr(toWTFString(urlString).utf8().data(), "/media/content/")
  937. #endif
  938. && !isLocalHost(host.get())) {
  939. bool mainFrameIsExternal = false;
  940. if (InjectedBundle::shared().isTestRunning()) {
  941. WKBundleFrameRef mainFrame = InjectedBundle::shared().topLoadingFrame();
  942. WKRetainPtr<WKURLRef> mainFrameURL = adoptWK(WKBundleFrameCopyURL(mainFrame));
  943. if (!mainFrameURL || WKStringIsEqualToUTF8CString(adoptWK(WKURLCopyString(mainFrameURL.get())).get(), "about:blank"))
  944. mainFrameURL = adoptWK(WKBundleFrameCopyProvisionalURL(mainFrame));
  945. WKRetainPtr<WKStringRef> mainFrameHost = WKURLCopyHostName(mainFrameURL.get());
  946. WKRetainPtr<WKStringRef> mainFrameScheme = WKURLCopyScheme(mainFrameURL.get());
  947. mainFrameIsExternal = isHTTPOrHTTPSScheme(mainFrameScheme.get()) && !isLocalHost(mainFrameHost.get());
  948. }
  949. if (!mainFrameIsExternal) {
  950. StringBuilder stringBuilder;
  951. stringBuilder.appendLiteral("Blocked access to external URL ");
  952. stringBuilder.append(toWTFString(urlString));
  953. stringBuilder.append('\n');
  954. InjectedBundle::shared().outputText(stringBuilder.toString());
  955. return 0;
  956. }
  957. }
  958. WKRetain(request);
  959. return request;
  960. }
  961. void InjectedBundlePage::didReceiveResponseForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, WKURLResponseRef response)
  962. {
  963. if (!InjectedBundle::shared().isTestRunning())
  964. return;
  965. if (InjectedBundle::shared().testRunner()->shouldDumpResourceLoadCallbacks()) {
  966. StringBuilder stringBuilder;
  967. dumpResourceURL(identifier, stringBuilder);
  968. stringBuilder.appendLiteral(" - didReceiveResponse ");
  969. dumpResponseDescriptionSuitableForTestResult(response, stringBuilder);
  970. stringBuilder.append('\n');
  971. InjectedBundle::shared().outputText(stringBuilder.toString());
  972. }
  973. if (!InjectedBundle::shared().testRunner()->shouldDumpResourceResponseMIMETypes())
  974. return;
  975. WKRetainPtr<WKURLRef> url = adoptWK(WKURLResponseCopyURL(response));
  976. WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyLastPathComponent(url.get()));
  977. WKRetainPtr<WKStringRef> mimeTypeString = adoptWK(WKURLResponseCopyMIMEType(response));
  978. StringBuilder stringBuilder;
  979. stringBuilder.append(toWTFString(urlString));
  980. stringBuilder.appendLiteral(" has MIME type ");
  981. stringBuilder.append(toWTFString(mimeTypeString));
  982. stringBuilder.append('\n');
  983. InjectedBundle::shared().outputText(stringBuilder.toString());
  984. }
  985. void InjectedBundlePage::didReceiveContentLengthForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t, uint64_t)
  986. {
  987. }
  988. void InjectedBundlePage::didFinishLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier)
  989. {
  990. if (!InjectedBundle::shared().isTestRunning())
  991. return;
  992. if (!InjectedBundle::shared().testRunner()->shouldDumpResourceLoadCallbacks())
  993. return;
  994. StringBuilder stringBuilder;
  995. dumpResourceURL(identifier, stringBuilder);
  996. stringBuilder.appendLiteral(" - didFinishLoading\n");
  997. InjectedBundle::shared().outputText(stringBuilder.toString());
  998. }
  999. void InjectedBundlePage::didFailLoadForResource(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier, WKErrorRef error)
  1000. {
  1001. if (!InjectedBundle::shared().isTestRunning())
  1002. return;
  1003. if (!InjectedBundle::shared().testRunner()->shouldDumpResourceLoadCallbacks())
  1004. return;
  1005. StringBuilder stringBuilder;
  1006. dumpResourceURL(identifier, stringBuilder);
  1007. stringBuilder.appendLiteral(" - didFailLoadingWithError: ");
  1008. dumpErrorDescriptionSuitableForTestResult(error, stringBuilder);
  1009. stringBuilder.append('\n');
  1010. InjectedBundle::shared().outputText(stringBuilder.toString());
  1011. }
  1012. bool InjectedBundlePage::shouldCacheResponse(WKBundlePageRef, WKBundleFrameRef, uint64_t identifier)
  1013. {
  1014. if (!InjectedBundle::shared().isTestRunning())
  1015. return true;
  1016. if (!InjectedBundle::shared().testRunner()->shouldDumpWillCacheResponse())
  1017. return true;
  1018. StringBuilder stringBuilder;
  1019. stringBuilder.appendNumber(identifier);
  1020. stringBuilder.appendLiteral(" - willCacheResponse: called\n");
  1021. InjectedBundle::shared().outputText(stringBuilder.toString());
  1022. // The default behavior is the cache the response.
  1023. return true;
  1024. }
  1025. // Policy Client Callbacks
  1026. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForNavigationAction(WKBundlePageRef page, WKBundleFrameRef frame, WKBundleNavigationActionRef navigationAction, WKURLRequestRef request, WKTypeRef* userData, const void* clientInfo)
  1027. {
  1028. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->decidePolicyForNavigationAction(page, frame, navigationAction, request, userData);
  1029. }
  1030. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForNewWindowAction(WKBundlePageRef page, WKBundleFrameRef frame, WKBundleNavigationActionRef navigationAction, WKURLRequestRef request, WKStringRef frameName, WKTypeRef* userData, const void* clientInfo)
  1031. {
  1032. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->decidePolicyForNewWindowAction(page, frame, navigationAction, request, frameName, userData);
  1033. }
  1034. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForResponse(WKBundlePageRef page, WKBundleFrameRef frame, WKURLResponseRef response, WKURLRequestRef request, WKTypeRef* userData, const void* clientInfo)
  1035. {
  1036. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->decidePolicyForResponse(page, frame, response, request, userData);
  1037. }
  1038. void InjectedBundlePage::unableToImplementPolicy(WKBundlePageRef page, WKBundleFrameRef frame, WKErrorRef error, WKTypeRef* userData, const void* clientInfo)
  1039. {
  1040. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->unableToImplementPolicy(page, frame, error, userData);
  1041. }
  1042. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForNavigationAction(WKBundlePageRef page, WKBundleFrameRef frame, WKBundleNavigationActionRef navigationAction, WKURLRequestRef request, WKTypeRef* userData)
  1043. {
  1044. if (!InjectedBundle::shared().isTestRunning())
  1045. return WKBundlePagePolicyActionUse;
  1046. if (!InjectedBundle::shared().testRunner()->isPolicyDelegateEnabled())
  1047. return WKBundlePagePolicyActionUse;
  1048. WKRetainPtr<WKURLRef> url = adoptWK(WKURLRequestCopyURL(request));
  1049. WKRetainPtr<WKStringRef> urlScheme = adoptWK(WKURLCopyScheme(url.get()));
  1050. StringBuilder stringBuilder;
  1051. stringBuilder.appendLiteral("Policy delegate: attempt to load ");
  1052. if (isLocalFileScheme(urlScheme.get())) {
  1053. WKRetainPtr<WKStringRef> filename = adoptWK(WKURLCopyLastPathComponent(url.get()));
  1054. stringBuilder.append(toWTFString(filename));
  1055. } else {
  1056. WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyString(url.get()));
  1057. stringBuilder.append(toWTFString(urlString));
  1058. }
  1059. stringBuilder.appendLiteral(" with navigation type \'");
  1060. stringBuilder.append(toWTFString(NavigationTypeToString(WKBundleNavigationActionGetNavigationType(navigationAction))));
  1061. stringBuilder.appendLiteral("\'");
  1062. WKBundleHitTestResultRef hitTestResultRef = WKBundleNavigationActionCopyHitTestResult(navigationAction);
  1063. if (hitTestResultRef) {
  1064. stringBuilder.appendLiteral(" originating from ");
  1065. stringBuilder.append(dumpPath(m_page, m_world.get(), WKBundleHitTestResultCopyNodeHandle(hitTestResultRef)));
  1066. }
  1067. stringBuilder.append('\n');
  1068. InjectedBundle::shared().outputText(stringBuilder.toString());
  1069. InjectedBundle::shared().testRunner()->notifyDone();
  1070. if (InjectedBundle::shared().testRunner()->isPolicyDelegatePermissive())
  1071. return WKBundlePagePolicyActionUse;
  1072. return WKBundlePagePolicyActionPassThrough;
  1073. }
  1074. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForNewWindowAction(WKBundlePageRef, WKBundleFrameRef, WKBundleNavigationActionRef, WKURLRequestRef, WKStringRef, WKTypeRef*)
  1075. {
  1076. return WKBundlePagePolicyActionUse;
  1077. }
  1078. WKBundlePagePolicyAction InjectedBundlePage::decidePolicyForResponse(WKBundlePageRef page, WKBundleFrameRef, WKURLResponseRef response, WKURLRequestRef, WKTypeRef*)
  1079. {
  1080. if (WKURLResponseIsAttachment(response)) {
  1081. StringBuilder stringBuilder;
  1082. WKRetainPtr<WKStringRef> filename = adoptWK(WKURLResponseCopySuggestedFilename(response));
  1083. stringBuilder.appendLiteral("Policy delegate: resource is an attachment, suggested file name \'");
  1084. stringBuilder.append(toWTFString(filename));
  1085. stringBuilder.appendLiteral("\'\n");
  1086. InjectedBundle::shared().outputText(stringBuilder.toString());
  1087. }
  1088. WKRetainPtr<WKStringRef> mimeType = adoptWK(WKURLResponseCopyMIMEType(response));
  1089. return WKBundlePageCanShowMIMEType(page, mimeType.get()) ? WKBundlePagePolicyActionUse : WKBundlePagePolicyActionPassThrough;
  1090. }
  1091. void InjectedBundlePage::unableToImplementPolicy(WKBundlePageRef, WKBundleFrameRef, WKErrorRef, WKTypeRef*)
  1092. {
  1093. }
  1094. // UI Client Callbacks
  1095. void InjectedBundlePage::willAddMessageToConsole(WKBundlePageRef page, WKStringRef message, uint32_t lineNumber, const void *clientInfo)
  1096. {
  1097. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willAddMessageToConsole(message, lineNumber);
  1098. }
  1099. void InjectedBundlePage::willSetStatusbarText(WKBundlePageRef page, WKStringRef statusbarText, const void *clientInfo)
  1100. {
  1101. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willSetStatusbarText(statusbarText);
  1102. }
  1103. void InjectedBundlePage::willRunJavaScriptAlert(WKBundlePageRef page, WKStringRef message, WKBundleFrameRef frame, const void *clientInfo)
  1104. {
  1105. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willRunJavaScriptAlert(message, frame);
  1106. }
  1107. void InjectedBundlePage::willRunJavaScriptConfirm(WKBundlePageRef page, WKStringRef message, WKBundleFrameRef frame, const void *clientInfo)
  1108. {
  1109. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willRunJavaScriptConfirm(message, frame);
  1110. }
  1111. void InjectedBundlePage::willRunJavaScriptPrompt(WKBundlePageRef page, WKStringRef message, WKStringRef defaultValue, WKBundleFrameRef frame, const void *clientInfo)
  1112. {
  1113. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->willRunJavaScriptPrompt(message, defaultValue, frame);
  1114. }
  1115. void InjectedBundlePage::didReachApplicationCacheOriginQuota(WKBundlePageRef page, WKSecurityOriginRef origin, int64_t totalBytesNeeded, const void* clientInfo)
  1116. {
  1117. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didReachApplicationCacheOriginQuota(origin, totalBytesNeeded);
  1118. }
  1119. uint64_t InjectedBundlePage::didExceedDatabaseQuota(WKBundlePageRef page, WKSecurityOriginRef origin, WKStringRef databaseName, WKStringRef databaseDisplayName, uint64_t currentQuotaBytes, uint64_t currentOriginUsageBytes, uint64_t currentDatabaseUsageBytes, uint64_t expectedUsageBytes, const void* clientInfo)
  1120. {
  1121. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didExceedDatabaseQuota(origin, databaseName, databaseDisplayName, currentQuotaBytes, currentOriginUsageBytes, currentDatabaseUsageBytes, expectedUsageBytes);
  1122. }
  1123. static WTF::String lastFileURLPathComponent(const WTF::String& path)
  1124. {
  1125. size_t pos = path.find("file://");
  1126. ASSERT(WTF::notFound != pos);
  1127. WTF::String tmpPath = path.substring(pos + 7);
  1128. if (tmpPath.length() < 2) // Keep the lone slash to avoid empty output.
  1129. return tmpPath;
  1130. // Remove the trailing delimiter
  1131. if (tmpPath[tmpPath.length() - 1] == '/')
  1132. tmpPath.remove(tmpPath.length() - 1);
  1133. pos = tmpPath.reverseFind('/');
  1134. if (WTF::notFound != pos)
  1135. return tmpPath.substring(pos + 1);
  1136. return tmpPath;
  1137. }
  1138. void InjectedBundlePage::willAddMessageToConsole(WKStringRef message, uint32_t lineNumber)
  1139. {
  1140. if (!InjectedBundle::shared().isTestRunning())
  1141. return;
  1142. WTF::String messageString = toWTFString(message);
  1143. size_t nullCharPos = messageString.find(UChar(0));
  1144. if (nullCharPos != WTF::notFound)
  1145. messageString.truncate(nullCharPos);
  1146. size_t fileProtocolStart = messageString.find("file://");
  1147. if (fileProtocolStart != WTF::notFound)
  1148. // FIXME: The code below does not handle additional text after url nor multiple urls. This matches DumpRenderTree implementation.
  1149. messageString = messageString.substring(0, fileProtocolStart) + lastFileURLPathComponent(messageString.substring(fileProtocolStart));
  1150. StringBuilder stringBuilder;
  1151. stringBuilder.appendLiteral("CONSOLE MESSAGE: ");
  1152. if (lineNumber) {
  1153. stringBuilder.appendLiteral("line ");
  1154. stringBuilder.appendNumber(lineNumber);
  1155. stringBuilder.appendLiteral(": ");
  1156. }
  1157. stringBuilder.append(messageString);
  1158. stringBuilder.append('\n');
  1159. InjectedBundle::shared().outputText(stringBuilder.toString());
  1160. }
  1161. void InjectedBundlePage::willSetStatusbarText(WKStringRef statusbarText)
  1162. {
  1163. if (!InjectedBundle::shared().isTestRunning())
  1164. return;
  1165. if (!InjectedBundle::shared().testRunner()->shouldDumpStatusCallbacks())
  1166. return;
  1167. StringBuilder stringBuilder;
  1168. stringBuilder.appendLiteral("UI DELEGATE STATUS CALLBACK: setStatusText:");
  1169. stringBuilder.append(toWTFString(statusbarText));
  1170. stringBuilder.append('\n');
  1171. InjectedBundle::shared().outputText(stringBuilder.toString());
  1172. }
  1173. void InjectedBundlePage::willRunJavaScriptAlert(WKStringRef message, WKBundleFrameRef)
  1174. {
  1175. if (!InjectedBundle::shared().isTestRunning())
  1176. return;
  1177. StringBuilder stringBuilder;
  1178. stringBuilder.appendLiteral("ALERT: ");
  1179. stringBuilder.append(toWTFString(message));
  1180. stringBuilder.append('\n');
  1181. InjectedBundle::shared().outputText(stringBuilder.toString());
  1182. }
  1183. void InjectedBundlePage::willRunJavaScriptConfirm(WKStringRef message, WKBundleFrameRef)
  1184. {
  1185. if (!InjectedBundle::shared().isTestRunning())
  1186. return;
  1187. StringBuilder stringBuilder;
  1188. stringBuilder.appendLiteral("CONFIRM: ");
  1189. stringBuilder.append(toWTFString(message));
  1190. stringBuilder.append('\n');
  1191. InjectedBundle::shared().outputText(stringBuilder.toString());
  1192. }
  1193. void InjectedBundlePage::willRunJavaScriptPrompt(WKStringRef message, WKStringRef defaultValue, WKBundleFrameRef)
  1194. {
  1195. StringBuilder stringBuilder;
  1196. stringBuilder.appendLiteral("PROMPT: ");
  1197. stringBuilder.append(toWTFString(message));
  1198. stringBuilder.appendLiteral(", default text: ");
  1199. stringBuilder.append(toWTFString(defaultValue));
  1200. stringBuilder.append('\n');
  1201. InjectedBundle::shared().outputText(stringBuilder.toString());
  1202. }
  1203. void InjectedBundlePage::didReachApplicationCacheOriginQuota(WKSecurityOriginRef origin, int64_t totalBytesNeeded)
  1204. {
  1205. if (InjectedBundle::shared().testRunner()->shouldDumpApplicationCacheDelegateCallbacks()) {
  1206. // For example, numbers from 30000 - 39999 will output as 30000.
  1207. // Rounding up or down does not really matter for these tests. It's
  1208. // sufficient to just get a range of 10000 to determine if we were
  1209. // above or below a threshold.
  1210. int64_t truncatedSpaceNeeded = (totalBytesNeeded / 10000) * 10000;
  1211. StringBuilder stringBuilder;
  1212. stringBuilder.appendLiteral("UI DELEGATE APPLICATION CACHE CALLBACK: exceededApplicationCacheOriginQuotaForSecurityOrigin:");
  1213. stringBuilder.append(securityOriginToStr(origin));
  1214. stringBuilder.appendLiteral(" totalSpaceNeeded:~");
  1215. stringBuilder.appendNumber(truncatedSpaceNeeded);
  1216. stringBuilder.append('\n');
  1217. InjectedBundle::shared().outputText(stringBuilder.toString());
  1218. }
  1219. if (InjectedBundle::shared().testRunner()->shouldDisallowIncreaseForApplicationCacheQuota())
  1220. return;
  1221. // Reset default application cache quota.
  1222. WKBundleResetApplicationCacheOriginQuota(InjectedBundle::shared().bundle(), adoptWK(WKSecurityOriginCopyToString(origin)).get());
  1223. }
  1224. uint64_t InjectedBundlePage::didExceedDatabaseQuota(WKSecurityOriginRef origin, WKStringRef databaseName, WKStringRef databaseDisplayName, uint64_t currentQuotaBytes, uint64_t currentOriginUsageBytes, uint64_t currentDatabaseUsageBytes, uint64_t expectedUsageBytes)
  1225. {
  1226. if (InjectedBundle::shared().testRunner()->shouldDumpDatabaseCallbacks()) {
  1227. StringBuilder stringBuilder;
  1228. stringBuilder.appendLiteral("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:");
  1229. stringBuilder.append(securityOriginToStr(origin));
  1230. stringBuilder.appendLiteral(" database:");
  1231. stringBuilder.append(toWTFString(databaseName));
  1232. stringBuilder.append('\n');
  1233. InjectedBundle::shared().outputText(stringBuilder.toString());
  1234. }
  1235. static const uint64_t defaultQuota = 5 * 1024 * 1024;
  1236. return defaultQuota;
  1237. }
  1238. // Editor Client Callbacks
  1239. bool InjectedBundlePage::shouldBeginEditing(WKBundlePageRef page, WKBundleRangeHandleRef range, const void* clientInfo)
  1240. {
  1241. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldBeginEditing(range);
  1242. }
  1243. bool InjectedBundlePage::shouldEndEditing(WKBundlePageRef page, WKBundleRangeHandleRef range, const void* clientInfo)
  1244. {
  1245. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldEndEditing(range);
  1246. }
  1247. bool InjectedBundlePage::shouldInsertNode(WKBundlePageRef page, WKBundleNodeHandleRef node, WKBundleRangeHandleRef rangeToReplace, WKInsertActionType action, const void* clientInfo)
  1248. {
  1249. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldInsertNode(node, rangeToReplace, action);
  1250. }
  1251. bool InjectedBundlePage::shouldInsertText(WKBundlePageRef page, WKStringRef text, WKBundleRangeHandleRef rangeToReplace, WKInsertActionType action, const void* clientInfo)
  1252. {
  1253. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldInsertText(text, rangeToReplace, action);
  1254. }
  1255. bool InjectedBundlePage::shouldDeleteRange(WKBundlePageRef page, WKBundleRangeHandleRef range, const void* clientInfo)
  1256. {
  1257. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldDeleteRange(range);
  1258. }
  1259. bool InjectedBundlePage::shouldChangeSelectedRange(WKBundlePageRef page, WKBundleRangeHandleRef fromRange, WKBundleRangeHandleRef toRange, WKAffinityType affinity, bool stillSelecting, const void* clientInfo)
  1260. {
  1261. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldChangeSelectedRange(fromRange, toRange, affinity, stillSelecting);
  1262. }
  1263. bool InjectedBundlePage::shouldApplyStyle(WKBundlePageRef page, WKBundleCSSStyleDeclarationRef style, WKBundleRangeHandleRef range, const void* clientInfo)
  1264. {
  1265. return static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->shouldApplyStyle(style, range);
  1266. }
  1267. void InjectedBundlePage::didBeginEditing(WKBundlePageRef page, WKStringRef notificationName, const void* clientInfo)
  1268. {
  1269. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didBeginEditing(notificationName);
  1270. }
  1271. void InjectedBundlePage::didEndEditing(WKBundlePageRef page, WKStringRef notificationName, const void* clientInfo)
  1272. {
  1273. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didEndEditing(notificationName);
  1274. }
  1275. void InjectedBundlePage::didChange(WKBundlePageRef page, WKStringRef notificationName, const void* clientInfo)
  1276. {
  1277. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didChange(notificationName);
  1278. }
  1279. void InjectedBundlePage::didChangeSelection(WKBundlePageRef page, WKStringRef notificationName, const void* clientInfo)
  1280. {
  1281. static_cast<InjectedBundlePage*>(const_cast<void*>(clientInfo))->didChangeSelection(notificationName);
  1282. }
  1283. bool InjectedBundlePage::shouldBeginEditing(WKBundleRangeHandleRef range)
  1284. {
  1285. if (!InjectedBundle::shared().isTestRunning())
  1286. return true;
  1287. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1288. StringBuilder stringBuilder;
  1289. stringBuilder.appendLiteral("EDITING DELEGATE: shouldBeginEditingInDOMRange:");
  1290. stringBuilder.append(rangeToStr(m_page, m_world.get(), range));
  1291. stringBuilder.append('\n');
  1292. InjectedBundle::shared().outputText(stringBuilder.toString());
  1293. }
  1294. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1295. }
  1296. bool InjectedBundlePage::shouldEndEditing(WKBundleRangeHandleRef range)
  1297. {
  1298. if (!InjectedBundle::shared().isTestRunning())
  1299. return true;
  1300. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1301. StringBuilder stringBuilder;
  1302. stringBuilder.appendLiteral("EDITING DELEGATE: shouldEndEditingInDOMRange:");
  1303. stringBuilder.append(rangeToStr(m_page, m_world.get(), range));
  1304. stringBuilder.append('\n');
  1305. InjectedBundle::shared().outputText(stringBuilder.toString());
  1306. }
  1307. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1308. }
  1309. bool InjectedBundlePage::shouldInsertNode(WKBundleNodeHandleRef node, WKBundleRangeHandleRef rangeToReplace, WKInsertActionType action)
  1310. {
  1311. if (!InjectedBundle::shared().isTestRunning())
  1312. return true;
  1313. static const char* insertactionstring[] = {
  1314. "WebViewInsertActionTyped",
  1315. "WebViewInsertActionPasted",
  1316. "WebViewInsertActionDropped",
  1317. };
  1318. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1319. StringBuilder stringBuilder;
  1320. stringBuilder.appendLiteral("EDITING DELEGATE: shouldInsertNode:");
  1321. stringBuilder.append(dumpPath(m_page, m_world.get(), node));
  1322. stringBuilder.appendLiteral(" replacingDOMRange:");
  1323. stringBuilder.append(rangeToStr(m_page, m_world.get(), rangeToReplace));
  1324. stringBuilder.appendLiteral(" givenAction:");
  1325. stringBuilder.append(insertactionstring[action]);
  1326. stringBuilder.append('\n');
  1327. InjectedBundle::shared().outputText(stringBuilder.toString());
  1328. }
  1329. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1330. }
  1331. bool InjectedBundlePage::shouldInsertText(WKStringRef text, WKBundleRangeHandleRef rangeToReplace, WKInsertActionType action)
  1332. {
  1333. if (!InjectedBundle::shared().isTestRunning())
  1334. return true;
  1335. static const char *insertactionstring[] = {
  1336. "WebViewInsertActionTyped",
  1337. "WebViewInsertActionPasted",
  1338. "WebViewInsertActionDropped",
  1339. };
  1340. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1341. StringBuilder stringBuilder;
  1342. stringBuilder.appendLiteral("EDITING DELEGATE: shouldInsertText:");
  1343. stringBuilder.append(toWTFString(text));
  1344. stringBuilder.appendLiteral(" replacingDOMRange:");
  1345. stringBuilder.append(rangeToStr(m_page, m_world.get(), rangeToReplace));
  1346. stringBuilder.appendLiteral(" givenAction:");
  1347. stringBuilder.append(insertactionstring[action]);
  1348. stringBuilder.append('\n');
  1349. InjectedBundle::shared().outputText(stringBuilder.toString());
  1350. }
  1351. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1352. }
  1353. bool InjectedBundlePage::shouldDeleteRange(WKBundleRangeHandleRef range)
  1354. {
  1355. if (!InjectedBundle::shared().isTestRunning())
  1356. return true;
  1357. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1358. StringBuilder stringBuilder;
  1359. stringBuilder.appendLiteral("EDITING DELEGATE: shouldDeleteDOMRange:");
  1360. stringBuilder.append(rangeToStr(m_page, m_world.get(), range));
  1361. stringBuilder.append('\n');
  1362. InjectedBundle::shared().outputText(stringBuilder.toString());
  1363. }
  1364. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1365. }
  1366. bool InjectedBundlePage::shouldChangeSelectedRange(WKBundleRangeHandleRef fromRange, WKBundleRangeHandleRef toRange, WKAffinityType affinity, bool stillSelecting)
  1367. {
  1368. if (!InjectedBundle::shared().isTestRunning())
  1369. return true;
  1370. static const char *affinitystring[] = {
  1371. "NSSelectionAffinityUpstream",
  1372. "NSSelectionAffinityDownstream"
  1373. };
  1374. static const char *boolstring[] = {
  1375. "FALSE",
  1376. "TRUE"
  1377. };
  1378. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1379. StringBuilder stringBuilder;
  1380. stringBuilder.appendLiteral("EDITING DELEGATE: shouldChangeSelectedDOMRange:");
  1381. stringBuilder.append(rangeToStr(m_page, m_world.get(), fromRange));
  1382. stringBuilder.appendLiteral(" toDOMRange:");
  1383. stringBuilder.append(rangeToStr(m_page, m_world.get(), toRange));
  1384. stringBuilder.appendLiteral(" affinity:");
  1385. stringBuilder.append(affinitystring[affinity]);
  1386. stringBuilder.appendLiteral(" stillSelecting:");
  1387. stringBuilder.append(boolstring[stillSelecting]);
  1388. stringBuilder.append('\n');
  1389. InjectedBundle::shared().outputText(stringBuilder.toString());
  1390. }
  1391. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1392. }
  1393. bool InjectedBundlePage::shouldApplyStyle(WKBundleCSSStyleDeclarationRef style, WKBundleRangeHandleRef range)
  1394. {
  1395. if (!InjectedBundle::shared().isTestRunning())
  1396. return true;
  1397. if (InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks()) {
  1398. StringBuilder stringBuilder;
  1399. stringBuilder.appendLiteral("EDITING DELEGATE: shouldApplyStyle:");
  1400. stringBuilder.append(styleDecToStr(style));
  1401. stringBuilder.appendLiteral(" toElementsInDOMRange:");
  1402. stringBuilder.append(rangeToStr(m_page, m_world.get(), range));
  1403. stringBuilder.append('\n');
  1404. InjectedBundle::shared().outputText(stringBuilder.toString());
  1405. }
  1406. return InjectedBundle::shared().testRunner()->shouldAllowEditing();
  1407. }
  1408. void InjectedBundlePage::didBeginEditing(WKStringRef notificationName)
  1409. {
  1410. if (!InjectedBundle::shared().isTestRunning())
  1411. return;
  1412. if (!InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks())
  1413. return;
  1414. StringBuilder stringBuilder;
  1415. stringBuilder.appendLiteral("EDITING DELEGATE: webViewDidBeginEditing:");
  1416. stringBuilder.append(toWTFString(notificationName));
  1417. stringBuilder.append('\n');
  1418. InjectedBundle::shared().outputText(stringBuilder.toString());
  1419. }
  1420. void InjectedBundlePage::didEndEditing(WKStringRef notificationName)
  1421. {
  1422. if (!InjectedBundle::shared().isTestRunning())
  1423. return;
  1424. if (!InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks())
  1425. return;
  1426. StringBuilder stringBuilder;
  1427. stringBuilder.appendLiteral("EDITING DELEGATE: webViewDidEndEditing:");
  1428. stringBuilder.append(toWTFString(notificationName));
  1429. stringBuilder.append('\n');
  1430. InjectedBundle::shared().outputText(stringBuilder.toString());
  1431. }
  1432. void InjectedBundlePage::didChange(WKStringRef notificationName)
  1433. {
  1434. if (!InjectedBundle::shared().isTestRunning())
  1435. return;
  1436. if (!InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks())
  1437. return;
  1438. StringBuilder stringBuilder;
  1439. stringBuilder.appendLiteral("EDITING DELEGATE: webViewDidChange:");
  1440. stringBuilder.append(toWTFString(notificationName));
  1441. stringBuilder.append('\n');
  1442. InjectedBundle::shared().outputText(stringBuilder.toString());
  1443. }
  1444. void InjectedBundlePage::didChangeSelection(WKStringRef notificationName)
  1445. {
  1446. if (!InjectedBundle::shared().isTestRunning())
  1447. return;
  1448. if (!InjectedBundle::shared().testRunner()->shouldDumpEditingCallbacks())
  1449. return;
  1450. StringBuilder stringBuilder;
  1451. stringBuilder.appendLiteral("EDITING DELEGATE: webViewDidChangeSelection:");
  1452. stringBuilder.append(toWTFString(notificationName));
  1453. stringBuilder.append('\n');
  1454. InjectedBundle::shared().outputText(stringBuilder.toString());
  1455. }
  1456. #if ENABLE(FULLSCREEN_API)
  1457. bool InjectedBundlePage::supportsFullScreen(WKBundlePageRef pageRef, WKFullScreenKeyboardRequestType requestType)
  1458. {
  1459. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1460. InjectedBundle::shared().outputText("supportsFullScreen() == true\n");
  1461. return true;
  1462. }
  1463. void InjectedBundlePage::enterFullScreenForElement(WKBundlePageRef pageRef, WKBundleNodeHandleRef elementRef)
  1464. {
  1465. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1466. InjectedBundle::shared().outputText("enterFullScreenForElement()\n");
  1467. if (!InjectedBundle::shared().testRunner()->hasCustomFullScreenBehavior()) {
  1468. WKBundlePageWillEnterFullScreen(pageRef);
  1469. WKBundlePageDidEnterFullScreen(pageRef);
  1470. }
  1471. }
  1472. void InjectedBundlePage::exitFullScreenForElement(WKBundlePageRef pageRef, WKBundleNodeHandleRef elementRef)
  1473. {
  1474. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1475. InjectedBundle::shared().outputText("exitFullScreenForElement()\n");
  1476. if (!InjectedBundle::shared().testRunner()->hasCustomFullScreenBehavior()) {
  1477. WKBundlePageWillExitFullScreen(pageRef);
  1478. WKBundlePageDidExitFullScreen(pageRef);
  1479. }
  1480. }
  1481. void InjectedBundlePage::beganEnterFullScreen(WKBundlePageRef, WKRect, WKRect)
  1482. {
  1483. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1484. InjectedBundle::shared().outputText("beganEnterFullScreen()\n");
  1485. }
  1486. void InjectedBundlePage::beganExitFullScreen(WKBundlePageRef, WKRect, WKRect)
  1487. {
  1488. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1489. InjectedBundle::shared().outputText("beganExitFullScreen()\n");
  1490. }
  1491. void InjectedBundlePage::closeFullScreen(WKBundlePageRef pageRef)
  1492. {
  1493. if (InjectedBundle::shared().testRunner()->shouldDumpFullScreenCallbacks())
  1494. InjectedBundle::shared().outputText("closeFullScreen()\n");
  1495. if (!InjectedBundle::shared().testRunner()->hasCustomFullScreenBehavior()) {
  1496. WKBundlePageWillExitFullScreen(pageRef);
  1497. WKBundlePageDidExitFullScreen(pageRef);
  1498. }
  1499. }
  1500. #endif
  1501. static bool compareByTargetName(WKBundleBackForwardListItemRef item1, WKBundleBackForwardListItemRef item2)
  1502. {
  1503. return toSTD(adoptWK(WKBundleBackForwardListItemCopyTarget(item1))) < toSTD(adoptWK(WKBundleBackForwardListItemCopyTarget(item2)));
  1504. }
  1505. static void dumpBackForwardListItem(WKBundleBackForwardListItemRef item, unsigned indent, bool isCurrentItem, StringBuilder& stringBuilder)
  1506. {
  1507. unsigned column = 0;
  1508. if (isCurrentItem) {
  1509. stringBuilder.appendLiteral("curr->");
  1510. column = 6;
  1511. }
  1512. for (unsigned i = column; i < indent; i++)
  1513. stringBuilder.append(' ');
  1514. WTF::String url = toWTFString(adoptWK(WKURLCopyString(adoptWK(WKBundleBackForwardListItemCopyURL(item)).get())));
  1515. if (hasPrefix(url, "file:")) {
  1516. WTF::String directoryName = "/LayoutTests/";
  1517. size_t start = url.find(directoryName);
  1518. if (start == WTF::notFound)
  1519. start = 0;
  1520. else
  1521. start += directoryName.length();
  1522. stringBuilder.appendLiteral("(file test):");
  1523. stringBuilder.append(url.substring(start));
  1524. } else
  1525. stringBuilder.append(url);
  1526. WTF::String target = toWTFString(adoptWK(WKBundleBackForwardListItemCopyTarget(item)));
  1527. if (target.length()) {
  1528. stringBuilder.appendLiteral(" (in frame \"");
  1529. stringBuilder.append(target);
  1530. stringBuilder.appendLiteral("\")");
  1531. }
  1532. // FIXME: Need WKBackForwardListItemIsTargetItem.
  1533. if (WKBundleBackForwardListItemIsTargetItem(item))
  1534. stringBuilder.appendLiteral(" **nav target**");
  1535. stringBuilder.append('\n');
  1536. if (WKRetainPtr<WKArrayRef> kids = adoptWK(WKBundleBackForwardListItemCopyChildren(item))) {
  1537. // Sort to eliminate arbitrary result ordering which defeats reproducible testing.
  1538. size_t size = WKArrayGetSize(kids.get());
  1539. Vector<WKBundleBackForwardListItemRef> sortedKids(size);
  1540. for (size_t i = 0; i < size; ++i)
  1541. sortedKids[i] = static_cast<WKBundleBackForwardListItemRef>(WKArrayGetItemAtIndex(kids.get(), i));
  1542. stable_sort(sortedKids.begin(), sortedKids.end(), compareByTargetName);
  1543. for (size_t i = 0; i < size; ++i)
  1544. dumpBackForwardListItem(sortedKids[i], indent + 4, false, stringBuilder);
  1545. }
  1546. }
  1547. void InjectedBundlePage::dumpBackForwardList(StringBuilder& stringBuilder)
  1548. {
  1549. stringBuilder.appendLiteral("\n============== Back Forward List ==============\n");
  1550. WKBundleBackForwardListRef list = WKBundlePageGetBackForwardList(m_page);
  1551. // Print out all items in the list after m_previousTestBackForwardListItem.
  1552. // Gather items from the end of the list, then print them out from oldest to newest.
  1553. Vector<WKRetainPtr<WKBundleBackForwardListItemRef> > itemsToPrint;
  1554. for (unsigned i = WKBundleBackForwardListGetForwardListCount(list); i; --i) {
  1555. WKRetainPtr<WKBundleBackForwardListItemRef> item = adoptWK(WKBundleBackForwardListCopyItemAtIndex(list, i));
  1556. // Something is wrong if the item from the last test is in the forward part of the list.
  1557. ASSERT(!WKBundleBackForwardListItemIsSame(item.get(), m_previousTestBackForwardListItem.get()));
  1558. itemsToPrint.append(item);
  1559. }
  1560. ASSERT(!WKBundleBackForwardListItemIsSame(adoptWK(WKBundleBackForwardListCopyItemAtIndex(list, 0)).get(), m_previousTestBackForwardListItem.get()));
  1561. itemsToPrint.append(adoptWK(WKBundleBackForwardListCopyItemAtIndex(list, 0)));
  1562. int currentItemIndex = itemsToPrint.size() - 1;
  1563. int backListCount = WKBundleBackForwardListGetBackListCount(list);
  1564. for (int i = -1; i >= -backListCount; --i) {
  1565. WKRetainPtr<WKBundleBackForwardListItemRef> item = adoptWK(WKBundleBackForwardListCopyItemAtIndex(list, i));
  1566. if (WKBundleBackForwardListItemIsSame(item.get(), m_previousTestBackForwardListItem.get()))
  1567. break;
  1568. itemsToPrint.append(item);
  1569. }
  1570. for (int i = itemsToPrint.size() - 1; i >= 0; i--)
  1571. dumpBackForwardListItem(itemsToPrint[i].get(), 8, i == currentItemIndex, stringBuilder);
  1572. stringBuilder.appendLiteral("===============================================\n");
  1573. }
  1574. #if !PLATFORM(MAC)
  1575. void InjectedBundlePage::platformDidStartProvisionalLoadForFrame(WKBundleFrameRef)
  1576. {
  1577. }
  1578. #endif
  1579. void InjectedBundlePage::frameDidChangeLocation(WKBundleFrameRef frame, bool shouldDump)
  1580. {
  1581. if (frame != InjectedBundle::shared().topLoadingFrame())
  1582. return;
  1583. InjectedBundle::shared().setTopLoadingFrame(0);
  1584. if (InjectedBundle::shared().testRunner()->waitToDump())
  1585. return;
  1586. if (InjectedBundle::shared().shouldProcessWorkQueue()) {
  1587. InjectedBundle::shared().processWorkQueue();
  1588. return;
  1589. }
  1590. if (shouldDump)
  1591. InjectedBundle::shared().page()->dump();
  1592. else
  1593. InjectedBundle::shared().done();
  1594. }
  1595. } // namespace WTR