WebFrame.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  1. /*
  2. * Copyright (C) 2006-2009, 2011, 2013 Apple Inc. All rights reserved.
  3. * Copyright (C) Research In Motion Limited 2009. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "WebKitDLL.h"
  28. #include "WebFrame.h"
  29. #include "CFDictionaryPropertyBag.h"
  30. #include "COMPropertyBag.h"
  31. #include "DOMCoreClasses.h"
  32. #include "DefaultPolicyDelegate.h"
  33. #include "HTMLFrameOwnerElement.h"
  34. #include "MarshallingHelpers.h"
  35. #include "WebActionPropertyBag.h"
  36. #include "WebChromeClient.h"
  37. #include "WebDataSource.h"
  38. #include "WebDocumentLoader.h"
  39. #include "WebDownload.h"
  40. #include "WebEditorClient.h"
  41. #include "WebError.h"
  42. #include "WebFrameNetworkingContext.h"
  43. #include "WebFramePolicyListener.h"
  44. #include "WebHistory.h"
  45. #include "WebHistoryItem.h"
  46. #include "WebKit.h"
  47. #include "WebKitStatisticsPrivate.h"
  48. #include "WebMutableURLRequest.h"
  49. #include "WebNotificationCenter.h"
  50. #include "WebScriptWorld.h"
  51. #include "WebURLResponse.h"
  52. #include "WebView.h"
  53. #include <WebCore/AnimationController.h>
  54. #include <WebCore/BString.h>
  55. #include <WebCore/COMPtr.h>
  56. #include <WebCore/MemoryCache.h>
  57. #include <WebCore/Document.h>
  58. #include <WebCore/DocumentLoader.h>
  59. #include <WebCore/DocumentMarkerController.h>
  60. #include <WebCore/DOMImplementation.h>
  61. #include <WebCore/DOMWindow.h>
  62. #include <WebCore/Editor.h>
  63. #include <WebCore/Event.h>
  64. #include <WebCore/EventHandler.h>
  65. #include <WebCore/FormState.h>
  66. #include <WebCore/Frame.h>
  67. #include <WebCore/FrameLoader.h>
  68. #include <WebCore/FrameLoadRequest.h>
  69. #include <WebCore/FrameTree.h>
  70. #include <WebCore/FrameView.h>
  71. #include <WebCore/FrameWin.h>
  72. #include <WebCore/GDIObjectCounter.h>
  73. #include <WebCore/GraphicsContext.h>
  74. #include <WebCore/HistoryItem.h>
  75. #include <WebCore/HTMLAppletElement.h>
  76. #include <WebCore/HTMLFormElement.h>
  77. #include <WebCore/HTMLFormControlElement.h>
  78. #include <WebCore/HTMLInputElement.h>
  79. #include <WebCore/HTMLNames.h>
  80. #include <WebCore/HTMLPlugInElement.h>
  81. #include <WebCore/JSDOMWindow.h>
  82. #include <WebCore/KeyboardEvent.h>
  83. #include <WebCore/MouseRelatedEvent.h>
  84. #include <WebCore/NotImplemented.h>
  85. #include <WebCore/Page.h>
  86. #include <WebCore/PlatformKeyboardEvent.h>
  87. #include <WebCore/PluginData.h>
  88. #include <WebCore/PluginDatabase.h>
  89. #include <WebCore/PluginView.h>
  90. #include <WebCore/PolicyChecker.h>
  91. #include <WebCore/PrintContext.h>
  92. #include <WebCore/ResourceHandle.h>
  93. #include <WebCore/ResourceLoader.h>
  94. #include <WebCore/ResourceRequest.h>
  95. #include <WebCore/RenderView.h>
  96. #include <WebCore/RenderTreeAsText.h>
  97. #include <WebCore/Settings.h>
  98. #include <WebCore/TextIterator.h>
  99. #include <WebCore/JSDOMBinding.h>
  100. #include <WebCore/ScriptController.h>
  101. #include <WebCore/ScriptValue.h>
  102. #include <WebCore/SecurityOrigin.h>
  103. #include <JavaScriptCore/APICast.h>
  104. #include <JavaScriptCore/JSCJSValue.h>
  105. #include <JavaScriptCore/JSLock.h>
  106. #include <JavaScriptCore/JSObject.h>
  107. #include <wtf/MathExtras.h>
  108. #if USE(CG)
  109. #include <CoreGraphics/CoreGraphics.h>
  110. #elif USE(CAIRO)
  111. #include "PlatformContextCairo.h"
  112. #include <cairo-win32.h>
  113. #endif
  114. #if USE(CG)
  115. // CG SPI used for printing
  116. extern "C" {
  117. CGAffineTransform CGContextGetBaseCTM(CGContextRef c);
  118. void CGContextSetBaseCTM(CGContextRef c, CGAffineTransform m);
  119. }
  120. #endif
  121. using namespace WebCore;
  122. using namespace HTMLNames;
  123. using namespace std;
  124. using JSC::JSGlobalObject;
  125. using JSC::JSLock;
  126. using JSC::JSValue;
  127. #define FLASH_REDRAW 0
  128. // By imaging to a width a little wider than the available pixels,
  129. // thin pages will be scaled down a little, matching the way they
  130. // print in IE and Camino. This lets them use fewer sheets than they
  131. // would otherwise, which is presumably why other browsers do this.
  132. // Wide pages will be scaled down more than this.
  133. const float PrintingMinimumShrinkFactor = 1.25f;
  134. // This number determines how small we are willing to reduce the page content
  135. // in order to accommodate the widest line. If the page would have to be
  136. // reduced smaller to make the widest line fit, we just clip instead (this
  137. // behavior matches MacIE and Mozilla, at least)
  138. const float PrintingMaximumShrinkFactor = 2.0f;
  139. //-----------------------------------------------------------------------------
  140. // Helpers to convert from WebCore to WebKit type
  141. WebFrame* kit(Frame* frame)
  142. {
  143. if (!frame)
  144. return 0;
  145. FrameLoaderClient* frameLoaderClient = frame->loader()->client();
  146. if (frameLoaderClient)
  147. return static_cast<WebFrame*>(frameLoaderClient); // eek, is there a better way than static cast?
  148. return 0;
  149. }
  150. Frame* core(WebFrame* webFrame)
  151. {
  152. if (!webFrame)
  153. return 0;
  154. return webFrame->impl();
  155. }
  156. // This function is not in WebFrame.h because we don't want to advertise the ability to get a non-const Frame from a const WebFrame
  157. Frame* core(const WebFrame* webFrame)
  158. {
  159. if (!webFrame)
  160. return 0;
  161. return const_cast<WebFrame*>(webFrame)->impl();
  162. }
  163. //-----------------------------------------------------------------------------
  164. static Element *elementFromDOMElement(IDOMElement *element)
  165. {
  166. if (!element)
  167. return 0;
  168. COMPtr<IDOMElementPrivate> elePriv;
  169. HRESULT hr = element->QueryInterface(IID_IDOMElementPrivate, (void**) &elePriv);
  170. if (SUCCEEDED(hr)) {
  171. Element* ele;
  172. hr = elePriv->coreElement((void**)&ele);
  173. if (SUCCEEDED(hr))
  174. return ele;
  175. }
  176. return 0;
  177. }
  178. static HTMLFormElement *formElementFromDOMElement(IDOMElement *element)
  179. {
  180. if (!element)
  181. return 0;
  182. IDOMElementPrivate* elePriv;
  183. HRESULT hr = element->QueryInterface(IID_IDOMElementPrivate, (void**) &elePriv);
  184. if (SUCCEEDED(hr)) {
  185. Element* ele;
  186. hr = elePriv->coreElement((void**)&ele);
  187. elePriv->Release();
  188. if (SUCCEEDED(hr) && ele && ele->hasTagName(formTag))
  189. return static_cast<HTMLFormElement*>(ele);
  190. }
  191. return 0;
  192. }
  193. static HTMLInputElement* inputElementFromDOMElement(IDOMElement* element)
  194. {
  195. if (!element)
  196. return 0;
  197. IDOMElementPrivate* elePriv;
  198. HRESULT hr = element->QueryInterface(IID_IDOMElementPrivate, (void**) &elePriv);
  199. if (SUCCEEDED(hr)) {
  200. Element* ele;
  201. hr = elePriv->coreElement((void**)&ele);
  202. elePriv->Release();
  203. if (SUCCEEDED(hr) && ele && ele->hasTagName(inputTag))
  204. return static_cast<HTMLInputElement*>(ele);
  205. }
  206. return 0;
  207. }
  208. // WebFramePrivate ------------------------------------------------------------
  209. class WebFrame::WebFramePrivate {
  210. public:
  211. WebFramePrivate()
  212. : frame(0)
  213. , webView(0)
  214. , m_policyFunction(0)
  215. {
  216. }
  217. ~WebFramePrivate() { }
  218. FrameView* frameView() { return frame ? frame->view() : 0; }
  219. Frame* frame;
  220. WebView* webView;
  221. FramePolicyFunction m_policyFunction;
  222. COMPtr<WebFramePolicyListener> m_policyListener;
  223. };
  224. // WebFrame ----------------------------------------------------------------
  225. WebFrame::WebFrame()
  226. : WebFrameLoaderClient(this)
  227. , m_refCount(0)
  228. , d(new WebFrame::WebFramePrivate)
  229. , m_quickRedirectComing(false)
  230. , m_inPrintingMode(false)
  231. , m_pageHeight(0)
  232. {
  233. WebFrameCount++;
  234. gClassCount++;
  235. gClassNameCount.add("WebFrame");
  236. }
  237. WebFrame::~WebFrame()
  238. {
  239. delete d;
  240. WebFrameCount--;
  241. gClassCount--;
  242. gClassNameCount.remove("WebFrame");
  243. }
  244. WebFrame* WebFrame::createInstance()
  245. {
  246. WebFrame* instance = new WebFrame();
  247. instance->AddRef();
  248. return instance;
  249. }
  250. HRESULT STDMETHODCALLTYPE WebFrame::setAllowsScrolling(
  251. /* [in] */ BOOL flag)
  252. {
  253. if (Frame* frame = core(this))
  254. if (FrameView* view = frame->view())
  255. view->setCanHaveScrollbars(!!flag);
  256. return S_OK;
  257. }
  258. HRESULT STDMETHODCALLTYPE WebFrame::allowsScrolling(
  259. /* [retval][out] */ BOOL *flag)
  260. {
  261. if (flag)
  262. if (Frame* frame = core(this))
  263. if (FrameView* view = frame->view())
  264. *flag = view->canHaveScrollbars();
  265. return S_OK;
  266. }
  267. HRESULT STDMETHODCALLTYPE WebFrame::setIsDisconnected(
  268. /* [in] */ BOOL flag)
  269. {
  270. return E_FAIL;
  271. }
  272. HRESULT STDMETHODCALLTYPE WebFrame::setExcludeFromTextSearch(
  273. /* [in] */ BOOL flag)
  274. {
  275. return E_FAIL;
  276. }
  277. HRESULT WebFrame::reloadFromOrigin()
  278. {
  279. Frame* coreFrame = core(this);
  280. if (!coreFrame)
  281. return E_FAIL;
  282. coreFrame->loader()->reload(true);
  283. return S_OK;
  284. }
  285. HRESULT STDMETHODCALLTYPE WebFrame::paintDocumentRectToContext(
  286. /* [in] */ RECT rect,
  287. /* [in] */ OLE_HANDLE deviceContext)
  288. {
  289. Frame* coreFrame = core(this);
  290. if (!coreFrame)
  291. return E_FAIL;
  292. FrameView* view = coreFrame->view();
  293. if (!view)
  294. return E_FAIL;
  295. // We can't paint with a layout still pending.
  296. view->updateLayoutAndStyleIfNeededRecursive();
  297. HDC dc = reinterpret_cast<HDC>(static_cast<ULONG64>(deviceContext));
  298. GraphicsContext gc(dc);
  299. gc.setShouldIncludeChildWindows(true);
  300. gc.save();
  301. LONG width = rect.right - rect.left;
  302. LONG height = rect.bottom - rect.top;
  303. FloatRect dirtyRect;
  304. dirtyRect.setWidth(width);
  305. dirtyRect.setHeight(height);
  306. gc.clip(dirtyRect);
  307. gc.translate(-rect.left, -rect.top);
  308. view->paintContents(&gc, rect);
  309. gc.restore();
  310. return S_OK;
  311. }
  312. HRESULT STDMETHODCALLTYPE WebFrame::paintScrollViewRectToContextAtPoint(
  313. /* [in] */ RECT rect,
  314. /* [in] */ POINT pt,
  315. /* [in] */ OLE_HANDLE deviceContext)
  316. {
  317. Frame* coreFrame = core(this);
  318. if (!coreFrame)
  319. return E_FAIL;
  320. FrameView* view = coreFrame->view();
  321. if (!view)
  322. return E_FAIL;
  323. // We can't paint with a layout still pending.
  324. view->updateLayoutAndStyleIfNeededRecursive();
  325. HDC dc = reinterpret_cast<HDC>(static_cast<ULONG64>(deviceContext));
  326. GraphicsContext gc(dc);
  327. gc.setShouldIncludeChildWindows(true);
  328. gc.save();
  329. IntRect dirtyRect(rect);
  330. dirtyRect.move(-pt.x, -pt.y);
  331. view->paint(&gc, dirtyRect);
  332. gc.restore();
  333. return S_OK;
  334. }
  335. // IUnknown -------------------------------------------------------------------
  336. HRESULT STDMETHODCALLTYPE WebFrame::QueryInterface(REFIID riid, void** ppvObject)
  337. {
  338. *ppvObject = 0;
  339. if (IsEqualGUID(riid, __uuidof(WebFrame)))
  340. *ppvObject = this;
  341. else if (IsEqualGUID(riid, IID_IUnknown))
  342. *ppvObject = static_cast<IWebFrame*>(this);
  343. else if (IsEqualGUID(riid, IID_IWebFrame))
  344. *ppvObject = static_cast<IWebFrame*>(this);
  345. else if (IsEqualGUID(riid, IID_IWebFramePrivate))
  346. *ppvObject = static_cast<IWebFramePrivate*>(this);
  347. else if (IsEqualGUID(riid, IID_IWebDocumentText))
  348. *ppvObject = static_cast<IWebDocumentText*>(this);
  349. else
  350. return E_NOINTERFACE;
  351. AddRef();
  352. return S_OK;
  353. }
  354. ULONG STDMETHODCALLTYPE WebFrame::AddRef(void)
  355. {
  356. return ++m_refCount;
  357. }
  358. ULONG STDMETHODCALLTYPE WebFrame::Release(void)
  359. {
  360. ULONG newRef = --m_refCount;
  361. if (!newRef)
  362. delete(this);
  363. return newRef;
  364. }
  365. // IWebFrame -------------------------------------------------------------------
  366. HRESULT STDMETHODCALLTYPE WebFrame::name(
  367. /* [retval][out] */ BSTR* frameName)
  368. {
  369. if (!frameName) {
  370. ASSERT_NOT_REACHED();
  371. return E_POINTER;
  372. }
  373. *frameName = 0;
  374. Frame* coreFrame = core(this);
  375. if (!coreFrame)
  376. return E_FAIL;
  377. *frameName = BString(coreFrame->tree()->uniqueName()).release();
  378. return S_OK;
  379. }
  380. HRESULT STDMETHODCALLTYPE WebFrame::webView(
  381. /* [retval][out] */ IWebView** view)
  382. {
  383. *view = 0;
  384. if (!d->webView)
  385. return E_FAIL;
  386. *view = d->webView;
  387. (*view)->AddRef();
  388. return S_OK;
  389. }
  390. HRESULT STDMETHODCALLTYPE WebFrame::frameView(
  391. /* [retval][out] */ IWebFrameView** /*view*/)
  392. {
  393. ASSERT_NOT_REACHED();
  394. return E_NOTIMPL;
  395. }
  396. HRESULT STDMETHODCALLTYPE WebFrame::DOMDocument(
  397. /* [retval][out] */ IDOMDocument** result)
  398. {
  399. if (!result) {
  400. ASSERT_NOT_REACHED();
  401. return E_POINTER;
  402. }
  403. *result = 0;
  404. if (Frame* coreFrame = core(this))
  405. if (Document* document = coreFrame->document())
  406. *result = DOMDocument::createInstance(document);
  407. return *result ? S_OK : E_FAIL;
  408. }
  409. HRESULT WebFrame::DOMWindow(/* [retval][out] */ IDOMWindow** window)
  410. {
  411. if (!window) {
  412. ASSERT_NOT_REACHED();
  413. return E_POINTER;
  414. }
  415. *window = 0;
  416. if (Frame* coreFrame = core(this)) {
  417. if (WebCore::DOMWindow* coreWindow = coreFrame->document()->domWindow())
  418. *window = ::DOMWindow::createInstance(coreWindow);
  419. }
  420. return *window ? S_OK : E_FAIL;
  421. }
  422. HRESULT STDMETHODCALLTYPE WebFrame::frameElement(
  423. /* [retval][out] */ IDOMHTMLElement** frameElement)
  424. {
  425. if (!frameElement)
  426. return E_POINTER;
  427. *frameElement = 0;
  428. Frame* coreFrame = core(this);
  429. if (!coreFrame)
  430. return E_FAIL;
  431. COMPtr<IDOMElement> domElement(AdoptCOM, DOMElement::createInstance(coreFrame->ownerElement()));
  432. COMPtr<IDOMHTMLElement> htmlElement(Query, domElement);
  433. if (!htmlElement)
  434. return E_FAIL;
  435. return htmlElement.copyRefTo(frameElement);
  436. }
  437. HRESULT STDMETHODCALLTYPE WebFrame::currentForm(
  438. /* [retval][out] */ IDOMElement **currentForm)
  439. {
  440. if (!currentForm) {
  441. ASSERT_NOT_REACHED();
  442. return E_POINTER;
  443. }
  444. *currentForm = 0;
  445. if (Frame* coreFrame = core(this)) {
  446. if (HTMLFormElement* formElement = coreFrame->selection()->currentForm())
  447. *currentForm = DOMElement::createInstance(formElement);
  448. }
  449. return *currentForm ? S_OK : E_FAIL;
  450. }
  451. JSGlobalContextRef STDMETHODCALLTYPE WebFrame::globalContext()
  452. {
  453. Frame* coreFrame = core(this);
  454. if (!coreFrame)
  455. return 0;
  456. return toGlobalRef(coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec());
  457. }
  458. JSGlobalContextRef WebFrame::globalContextForScriptWorld(IWebScriptWorld* iWorld)
  459. {
  460. Frame* coreFrame = core(this);
  461. if (!coreFrame)
  462. return 0;
  463. COMPtr<WebScriptWorld> world(Query, iWorld);
  464. if (!world)
  465. return 0;
  466. return toGlobalRef(coreFrame->script()->globalObject(world->world())->globalExec());
  467. }
  468. HRESULT STDMETHODCALLTYPE WebFrame::loadRequest(
  469. /* [in] */ IWebURLRequest* request)
  470. {
  471. COMPtr<WebMutableURLRequest> requestImpl;
  472. HRESULT hr = request->QueryInterface(&requestImpl);
  473. if (FAILED(hr))
  474. return hr;
  475. Frame* coreFrame = core(this);
  476. if (!coreFrame)
  477. return E_FAIL;
  478. coreFrame->loader()->load(FrameLoadRequest(coreFrame, requestImpl->resourceRequest()));
  479. return S_OK;
  480. }
  481. void WebFrame::loadData(PassRefPtr<WebCore::SharedBuffer> data, BSTR mimeType, BSTR textEncodingName, BSTR baseURL, BSTR failingURL)
  482. {
  483. String mimeTypeString(mimeType, SysStringLen(mimeType));
  484. if (!mimeType)
  485. mimeTypeString = "text/html";
  486. String encodingString(textEncodingName, SysStringLen(textEncodingName));
  487. // FIXME: We should really be using MarshallingHelpers::BSTRToKURL here,
  488. // but that would turn a null BSTR into a null KURL, and we crash inside of
  489. // WebCore if we use a null KURL in constructing the ResourceRequest.
  490. KURL baseKURL = KURL(KURL(), String(baseURL ? baseURL : L"", SysStringLen(baseURL)));
  491. KURL failingKURL = MarshallingHelpers::BSTRToKURL(failingURL);
  492. ResourceRequest request(baseKURL);
  493. SubstituteData substituteData(data, mimeTypeString, encodingString, failingKURL);
  494. // This method is only called from IWebFrame methods, so don't ASSERT that the Frame pointer isn't null.
  495. if (Frame* coreFrame = core(this))
  496. coreFrame->loader()->load(FrameLoadRequest(coreFrame, request, substituteData));
  497. }
  498. HRESULT STDMETHODCALLTYPE WebFrame::loadData(
  499. /* [in] */ IStream* data,
  500. /* [in] */ BSTR mimeType,
  501. /* [in] */ BSTR textEncodingName,
  502. /* [in] */ BSTR url)
  503. {
  504. RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create();
  505. STATSTG stat;
  506. if (SUCCEEDED(data->Stat(&stat, STATFLAG_NONAME))) {
  507. if (!stat.cbSize.HighPart && stat.cbSize.LowPart) {
  508. Vector<char> dataBuffer(stat.cbSize.LowPart);
  509. ULONG read;
  510. // FIXME: this does a needless copy, would be better to read right into the SharedBuffer
  511. // or adopt the Vector or something.
  512. if (SUCCEEDED(data->Read(dataBuffer.data(), static_cast<ULONG>(dataBuffer.size()), &read)))
  513. sharedBuffer->append(dataBuffer.data(), static_cast<int>(dataBuffer.size()));
  514. }
  515. }
  516. loadData(sharedBuffer, mimeType, textEncodingName, url, 0);
  517. return S_OK;
  518. }
  519. HRESULT WebFrame::loadPlainTextString(
  520. /* [in] */ BSTR string,
  521. /* [in] */ BSTR url)
  522. {
  523. RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<char*>(string), sizeof(UChar) * SysStringLen(string));
  524. BString plainTextMimeType(TEXT("text/plain"), 10);
  525. BString utf16Encoding(TEXT("utf-16"), 6);
  526. loadData(sharedBuffer.release(), plainTextMimeType, utf16Encoding, url, 0);
  527. return S_OK;
  528. }
  529. void WebFrame::loadHTMLString(BSTR string, BSTR baseURL, BSTR unreachableURL)
  530. {
  531. RefPtr<SharedBuffer> sharedBuffer = SharedBuffer::create(reinterpret_cast<char*>(string), sizeof(UChar) * SysStringLen(string));
  532. BString utf16Encoding(TEXT("utf-16"), 6);
  533. loadData(sharedBuffer.release(), 0, utf16Encoding, baseURL, unreachableURL);
  534. }
  535. HRESULT STDMETHODCALLTYPE WebFrame::loadHTMLString(
  536. /* [in] */ BSTR string,
  537. /* [in] */ BSTR baseURL)
  538. {
  539. loadHTMLString(string, baseURL, 0);
  540. return S_OK;
  541. }
  542. HRESULT STDMETHODCALLTYPE WebFrame::loadAlternateHTMLString(
  543. /* [in] */ BSTR str,
  544. /* [in] */ BSTR baseURL,
  545. /* [in] */ BSTR unreachableURL)
  546. {
  547. loadHTMLString(str, baseURL, unreachableURL);
  548. return S_OK;
  549. }
  550. HRESULT STDMETHODCALLTYPE WebFrame::loadArchive(
  551. /* [in] */ IWebArchive* /*archive*/)
  552. {
  553. ASSERT_NOT_REACHED();
  554. return E_NOTIMPL;
  555. }
  556. static inline WebDataSource *getWebDataSource(DocumentLoader* loader)
  557. {
  558. return loader ? static_cast<WebDocumentLoader*>(loader)->dataSource() : 0;
  559. }
  560. HRESULT STDMETHODCALLTYPE WebFrame::dataSource(
  561. /* [retval][out] */ IWebDataSource** source)
  562. {
  563. if (!source) {
  564. ASSERT_NOT_REACHED();
  565. return E_POINTER;
  566. }
  567. *source = 0;
  568. Frame* coreFrame = core(this);
  569. if (!coreFrame)
  570. return E_FAIL;
  571. WebDataSource* webDataSource = getWebDataSource(coreFrame->loader()->documentLoader());
  572. *source = webDataSource;
  573. if (webDataSource)
  574. webDataSource->AddRef();
  575. return *source ? S_OK : E_FAIL;
  576. }
  577. HRESULT STDMETHODCALLTYPE WebFrame::provisionalDataSource(
  578. /* [retval][out] */ IWebDataSource** source)
  579. {
  580. if (!source) {
  581. ASSERT_NOT_REACHED();
  582. return E_POINTER;
  583. }
  584. *source = 0;
  585. Frame* coreFrame = core(this);
  586. if (!coreFrame)
  587. return E_FAIL;
  588. WebDataSource* webDataSource = getWebDataSource(coreFrame->loader()->provisionalDocumentLoader());
  589. *source = webDataSource;
  590. if (webDataSource)
  591. webDataSource->AddRef();
  592. return *source ? S_OK : E_FAIL;
  593. }
  594. KURL WebFrame::url() const
  595. {
  596. Frame* coreFrame = core(this);
  597. if (!coreFrame)
  598. return KURL();
  599. return coreFrame->document()->url();
  600. }
  601. HRESULT STDMETHODCALLTYPE WebFrame::stopLoading( void)
  602. {
  603. if (Frame* coreFrame = core(this))
  604. coreFrame->loader()->stopAllLoaders();
  605. return S_OK;
  606. }
  607. HRESULT STDMETHODCALLTYPE WebFrame::reload( void)
  608. {
  609. Frame* coreFrame = core(this);
  610. if (!coreFrame)
  611. return E_FAIL;
  612. coreFrame->loader()->reload();
  613. return S_OK;
  614. }
  615. HRESULT STDMETHODCALLTYPE WebFrame::findFrameNamed(
  616. /* [in] */ BSTR name,
  617. /* [retval][out] */ IWebFrame** frame)
  618. {
  619. if (!frame) {
  620. ASSERT_NOT_REACHED();
  621. return E_POINTER;
  622. }
  623. *frame = 0;
  624. Frame* coreFrame = core(this);
  625. if (!coreFrame)
  626. return E_FAIL;
  627. Frame* foundFrame = coreFrame->tree()->find(AtomicString(name, SysStringLen(name)));
  628. if (!foundFrame)
  629. return S_OK;
  630. WebFrame* foundWebFrame = kit(foundFrame);
  631. if (!foundWebFrame)
  632. return E_FAIL;
  633. return foundWebFrame->QueryInterface(IID_IWebFrame, (void**)frame);
  634. }
  635. HRESULT STDMETHODCALLTYPE WebFrame::parentFrame(
  636. /* [retval][out] */ IWebFrame** frame)
  637. {
  638. HRESULT hr = S_OK;
  639. *frame = 0;
  640. if (Frame* coreFrame = core(this))
  641. if (WebFrame* webFrame = kit(coreFrame->tree()->parent()))
  642. hr = webFrame->QueryInterface(IID_IWebFrame, (void**) frame);
  643. return hr;
  644. }
  645. class EnumChildFrames : public IEnumVARIANT
  646. {
  647. public:
  648. EnumChildFrames(Frame* f) : m_refCount(1), m_frame(f), m_curChild(f ? f->tree()->firstChild() : 0) { }
  649. virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject)
  650. {
  651. *ppvObject = 0;
  652. if (IsEqualGUID(riid, IID_IUnknown) || IsEqualGUID(riid, IID_IEnumVARIANT))
  653. *ppvObject = this;
  654. else
  655. return E_NOINTERFACE;
  656. AddRef();
  657. return S_OK;
  658. }
  659. virtual ULONG STDMETHODCALLTYPE AddRef(void)
  660. {
  661. return ++m_refCount;
  662. }
  663. virtual ULONG STDMETHODCALLTYPE Release(void)
  664. {
  665. ULONG newRef = --m_refCount;
  666. if (!newRef)
  667. delete(this);
  668. return newRef;
  669. }
  670. virtual HRESULT STDMETHODCALLTYPE Next(ULONG celt, VARIANT *rgVar, ULONG *pCeltFetched)
  671. {
  672. if (pCeltFetched)
  673. *pCeltFetched = 0;
  674. if (!rgVar)
  675. return E_POINTER;
  676. VariantInit(rgVar);
  677. if (!celt || celt > 1)
  678. return S_FALSE;
  679. if (!m_frame || !m_curChild)
  680. return S_FALSE;
  681. WebFrame* webFrame = kit(m_curChild);
  682. IUnknown* unknown;
  683. HRESULT hr = webFrame->QueryInterface(IID_IUnknown, (void**)&unknown);
  684. if (FAILED(hr))
  685. return hr;
  686. V_VT(rgVar) = VT_UNKNOWN;
  687. V_UNKNOWN(rgVar) = unknown;
  688. m_curChild = m_curChild->tree()->nextSibling();
  689. if (pCeltFetched)
  690. *pCeltFetched = 1;
  691. return S_OK;
  692. }
  693. virtual HRESULT STDMETHODCALLTYPE Skip(ULONG celt)
  694. {
  695. if (!m_frame)
  696. return S_FALSE;
  697. for (unsigned i = 0; i < celt && m_curChild; i++)
  698. m_curChild = m_curChild->tree()->nextSibling();
  699. return m_curChild ? S_OK : S_FALSE;
  700. }
  701. virtual HRESULT STDMETHODCALLTYPE Reset(void)
  702. {
  703. if (!m_frame)
  704. return S_FALSE;
  705. m_curChild = m_frame->tree()->firstChild();
  706. return S_OK;
  707. }
  708. virtual HRESULT STDMETHODCALLTYPE Clone(IEnumVARIANT**)
  709. {
  710. return E_NOTIMPL;
  711. }
  712. private:
  713. ULONG m_refCount;
  714. Frame* m_frame;
  715. Frame* m_curChild;
  716. };
  717. HRESULT STDMETHODCALLTYPE WebFrame::childFrames(
  718. /* [retval][out] */ IEnumVARIANT **enumFrames)
  719. {
  720. if (!enumFrames)
  721. return E_POINTER;
  722. *enumFrames = new EnumChildFrames(core(this));
  723. return S_OK;
  724. }
  725. // IWebFramePrivate ------------------------------------------------------
  726. HRESULT WebFrame::renderTreeAsExternalRepresentation(BOOL forPrinting, BSTR *result)
  727. {
  728. if (!result)
  729. return E_POINTER;
  730. Frame* coreFrame = core(this);
  731. if (!coreFrame)
  732. return E_FAIL;
  733. *result = BString(externalRepresentation(coreFrame, forPrinting ? RenderAsTextPrintingMode : RenderAsTextBehaviorNormal)).release();
  734. return S_OK;
  735. }
  736. HRESULT STDMETHODCALLTYPE WebFrame::pageNumberForElementById(
  737. /* [in] */ BSTR id,
  738. /* [in] */ float pageWidthInPixels,
  739. /* [in] */ float pageHeightInPixels,
  740. /* [retval][out] */ int* result)
  741. {
  742. // TODO: Please remove this function if not needed as this is LTC specific function
  743. // and has been moved to Internals.
  744. notImplemented();
  745. return E_FAIL;
  746. }
  747. HRESULT STDMETHODCALLTYPE WebFrame::numberOfPages(
  748. /* [in] */ float pageWidthInPixels,
  749. /* [in] */ float pageHeightInPixels,
  750. /* [retval][out] */ int* result)
  751. {
  752. // TODO: Please remove this function if not needed as this is LTC specific function
  753. // and has been moved to Internals.
  754. notImplemented();
  755. return E_FAIL;
  756. }
  757. HRESULT STDMETHODCALLTYPE WebFrame::scrollOffset(
  758. /* [retval][out] */ SIZE* offset)
  759. {
  760. if (!offset) {
  761. ASSERT_NOT_REACHED();
  762. return E_POINTER;
  763. }
  764. Frame* coreFrame = core(this);
  765. if (!coreFrame)
  766. return E_FAIL;
  767. FrameView* view = coreFrame->view();
  768. if (!view)
  769. return E_FAIL;
  770. *offset = view->scrollOffset();
  771. return S_OK;
  772. }
  773. HRESULT STDMETHODCALLTYPE WebFrame::layout()
  774. {
  775. Frame* coreFrame = core(this);
  776. if (!coreFrame)
  777. return E_FAIL;
  778. FrameView* view = coreFrame->view();
  779. if (!view)
  780. return E_FAIL;
  781. view->layout();
  782. return S_OK;
  783. }
  784. HRESULT STDMETHODCALLTYPE WebFrame::firstLayoutDone(
  785. /* [retval][out] */ BOOL* result)
  786. {
  787. if (!result) {
  788. ASSERT_NOT_REACHED();
  789. return E_POINTER;
  790. }
  791. *result = 0;
  792. Frame* coreFrame = core(this);
  793. if (!coreFrame)
  794. return E_FAIL;
  795. *result = coreFrame->loader()->stateMachine()->firstLayoutDone();
  796. return S_OK;
  797. }
  798. HRESULT STDMETHODCALLTYPE WebFrame::loadType(
  799. /* [retval][out] */ WebFrameLoadType* type)
  800. {
  801. if (!type) {
  802. ASSERT_NOT_REACHED();
  803. return E_POINTER;
  804. }
  805. *type = (WebFrameLoadType)0;
  806. Frame* coreFrame = core(this);
  807. if (!coreFrame)
  808. return E_FAIL;
  809. *type = (WebFrameLoadType)coreFrame->loader()->loadType();
  810. return S_OK;
  811. }
  812. HRESULT STDMETHODCALLTYPE WebFrame::pendingFrameUnloadEventCount(
  813. /* [retval][out] */ UINT* result)
  814. {
  815. if (!result) {
  816. ASSERT_NOT_REACHED();
  817. return E_POINTER;
  818. }
  819. *result = 0;
  820. Frame* coreFrame = core(this);
  821. if (!coreFrame)
  822. return E_FAIL;
  823. *result = coreFrame->document()->domWindow()->pendingUnloadEventListeners();
  824. return S_OK;
  825. }
  826. HRESULT STDMETHODCALLTYPE WebFrame::unused2()
  827. {
  828. return E_NOTIMPL;
  829. }
  830. HRESULT STDMETHODCALLTYPE WebFrame::hasSpellingMarker(
  831. /* [in] */ UINT from,
  832. /* [in] */ UINT length,
  833. /* [retval][out] */ BOOL* result)
  834. {
  835. Frame* coreFrame = core(this);
  836. if (!coreFrame)
  837. return E_FAIL;
  838. *result = coreFrame->editor().selectionStartHasMarkerFor(DocumentMarker::Spelling, from, length);
  839. return S_OK;
  840. }
  841. HRESULT STDMETHODCALLTYPE WebFrame::clearOpener()
  842. {
  843. HRESULT hr = S_OK;
  844. if (Frame* coreFrame = core(this))
  845. coreFrame->loader()->setOpener(0);
  846. return hr;
  847. }
  848. HRESULT WebFrame::setTextDirection(BSTR direction)
  849. {
  850. Frame* coreFrame = core(this);
  851. if (!coreFrame)
  852. return E_FAIL;
  853. String directionString(direction, SysStringLen(direction));
  854. if (directionString == "auto")
  855. coreFrame->editor().setBaseWritingDirection(NaturalWritingDirection);
  856. else if (directionString == "ltr")
  857. coreFrame->editor().setBaseWritingDirection(LeftToRightWritingDirection);
  858. else if (directionString == "rtl")
  859. coreFrame->editor().setBaseWritingDirection(RightToLeftWritingDirection);
  860. return S_OK;
  861. }
  862. // IWebDocumentText -----------------------------------------------------------
  863. HRESULT STDMETHODCALLTYPE WebFrame::supportsTextEncoding(
  864. /* [retval][out] */ BOOL* result)
  865. {
  866. *result = FALSE;
  867. return E_NOTIMPL;
  868. }
  869. HRESULT STDMETHODCALLTYPE WebFrame::selectedString(
  870. /* [retval][out] */ BSTR* result)
  871. {
  872. *result = 0;
  873. Frame* coreFrame = core(this);
  874. if (!coreFrame)
  875. return E_FAIL;
  876. String text = coreFrame->displayStringModifiedByEncoding(coreFrame->editor().selectedText());
  877. *result = BString(text).release();
  878. return S_OK;
  879. }
  880. HRESULT STDMETHODCALLTYPE WebFrame::selectAll()
  881. {
  882. Frame* coreFrame = core(this);
  883. if (!coreFrame)
  884. return E_FAIL;
  885. if (!coreFrame->editor().command("SelectAll").execute())
  886. return E_FAIL;
  887. return S_OK;
  888. }
  889. HRESULT STDMETHODCALLTYPE WebFrame::deselectAll()
  890. {
  891. return E_NOTIMPL;
  892. }
  893. // WebFrame ---------------------------------------------------------------
  894. PassRefPtr<Frame> WebFrame::init(IWebView* webView, Page* page, HTMLFrameOwnerElement* ownerElement)
  895. {
  896. webView->QueryInterface(&d->webView);
  897. d->webView->Release(); // don't hold the extra ref
  898. HWND viewWindow;
  899. d->webView->viewWindow((OLE_HANDLE*)&viewWindow);
  900. this->AddRef(); // We release this ref in frameLoaderDestroyed()
  901. RefPtr<Frame> frame = Frame::create(page, ownerElement, this);
  902. d->frame = frame.get();
  903. return frame.release();
  904. }
  905. Frame* WebFrame::impl()
  906. {
  907. return d->frame;
  908. }
  909. void WebFrame::invalidate()
  910. {
  911. Frame* coreFrame = core(this);
  912. ASSERT(coreFrame);
  913. if (Document* document = coreFrame->document())
  914. document->recalcStyle(Node::Force);
  915. }
  916. HRESULT WebFrame::inViewSourceMode(BOOL* flag)
  917. {
  918. if (!flag) {
  919. ASSERT_NOT_REACHED();
  920. return E_POINTER;
  921. }
  922. *flag = FALSE;
  923. Frame* coreFrame = core(this);
  924. if (!coreFrame)
  925. return E_FAIL;
  926. *flag = coreFrame->inViewSourceMode() ? TRUE : FALSE;
  927. return S_OK;
  928. }
  929. HRESULT WebFrame::setInViewSourceMode(BOOL flag)
  930. {
  931. Frame* coreFrame = core(this);
  932. if (!coreFrame)
  933. return E_FAIL;
  934. coreFrame->setInViewSourceMode(!!flag);
  935. return S_OK;
  936. }
  937. HRESULT WebFrame::elementWithName(BSTR name, IDOMElement* form, IDOMElement** element)
  938. {
  939. if (!form)
  940. return E_INVALIDARG;
  941. HTMLFormElement* formElement = formElementFromDOMElement(form);
  942. if (formElement) {
  943. const Vector<FormAssociatedElement*>& elements = formElement->associatedElements();
  944. AtomicString targetName((UChar*)name, SysStringLen(name));
  945. for (unsigned int i = 0; i < elements.size(); i++) {
  946. if (!elements[i]->isFormControlElement())
  947. continue;
  948. HTMLFormControlElement* elt = static_cast<HTMLFormControlElement*>(elements[i]);
  949. // Skip option elements, other duds
  950. if (elt->name() == targetName) {
  951. *element = DOMElement::createInstance(elt);
  952. return S_OK;
  953. }
  954. }
  955. }
  956. return E_FAIL;
  957. }
  958. HRESULT WebFrame::formForElement(IDOMElement* element, IDOMElement** form)
  959. {
  960. if (!element)
  961. return E_INVALIDARG;
  962. HTMLInputElement *inputElement = inputElementFromDOMElement(element);
  963. if (!inputElement)
  964. return E_FAIL;
  965. HTMLFormElement *formElement = inputElement->form();
  966. if (!formElement)
  967. return E_FAIL;
  968. *form = DOMElement::createInstance(formElement);
  969. return S_OK;
  970. }
  971. HRESULT WebFrame::elementDoesAutoComplete(IDOMElement *element, BOOL *result)
  972. {
  973. *result = false;
  974. if (!element)
  975. return E_INVALIDARG;
  976. HTMLInputElement *inputElement = inputElementFromDOMElement(element);
  977. if (!inputElement)
  978. *result = false;
  979. else
  980. *result = inputElement->isTextField() && !inputElement->isPasswordField() && inputElement->shouldAutocomplete();
  981. return S_OK;
  982. }
  983. HRESULT STDMETHODCALLTYPE WebFrame::resumeAnimations()
  984. {
  985. Frame* frame = core(this);
  986. if (!frame)
  987. return E_FAIL;
  988. frame->animation()->resumeAnimations();
  989. return S_OK;
  990. }
  991. HRESULT STDMETHODCALLTYPE WebFrame::suspendAnimations()
  992. {
  993. Frame* frame = core(this);
  994. if (!frame)
  995. return E_FAIL;
  996. frame->animation()->suspendAnimations();
  997. return S_OK;
  998. }
  999. HRESULT WebFrame::pauseAnimation(BSTR animationName, IDOMNode* node, double secondsFromNow, BOOL* animationWasRunning)
  1000. {
  1001. if (!node || !animationWasRunning)
  1002. return E_POINTER;
  1003. *animationWasRunning = FALSE;
  1004. Frame* frame = core(this);
  1005. if (!frame)
  1006. return E_FAIL;
  1007. AnimationController* controller = frame->animation();
  1008. if (!controller)
  1009. return E_FAIL;
  1010. COMPtr<DOMNode> domNode(Query, node);
  1011. if (!domNode)
  1012. return E_FAIL;
  1013. *animationWasRunning = controller->pauseAnimationAtTime(domNode->node()->renderer(), String(animationName, SysStringLen(animationName)), secondsFromNow);
  1014. return S_OK;
  1015. }
  1016. HRESULT WebFrame::pauseTransition(BSTR propertyName, IDOMNode* node, double secondsFromNow, BOOL* transitionWasRunning)
  1017. {
  1018. if (!node || !transitionWasRunning)
  1019. return E_POINTER;
  1020. *transitionWasRunning = FALSE;
  1021. Frame* frame = core(this);
  1022. if (!frame)
  1023. return E_FAIL;
  1024. AnimationController* controller = frame->animation();
  1025. if (!controller)
  1026. return E_FAIL;
  1027. COMPtr<DOMNode> domNode(Query, node);
  1028. if (!domNode)
  1029. return E_FAIL;
  1030. *transitionWasRunning = controller->pauseTransitionAtTime(domNode->node()->renderer(), String(propertyName, SysStringLen(propertyName)), secondsFromNow);
  1031. return S_OK;
  1032. }
  1033. HRESULT WebFrame::visibleContentRect(RECT* rect)
  1034. {
  1035. if (!rect)
  1036. return E_POINTER;
  1037. SetRectEmpty(rect);
  1038. Frame* frame = core(this);
  1039. if (!frame)
  1040. return E_FAIL;
  1041. FrameView* view = frame->view();
  1042. if (!view)
  1043. return E_FAIL;
  1044. *rect = view->visibleContentRect();
  1045. return S_OK;
  1046. }
  1047. HRESULT WebFrame::numberOfActiveAnimations(UINT* number)
  1048. {
  1049. if (!number)
  1050. return E_POINTER;
  1051. *number = 0;
  1052. Frame* frame = core(this);
  1053. if (!frame)
  1054. return E_FAIL;
  1055. AnimationController* controller = frame->animation();
  1056. if (!controller)
  1057. return E_FAIL;
  1058. *number = controller->numberOfActiveAnimations(frame->document());
  1059. return S_OK;
  1060. }
  1061. HRESULT WebFrame::isDisplayingStandaloneImage(BOOL* result)
  1062. {
  1063. if (!result)
  1064. return E_POINTER;
  1065. *result = FALSE;
  1066. Frame* frame = core(this);
  1067. if (!frame)
  1068. return E_FAIL;
  1069. Document* document = frame->document();
  1070. *result = document && document->isImageDocument();
  1071. return S_OK;
  1072. }
  1073. HRESULT WebFrame::allowsFollowingLink(BSTR url, BOOL* result)
  1074. {
  1075. if (!result)
  1076. return E_POINTER;
  1077. *result = TRUE;
  1078. Frame* frame = core(this);
  1079. if (!frame)
  1080. return E_FAIL;
  1081. *result = frame->document()->securityOrigin()->canDisplay(MarshallingHelpers::BSTRToKURL(url));
  1082. return S_OK;
  1083. }
  1084. HRESULT WebFrame::controlsInForm(IDOMElement* form, IDOMElement** controls, int* cControls)
  1085. {
  1086. if (!form)
  1087. return E_INVALIDARG;
  1088. HTMLFormElement* formElement = formElementFromDOMElement(form);
  1089. if (!formElement)
  1090. return E_FAIL;
  1091. int inCount = *cControls;
  1092. int count = (int) formElement->associatedElements().size();
  1093. *cControls = count;
  1094. if (!controls)
  1095. return S_OK;
  1096. if (inCount < count)
  1097. return E_FAIL;
  1098. *cControls = 0;
  1099. const Vector<FormAssociatedElement*>& elements = formElement->associatedElements();
  1100. for (int i = 0; i < count; i++) {
  1101. if (elements.at(i)->isEnumeratable()) { // Skip option elements, other duds
  1102. controls[*cControls] = DOMElement::createInstance(toHTMLElement(elements.at(i)));
  1103. (*cControls)++;
  1104. }
  1105. }
  1106. return S_OK;
  1107. }
  1108. HRESULT WebFrame::elementIsPassword(IDOMElement *element, bool *result)
  1109. {
  1110. HTMLInputElement* inputElement = inputElementFromDOMElement(element);
  1111. *result = inputElement && inputElement->isPasswordField();
  1112. return S_OK;
  1113. }
  1114. HRESULT WebFrame::searchForLabelsBeforeElement(const BSTR* labels, unsigned cLabels, IDOMElement* beforeElement, unsigned* outResultDistance, BOOL* outResultIsInCellAbove, BSTR* result)
  1115. {
  1116. if (!result) {
  1117. ASSERT_NOT_REACHED();
  1118. return E_POINTER;
  1119. }
  1120. if (outResultDistance)
  1121. *outResultDistance = 0;
  1122. if (outResultIsInCellAbove)
  1123. *outResultIsInCellAbove = FALSE;
  1124. *result = 0;
  1125. if (!cLabels)
  1126. return S_OK;
  1127. if (cLabels < 1)
  1128. return E_INVALIDARG;
  1129. Frame* coreFrame = core(this);
  1130. if (!coreFrame)
  1131. return E_FAIL;
  1132. Vector<String> labelStrings(cLabels);
  1133. for (int i=0; i<cLabels; i++)
  1134. labelStrings[i] = String(labels[i], SysStringLen(labels[i]));
  1135. Element *coreElement = elementFromDOMElement(beforeElement);
  1136. if (!coreElement)
  1137. return E_FAIL;
  1138. size_t resultDistance;
  1139. bool resultIsInCellAbove;
  1140. String label = coreFrame->searchForLabelsBeforeElement(labelStrings, coreElement, &resultDistance, &resultIsInCellAbove);
  1141. *result = SysAllocStringLen(label.characters(), label.length());
  1142. if (label.length() && !*result)
  1143. return E_OUTOFMEMORY;
  1144. if (outResultDistance)
  1145. *outResultDistance = resultDistance;
  1146. if (outResultIsInCellAbove)
  1147. *outResultIsInCellAbove = resultIsInCellAbove;
  1148. return S_OK;
  1149. }
  1150. HRESULT WebFrame::matchLabelsAgainstElement(const BSTR* labels, int cLabels, IDOMElement* againstElement, BSTR* result)
  1151. {
  1152. if (!result) {
  1153. ASSERT_NOT_REACHED();
  1154. return E_POINTER;
  1155. }
  1156. *result = 0;
  1157. if (!cLabels)
  1158. return S_OK;
  1159. if (cLabels < 1)
  1160. return E_INVALIDARG;
  1161. Frame* coreFrame = core(this);
  1162. if (!coreFrame)
  1163. return E_FAIL;
  1164. Vector<String> labelStrings(cLabels);
  1165. for (int i=0; i<cLabels; i++)
  1166. labelStrings[i] = String(labels[i], SysStringLen(labels[i]));
  1167. Element *coreElement = elementFromDOMElement(againstElement);
  1168. if (!coreElement)
  1169. return E_FAIL;
  1170. String label = coreFrame->matchLabelsAgainstElement(labelStrings, coreElement);
  1171. *result = SysAllocStringLen(label.characters(), label.length());
  1172. if (label.length() && !*result)
  1173. return E_OUTOFMEMORY;
  1174. return S_OK;
  1175. }
  1176. HRESULT WebFrame::canProvideDocumentSource(bool* result)
  1177. {
  1178. HRESULT hr = S_OK;
  1179. *result = false;
  1180. COMPtr<IWebDataSource> dataSource;
  1181. hr = WebFrame::dataSource(&dataSource);
  1182. if (FAILED(hr))
  1183. return hr;
  1184. COMPtr<IWebURLResponse> urlResponse;
  1185. hr = dataSource->response(&urlResponse);
  1186. if (SUCCEEDED(hr) && urlResponse) {
  1187. BString mimeTypeBStr;
  1188. if (SUCCEEDED(urlResponse->MIMEType(&mimeTypeBStr))) {
  1189. String mimeType(mimeTypeBStr, SysStringLen(mimeTypeBStr));
  1190. *result = mimeType == "text/html" || WebCore::DOMImplementation::isXMLMIMEType(mimeType);
  1191. }
  1192. }
  1193. return hr;
  1194. }
  1195. HRESULT STDMETHODCALLTYPE WebFrame::layerTreeAsText(BSTR* result)
  1196. {
  1197. if (!result)
  1198. return E_POINTER;
  1199. *result = 0;
  1200. Frame* frame = core(this);
  1201. if (!frame)
  1202. return E_FAIL;
  1203. String text = frame->layerTreeAsText();
  1204. *result = BString(text).release();
  1205. return S_OK;
  1206. }
  1207. void WebFrame::frameLoaderDestroyed()
  1208. {
  1209. // The FrameLoader going away is equivalent to the Frame going away,
  1210. // so we now need to clear our frame pointer.
  1211. d->frame = 0;
  1212. this->Release();
  1213. }
  1214. void WebFrame::makeRepresentation(DocumentLoader*)
  1215. {
  1216. notImplemented();
  1217. }
  1218. void WebFrame::forceLayoutForNonHTML()
  1219. {
  1220. notImplemented();
  1221. }
  1222. void WebFrame::setCopiesOnScroll()
  1223. {
  1224. notImplemented();
  1225. }
  1226. void WebFrame::detachedFromParent2()
  1227. {
  1228. notImplemented();
  1229. }
  1230. void WebFrame::detachedFromParent3()
  1231. {
  1232. notImplemented();
  1233. }
  1234. void WebFrame::cancelPolicyCheck()
  1235. {
  1236. if (d->m_policyListener) {
  1237. d->m_policyListener->invalidate();
  1238. d->m_policyListener = 0;
  1239. }
  1240. d->m_policyFunction = 0;
  1241. }
  1242. void WebFrame::dispatchWillSendSubmitEvent(PassRefPtr<WebCore::FormState>)
  1243. {
  1244. }
  1245. void WebFrame::dispatchWillSubmitForm(FramePolicyFunction function, PassRefPtr<FormState> formState)
  1246. {
  1247. Frame* coreFrame = core(this);
  1248. ASSERT(coreFrame);
  1249. COMPtr<IWebFormDelegate> formDelegate;
  1250. if (FAILED(d->webView->formDelegate(&formDelegate))) {
  1251. (coreFrame->loader()->policyChecker()->*function)(PolicyUse);
  1252. return;
  1253. }
  1254. COMPtr<IDOMElement> formElement(AdoptCOM, DOMElement::createInstance(formState->form()));
  1255. HashMap<String, String> formValuesMap;
  1256. const StringPairVector& textFieldValues = formState->textFieldValues();
  1257. size_t size = textFieldValues.size();
  1258. for (size_t i = 0; i < size; ++i)
  1259. formValuesMap.add(textFieldValues[i].first, textFieldValues[i].second);
  1260. COMPtr<IPropertyBag> formValuesPropertyBag(AdoptCOM, COMPropertyBag<String>::createInstance(formValuesMap));
  1261. COMPtr<WebFrame> sourceFrame(kit(formState->sourceDocument()->frame()));
  1262. if (SUCCEEDED(formDelegate->willSubmitForm(this, sourceFrame.get(), formElement.get(), formValuesPropertyBag.get(), setUpPolicyListener(function).get())))
  1263. return;
  1264. // FIXME: Add a sane default implementation
  1265. (coreFrame->loader()->policyChecker()->*function)(PolicyUse);
  1266. }
  1267. void WebFrame::revertToProvisionalState(DocumentLoader*)
  1268. {
  1269. notImplemented();
  1270. }
  1271. void WebFrame::setMainFrameDocumentReady(bool)
  1272. {
  1273. notImplemented();
  1274. }
  1275. void WebFrame::willChangeTitle(DocumentLoader*)
  1276. {
  1277. notImplemented();
  1278. }
  1279. void WebFrame::didChangeTitle(DocumentLoader*)
  1280. {
  1281. notImplemented();
  1282. }
  1283. void WebFrame::didChangeIcons(DocumentLoader*)
  1284. {
  1285. notImplemented();
  1286. }
  1287. bool WebFrame::canHandleRequest(const ResourceRequest& request) const
  1288. {
  1289. return WebView::canHandleRequest(request);
  1290. }
  1291. bool WebFrame::canShowMIMETypeAsHTML(const String& /*MIMEType*/) const
  1292. {
  1293. notImplemented();
  1294. return true;
  1295. }
  1296. bool WebFrame::canShowMIMEType(const String& /*MIMEType*/) const
  1297. {
  1298. notImplemented();
  1299. return true;
  1300. }
  1301. bool WebFrame::representationExistsForURLScheme(const String& /*URLScheme*/) const
  1302. {
  1303. notImplemented();
  1304. return false;
  1305. }
  1306. String WebFrame::generatedMIMETypeForURLScheme(const String& /*URLScheme*/) const
  1307. {
  1308. notImplemented();
  1309. ASSERT_NOT_REACHED();
  1310. return String();
  1311. }
  1312. void WebFrame::frameLoadCompleted()
  1313. {
  1314. }
  1315. void WebFrame::restoreViewState()
  1316. {
  1317. }
  1318. void WebFrame::provisionalLoadStarted()
  1319. {
  1320. notImplemented();
  1321. }
  1322. bool WebFrame::shouldTreatURLAsSameAsCurrent(const KURL&) const
  1323. {
  1324. notImplemented();
  1325. return false;
  1326. }
  1327. void WebFrame::addHistoryItemForFragmentScroll()
  1328. {
  1329. notImplemented();
  1330. }
  1331. void WebFrame::didFinishLoad()
  1332. {
  1333. notImplemented();
  1334. }
  1335. void WebFrame::prepareForDataSourceReplacement()
  1336. {
  1337. notImplemented();
  1338. }
  1339. String WebFrame::userAgent(const KURL& url)
  1340. {
  1341. return d->webView->userAgentForKURL(url);
  1342. }
  1343. void WebFrame::saveViewStateToItem(HistoryItem*)
  1344. {
  1345. }
  1346. ResourceError WebFrame::cancelledError(const ResourceRequest& request)
  1347. {
  1348. // FIXME: Need ChickenCat to include CFNetwork/CFURLError.h to get these values
  1349. // Alternatively, we could create our own error domain/codes.
  1350. return ResourceError(String(WebURLErrorDomain), -999, request.url().string(), String());
  1351. }
  1352. ResourceError WebFrame::blockedError(const ResourceRequest& request)
  1353. {
  1354. // FIXME: Need to implement the String descriptions for errors in the WebKitErrorDomain and have them localized
  1355. return ResourceError(String(WebKitErrorDomain), WebKitErrorCannotUseRestrictedPort, request.url().string(), String());
  1356. }
  1357. ResourceError WebFrame::cannotShowURLError(const ResourceRequest& request)
  1358. {
  1359. // FIXME: Need to implement the String descriptions for errors in the WebKitErrorDomain and have them localized
  1360. return ResourceError(String(WebKitErrorDomain), WebKitErrorCannotShowURL, request.url().string(), String());
  1361. }
  1362. ResourceError WebFrame::interruptedForPolicyChangeError(const ResourceRequest& request)
  1363. {
  1364. // FIXME: Need to implement the String descriptions for errors in the WebKitErrorDomain and have them localized
  1365. return ResourceError(String(WebKitErrorDomain), WebKitErrorFrameLoadInterruptedByPolicyChange, request.url().string(), String());
  1366. }
  1367. ResourceError WebFrame::cannotShowMIMETypeError(const ResourceResponse&)
  1368. {
  1369. notImplemented();
  1370. return ResourceError();
  1371. }
  1372. ResourceError WebFrame::fileDoesNotExistError(const ResourceResponse&)
  1373. {
  1374. notImplemented();
  1375. return ResourceError();
  1376. }
  1377. ResourceError WebFrame::pluginWillHandleLoadError(const ResourceResponse& response)
  1378. {
  1379. return ResourceError(String(WebKitErrorDomain), WebKitErrorPlugInWillHandleLoad, response.url().string(), String());
  1380. }
  1381. bool WebFrame::shouldFallBack(const ResourceError& error)
  1382. {
  1383. if (error.errorCode() == WebURLErrorCancelled && error.domain() == String(WebURLErrorDomain))
  1384. return false;
  1385. if (error.errorCode() == WebKitErrorPlugInWillHandleLoad && error.domain() == String(WebKitErrorDomain))
  1386. return false;
  1387. return true;
  1388. }
  1389. COMPtr<WebFramePolicyListener> WebFrame::setUpPolicyListener(WebCore::FramePolicyFunction function)
  1390. {
  1391. // FIXME: <rdar://5634381> We need to support multiple active policy listeners.
  1392. if (d->m_policyListener)
  1393. d->m_policyListener->invalidate();
  1394. Frame* coreFrame = core(this);
  1395. ASSERT(coreFrame);
  1396. d->m_policyListener.adoptRef(WebFramePolicyListener::createInstance(coreFrame));
  1397. d->m_policyFunction = function;
  1398. return d->m_policyListener;
  1399. }
  1400. void WebFrame::receivedPolicyDecision(PolicyAction action)
  1401. {
  1402. ASSERT(d->m_policyListener);
  1403. ASSERT(d->m_policyFunction);
  1404. FramePolicyFunction function = d->m_policyFunction;
  1405. d->m_policyListener = 0;
  1406. d->m_policyFunction = 0;
  1407. Frame* coreFrame = core(this);
  1408. ASSERT(coreFrame);
  1409. (coreFrame->loader()->policyChecker()->*function)(action);
  1410. }
  1411. void WebFrame::dispatchDecidePolicyForResponse(FramePolicyFunction function, const ResourceResponse& response, const ResourceRequest& request)
  1412. {
  1413. Frame* coreFrame = core(this);
  1414. ASSERT(coreFrame);
  1415. COMPtr<IWebPolicyDelegate> policyDelegate;
  1416. if (FAILED(d->webView->policyDelegate(&policyDelegate)))
  1417. policyDelegate = DefaultPolicyDelegate::sharedInstance();
  1418. COMPtr<IWebURLRequest> urlRequest(AdoptCOM, WebMutableURLRequest::createInstance(request));
  1419. if (SUCCEEDED(policyDelegate->decidePolicyForMIMEType(d->webView, BString(response.mimeType()), urlRequest.get(), this, setUpPolicyListener(function).get())))
  1420. return;
  1421. (coreFrame->loader()->policyChecker()->*function)(PolicyUse);
  1422. }
  1423. void WebFrame::dispatchDecidePolicyForNewWindowAction(FramePolicyFunction function, const NavigationAction& action, const ResourceRequest& request, PassRefPtr<FormState> formState, const String& frameName)
  1424. {
  1425. Frame* coreFrame = core(this);
  1426. ASSERT(coreFrame);
  1427. COMPtr<IWebPolicyDelegate> policyDelegate;
  1428. if (FAILED(d->webView->policyDelegate(&policyDelegate)))
  1429. policyDelegate = DefaultPolicyDelegate::sharedInstance();
  1430. COMPtr<IWebURLRequest> urlRequest(AdoptCOM, WebMutableURLRequest::createInstance(request));
  1431. COMPtr<WebActionPropertyBag> actionInformation(AdoptCOM, WebActionPropertyBag::createInstance(action, formState ? formState->form() : 0, coreFrame));
  1432. if (SUCCEEDED(policyDelegate->decidePolicyForNewWindowAction(d->webView, actionInformation.get(), urlRequest.get(), BString(frameName), setUpPolicyListener(function).get())))
  1433. return;
  1434. (coreFrame->loader()->policyChecker()->*function)(PolicyUse);
  1435. }
  1436. void WebFrame::dispatchDecidePolicyForNavigationAction(FramePolicyFunction function, const NavigationAction& action, const ResourceRequest& request, PassRefPtr<FormState> formState)
  1437. {
  1438. Frame* coreFrame = core(this);
  1439. ASSERT(coreFrame);
  1440. COMPtr<IWebPolicyDelegate> policyDelegate;
  1441. if (FAILED(d->webView->policyDelegate(&policyDelegate)))
  1442. policyDelegate = DefaultPolicyDelegate::sharedInstance();
  1443. COMPtr<IWebURLRequest> urlRequest(AdoptCOM, WebMutableURLRequest::createInstance(request));
  1444. COMPtr<WebActionPropertyBag> actionInformation(AdoptCOM, WebActionPropertyBag::createInstance(action, formState ? formState->form() : 0, coreFrame));
  1445. if (SUCCEEDED(policyDelegate->decidePolicyForNavigationAction(d->webView, actionInformation.get(), urlRequest.get(), this, setUpPolicyListener(function).get())))
  1446. return;
  1447. (coreFrame->loader()->policyChecker()->*function)(PolicyUse);
  1448. }
  1449. void WebFrame::dispatchUnableToImplementPolicy(const ResourceError& error)
  1450. {
  1451. COMPtr<IWebPolicyDelegate> policyDelegate;
  1452. if (FAILED(d->webView->policyDelegate(&policyDelegate)))
  1453. policyDelegate = DefaultPolicyDelegate::sharedInstance();
  1454. COMPtr<IWebError> webError(AdoptCOM, WebError::createInstance(error));
  1455. policyDelegate->unableToImplementPolicyWithError(d->webView, webError.get(), this);
  1456. }
  1457. void WebFrame::convertMainResourceLoadToDownload(DocumentLoader* documentLoader, const ResourceRequest& request, const ResourceResponse& response)
  1458. {
  1459. COMPtr<IWebDownloadDelegate> downloadDelegate;
  1460. COMPtr<IWebView> webView;
  1461. if (SUCCEEDED(this->webView(&webView))) {
  1462. if (FAILED(webView->downloadDelegate(&downloadDelegate))) {
  1463. // If the WebView doesn't successfully provide a download delegate we'll pass a null one
  1464. // into the WebDownload - which may or may not decide to use a DefaultDownloadDelegate
  1465. LOG_ERROR("Failed to get downloadDelegate from WebView");
  1466. downloadDelegate = 0;
  1467. }
  1468. }
  1469. // Its the delegate's job to ref the WebDownload to keep it alive - otherwise it will be destroyed
  1470. // when this method returns
  1471. COMPtr<WebDownload> download;
  1472. download.adoptRef(WebDownload::createInstance(documentLoader->mainResourceLoader()->handle(), request, response, downloadDelegate.get()));
  1473. }
  1474. bool WebFrame::dispatchDidLoadResourceFromMemoryCache(DocumentLoader*, const ResourceRequest&, const ResourceResponse&, int /*length*/)
  1475. {
  1476. notImplemented();
  1477. return false;
  1478. }
  1479. void WebFrame::dispatchDidFailProvisionalLoad(const ResourceError& error)
  1480. {
  1481. COMPtr<IWebFrameLoadDelegate> frameLoadDelegate;
  1482. if (SUCCEEDED(d->webView->frameLoadDelegate(&frameLoadDelegate))) {
  1483. COMPtr<IWebError> webError;
  1484. webError.adoptRef(WebError::createInstance(error));
  1485. frameLoadDelegate->didFailProvisionalLoadWithError(d->webView, webError.get(), this);
  1486. }
  1487. }
  1488. void WebFrame::dispatchDidFailLoad(const ResourceError& error)
  1489. {
  1490. COMPtr<IWebFrameLoadDelegate> frameLoadDelegate;
  1491. if (SUCCEEDED(d->webView->frameLoadDelegate(&frameLoadDelegate))) {
  1492. COMPtr<IWebError> webError;
  1493. webError.adoptRef(WebError::createInstance(error));
  1494. frameLoadDelegate->didFailLoadWithError(d->webView, webError.get(), this);
  1495. }
  1496. }
  1497. void WebFrame::startDownload(const ResourceRequest& request, const String& /* suggestedName */)
  1498. {
  1499. d->webView->downloadURL(request.url());
  1500. }
  1501. PassRefPtr<Widget> WebFrame::createJavaAppletWidget(const IntSize& pluginSize, HTMLAppletElement* element, const KURL& /*baseURL*/, const Vector<String>& paramNames, const Vector<String>& paramValues)
  1502. {
  1503. RefPtr<PluginView> pluginView = PluginView::create(core(this), pluginSize, element, KURL(), paramNames, paramValues, "application/x-java-applet", false);
  1504. // Check if the plugin can be loaded successfully
  1505. if (pluginView->plugin() && pluginView->plugin()->load())
  1506. return pluginView;
  1507. COMPtr<IWebResourceLoadDelegate> resourceLoadDelegate;
  1508. if (FAILED(d->webView->resourceLoadDelegate(&resourceLoadDelegate)))
  1509. return pluginView;
  1510. COMPtr<CFDictionaryPropertyBag> userInfoBag = CFDictionaryPropertyBag::createInstance();
  1511. ResourceError resourceError(String(WebKitErrorDomain), WebKitErrorJavaUnavailable, String(), String());
  1512. COMPtr<IWebError> error(AdoptCOM, WebError::createInstance(resourceError, userInfoBag.get()));
  1513. resourceLoadDelegate->plugInFailedWithError(d->webView, error.get(), getWebDataSource(d->frame->loader()->documentLoader()));
  1514. return pluginView;
  1515. }
  1516. ObjectContentType WebFrame::objectContentType(const KURL& url, const String& mimeType, bool shouldPreferPlugInsForImages)
  1517. {
  1518. return WebCore::FrameLoader::defaultObjectContentType(url, mimeType, shouldPreferPlugInsForImages);
  1519. }
  1520. String WebFrame::overrideMediaType() const
  1521. {
  1522. notImplemented();
  1523. return String();
  1524. }
  1525. void WebFrame::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld* world)
  1526. {
  1527. Frame* coreFrame = core(this);
  1528. ASSERT(coreFrame);
  1529. Settings* settings = coreFrame->settings();
  1530. if (!settings || !settings->isScriptEnabled())
  1531. return;
  1532. COMPtr<IWebFrameLoadDelegate> frameLoadDelegate;
  1533. if (FAILED(d->webView->frameLoadDelegate(&frameLoadDelegate)))
  1534. return;
  1535. COMPtr<IWebFrameLoadDelegatePrivate2> delegatePrivate(Query, frameLoadDelegate);
  1536. if (delegatePrivate && delegatePrivate->didClearWindowObjectForFrameInScriptWorld(d->webView, this, WebScriptWorld::findOrCreateWorld(world).get()) != E_NOTIMPL)
  1537. return;
  1538. if (world != mainThreadNormalWorld())
  1539. return;
  1540. JSContextRef context = toRef(coreFrame->script()->globalObject(world)->globalExec());
  1541. JSObjectRef windowObject = toRef(coreFrame->script()->globalObject(world));
  1542. ASSERT(windowObject);
  1543. if (FAILED(frameLoadDelegate->didClearWindowObject(d->webView, context, windowObject, this)))
  1544. frameLoadDelegate->windowScriptObjectAvailable(d->webView, context, windowObject);
  1545. }
  1546. void WebFrame::documentElementAvailable()
  1547. {
  1548. }
  1549. void WebFrame::didPerformFirstNavigation() const
  1550. {
  1551. COMPtr<IWebPreferences> preferences;
  1552. if (FAILED(d->webView->preferences(&preferences)))
  1553. return;
  1554. COMPtr<IWebPreferencesPrivate> preferencesPrivate(Query, preferences);
  1555. if (!preferencesPrivate)
  1556. return;
  1557. BOOL automaticallyDetectsCacheModel;
  1558. if (FAILED(preferencesPrivate->automaticallyDetectsCacheModel(&automaticallyDetectsCacheModel)))
  1559. return;
  1560. WebCacheModel cacheModel;
  1561. if (FAILED(preferences->cacheModel(&cacheModel)))
  1562. return;
  1563. if (automaticallyDetectsCacheModel && cacheModel < WebCacheModelDocumentBrowser)
  1564. preferences->setCacheModel(WebCacheModelDocumentBrowser);
  1565. }
  1566. void WebFrame::registerForIconNotification(bool listen)
  1567. {
  1568. d->webView->registerForIconNotification(listen);
  1569. }
  1570. static IntRect printerRect(HDC printDC)
  1571. {
  1572. return IntRect(0, 0,
  1573. GetDeviceCaps(printDC, PHYSICALWIDTH) - 2 * GetDeviceCaps(printDC, PHYSICALOFFSETX),
  1574. GetDeviceCaps(printDC, PHYSICALHEIGHT) - 2 * GetDeviceCaps(printDC, PHYSICALOFFSETY));
  1575. }
  1576. void WebFrame::setPrinting(bool printing, const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkRatio, AdjustViewSizeOrNot adjustViewSize)
  1577. {
  1578. Frame* coreFrame = core(this);
  1579. ASSERT(coreFrame);
  1580. coreFrame->setPrinting(printing, pageSize, originalPageSize, maximumShrinkRatio, adjustViewSize ? AdjustViewSize : DoNotAdjustViewSize);
  1581. }
  1582. HRESULT STDMETHODCALLTYPE WebFrame::setInPrintingMode(
  1583. /* [in] */ BOOL value,
  1584. /* [in] */ HDC printDC)
  1585. {
  1586. if (m_inPrintingMode == !!value)
  1587. return S_OK;
  1588. Frame* coreFrame = core(this);
  1589. if (!coreFrame || !coreFrame->document())
  1590. return E_FAIL;
  1591. m_inPrintingMode = !!value;
  1592. // If we are a frameset just print with the layout we have onscreen, otherwise relayout
  1593. // according to the paper size
  1594. FloatSize minLayoutSize(0.0, 0.0);
  1595. FloatSize originalPageSize(0.0, 0.0);
  1596. if (m_inPrintingMode && !coreFrame->document()->isFrameSet()) {
  1597. if (!printDC) {
  1598. ASSERT_NOT_REACHED();
  1599. return E_POINTER;
  1600. }
  1601. const int desiredPixelsPerInch = 72;
  1602. IntRect printRect = printerRect(printDC);
  1603. int paperHorizontalPixelsPerInch = ::GetDeviceCaps(printDC, LOGPIXELSX);
  1604. int paperVerticalPixelsPerInch = ::GetDeviceCaps(printDC, LOGPIXELSY);
  1605. int paperWidth = printRect.width() * desiredPixelsPerInch / paperHorizontalPixelsPerInch;
  1606. int paperHeight = printRect.height() * desiredPixelsPerInch / paperVerticalPixelsPerInch;
  1607. originalPageSize = FloatSize(paperWidth, paperHeight);
  1608. Frame* coreFrame = core(this);
  1609. minLayoutSize = coreFrame->resizePageRectsKeepingRatio(originalPageSize, FloatSize(paperWidth * PrintingMinimumShrinkFactor, paperHeight * PrintingMinimumShrinkFactor));
  1610. }
  1611. setPrinting(m_inPrintingMode, minLayoutSize, originalPageSize, PrintingMaximumShrinkFactor / PrintingMinimumShrinkFactor, AdjustViewSize);
  1612. if (!m_inPrintingMode)
  1613. m_pageRects.clear();
  1614. return S_OK;
  1615. }
  1616. void WebFrame::headerAndFooterHeights(float* headerHeight, float* footerHeight)
  1617. {
  1618. if (headerHeight)
  1619. *headerHeight = 0;
  1620. if (footerHeight)
  1621. *footerHeight = 0;
  1622. float height = 0;
  1623. COMPtr<IWebUIDelegate> ui;
  1624. if (FAILED(d->webView->uiDelegate(&ui)))
  1625. return;
  1626. if (headerHeight && SUCCEEDED(ui->webViewHeaderHeight(d->webView, &height)))
  1627. *headerHeight = height;
  1628. if (footerHeight && SUCCEEDED(ui->webViewFooterHeight(d->webView, &height)))
  1629. *footerHeight = height;
  1630. }
  1631. IntRect WebFrame::printerMarginRect(HDC printDC)
  1632. {
  1633. IntRect emptyRect(0, 0, 0, 0);
  1634. COMPtr<IWebUIDelegate> ui;
  1635. if (FAILED(d->webView->uiDelegate(&ui)))
  1636. return emptyRect;
  1637. RECT rect;
  1638. if (FAILED(ui->webViewPrintingMarginRect(d->webView, &rect)))
  1639. return emptyRect;
  1640. rect.left = MulDiv(rect.left, ::GetDeviceCaps(printDC, LOGPIXELSX), 1000);
  1641. rect.top = MulDiv(rect.top, ::GetDeviceCaps(printDC, LOGPIXELSY), 1000);
  1642. rect.right = MulDiv(rect.right, ::GetDeviceCaps(printDC, LOGPIXELSX), 1000);
  1643. rect.bottom = MulDiv(rect.bottom, ::GetDeviceCaps(printDC, LOGPIXELSY), 1000);
  1644. return IntRect(rect.left, rect.top, (rect.right - rect.left), rect.bottom - rect.top);
  1645. }
  1646. const Vector<WebCore::IntRect>& WebFrame::computePageRects(HDC printDC)
  1647. {
  1648. ASSERT(m_inPrintingMode);
  1649. Frame* coreFrame = core(this);
  1650. ASSERT(coreFrame);
  1651. ASSERT(coreFrame->document());
  1652. if (!printDC)
  1653. return m_pageRects;
  1654. // adjust the page rect by the header and footer
  1655. float headerHeight = 0, footerHeight = 0;
  1656. headerAndFooterHeights(&headerHeight, &footerHeight);
  1657. IntRect pageRect = printerRect(printDC);
  1658. IntRect marginRect = printerMarginRect(printDC);
  1659. IntRect adjustedRect = IntRect(
  1660. pageRect.x() + marginRect.x(),
  1661. pageRect.y() + marginRect.y(),
  1662. pageRect.width() - marginRect.x() - marginRect.maxX(),
  1663. pageRect.height() - marginRect.y() - marginRect.maxY());
  1664. computePageRectsForFrame(coreFrame, adjustedRect, headerHeight, footerHeight, 1.0,m_pageRects, m_pageHeight);
  1665. return m_pageRects;
  1666. }
  1667. HRESULT STDMETHODCALLTYPE WebFrame::getPrintedPageCount(
  1668. /* [in] */ HDC printDC,
  1669. /* [retval][out] */ UINT *pageCount)
  1670. {
  1671. if (!pageCount || !printDC) {
  1672. ASSERT_NOT_REACHED();
  1673. return E_POINTER;
  1674. }
  1675. *pageCount = 0;
  1676. if (!m_inPrintingMode) {
  1677. ASSERT_NOT_REACHED();
  1678. return E_FAIL;
  1679. }
  1680. Frame* coreFrame = core(this);
  1681. if (!coreFrame || !coreFrame->document())
  1682. return E_FAIL;
  1683. const Vector<IntRect>& pages = computePageRects(printDC);
  1684. *pageCount = (UINT) pages.size();
  1685. return S_OK;
  1686. }
  1687. #if USE(CG)
  1688. void WebFrame::drawHeader(PlatformGraphicsContext* pctx, IWebUIDelegate* ui, const IntRect& pageRect, float headerHeight)
  1689. {
  1690. int x = pageRect.x();
  1691. int y = 0;
  1692. RECT headerRect = {x, y, x+pageRect.width(), y+static_cast<int>(headerHeight)};
  1693. ui->drawHeaderInRect(d->webView, &headerRect, static_cast<OLE_HANDLE>(reinterpret_cast<LONG64>(pctx)));
  1694. }
  1695. void WebFrame::drawFooter(PlatformGraphicsContext* pctx, IWebUIDelegate* ui, const IntRect& pageRect, UINT page, UINT pageCount, float headerHeight, float footerHeight)
  1696. {
  1697. int x = pageRect.x();
  1698. int y = max((int)headerHeight+pageRect.height(), m_pageHeight-static_cast<int>(footerHeight));
  1699. RECT footerRect = {x, y, x+pageRect.width(), y+static_cast<int>(footerHeight)};
  1700. ui->drawFooterInRect(d->webView, &footerRect, static_cast<OLE_HANDLE>(reinterpret_cast<LONG64>(pctx)), page+1, pageCount);
  1701. }
  1702. void WebFrame::spoolPage(PlatformGraphicsContext* pctx, GraphicsContext* spoolCtx, HDC printDC, IWebUIDelegate* ui, float headerHeight, float footerHeight, UINT page, UINT pageCount)
  1703. {
  1704. Frame* coreFrame = core(this);
  1705. IntRect pageRect = m_pageRects[page];
  1706. CGContextSaveGState(pctx);
  1707. IntRect printRect = printerRect(printDC);
  1708. CGRect mediaBox = CGRectMake(CGFloat(0),
  1709. CGFloat(0),
  1710. CGFloat(printRect.width()),
  1711. CGFloat(printRect.height()));
  1712. CGContextBeginPage(pctx, &mediaBox);
  1713. CGFloat scale = static_cast<float>(mediaBox.size.width)/static_cast<float>(pageRect.width());
  1714. CGAffineTransform ctm = CGContextGetBaseCTM(pctx);
  1715. ctm = CGAffineTransformScale(ctm, -scale, -scale);
  1716. ctm = CGAffineTransformTranslate(ctm, CGFloat(-pageRect.x()), CGFloat(-pageRect.y()+headerHeight)); // reserves space for header
  1717. CGContextScaleCTM(pctx, scale, scale);
  1718. CGContextTranslateCTM(pctx, CGFloat(-pageRect.x()), CGFloat(-pageRect.y()+headerHeight)); // reserves space for header
  1719. CGContextSetBaseCTM(pctx, ctm);
  1720. coreFrame->view()->paintContents(spoolCtx, pageRect);
  1721. CGContextTranslateCTM(pctx, CGFloat(pageRect.x()), CGFloat(pageRect.y())-headerHeight);
  1722. if (headerHeight)
  1723. drawHeader(pctx, ui, pageRect, headerHeight);
  1724. if (footerHeight)
  1725. drawFooter(pctx, ui, pageRect, page, pageCount, headerHeight, footerHeight);
  1726. CGContextEndPage(pctx);
  1727. CGContextRestoreGState(pctx);
  1728. }
  1729. #elif USE(CAIRO)
  1730. static float scaleFactor(HDC printDC, const IntRect& marginRect, const IntRect& pageRect)
  1731. {
  1732. const IntRect& printRect = printerRect(printDC);
  1733. IntRect adjustedRect = IntRect(
  1734. printRect.x() + marginRect.x(),
  1735. printRect.y() + marginRect.y(),
  1736. printRect.width() - marginRect.x() - marginRect.maxX(),
  1737. printRect.height() - marginRect.y() - marginRect.maxY());
  1738. float scale = static_cast<float>(adjustedRect.width()) / static_cast<float>(pageRect.width());
  1739. if (!scale)
  1740. scale = 1.0;
  1741. return scale;
  1742. }
  1743. static HDC hdcFromContext(PlatformGraphicsContext* pctx)
  1744. {
  1745. return cairo_win32_surface_get_dc(cairo_get_target(pctx->cr()));
  1746. }
  1747. void WebFrame::drawHeader(PlatformGraphicsContext* pctx, IWebUIDelegate* ui, const IntRect& pageRect, float headerHeight)
  1748. {
  1749. HDC hdc = hdcFromContext(pctx);
  1750. int x = pageRect.x();
  1751. int y = 0;
  1752. RECT headerRect = {x, y, x + pageRect.width(), y + static_cast<int>(headerHeight)};
  1753. ui->drawHeaderInRect(d->webView, &headerRect, static_cast<OLE_HANDLE>(reinterpret_cast<LONG64>(hdc)));
  1754. }
  1755. void WebFrame::drawFooter(PlatformGraphicsContext* pctx, IWebUIDelegate* ui, const IntRect& pageRect, UINT page, UINT pageCount, float headerHeight, float footerHeight)
  1756. {
  1757. HDC hdc = hdcFromContext(pctx);
  1758. int x = pageRect.x();
  1759. int y = max(static_cast<int>(headerHeight) + pageRect.height(), m_pageHeight -static_cast<int>(footerHeight));
  1760. RECT footerRect = {x, y, x + pageRect.width(), y + static_cast<int>(footerHeight)};
  1761. ui->drawFooterInRect(d->webView, &footerRect, static_cast<OLE_HANDLE>(reinterpret_cast<LONG64>(hdc)), page+1, pageCount);
  1762. }
  1763. static XFORM buildXFORMFromCairo(HDC targetDC, cairo_t* previewContext)
  1764. {
  1765. XFORM scaled;
  1766. GetWorldTransform(targetDC, &scaled);
  1767. cairo_matrix_t ctm;
  1768. cairo_get_matrix(previewContext, &ctm);
  1769. // Scale to the preview screen bounds
  1770. scaled.eM11 = ctm.xx;
  1771. scaled.eM22 = ctm.yy;
  1772. return scaled;
  1773. }
  1774. void WebFrame::spoolPage(PlatformGraphicsContext* pctx, GraphicsContext* spoolCtx, HDC printDC, IWebUIDelegate* ui, float headerHeight, float footerHeight, UINT page, UINT pageCount)
  1775. {
  1776. Frame* coreFrame = core(this);
  1777. const IntRect& pageRect = m_pageRects[page];
  1778. const IntRect& marginRect = printerMarginRect(printDC);
  1779. // In preview, the printDC is a placeholder, so just always use the HDC backing the graphics context.
  1780. HDC hdc = hdcFromContext(pctx);
  1781. spoolCtx->save();
  1782. XFORM original, scaled;
  1783. GetWorldTransform(hdc, &original);
  1784. cairo_t* cr = pctx->cr();
  1785. bool preview = (hdc != printDC);
  1786. if (preview) {
  1787. // If this is a preview, the Windows HDC was set to a non-scaled state so that Cairo will
  1788. // draw correctly. We need to retain the correct preview scale here for use when the Cairo
  1789. // drawing completes so that we can scale our GDI-based header/footer calls. This is a
  1790. // workaround for a bug in Cairo (see https://bugs.freedesktop.org/show_bug.cgi?id=28161)
  1791. scaled = buildXFORMFromCairo(hdc, cr);
  1792. }
  1793. float scale = scaleFactor(printDC, marginRect, pageRect);
  1794. IntRect cairoMarginRect(marginRect);
  1795. cairoMarginRect.scale(1 / scale);
  1796. // We cannot scale the display HDC because the print surface also scales fonts,
  1797. // resulting in invalid printing (and print preview)
  1798. cairo_scale(cr, scale, scale);
  1799. cairo_translate(cr, cairoMarginRect.x(), cairoMarginRect.y() + headerHeight);
  1800. // Modify Cairo (only) to account for page position.
  1801. cairo_translate(cr, -pageRect.x(), -pageRect.y());
  1802. coreFrame->view()->paintContents(spoolCtx, pageRect);
  1803. cairo_translate(cr, pageRect.x(), pageRect.y());
  1804. if (preview) {
  1805. // If this is a preview, the Windows HDC was set to a non-scaled state so that Cairo would
  1806. // draw correctly. We need to rescale the HDC to the correct preview scale so our GDI-based
  1807. // header/footer calls will draw properly. This is a workaround for a bug in Cairo.
  1808. // (see https://bugs.freedesktop.org/show_bug.cgi?id=28161)
  1809. SetWorldTransform(hdc, &scaled);
  1810. }
  1811. XFORM xform = TransformationMatrix().translate(marginRect.x(), marginRect.y()).scale(scale);
  1812. ModifyWorldTransform(hdc, &xform, MWT_LEFTMULTIPLY);
  1813. if (headerHeight)
  1814. drawHeader(pctx, ui, pageRect, headerHeight);
  1815. if (footerHeight)
  1816. drawFooter(pctx, ui, pageRect, page, pageCount, headerHeight, footerHeight);
  1817. SetWorldTransform(hdc, &original);
  1818. cairo_show_page(cr);
  1819. ASSERT(!cairo_status(cr));
  1820. spoolCtx->restore();
  1821. }
  1822. static void setCairoTransformToPreviewHDC(cairo_t* previewCtx, HDC previewDC)
  1823. {
  1824. XFORM passedCTM;
  1825. GetWorldTransform(previewDC, &passedCTM);
  1826. // Reset HDC WorldTransform to unscaled state. Scaling must be
  1827. // done in Cairo to avoid drawing errors.
  1828. XFORM unscaledCTM = passedCTM;
  1829. unscaledCTM.eM11 = 1.0;
  1830. unscaledCTM.eM22 = 1.0;
  1831. SetWorldTransform(previewDC, &unscaledCTM);
  1832. // Make the Cairo transform match the information passed to WebKit
  1833. // in the HDC's WorldTransform.
  1834. cairo_matrix_t ctm = { passedCTM.eM11, passedCTM.eM12, passedCTM.eM21,
  1835. passedCTM.eM22, passedCTM.eDx, passedCTM.eDy };
  1836. cairo_set_matrix(previewCtx, &ctm);
  1837. }
  1838. #endif
  1839. HRESULT STDMETHODCALLTYPE WebFrame::spoolPages(
  1840. /* [in] */ HDC printDC,
  1841. /* [in] */ UINT startPage,
  1842. /* [in] */ UINT endPage,
  1843. /* [retval][out] */ void* ctx)
  1844. {
  1845. #if USE(CG)
  1846. if (!printDC || !ctx) {
  1847. ASSERT_NOT_REACHED();
  1848. return E_POINTER;
  1849. }
  1850. #elif USE(CAIRO)
  1851. if (!printDC) {
  1852. ASSERT_NOT_REACHED();
  1853. return E_POINTER;
  1854. }
  1855. HDC targetDC = (ctx) ? (HDC)ctx : printDC;
  1856. cairo_surface_t* printSurface = 0;
  1857. if (ctx)
  1858. printSurface = cairo_win32_surface_create(targetDC); // in-memory
  1859. else
  1860. printSurface = cairo_win32_printing_surface_create(targetDC); // metafile
  1861. cairo_t* cr = cairo_create(printSurface);
  1862. if (!cr) {
  1863. cairo_surface_destroy(printSurface);
  1864. return E_FAIL;
  1865. }
  1866. PlatformContextCairo platformContext(cr);
  1867. PlatformGraphicsContext* pctx = &platformContext;
  1868. cairo_destroy(cr);
  1869. if (ctx) {
  1870. // If this is a preview, the Windows HDC was sent with scaling information.
  1871. // Retrieve it and reset it so that it draws properly. This is a workaround
  1872. // for a bug in Cairo (see https://bugs.freedesktop.org/show_bug.cgi?id=28161)
  1873. setCairoTransformToPreviewHDC(cr, targetDC);
  1874. }
  1875. cairo_surface_set_fallback_resolution(printSurface, 72.0, 72.0);
  1876. #endif
  1877. if (!m_inPrintingMode) {
  1878. ASSERT_NOT_REACHED();
  1879. return E_FAIL;
  1880. }
  1881. Frame* coreFrame = core(this);
  1882. if (!coreFrame || !coreFrame->document())
  1883. return E_FAIL;
  1884. UINT pageCount = (UINT) m_pageRects.size();
  1885. #if USE(CG)
  1886. PlatformGraphicsContext* pctx = (PlatformGraphicsContext*)ctx;
  1887. #endif
  1888. if (!pageCount || startPage > pageCount) {
  1889. ASSERT_NOT_REACHED();
  1890. return E_FAIL;
  1891. }
  1892. if (startPage > 0)
  1893. startPage--;
  1894. if (endPage == 0)
  1895. endPage = pageCount;
  1896. COMPtr<IWebUIDelegate> ui;
  1897. if (FAILED(d->webView->uiDelegate(&ui)))
  1898. return E_FAIL;
  1899. float headerHeight = 0, footerHeight = 0;
  1900. headerAndFooterHeights(&headerHeight, &footerHeight);
  1901. GraphicsContext spoolCtx(pctx);
  1902. spoolCtx.setShouldIncludeChildWindows(true);
  1903. for (UINT ii = startPage; ii < endPage; ii++)
  1904. spoolPage(pctx, &spoolCtx, printDC, ui.get(), headerHeight, footerHeight, ii, pageCount);
  1905. #if USE(CAIRO)
  1906. cairo_surface_finish(printSurface);
  1907. ASSERT(!cairo_surface_status(printSurface));
  1908. cairo_surface_destroy(printSurface);
  1909. #endif
  1910. return S_OK;
  1911. }
  1912. HRESULT STDMETHODCALLTYPE WebFrame::isFrameSet(
  1913. /* [retval][out] */ BOOL* result)
  1914. {
  1915. *result = FALSE;
  1916. Frame* coreFrame = core(this);
  1917. if (!coreFrame || !coreFrame->document())
  1918. return E_FAIL;
  1919. *result = coreFrame->document()->isFrameSet() ? TRUE : FALSE;
  1920. return S_OK;
  1921. }
  1922. HRESULT STDMETHODCALLTYPE WebFrame::string(
  1923. /* [retval][out] */ BSTR *result)
  1924. {
  1925. *result = 0;
  1926. Frame* coreFrame = core(this);
  1927. if (!coreFrame)
  1928. return E_FAIL;
  1929. RefPtr<Range> allRange(rangeOfContents(coreFrame->document()));
  1930. String allString = plainText(allRange.get());
  1931. *result = BString(allString).release();
  1932. return S_OK;
  1933. }
  1934. HRESULT STDMETHODCALLTYPE WebFrame::size(
  1935. /* [retval][out] */ SIZE *size)
  1936. {
  1937. if (!size)
  1938. return E_POINTER;
  1939. size->cx = size->cy = 0;
  1940. Frame* coreFrame = core(this);
  1941. if (!coreFrame)
  1942. return E_FAIL;
  1943. FrameView* view = coreFrame->view();
  1944. if (!view)
  1945. return E_FAIL;
  1946. size->cx = view->width();
  1947. size->cy = view->height();
  1948. return S_OK;
  1949. }
  1950. HRESULT STDMETHODCALLTYPE WebFrame::hasScrollBars(
  1951. /* [retval][out] */ BOOL *result)
  1952. {
  1953. if (!result)
  1954. return E_POINTER;
  1955. *result = FALSE;
  1956. Frame* coreFrame = core(this);
  1957. if (!coreFrame)
  1958. return E_FAIL;
  1959. FrameView* view = coreFrame->view();
  1960. if (!view)
  1961. return E_FAIL;
  1962. if (view->horizontalScrollbar() || view->verticalScrollbar())
  1963. *result = TRUE;
  1964. return S_OK;
  1965. }
  1966. HRESULT STDMETHODCALLTYPE WebFrame::contentBounds(
  1967. /* [retval][out] */ RECT *result)
  1968. {
  1969. if (!result)
  1970. return E_POINTER;
  1971. ::SetRectEmpty(result);
  1972. Frame* coreFrame = core(this);
  1973. if (!coreFrame)
  1974. return E_FAIL;
  1975. FrameView* view = coreFrame->view();
  1976. if (!view)
  1977. return E_FAIL;
  1978. result->bottom = view->contentsHeight();
  1979. result->right = view->contentsWidth();
  1980. return S_OK;
  1981. }
  1982. HRESULT STDMETHODCALLTYPE WebFrame::frameBounds(
  1983. /* [retval][out] */ RECT *result)
  1984. {
  1985. if (!result)
  1986. return E_POINTER;
  1987. ::SetRectEmpty(result);
  1988. Frame* coreFrame = core(this);
  1989. if (!coreFrame)
  1990. return E_FAIL;
  1991. FrameView* view = coreFrame->view();
  1992. if (!view)
  1993. return E_FAIL;
  1994. FloatRect bounds = view->visibleContentRect(ScrollableArea::IncludeScrollbars);
  1995. result->bottom = (LONG) bounds.height();
  1996. result->right = (LONG) bounds.width();
  1997. return S_OK;
  1998. }
  1999. HRESULT STDMETHODCALLTYPE WebFrame::isDescendantOfFrame(
  2000. /* [in] */ IWebFrame *ancestor,
  2001. /* [retval][out] */ BOOL *result)
  2002. {
  2003. if (!result)
  2004. return E_POINTER;
  2005. *result = FALSE;
  2006. Frame* coreFrame = core(this);
  2007. COMPtr<WebFrame> ancestorWebFrame(Query, ancestor);
  2008. if (!ancestorWebFrame)
  2009. return S_OK;
  2010. *result = (coreFrame && coreFrame->tree()->isDescendantOf(core(ancestorWebFrame.get()))) ? TRUE : FALSE;
  2011. return S_OK;
  2012. }
  2013. HRESULT WebFrame::stringByEvaluatingJavaScriptInScriptWorld(IWebScriptWorld* iWorld, JSObjectRef globalObjectRef, BSTR script, BSTR* evaluationResult)
  2014. {
  2015. if (!evaluationResult)
  2016. return E_POINTER;
  2017. *evaluationResult = 0;
  2018. if (!iWorld)
  2019. return E_POINTER;
  2020. COMPtr<WebScriptWorld> world(Query, iWorld);
  2021. if (!world)
  2022. return E_INVALIDARG;
  2023. Frame* coreFrame = core(this);
  2024. String string = String(script, SysStringLen(script));
  2025. // Start off with some guess at a frame and a global object, we'll try to do better...!
  2026. JSDOMWindow* anyWorldGlobalObject = coreFrame->script()->globalObject(mainThreadNormalWorld());
  2027. // The global object is probably a shell object? - if so, we know how to use this!
  2028. JSC::JSObject* globalObjectObj = toJS(globalObjectRef);
  2029. if (!strcmp(globalObjectObj->classInfo()->className, "JSDOMWindowShell"))
  2030. anyWorldGlobalObject = static_cast<JSDOMWindowShell*>(globalObjectObj)->window();
  2031. // Get the frame frome the global object we've settled on.
  2032. Frame* frame = anyWorldGlobalObject->impl()->frame();
  2033. ASSERT(frame->document());
  2034. JSValue result = frame->script()->executeScriptInWorld(world->world(), string, true).jsValue();
  2035. if (!frame) // In case the script removed our frame from the page.
  2036. return S_OK;
  2037. // This bizarre set of rules matches behavior from WebKit for Safari 2.0.
  2038. // If you don't like it, use -[WebScriptObject evaluateWebScript:] or
  2039. // JSEvaluateScript instead, since they have less surprising semantics.
  2040. if (!result || !result.isBoolean() && !result.isString() && !result.isNumber())
  2041. return S_OK;
  2042. JSC::ExecState* exec = anyWorldGlobalObject->globalExec();
  2043. JSC::JSLockHolder lock(exec);
  2044. String resultString = result.toWTFString(exec);
  2045. *evaluationResult = BString(resultString).release();
  2046. return S_OK;
  2047. }
  2048. void WebFrame::unmarkAllMisspellings()
  2049. {
  2050. Frame* coreFrame = core(this);
  2051. for (Frame* frame = coreFrame; frame; frame = frame->tree()->traverseNext(coreFrame)) {
  2052. Document *doc = frame->document();
  2053. if (!doc)
  2054. return;
  2055. doc->markers()->removeMarkers(DocumentMarker::Spelling);
  2056. }
  2057. }
  2058. void WebFrame::unmarkAllBadGrammar()
  2059. {
  2060. Frame* coreFrame = core(this);
  2061. for (Frame* frame = coreFrame; frame; frame = frame->tree()->traverseNext(coreFrame)) {
  2062. Document *doc = frame->document();
  2063. if (!doc)
  2064. return;
  2065. doc->markers()->removeMarkers(DocumentMarker::Grammar);
  2066. }
  2067. }
  2068. WebView* WebFrame::webView() const
  2069. {
  2070. return d->webView;
  2071. }
  2072. void WebFrame::setWebView(WebView* webView)
  2073. {
  2074. d->webView = webView;
  2075. }
  2076. COMPtr<IAccessible> WebFrame::accessible() const
  2077. {
  2078. Frame* coreFrame = core(this);
  2079. ASSERT(coreFrame);
  2080. Document* currentDocument = coreFrame->document();
  2081. if (!currentDocument)
  2082. m_accessible = 0;
  2083. else if (!m_accessible || m_accessible->document() != currentDocument) {
  2084. // Either we've never had a wrapper for this frame's top-level Document,
  2085. // the Document renderer was destroyed and its wrapper was detached, or
  2086. // the previous Document is in the page cache, and the current document
  2087. // needs to be wrapped.
  2088. m_accessible = new AccessibleDocument(currentDocument, webView()->viewWindow());
  2089. }
  2090. return m_accessible.get();
  2091. }
  2092. void WebFrame::updateBackground()
  2093. {
  2094. Color backgroundColor = webView()->transparent() ? Color::transparent : Color::white;
  2095. Frame* coreFrame = core(this);
  2096. if (!coreFrame || !coreFrame->view())
  2097. return;
  2098. coreFrame->view()->updateBackgroundRecursively(backgroundColor, webView()->transparent());
  2099. }
  2100. PassRefPtr<FrameNetworkingContext> WebFrame::createNetworkingContext()
  2101. {
  2102. return WebFrameNetworkingContext::create(core(this), userAgent(url()));
  2103. }