Adopt is/toHTMLInputElement for code cleanup
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / WebFrame.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebFrame.h"
28
29 #include "DownloadManager.h"
30 #include "InjectedBundleHitTestResult.h"
31 #include "InjectedBundleNodeHandle.h"
32 #include "InjectedBundleRangeHandle.h"
33 #include "InjectedBundleScriptWorld.h"
34 #include "PluginView.h"
35 #include "WKAPICast.h"
36 #include "WKBundleAPICast.h"
37 #include "WebChromeClient.h"
38 #include "WebPage.h"
39 #include "WebPageProxyMessages.h"
40 #include "WebProcess.h"
41 #include <JavaScriptCore/APICast.h>
42 #include <JavaScriptCore/JSContextRef.h>
43 #include <JavaScriptCore/JSLock.h>
44 #include <JavaScriptCore/JSValueRef.h>
45 #include <WebCore/ArchiveResource.h>
46 #include <WebCore/Chrome.h>
47 #include <WebCore/DocumentLoader.h>
48 #include <WebCore/EventHandler.h>
49 #include <WebCore/Frame.h>
50 #include <WebCore/FrameView.h>
51 #include <WebCore/HTMLFormElement.h>
52 #include <WebCore/HTMLFrameOwnerElement.h>
53 #include <WebCore/HTMLInputElement.h>
54 #include <WebCore/HTMLNames.h>
55 #include <WebCore/JSCSSStyleDeclaration.h>
56 #include <WebCore/JSElement.h>
57 #include <WebCore/JSRange.h>
58 #include <WebCore/NetworkingContext.h>
59 #include <WebCore/NodeTraversal.h>
60 #include <WebCore/Page.h>
61 #include <WebCore/PluginDocument.h>
62 #include <WebCore/RenderTreeAsText.h>
63 #include <WebCore/ResourceBuffer.h>
64 #include <WebCore/ResourceLoader.h>
65 #include <WebCore/ScriptController.h>
66 #include <WebCore/SecurityOrigin.h>
67 #include <WebCore/TextIterator.h>
68 #include <WebCore/TextResourceDecoder.h>
69 #include <wtf/text/StringBuilder.h>
70
71 #if PLATFORM(MAC)
72 #include <WebCore/LegacyWebArchive.h>
73 #endif
74
75 #if ENABLE(NETWORK_PROCESS)
76 #include "NetworkConnectionToWebProcessMessages.h"
77 #include "NetworkProcessConnection.h"
78 #include "WebCoreArgumentCoders.h"
79 #endif
80
81 #ifndef NDEBUG
82 #include <wtf/RefCountedLeakCounter.h>
83 #endif
84
85 using namespace JSC;
86 using namespace WebCore;
87
88 namespace WebKit {
89
90 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, webFrameCounter, ("WebFrame"));
91
92 static uint64_t generateFrameID()
93 {
94     static uint64_t uniqueFrameID = 1;
95     return uniqueFrameID++;
96 }
97
98 static uint64_t generateListenerID()
99 {
100     static uint64_t uniqueListenerID = 1;
101     return uniqueListenerID++;
102 }
103
104 PassRefPtr<WebFrame> WebFrame::createMainFrame(WebPage* page)
105 {
106     RefPtr<WebFrame> frame = create();
107
108     page->send(Messages::WebPageProxy::DidCreateMainFrame(frame->frameID()));
109
110     frame->init(page, String(), 0);
111
112     return frame.release();
113 }
114
115 PassRefPtr<WebFrame> WebFrame::createSubframe(WebPage* page, const String& frameName, HTMLFrameOwnerElement* ownerElement)
116 {
117     RefPtr<WebFrame> frame = create();
118
119     page->send(Messages::WebPageProxy::DidCreateSubframe(frame->frameID()));
120
121     frame->init(page, frameName, ownerElement);
122
123     return frame.release();
124 }
125
126 PassRefPtr<WebFrame> WebFrame::create()
127 {
128     RefPtr<WebFrame> frame = adoptRef(new WebFrame);
129
130     // Add explict ref() that will be balanced in WebFrameLoaderClient::frameLoaderDestroyed().
131     frame->ref();
132
133     return frame.release();
134 }
135
136 WebFrame::WebFrame()
137     : m_coreFrame(0)
138     , m_policyListenerID(0)
139     , m_policyFunction(0)
140     , m_policyDownloadID(0)
141     , m_frameLoaderClient(this)
142     , m_loadListener(0)
143     , m_frameID(generateFrameID())
144 {
145     WebProcess::shared().addWebFrame(m_frameID, this);
146
147 #ifndef NDEBUG
148     webFrameCounter.increment();
149 #endif
150 }
151
152 WebFrame::~WebFrame()
153 {
154     ASSERT(!m_coreFrame);
155
156 #ifndef NDEBUG
157     webFrameCounter.decrement();
158 #endif
159 }
160
161 void WebFrame::init(WebPage* page, const String& frameName, HTMLFrameOwnerElement* ownerElement)
162 {
163     RefPtr<Frame> frame = Frame::create(page->corePage(), ownerElement, &m_frameLoaderClient);
164     m_coreFrame = frame.get();
165
166     frame->tree()->setName(frameName);
167
168     if (ownerElement) {
169         ASSERT(ownerElement->document()->frame());
170         ownerElement->document()->frame()->tree()->appendChild(frame);
171     }
172
173     frame->init();
174 }
175
176 WebPage* WebFrame::page() const
177
178     if (!m_coreFrame)
179         return 0;
180     
181     if (Page* page = m_coreFrame->page())
182         return WebPage::fromCorePage(page);
183
184     return 0;
185 }
186
187 void WebFrame::invalidate()
188 {
189     WebProcess::shared().removeWebFrame(m_frameID);
190     m_coreFrame = 0;
191 }
192
193 uint64_t WebFrame::setUpPolicyListener(WebCore::FramePolicyFunction policyFunction)
194 {
195     // FIXME: <rdar://5634381> We need to support multiple active policy listeners.
196
197     invalidatePolicyListener();
198
199     m_policyListenerID = generateListenerID();
200     m_policyFunction = policyFunction;
201     return m_policyListenerID;
202 }
203
204 void WebFrame::invalidatePolicyListener()
205 {
206     if (!m_policyListenerID)
207         return;
208
209     m_policyDownloadID = 0;
210     m_policyListenerID = 0;
211     m_policyFunction = 0;
212 }
213
214 void WebFrame::didReceivePolicyDecision(uint64_t listenerID, PolicyAction action, uint64_t downloadID)
215 {
216     if (!m_coreFrame)
217         return;
218
219     if (!m_policyListenerID)
220         return;
221
222     if (listenerID != m_policyListenerID)
223         return;
224
225     ASSERT(m_policyFunction);
226
227     FramePolicyFunction function = m_policyFunction;
228
229     invalidatePolicyListener();
230
231     m_policyDownloadID = downloadID;
232
233     (m_coreFrame->loader()->policyChecker()->*function)(action);
234 }
235
236 void WebFrame::startDownload(const WebCore::ResourceRequest& request)
237 {
238     ASSERT(m_policyDownloadID);
239
240     uint64_t policyDownloadID = m_policyDownloadID;
241     m_policyDownloadID = 0;
242
243 #if ENABLE(NETWORK_PROCESS)
244     if (WebProcess::shared().usesNetworkProcess()) {
245         bool privateBrowsingEnabled = m_coreFrame->loader()->networkingContext()->storageSession().isPrivateBrowsingSession();
246         WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::StartDownload(privateBrowsingEnabled, policyDownloadID, request), 0);
247         return;
248     }
249 #endif
250
251     WebProcess::shared().downloadManager().startDownload(policyDownloadID, request);
252 }
253
254 void WebFrame::convertMainResourceLoadToDownload(DocumentLoader* documentLoader, const ResourceRequest& request, const ResourceResponse& response)
255 {
256     ASSERT(m_policyDownloadID);
257
258     uint64_t policyDownloadID = m_policyDownloadID;
259     m_policyDownloadID = 0;
260
261     ResourceLoader* mainResourceLoader = documentLoader->mainResourceLoader();
262
263 #if ENABLE(NETWORK_PROCESS)
264     if (WebProcess::shared().usesNetworkProcess()) {
265         // Use 0 to indicate that there is no main resource loader.
266         // This can happen if the main resource is in the WebCore memory cache.
267         uint64_t mainResourceLoadIdentifier;
268         if (mainResourceLoader)
269             mainResourceLoadIdentifier = mainResourceLoader->identifier();
270         else
271             mainResourceLoadIdentifier = 0;
272
273         WebProcess::shared().networkConnection()->connection()->send(Messages::NetworkConnectionToWebProcess::ConvertMainResourceLoadToDownload(mainResourceLoadIdentifier, policyDownloadID, request, response), 0);
274         return;
275     }
276 #endif
277
278     if (!mainResourceLoader) {
279         // The main resource has already been loaded. Start a new download instead.
280         WebProcess::shared().downloadManager().startDownload(policyDownloadID, request);
281         return;
282     }
283
284     WebProcess::shared().downloadManager().convertHandleToDownload(policyDownloadID, documentLoader->mainResourceLoader()->handle(), request, response);
285 }
286
287 String WebFrame::source() const 
288 {
289     if (!m_coreFrame)
290         return String();
291     Document* document = m_coreFrame->document();
292     if (!document)
293         return String();
294     TextResourceDecoder* decoder = document->decoder();
295     if (!decoder)
296         return String();
297     DocumentLoader* documentLoader = m_coreFrame->loader()->activeDocumentLoader();
298     if (!documentLoader)
299         return String();
300     RefPtr<ResourceBuffer> mainResourceData = documentLoader->mainResourceData();
301     if (!mainResourceData)
302         return String();
303     return decoder->encoding().decode(mainResourceData->data(), mainResourceData->size());
304 }
305
306 String WebFrame::contentsAsString() const 
307 {
308     if (!m_coreFrame)
309         return String();
310
311     if (isFrameSet()) {
312         StringBuilder builder;
313         for (Frame* child = m_coreFrame->tree()->firstChild(); child; child = child->tree()->nextSibling()) {
314             if (!builder.isEmpty())
315                 builder.append(' ');
316
317             WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(child->loader()->client());
318             WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
319             ASSERT(webFrame);
320
321             builder.append(webFrame->contentsAsString());
322         }
323         // FIXME: It may make sense to use toStringPreserveCapacity() here.
324         return builder.toString();
325     }
326
327     Document* document = m_coreFrame->document();
328     if (!document)
329         return String();
330
331     RefPtr<Element> documentElement = document->documentElement();
332     if (!documentElement)
333         return String();
334
335     RefPtr<Range> range = document->createRange();
336
337     ExceptionCode ec = 0;
338     range->selectNode(documentElement.get(), ec);
339     if (ec)
340         return String();
341
342     return plainText(range.get());
343 }
344
345 String WebFrame::selectionAsString() const 
346 {
347     if (!m_coreFrame)
348         return String();
349
350     return m_coreFrame->displayStringModifiedByEncoding(m_coreFrame->editor().selectedText());
351 }
352
353 IntSize WebFrame::size() const
354 {
355     if (!m_coreFrame)
356         return IntSize();
357
358     FrameView* frameView = m_coreFrame->view();
359     if (!frameView)
360         return IntSize();
361
362     return frameView->contentsSize();
363 }
364
365 bool WebFrame::isFrameSet() const
366 {
367     if (!m_coreFrame)
368         return false;
369
370     Document* document = m_coreFrame->document();
371     if (!document)
372         return false;
373     return document->isFrameSet();
374 }
375
376 bool WebFrame::isMainFrame() const
377 {
378     if (WebPage* p = page())
379         return p->mainWebFrame() == this;
380
381     return false;
382 }
383
384 String WebFrame::name() const
385 {
386     if (!m_coreFrame)
387         return String();
388
389     return m_coreFrame->tree()->uniqueName();
390 }
391
392 String WebFrame::url() const
393 {
394     if (!m_coreFrame)
395         return String();
396
397     DocumentLoader* documentLoader = m_coreFrame->loader()->documentLoader();
398     if (!documentLoader)
399         return String();
400
401     return documentLoader->url().string();
402 }
403
404 String WebFrame::innerText() const
405 {
406     if (!m_coreFrame)
407         return String();
408
409     if (!m_coreFrame->document()->documentElement())
410         return String();
411
412     return m_coreFrame->document()->documentElement()->innerText();
413 }
414
415 WebFrame* WebFrame::parentFrame() const
416 {
417     if (!m_coreFrame || !m_coreFrame->ownerElement() || !m_coreFrame->ownerElement()->document())
418         return 0;
419
420     WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(m_coreFrame->ownerElement()->document()->frame()->loader()->client());
421     return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
422 }
423
424 PassRefPtr<ImmutableArray> WebFrame::childFrames()
425 {
426     if (!m_coreFrame)
427         return ImmutableArray::create();
428
429     size_t size = m_coreFrame->tree()->childCount();
430     if (!size)
431         return ImmutableArray::create();
432
433     Vector<RefPtr<APIObject>> vector;
434     vector.reserveInitialCapacity(size);
435
436     for (Frame* child = m_coreFrame->tree()->firstChild(); child; child = child->tree()->nextSibling()) {
437         WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(child->loader()->client());
438         WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
439         ASSERT(webFrame);
440         vector.uncheckedAppend(webFrame);
441     }
442
443     return ImmutableArray::adopt(vector);
444 }
445
446 String WebFrame::layerTreeAsText() const
447 {
448     if (!m_coreFrame)
449         return "";
450
451     return m_coreFrame->layerTreeAsText(0);
452 }
453
454 unsigned WebFrame::pendingUnloadCount() const
455 {
456     if (!m_coreFrame)
457         return 0;
458
459     return m_coreFrame->document()->domWindow()->pendingUnloadEventListeners();
460 }
461
462 bool WebFrame::allowsFollowingLink(const WebCore::KURL& url) const
463 {
464     if (!m_coreFrame)
465         return true;
466         
467     return m_coreFrame->document()->securityOrigin()->canDisplay(url);
468 }
469
470 JSGlobalContextRef WebFrame::jsContext()
471 {
472     return toGlobalRef(m_coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec());
473 }
474
475 JSGlobalContextRef WebFrame::jsContextForWorld(InjectedBundleScriptWorld* world)
476 {
477     return toGlobalRef(m_coreFrame->script()->globalObject(world->coreWorld())->globalExec());
478 }
479
480 bool WebFrame::handlesPageScaleGesture() const
481 {
482     if (!m_coreFrame->document()->isPluginDocument())
483         return 0;
484
485     PluginDocument* pluginDocument = static_cast<PluginDocument*>(m_coreFrame->document());
486     PluginView* pluginView = static_cast<PluginView*>(pluginDocument->pluginWidget());
487
488     return pluginView->handlesPageScaleFactor();
489 }
490
491 IntRect WebFrame::contentBounds() const
492 {    
493     if (!m_coreFrame)
494         return IntRect();
495     
496     FrameView* view = m_coreFrame->view();
497     if (!view)
498         return IntRect();
499     
500     return IntRect(0, 0, view->contentsWidth(), view->contentsHeight());
501 }
502
503 IntRect WebFrame::visibleContentBounds() const
504 {
505     if (!m_coreFrame)
506         return IntRect();
507     
508     FrameView* view = m_coreFrame->view();
509     if (!view)
510         return IntRect();
511     
512     IntRect contentRect = view->visibleContentRect(ScrollableArea::IncludeScrollbars);
513     return IntRect(0, 0, contentRect.width(), contentRect.height());
514 }
515
516 IntRect WebFrame::visibleContentBoundsExcludingScrollbars() const
517 {
518     if (!m_coreFrame)
519         return IntRect();
520     
521     FrameView* view = m_coreFrame->view();
522     if (!view)
523         return IntRect();
524     
525     IntRect contentRect = view->visibleContentRect();
526     return IntRect(0, 0, contentRect.width(), contentRect.height());
527 }
528
529 IntSize WebFrame::scrollOffset() const
530 {
531     if (!m_coreFrame)
532         return IntSize();
533     
534     FrameView* view = m_coreFrame->view();
535     if (!view)
536         return IntSize();
537
538     return view->scrollOffset();
539 }
540
541 bool WebFrame::hasHorizontalScrollbar() const
542 {
543     if (!m_coreFrame)
544         return false;
545
546     FrameView* view = m_coreFrame->view();
547     if (!view)
548         return false;
549
550     return view->horizontalScrollbar();
551 }
552
553 bool WebFrame::hasVerticalScrollbar() const
554 {
555     if (!m_coreFrame)
556         return false;
557
558     FrameView* view = m_coreFrame->view();
559     if (!view)
560         return false;
561
562     return view->verticalScrollbar();
563 }
564
565 PassRefPtr<InjectedBundleHitTestResult> WebFrame::hitTest(const IntPoint point) const
566 {
567     if (!m_coreFrame)
568         return 0;
569
570     return InjectedBundleHitTestResult::create(m_coreFrame->eventHandler()->hitTestResultAtPoint(point, HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowShadowContent));
571 }
572
573 bool WebFrame::getDocumentBackgroundColor(double* red, double* green, double* blue, double* alpha)
574 {
575     if (!m_coreFrame)
576         return false;
577
578     FrameView* view = m_coreFrame->view();
579     if (!view)
580         return false;
581
582     Color bgColor = view->documentBackgroundColor();
583     if (!bgColor.isValid())
584         return false;
585
586     bgColor.getRGBA(*red, *green, *blue, *alpha);
587     return true;
588 }
589
590 bool WebFrame::containsAnyFormElements() const
591 {
592     if (!m_coreFrame)
593         return false;
594     
595     Document* document = m_coreFrame->document();
596     if (!document)
597         return false;
598
599     for (Node* node = document->documentElement(); node; node = NodeTraversal::next(node)) {
600         if (!node->isElementNode())
601             continue;
602         if (isHTMLFormElement(node))
603             return true;
604     }
605     return false;
606 }
607
608 bool WebFrame::containsAnyFormControls() const
609 {
610     if (!m_coreFrame)
611         return false;
612     
613     Document* document = m_coreFrame->document();
614     if (!document)
615         return false;
616
617     for (Node* node = document->documentElement(); node; node = NodeTraversal::next(node)) {
618         if (!node->isElementNode())
619             continue;
620         if (isHTMLInputElement(node) || toElement(node)->hasTagName(HTMLNames::selectTag) || toElement(node)->hasTagName(HTMLNames::textareaTag))
621             return true;
622     }
623     return false;
624 }
625
626 void WebFrame::stopLoading()
627 {
628     if (!m_coreFrame)
629         return;
630
631     m_coreFrame->loader()->stopForUserCancel();
632 }
633
634 WebFrame* WebFrame::frameForContext(JSContextRef context)
635 {
636     JSObjectRef globalObjectRef = JSContextGetGlobalObject(context);
637     JSC::JSObject* globalObjectObj = toJS(globalObjectRef);
638     if (strcmp(globalObjectObj->classInfo()->className, "JSDOMWindowShell") != 0)
639         return 0;
640
641     Frame* coreFrame = static_cast<JSDOMWindowShell*>(globalObjectObj)->window()->impl()->frame();
642
643     WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(coreFrame->loader()->client());
644     return webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
645 }
646
647 JSValueRef WebFrame::jsWrapperForWorld(InjectedBundleNodeHandle* nodeHandle, InjectedBundleScriptWorld* world)
648 {
649     if (!m_coreFrame)
650         return 0;
651
652     JSDOMWindow* globalObject = m_coreFrame->script()->globalObject(world->coreWorld());
653     ExecState* exec = globalObject->globalExec();
654
655     JSLockHolder lock(exec);
656     return toRef(exec, toJS(exec, globalObject, nodeHandle->coreNode()));
657 }
658
659 JSValueRef WebFrame::jsWrapperForWorld(InjectedBundleRangeHandle* rangeHandle, InjectedBundleScriptWorld* world)
660 {
661     if (!m_coreFrame)
662         return 0;
663
664     JSDOMWindow* globalObject = m_coreFrame->script()->globalObject(world->coreWorld());
665     ExecState* exec = globalObject->globalExec();
666
667     JSLockHolder lock(exec);
668     return toRef(exec, toJS(exec, globalObject, rangeHandle->coreRange()));
669 }
670
671 String WebFrame::counterValue(JSObjectRef element)
672 {
673     if (!toJS(element)->inherits(&JSElement::s_info))
674         return String();
675
676     return counterValueForElement(static_cast<JSElement*>(toJS(element))->impl());
677 }
678
679 String WebFrame::provisionalURL() const
680 {
681     if (!m_coreFrame)
682         return String();
683
684     return m_coreFrame->loader()->provisionalDocumentLoader()->url().string();
685 }
686
687 String WebFrame::suggestedFilenameForResourceWithURL(const KURL& url) const
688 {
689     if (!m_coreFrame)
690         return String();
691
692     DocumentLoader* loader = m_coreFrame->loader()->documentLoader();
693     if (!loader)
694         return String();
695
696     // First, try the main resource.
697     if (loader->url() == url)
698         return loader->response().suggestedFilename();
699
700     // Next, try subresources.
701     RefPtr<ArchiveResource> resource = loader->subresource(url);
702     if (resource)
703         return resource->response().suggestedFilename();
704
705     return page()->cachedSuggestedFilenameForURL(url);
706 }
707
708 String WebFrame::mimeTypeForResourceWithURL(const KURL& url) const
709 {
710     if (!m_coreFrame)
711         return String();
712
713     DocumentLoader* loader = m_coreFrame->loader()->documentLoader();
714     if (!loader)
715         return String();
716
717     // First, try the main resource.
718     if (loader->url() == url)
719         return loader->response().mimeType();
720
721     // Next, try subresources.
722     RefPtr<ArchiveResource> resource = loader->subresource(url);
723     if (resource)
724         return resource->mimeType();
725
726     return page()->cachedResponseMIMETypeForURL(url);
727 }
728
729 void WebFrame::setTextDirection(const String& direction)
730 {
731     if (!m_coreFrame)
732         return;
733
734     if (direction == "auto")
735         m_coreFrame->editor().setBaseWritingDirection(NaturalWritingDirection);
736     else if (direction == "ltr")
737         m_coreFrame->editor().setBaseWritingDirection(LeftToRightWritingDirection);
738     else if (direction == "rtl")
739         m_coreFrame->editor().setBaseWritingDirection(RightToLeftWritingDirection);
740 }
741
742 #if PLATFORM(MAC)
743
744 class WebFrameFilter : public FrameFilter {
745 public:
746     WebFrameFilter(WebFrame*, WebFrame::FrameFilterFunction, void* context);
747         
748 private:
749     virtual bool shouldIncludeSubframe(Frame*) const OVERRIDE;
750
751     WebFrame* m_topLevelWebFrame;
752     WebFrame::FrameFilterFunction m_callback;
753     void* m_context;
754 };
755
756 WebFrameFilter::WebFrameFilter(WebFrame* topLevelWebFrame, WebFrame::FrameFilterFunction callback, void* context)
757     : m_topLevelWebFrame(topLevelWebFrame)
758     , m_callback(callback)
759     , m_context(context)
760 {
761 }
762
763 bool WebFrameFilter::shouldIncludeSubframe(Frame* frame) const
764 {
765     if (!m_callback)
766         return true;
767
768     WebFrameLoaderClient* webFrameLoaderClient = toWebFrameLoaderClient(frame->loader()->client());
769     WebFrame* webFrame = webFrameLoaderClient ? webFrameLoaderClient->webFrame() : 0;
770     ASSERT(webFrame);
771
772     return m_callback(toAPI(m_topLevelWebFrame), toAPI(webFrame), m_context);
773 }
774
775 RetainPtr<CFDataRef> WebFrame::webArchiveData(FrameFilterFunction callback, void* context)
776 {
777     WebFrameFilter filter(this, callback, context);
778
779     if (RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(coreFrame()->document(), &filter))
780         return archive->rawDataRepresentation();
781     
782     return 0;
783 }
784 #endif
785     
786 } // namespace WebKit