2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // How ownership works
32 // -------------------
34 // Big oh represents a refcounted relationship: owner O--- ownee
36 // WebView (for the toplevel frame only)
39 // Page O------- Frame (m_mainFrame) O-------O FrameView
42 // FrameLoader O-------- WebFrame (via FrameLoaderClient)
44 // FrameLoader and Frame are formerly one object that was split apart because
45 // it got too big. They basically have the same lifetime, hence the double line.
47 // WebFrame is refcounted and has one ref on behalf of the FrameLoader/Frame.
48 // This is not a normal reference counted pointer because that would require
49 // changing WebKit code that we don't control. Instead, it is created with this
50 // ref initially and it is removed when the FrameLoader is getting destroyed.
52 // WebFrames are created in two places, first in WebViewImpl when the root
53 // frame is created, and second in WebFrame::CreateChildFrame when sub-frames
54 // are created. WebKit will hook up this object to the FrameLoader/Frame
55 // and the refcount will be correct.
57 // How frames are destroyed
58 // ------------------------
60 // The main frame is never destroyed and is re-used. The FrameLoader is re-used
61 // and a reference to the main frame is kept by the Page.
63 // When frame content is replaced, all subframes are destroyed. This happens
64 // in FrameLoader::detachFromParent for each subframe.
66 // Frame going away causes the FrameLoader to get deleted. In FrameLoader's
67 // destructor, it notifies its client with frameLoaderDestroyed. This calls
68 // WebFrame::Closing and then derefs the WebFrame and will cause it to be
69 // deleted (unless an external someone is also holding a reference).
72 #include "WebFrameImpl.h"
75 #include "ChromiumBridge.h"
76 #include "ClipboardUtilitiesChromium.h"
78 #include "DOMUtilitiesPrivate.h"
79 #include "DOMWindow.h"
81 #include "DocumentFragment.h" // Only needed for ReplaceSelectionCommand.h :(
82 #include "DocumentLoader.h"
83 #include "DocumentMarker.h"
85 #include "EventHandler.h"
86 #include "FormState.h"
87 #include "FrameLoadRequest.h"
88 #include "FrameLoader.h"
89 #include "FrameTree.h"
90 #include "FrameView.h"
91 #include "GraphicsContext.h"
92 #include "HTMLCollection.h"
93 #include "HTMLFormElement.h"
94 #include "HTMLFrameOwnerElement.h"
95 #include "HTMLHeadElement.h"
96 #include "HTMLInputElement.h"
97 #include "HTMLLinkElement.h"
98 #include "HTMLNames.h"
99 #include "HistoryItem.h"
100 #include "InspectorController.h"
102 #include "PlatformContextSkia.h"
103 #include "PluginDocument.h"
104 #include "PrintContext.h"
105 #include "RenderFrame.h"
106 #include "RenderTreeAsText.h"
107 #include "RenderView.h"
108 #include "RenderWidget.h"
109 #include "ReplaceSelectionCommand.h"
110 #include "ResourceHandle.h"
111 #include "ResourceRequest.h"
112 #include "ScriptController.h"
113 #include "ScriptSourceCode.h"
114 #include "ScriptValue.h"
115 #include "ScrollTypes.h"
116 #include "ScrollbarTheme.h"
117 #include "SelectionController.h"
118 #include "Settings.h"
119 #include "SkiaUtils.h"
120 #include "SubstituteData.h"
121 #include "TextAffinity.h"
122 #include "TextIterator.h"
123 #include "WebAnimationControllerImpl.h"
124 #include "WebConsoleMessage.h"
125 #include "WebDataSourceImpl.h"
126 #include "WebDocument.h"
127 #include "WebFindOptions.h"
128 #include "WebFormElement.h"
129 #include "WebFrameClient.h"
130 #include "WebHistoryItem.h"
131 #include "WebInputElement.h"
132 #include "WebPasswordAutocompleteListener.h"
133 #include "WebPluginContainerImpl.h"
134 #include "WebRange.h"
136 #include "WebScriptSource.h"
137 #include "WebSecurityOrigin.h"
139 #include "WebURLError.h"
140 #include "WebVector.h"
141 #include "WebViewImpl.h"
142 #include "XPathResult.h"
146 #include <wtf/CurrentTime.h>
150 #include "LocalCurrentGraphicsContext.h"
157 using namespace WebCore;
161 static int frameCount = 0;
163 // Key for a StatsCounter tracking how many WebFrames are active.
164 static const char* const webFrameActiveCount = "WebFrameActiveCount";
166 static const char* const osdType = "application/opensearchdescription+xml";
167 static const char* const osdRel = "search";
169 // Backend for contentAsPlainText, this is a recursive function that gets
170 // the text for the current frame and all of its subframes. It will append
171 // the text of each frame in turn to the |output| up to |maxChars| length.
173 // The |frame| must be non-null.
174 static void frameContentAsPlainText(size_t maxChars, Frame* frame,
175 Vector<UChar>* output)
177 Document* doc = frame->document();
184 // TextIterator iterates over the visual representation of the DOM. As such,
185 // it requires you to do a layout before using it (otherwise it'll crash).
186 if (frame->view()->needsLayout())
187 frame->view()->layout();
189 // Select the document body.
190 RefPtr<Range> range(doc->createRange());
191 ExceptionCode exception = 0;
192 range->selectNodeContents(doc->body(), exception);
195 // The text iterator will walk nodes giving us text. This is similar to
196 // the plainText() function in TextIterator.h, but we implement the maximum
197 // size and also copy the results directly into a wstring, avoiding the
198 // string conversion.
199 for (TextIterator it(range.get()); !it.atEnd(); it.advance()) {
200 const UChar* chars = it.characters();
203 // It appears from crash reports that an iterator can get into a state
204 // where the character count is nonempty but the character pointer is
205 // null. advance()ing it will then just add that many to the null
206 // pointer which won't be caught in a null check but will crash.
208 // A null pointer and 0 length is common for some nodes.
210 // IF YOU CATCH THIS IN A DEBUGGER please let brettw know. We don't
211 // currently understand the conditions for this to occur. Ideally, the
212 // iterators would never get into the condition so we should fix them
214 ASSERT_NOT_REACHED();
218 // Just got a null node, we can forge ahead!
222 std::min(static_cast<size_t>(it.length()), maxChars - output->size());
223 output->append(chars, toAppend);
224 if (output->size() >= maxChars)
225 return; // Filled up the buffer.
229 // The separator between frames when the frames are converted to plain text.
230 const UChar frameSeparator[] = { '\n', '\n' };
231 const size_t frameSeparatorLen = 2;
233 // Recursively walk the children.
234 FrameTree* frameTree = frame->tree();
235 for (Frame* curChild = frameTree->firstChild(); curChild; curChild = curChild->tree()->nextSibling()) {
236 // Make sure the frame separator won't fill up the buffer, and give up if
237 // it will. The danger is if the separator will make the buffer longer than
238 // maxChars. This will cause the computation above:
239 // maxChars - output->size()
240 // to be a negative number which will crash when the subframe is added.
241 if (output->size() >= maxChars - frameSeparatorLen)
244 output->append(frameSeparator, frameSeparatorLen);
245 frameContentAsPlainText(maxChars, curChild, output);
246 if (output->size() >= maxChars)
247 return; // Filled up the buffer.
251 // If the frame hosts a PluginDocument, this method returns the WebPluginContainerImpl
252 // that hosts the plugin.
253 static WebPluginContainerImpl* pluginContainerFromFrame(Frame* frame)
257 if (!frame->document() || !frame->document()->isPluginDocument())
259 PluginDocument* pluginDocument = static_cast<PluginDocument*>(frame->document());
260 return static_cast<WebPluginContainerImpl *>(pluginDocument->pluginWidget());
263 // Simple class to override some of PrintContext behavior. Some of the methods
264 // made virtual so that they can be overriden by ChromePluginPrintContext.
265 class ChromePrintContext : public PrintContext, public Noncopyable {
267 ChromePrintContext(Frame* frame)
268 : PrintContext(frame)
269 , m_printedPageWidth(0)
273 virtual void begin(float width)
275 ASSERT(!m_printedPageWidth);
276 m_printedPageWidth = width;
277 PrintContext::begin(m_printedPageWidth);
285 virtual float getPageShrink(int pageNumber) const
287 IntRect pageRect = m_pageRects[pageNumber];
288 return m_printedPageWidth / pageRect.width();
291 // Spools the printed page, a subrect of m_frame. Skip the scale step.
292 // NativeTheme doesn't play well with scaling. Scaling is done browser side
293 // instead. Returns the scale to be applied.
294 // On Linux, we don't have the problem with NativeTheme, hence we let WebKit
295 // do the scaling and ignore the return value.
296 virtual float spoolPage(GraphicsContext& ctx, int pageNumber)
298 IntRect pageRect = m_pageRects[pageNumber];
299 float scale = m_printedPageWidth / pageRect.width();
303 ctx.scale(WebCore::FloatSize(scale, scale));
305 ctx.translate(static_cast<float>(-pageRect.x()),
306 static_cast<float>(-pageRect.y()));
308 m_frame->view()->paintContents(&ctx, pageRect);
313 virtual void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight)
315 return PrintContext::computePageRects(printRect, headerHeight, footerHeight, userScaleFactor, outPageHeight);
318 virtual int pageCount() const
320 return PrintContext::pageCount();
323 virtual bool shouldUseBrowserOverlays() const
329 // Set when printing.
330 float m_printedPageWidth;
333 // Simple class to override some of PrintContext behavior. This is used when
334 // the frame hosts a plugin that supports custom printing. In this case, we
335 // want to delegate all printing related calls to the plugin.
336 class ChromePluginPrintContext : public ChromePrintContext {
338 ChromePluginPrintContext(Frame* frame, int printerDPI)
339 : ChromePrintContext(frame), m_pageCount(0), m_printerDPI(printerDPI)
341 // This HAS to be a frame hosting a full-mode plugin
342 ASSERT(frame->document()->isPluginDocument());
345 virtual void begin(float width)
351 WebPluginContainerImpl* pluginContainer = pluginContainerFromFrame(m_frame);
352 if (pluginContainer && pluginContainer->supportsPaginatedPrint())
353 pluginContainer->printEnd();
355 ASSERT_NOT_REACHED();
358 virtual float getPageShrink(int pageNumber) const
360 // We don't shrink the page (maybe we should ask the widget ??)
364 virtual void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight)
366 WebPluginContainerImpl* pluginContainer = pluginContainerFromFrame(m_frame);
367 if (pluginContainer && pluginContainer->supportsPaginatedPrint())
368 m_pageCount = pluginContainer->printBegin(IntRect(printRect), m_printerDPI);
370 ASSERT_NOT_REACHED();
373 virtual int pageCount() const
378 // Spools the printed page, a subrect of m_frame. Skip the scale step.
379 // NativeTheme doesn't play well with scaling. Scaling is done browser side
380 // instead. Returns the scale to be applied.
381 virtual float spoolPage(GraphicsContext& ctx, int pageNumber)
383 WebPluginContainerImpl* pluginContainer = pluginContainerFromFrame(m_frame);
384 if (pluginContainer && pluginContainer->supportsPaginatedPrint())
385 pluginContainer->printPage(pageNumber, &ctx);
387 ASSERT_NOT_REACHED();
391 virtual bool shouldUseBrowserOverlays() const
397 // Set when printing.
402 static WebDataSource* DataSourceForDocLoader(DocumentLoader* loader)
404 return loader ? WebDataSourceImpl::fromDocumentLoader(loader) : 0;
408 // WebFrame -------------------------------------------------------------------
410 class WebFrameImpl::DeferredScopeStringMatches {
412 DeferredScopeStringMatches(WebFrameImpl* webFrame,
414 const WebString& searchText,
415 const WebFindOptions& options,
417 : m_timer(this, &DeferredScopeStringMatches::doTimeout)
418 , m_webFrame(webFrame)
419 , m_identifier(identifier)
420 , m_searchText(searchText)
424 m_timer.startOneShot(0.0);
428 void doTimeout(Timer<DeferredScopeStringMatches>*)
430 m_webFrame->callScopeStringMatches(
431 this, m_identifier, m_searchText, m_options, m_reset);
434 Timer<DeferredScopeStringMatches> m_timer;
435 RefPtr<WebFrameImpl> m_webFrame;
437 WebString m_searchText;
438 WebFindOptions m_options;
443 // WebFrame -------------------------------------------------------------------
445 int WebFrame::instanceCount()
450 WebFrame* WebFrame::frameForEnteredContext()
453 ScriptController::retrieveFrameForEnteredContext();
454 return WebFrameImpl::fromFrame(frame);
457 WebFrame* WebFrame::frameForCurrentContext()
460 ScriptController::retrieveFrameForCurrentContext();
461 return WebFrameImpl::fromFrame(frame);
464 WebFrame* WebFrame::fromFrameOwnerElement(const WebElement& element)
466 return WebFrameImpl::fromFrameOwnerElement(
467 PassRefPtr<Element>(element).get());
470 WebString WebFrameImpl::name() const
472 return m_frame->tree()->name();
475 void WebFrameImpl::setName(const WebString& name)
477 m_frame->tree()->setName(name);
480 WebURL WebFrameImpl::url() const
482 const WebDataSource* ds = dataSource();
485 return ds->request().url();
488 WebURL WebFrameImpl::favIconURL() const
490 FrameLoader* frameLoader = m_frame->loader();
491 // The URL to the favicon may be in the header. As such, only
492 // ask the loader for the favicon if it's finished loading.
493 if (frameLoader->state() == FrameStateComplete) {
494 const KURL& url = frameLoader->iconURL();
501 WebURL WebFrameImpl::openSearchDescriptionURL() const
503 FrameLoader* frameLoader = m_frame->loader();
504 if (frameLoader->state() == FrameStateComplete
505 && m_frame->document() && m_frame->document()->head()
506 && !m_frame->tree()->parent()) {
507 HTMLHeadElement* head = m_frame->document()->head();
509 RefPtr<HTMLCollection> children = head->children();
510 for (Node* child = children->firstItem(); child; child = children->nextItem()) {
511 HTMLLinkElement* linkElement = toHTMLLinkElement(child);
513 && linkElement->type() == osdType
514 && linkElement->rel() == osdRel
515 && !linkElement->href().isEmpty())
516 return linkElement->href();
523 WebString WebFrameImpl::encoding() const
525 return frame()->loader()->writer()->encoding();
528 WebSize WebFrameImpl::scrollOffset() const
530 FrameView* view = frameView();
532 return view->scrollOffset();
537 WebSize WebFrameImpl::contentsSize() const
539 return frame()->view()->contentsSize();
542 int WebFrameImpl::contentsPreferredWidth() const
544 if (m_frame->document() && m_frame->document()->renderView())
545 return m_frame->document()->renderView()->minPrefWidth();
549 int WebFrameImpl::documentElementScrollHeight() const
551 if (m_frame->document() && m_frame->document()->documentElement())
552 return m_frame->document()->documentElement()->scrollHeight();
556 bool WebFrameImpl::hasVisibleContent() const
558 return frame()->view()->visibleWidth() > 0 && frame()->view()->visibleHeight() > 0;
561 WebView* WebFrameImpl::view() const
566 WebFrame* WebFrameImpl::opener() const
570 opener = m_frame->loader()->opener();
571 return fromFrame(opener);
574 WebFrame* WebFrameImpl::parent() const
578 parent = m_frame->tree()->parent();
579 return fromFrame(parent);
582 WebFrame* WebFrameImpl::top() const
585 return fromFrame(m_frame->tree()->top());
590 WebFrame* WebFrameImpl::firstChild() const
592 return fromFrame(frame()->tree()->firstChild());
595 WebFrame* WebFrameImpl::lastChild() const
597 return fromFrame(frame()->tree()->lastChild());
600 WebFrame* WebFrameImpl::nextSibling() const
602 return fromFrame(frame()->tree()->nextSibling());
605 WebFrame* WebFrameImpl::previousSibling() const
607 return fromFrame(frame()->tree()->previousSibling());
610 WebFrame* WebFrameImpl::traverseNext(bool wrap) const
612 return fromFrame(frame()->tree()->traverseNextWithWrap(wrap));
615 WebFrame* WebFrameImpl::traversePrevious(bool wrap) const
617 return fromFrame(frame()->tree()->traversePreviousWithWrap(wrap));
620 WebFrame* WebFrameImpl::findChildByName(const WebString& name) const
622 return fromFrame(frame()->tree()->child(name));
625 WebFrame* WebFrameImpl::findChildByExpression(const WebString& xpath) const
630 Document* document = m_frame->document();
632 ExceptionCode ec = 0;
633 PassRefPtr<XPathResult> xpathResult =
634 document->evaluate(xpath,
637 XPathResult::ORDERED_NODE_ITERATOR_TYPE,
638 0, // XPathResult object
640 if (!xpathResult.get())
643 Node* node = xpathResult->iterateNext(ec);
645 if (!node || !node->isFrameOwnerElement())
647 HTMLFrameOwnerElement* frameElement =
648 static_cast<HTMLFrameOwnerElement*>(node);
649 return fromFrame(frameElement->contentFrame());
652 WebDocument WebFrameImpl::document() const
654 if (!m_frame || !m_frame->document())
655 return WebDocument();
656 return WebDocument(m_frame->document());
659 void WebFrameImpl::forms(WebVector<WebFormElement>& results) const
664 RefPtr<HTMLCollection> forms = m_frame->document()->forms();
665 size_t formCount = 0;
666 for (size_t i = 0; i < forms->length(); ++i) {
667 Node* node = forms->item(i);
668 if (node && node->isHTMLElement())
672 WebVector<WebFormElement> temp(formCount);
673 for (size_t i = 0; i < formCount; ++i) {
674 Node* node = forms->item(i);
675 // Strange but true, sometimes item can be 0.
676 if (node && node->isHTMLElement())
677 temp[i] = static_cast<HTMLFormElement*>(node);
682 WebAnimationController* WebFrameImpl::animationController()
684 return &m_animationController;
687 WebSecurityOrigin WebFrameImpl::securityOrigin() const
689 if (!m_frame || !m_frame->document())
690 return WebSecurityOrigin();
692 return WebSecurityOrigin(m_frame->document()->securityOrigin());
695 void WebFrameImpl::grantUniversalAccess()
697 ASSERT(m_frame && m_frame->document());
698 if (m_frame && m_frame->document())
699 m_frame->document()->securityOrigin()->grantUniversalAccess();
702 NPObject* WebFrameImpl::windowObject() const
707 return m_frame->script()->windowScriptNPObject();
710 void WebFrameImpl::bindToWindowObject(const WebString& name, NPObject* object)
713 if (!m_frame || !m_frame->script()->canExecuteScripts(NotAboutToExecuteScript))
718 m_frame->script()->bindToWindowObject(m_frame, key, object);
724 void WebFrameImpl::executeScript(const WebScriptSource& source)
726 m_frame->script()->executeScript(
727 ScriptSourceCode(source.code, source.url, source.startLine));
730 void WebFrameImpl::executeScriptInIsolatedWorld(
731 int worldId, const WebScriptSource* sourcesIn, unsigned numSources,
734 Vector<ScriptSourceCode> sources;
736 for (unsigned i = 0; i < numSources; ++i) {
737 sources.append(ScriptSourceCode(
738 sourcesIn[i].code, sourcesIn[i].url, sourcesIn[i].startLine));
741 m_frame->script()->evaluateInIsolatedWorld(worldId, sources, extensionGroup);
744 void WebFrameImpl::addMessageToConsole(const WebConsoleMessage& message)
748 MessageLevel webCoreMessageLevel;
749 switch (message.level) {
750 case WebConsoleMessage::LevelTip:
751 webCoreMessageLevel = TipMessageLevel;
753 case WebConsoleMessage::LevelLog:
754 webCoreMessageLevel = LogMessageLevel;
756 case WebConsoleMessage::LevelWarning:
757 webCoreMessageLevel = WarningMessageLevel;
759 case WebConsoleMessage::LevelError:
760 webCoreMessageLevel = ErrorMessageLevel;
763 ASSERT_NOT_REACHED();
767 frame()->domWindow()->console()->addMessage(
768 OtherMessageSource, LogMessageType, webCoreMessageLevel, message.text,
772 void WebFrameImpl::collectGarbage()
776 if (!m_frame->settings()->isJavaScriptEnabled())
778 // FIXME: Move this to the ScriptController and make it JS neutral.
780 m_frame->script()->collectGarbage();
787 v8::Handle<v8::Value> WebFrameImpl::executeScriptAndReturnValue(
788 const WebScriptSource& source)
790 return m_frame->script()->executeScript(
791 ScriptSourceCode(source.code, source.url, source.startLine)).v8Value();
794 // Returns the V8 context for this frame, or an empty handle if there is none.
795 v8::Local<v8::Context> WebFrameImpl::mainWorldScriptContext() const
798 return v8::Local<v8::Context>();
800 return V8Proxy::mainWorldContext(m_frame);
804 bool WebFrameImpl::insertStyleText(
805 const WebString& css, const WebString& id) {
806 Document* document = frame()->document();
809 Element* documentElement = document->documentElement();
810 if (!documentElement)
813 ExceptionCode err = 0;
816 Element* oldElement = document->getElementById(id);
818 Node* parent = oldElement->parent();
821 parent->removeChild(oldElement, err);
825 RefPtr<Element> stylesheet = document->createElement(
826 HTMLNames::styleTag, false);
828 stylesheet->setAttribute(HTMLNames::idAttr, id);
829 stylesheet->setTextContent(css, err);
831 Node* first = documentElement->firstChild();
832 bool success = documentElement->insertBefore(stylesheet, first, err);
837 void WebFrameImpl::reload(bool ignoreCache)
839 m_frame->loader()->history()->saveDocumentAndScrollState();
840 m_frame->loader()->reload(ignoreCache);
843 void WebFrameImpl::loadRequest(const WebURLRequest& request)
845 ASSERT(!request.isNull());
846 const ResourceRequest& resourceRequest = request.toResourceRequest();
848 if (resourceRequest.url().protocolIs("javascript")) {
849 loadJavaScriptURL(resourceRequest.url());
853 m_frame->loader()->load(resourceRequest, false);
856 void WebFrameImpl::loadHistoryItem(const WebHistoryItem& item)
858 RefPtr<HistoryItem> historyItem = PassRefPtr<HistoryItem>(item);
859 ASSERT(historyItem.get());
861 // If there is no currentItem, which happens when we are navigating in
862 // session history after a crash, we need to manufacture one otherwise WebKit
863 // hoarks. This is probably the wrong thing to do, but it seems to work.
864 RefPtr<HistoryItem> currentItem = m_frame->loader()->history()->currentItem();
866 currentItem = HistoryItem::create();
867 currentItem->setLastVisitWasFailure(true);
868 m_frame->loader()->history()->setCurrentItem(currentItem.get());
869 viewImpl()->setCurrentHistoryItem(currentItem.get());
872 m_frame->loader()->history()->goToItem(
873 historyItem.get(), FrameLoadTypeIndexedBackForward);
876 void WebFrameImpl::loadData(const WebData& data,
877 const WebString& mimeType,
878 const WebString& textEncoding,
879 const WebURL& baseURL,
880 const WebURL& unreachableURL,
883 SubstituteData substData(data, mimeType, textEncoding, unreachableURL);
884 ASSERT(substData.isValid());
886 // If we are loading substitute data to replace an existing load, then
887 // inherit all of the properties of that original request. This way,
888 // reload will re-attempt the original request. It is essential that
889 // we only do this when there is an unreachableURL since a non-empty
890 // unreachableURL informs FrameLoader::reload to load unreachableURL
891 // instead of the currently loaded URL.
892 ResourceRequest request;
893 if (replace && !unreachableURL.isEmpty())
894 request = m_frame->loader()->originalRequest();
895 request.setURL(baseURL);
897 m_frame->loader()->load(request, substData, false);
899 // Do this to force WebKit to treat the load as replacing the currently
901 m_frame->loader()->setReplacing();
905 void WebFrameImpl::loadHTMLString(const WebData& data,
906 const WebURL& baseURL,
907 const WebURL& unreachableURL,
911 WebString::fromUTF8("text/html"),
912 WebString::fromUTF8("UTF-8"),
918 bool WebFrameImpl::isLoading() const
922 return m_frame->loader()->isLoading();
925 void WebFrameImpl::stopLoading()
930 // FIXME: Figure out what we should really do here. It seems like a bug
931 // that FrameLoader::stopLoading doesn't call stopAllLoaders.
932 m_frame->loader()->stopAllLoaders();
933 m_frame->loader()->stopLoading(UnloadEventPolicyNone);
936 WebDataSource* WebFrameImpl::provisionalDataSource() const
938 FrameLoader* frameLoader = m_frame->loader();
940 // We regard the policy document loader as still provisional.
941 DocumentLoader* docLoader = frameLoader->provisionalDocumentLoader();
943 docLoader = frameLoader->policyDocumentLoader();
945 return DataSourceForDocLoader(docLoader);
948 WebDataSource* WebFrameImpl::dataSource() const
950 return DataSourceForDocLoader(m_frame->loader()->documentLoader());
953 WebHistoryItem WebFrameImpl::previousHistoryItem() const
955 // We use the previous item here because documentState (filled-out forms)
956 // only get saved to history when it becomes the previous item. The caller
957 // is expected to query the history item after a navigation occurs, after
958 // the desired history item has become the previous entry.
959 return WebHistoryItem(viewImpl()->previousHistoryItem());
962 WebHistoryItem WebFrameImpl::currentHistoryItem() const
964 // If we are still loading, then we don't want to clobber the current
965 // history item as this could cause us to lose the scroll position and
966 // document state. However, it is OK for new navigations.
967 if (m_frame->loader()->loadType() == FrameLoadTypeStandard
968 || !m_frame->loader()->activeDocumentLoader()->isLoadingInAPISense())
969 m_frame->loader()->history()->saveDocumentAndScrollState();
971 return WebHistoryItem(m_frame->page()->backForwardList()->currentItem());
974 void WebFrameImpl::enableViewSourceMode(bool enable)
977 m_frame->setInViewSourceMode(enable);
980 bool WebFrameImpl::isViewSourceModeEnabled() const
983 return m_frame->inViewSourceMode();
988 void WebFrameImpl::setReferrerForRequest(
989 WebURLRequest& request, const WebURL& referrerURL) {
991 if (referrerURL.isEmpty())
992 referrer = m_frame->loader()->outgoingReferrer();
994 referrer = referrerURL.spec().utf16();
995 if (SecurityOrigin::shouldHideReferrer(request.url(), referrer))
997 request.setHTTPHeaderField(WebString::fromUTF8("Referer"), referrer);
1000 void WebFrameImpl::dispatchWillSendRequest(WebURLRequest& request)
1002 ResourceResponse response;
1003 m_frame->loader()->client()->dispatchWillSendRequest(
1004 0, 0, request.toMutableResourceRequest(), response);
1007 void WebFrameImpl::commitDocumentData(const char* data, size_t dataLen)
1009 DocumentLoader* documentLoader = m_frame->loader()->documentLoader();
1011 // Set the text encoding. This calls begin() for us. It is safe to call
1012 // this multiple times (Mac does: page/mac/WebCoreFrameBridge.mm).
1013 bool userChosen = true;
1014 String encoding = documentLoader->overrideEncoding();
1015 if (encoding.isNull()) {
1017 encoding = documentLoader->response().textEncodingName();
1019 m_frame->loader()->writer()->setEncoding(encoding, userChosen);
1021 // NOTE: mac only does this if there is a document
1022 m_frame->loader()->addData(data, dataLen);
1025 unsigned WebFrameImpl::unloadListenerCount() const
1027 return frame()->domWindow()->pendingUnloadEventListeners();
1030 bool WebFrameImpl::isProcessingUserGesture() const
1032 return frame()->loader()->isProcessingUserGesture();
1035 bool WebFrameImpl::willSuppressOpenerInNewFrame() const
1037 return frame()->loader()->suppressOpenerInNewFrame();
1040 void WebFrameImpl::replaceSelection(const WebString& text)
1042 RefPtr<DocumentFragment> fragment = createFragmentFromText(
1043 frame()->selection()->toNormalizedRange().get(), text);
1044 applyCommand(ReplaceSelectionCommand::create(
1045 frame()->document(), fragment.get(), false, true, true));
1048 void WebFrameImpl::insertText(const WebString& text)
1050 frame()->editor()->insertText(text, 0);
1053 void WebFrameImpl::setMarkedText(
1054 const WebString& text, unsigned location, unsigned length)
1056 Editor* editor = frame()->editor();
1058 editor->confirmComposition(text);
1060 Vector<CompositionUnderline> decorations;
1061 editor->setComposition(text, decorations, location, length);
1064 void WebFrameImpl::unmarkText()
1066 frame()->editor()->confirmCompositionWithoutDisturbingSelection();
1069 bool WebFrameImpl::hasMarkedText() const
1071 return frame()->editor()->hasComposition();
1074 WebRange WebFrameImpl::markedRange() const
1076 return frame()->editor()->compositionRange();
1079 bool WebFrameImpl::executeCommand(const WebString& name)
1083 if (name.length() <= 2)
1086 // Since we don't have NSControl, we will convert the format of command
1087 // string and call the function on Editor directly.
1088 String command = name;
1090 // Make sure the first letter is upper case.
1091 command.replace(0, 1, command.substring(0, 1).upper());
1093 // Remove the trailing ':' if existing.
1094 if (command[command.length() - 1] == UChar(':'))
1095 command = command.substring(0, command.length() - 1);
1099 // Specially handling commands that Editor::execCommand does not directly
1101 if (command == "DeleteToEndOfParagraph") {
1102 Editor* editor = frame()->editor();
1103 if (!editor->deleteWithDirection(SelectionController::DirectionForward,
1107 editor->deleteWithDirection(SelectionController::DirectionForward,
1108 CharacterGranularity,
1112 } else if (command == "Indent")
1113 frame()->editor()->indent();
1114 else if (command == "Outdent")
1115 frame()->editor()->outdent();
1116 else if (command == "DeleteBackward")
1117 rv = frame()->editor()->command(AtomicString("BackwardDelete")).execute();
1118 else if (command == "DeleteForward")
1119 rv = frame()->editor()->command(AtomicString("ForwardDelete")).execute();
1120 else if (command == "AdvanceToNextMisspelling") {
1121 // False must be passed here, or the currently selected word will never be
1123 frame()->editor()->advanceToNextMisspelling(false);
1124 } else if (command == "ToggleSpellPanel")
1125 frame()->editor()->showSpellingGuessPanel();
1127 rv = frame()->editor()->command(command).execute();
1131 bool WebFrameImpl::executeCommand(const WebString& name, const WebString& value)
1134 String webName = name;
1136 // moveToBeginningOfDocument and moveToEndfDocument are only handled by WebKit
1137 // for editable nodes.
1138 if (!frame()->editor()->canEdit() && webName == "moveToBeginningOfDocument")
1139 return viewImpl()->propagateScroll(ScrollUp, ScrollByDocument);
1141 if (!frame()->editor()->canEdit() && webName == "moveToEndOfDocument")
1142 return viewImpl()->propagateScroll(ScrollDown, ScrollByDocument);
1144 return frame()->editor()->command(webName).execute(value);
1147 bool WebFrameImpl::isCommandEnabled(const WebString& name) const
1150 return frame()->editor()->command(name).isEnabled();
1153 void WebFrameImpl::enableContinuousSpellChecking(bool enable)
1155 if (enable == isContinuousSpellCheckingEnabled())
1157 frame()->editor()->toggleContinuousSpellChecking();
1160 bool WebFrameImpl::isContinuousSpellCheckingEnabled() const
1162 return frame()->editor()->isContinuousSpellCheckingEnabled();
1165 bool WebFrameImpl::hasSelection() const
1167 // frame()->selection()->isNone() never returns true.
1168 return (frame()->selection()->start() != frame()->selection()->end());
1171 WebRange WebFrameImpl::selectionRange() const
1173 return frame()->selection()->toNormalizedRange();
1176 WebString WebFrameImpl::selectionAsText() const
1178 RefPtr<Range> range = frame()->selection()->toNormalizedRange();
1182 String text = range->text();
1184 replaceNewlinesWithWindowsStyleNewlines(text);
1186 replaceNBSPWithSpace(text);
1190 WebString WebFrameImpl::selectionAsMarkup() const
1192 RefPtr<Range> range = frame()->selection()->toNormalizedRange();
1196 return createMarkup(range.get(), 0);
1199 void WebFrameImpl::selectWordAroundPosition(Frame* frame, VisiblePosition pos)
1201 VisibleSelection selection(pos);
1202 selection.expandUsingGranularity(WordGranularity);
1204 if (frame->shouldChangeSelection(selection)) {
1205 TextGranularity granularity = selection.isRange() ? WordGranularity : CharacterGranularity;
1206 frame->selection()->setSelection(selection, granularity);
1210 bool WebFrameImpl::selectWordAroundCaret()
1212 SelectionController* controller = frame()->selection();
1213 ASSERT(!controller->isNone());
1214 if (controller->isNone() || controller->isRange())
1216 selectWordAroundPosition(frame(), controller->selection().visibleStart());
1220 int WebFrameImpl::printBegin(const WebSize& pageSize, int printerDPI, bool *useBrowserOverlays)
1222 ASSERT(!frame()->document()->isFrameSet());
1223 // If this is a plugin document, check if the plugin supports its own
1224 // printing. If it does, we will delegate all printing to that.
1225 WebPluginContainerImpl* pluginContainer = pluginContainerFromFrame(frame());
1226 if (pluginContainer && pluginContainer->supportsPaginatedPrint())
1227 m_printContext.set(new ChromePluginPrintContext(frame(), printerDPI));
1229 m_printContext.set(new ChromePrintContext(frame()));
1231 FloatRect rect(0, 0, static_cast<float>(pageSize.width),
1232 static_cast<float>(pageSize.height));
1233 m_printContext->begin(rect.width());
1235 // We ignore the overlays calculation for now since they are generated in the
1236 // browser. pageHeight is actually an output parameter.
1237 m_printContext->computePageRects(rect, 0, 0, 1.0, pageHeight);
1238 if (useBrowserOverlays)
1239 *useBrowserOverlays = m_printContext->shouldUseBrowserOverlays();
1241 return m_printContext->pageCount();
1244 float WebFrameImpl::getPrintPageShrink(int page)
1246 // Ensure correct state.
1247 if (!m_printContext.get() || page < 0) {
1248 ASSERT_NOT_REACHED();
1252 return m_printContext->getPageShrink(page);
1255 float WebFrameImpl::printPage(int page, WebCanvas* canvas)
1257 // Ensure correct state.
1258 if (!m_printContext.get() || page < 0 || !frame() || !frame()->document()) {
1259 ASSERT_NOT_REACHED();
1263 #if OS(WINDOWS) || OS(LINUX) || OS(FREEBSD) || OS(SOLARIS)
1264 PlatformContextSkia context(canvas);
1265 GraphicsContext spool(&context);
1267 GraphicsContext spool(canvas);
1268 LocalCurrentGraphicsContext localContext(&spool);
1271 return m_printContext->spoolPage(spool, page);
1274 void WebFrameImpl::printEnd()
1276 ASSERT(m_printContext.get());
1277 if (m_printContext.get())
1278 m_printContext->end();
1279 m_printContext.clear();
1282 bool WebFrameImpl::isPageBoxVisible(int pageIndex)
1284 return frame()->document()->isPageBoxVisible(pageIndex);
1287 WebRect WebFrameImpl::pageAreaRectInPixels(int pageIndex)
1289 return frame()->document()->pageAreaRectInPixels(pageIndex);
1292 WebSize WebFrameImpl::preferredPageSizeInPixels(int pageIndex)
1294 return frame()->document()->preferredPageSizeInPixels(pageIndex);
1297 bool WebFrameImpl::find(int identifier,
1298 const WebString& searchText,
1299 const WebFindOptions& options,
1300 bool wrapWithinFrame,
1301 WebRect* selectionRect)
1303 WebFrameImpl* mainFrameImpl = viewImpl()->mainFrameImpl();
1305 if (!options.findNext)
1306 frame()->page()->unmarkAllTextMatches();
1308 setMarkerActive(m_activeMatch.get(), false); // Active match is changing.
1310 // Starts the search from the current selection.
1311 bool startInSelection = true;
1313 // If the user has selected something since the last Find operation we want
1314 // to start from there. Otherwise, we start searching from where the last Find
1315 // operation left off (either a Find or a FindNext operation).
1316 VisibleSelection selection(frame()->selection()->selection());
1317 bool activeSelection = !selection.isNone();
1318 if (!activeSelection && m_activeMatch) {
1319 selection = VisibleSelection(m_activeMatch.get());
1320 frame()->selection()->setSelection(selection);
1323 ASSERT(frame() && frame()->view());
1324 bool found = frame()->findString(
1325 searchText, options.forward, options.matchCase, wrapWithinFrame,
1328 // Store which frame was active. This will come in handy later when we
1329 // change the active match ordinal below.
1330 WebFrameImpl* oldActiveFrame = mainFrameImpl->m_activeMatchFrame;
1331 // Set this frame as the active frame (the one with the active highlight).
1332 mainFrameImpl->m_activeMatchFrame = this;
1334 // We found something, so we can now query the selection for its position.
1335 VisibleSelection newSelection(frame()->selection()->selection());
1336 IntRect currSelectionRect;
1338 // If we thought we found something, but it couldn't be selected (perhaps
1339 // because it was marked -webkit-user-select: none), we can't set it to
1340 // be active but we still continue searching. This matches Safari's
1341 // behavior, including some oddities when selectable and un-selectable text
1342 // are mixed on a page: see https://bugs.webkit.org/show_bug.cgi?id=19127.
1343 if (newSelection.isNone() || (newSelection.start() == newSelection.end()))
1346 m_activeMatch = newSelection.toNormalizedRange();
1347 currSelectionRect = m_activeMatch->boundingBox();
1348 setMarkerActive(m_activeMatch.get(), true); // Active.
1349 // WebKit draws the highlighting for all matches.
1350 executeCommand(WebString::fromUTF8("Unselect"));
1353 // Make sure no node is focused. See http://crbug.com/38700.
1354 frame()->document()->setFocusedNode(0);
1356 if (!options.findNext || activeSelection) {
1357 // This is either a Find operation or a Find-next from a new start point
1358 // due to a selection, so we set the flag to ask the scoping effort
1359 // to find the active rect for us so we can update the ordinal (n of m).
1360 m_locatingActiveRect = true;
1362 if (oldActiveFrame != this) {
1363 // If the active frame has changed it means that we have a multi-frame
1364 // page and we just switch to searching in a new frame. Then we just
1365 // want to reset the index.
1366 if (options.forward)
1367 m_activeMatchIndex = 0;
1369 m_activeMatchIndex = m_lastMatchCount - 1;
1371 // We are still the active frame, so increment (or decrement) the
1372 // |m_activeMatchIndex|, wrapping if needed (on single frame pages).
1373 options.forward ? ++m_activeMatchIndex : --m_activeMatchIndex;
1374 if (m_activeMatchIndex + 1 > m_lastMatchCount)
1375 m_activeMatchIndex = 0;
1376 if (m_activeMatchIndex == -1)
1377 m_activeMatchIndex = m_lastMatchCount - 1;
1379 if (selectionRect) {
1380 WebRect rect = frame()->view()->convertToContainingWindow(currSelectionRect);
1381 rect.x -= frameView()->scrollOffset().width();
1382 rect.y -= frameView()->scrollOffset().height();
1383 *selectionRect = rect;
1385 reportFindInPageSelection(rect, m_activeMatchIndex + 1, identifier);
1389 // Nothing was found in this frame.
1392 // Erase all previous tickmarks and highlighting.
1393 invalidateArea(InvalidateAll);
1399 void WebFrameImpl::stopFinding(bool clearSelection)
1401 if (!clearSelection)
1402 setFindEndstateFocusAndSelection();
1403 cancelPendingScopingEffort();
1405 // Remove all markers for matches found and turn off the highlighting.
1406 frame()->document()->removeMarkers(DocumentMarker::TextMatch);
1407 frame()->setMarkedTextMatchesAreHighlighted(false);
1409 // Let the frame know that we don't want tickmarks or highlighting anymore.
1410 invalidateArea(InvalidateAll);
1413 void WebFrameImpl::scopeStringMatches(int identifier,
1414 const WebString& searchText,
1415 const WebFindOptions& options,
1418 if (!shouldScopeMatches(searchText))
1421 WebFrameImpl* mainFrameImpl = viewImpl()->mainFrameImpl();
1424 // This is a brand new search, so we need to reset everything.
1425 // Scoping is just about to begin.
1426 m_scopingComplete = false;
1427 // Clear highlighting for this frame.
1428 if (frame()->markedTextMatchesAreHighlighted())
1429 frame()->page()->unmarkAllTextMatches();
1430 // Clear the counters from last operation.
1431 m_lastMatchCount = 0;
1432 m_nextInvalidateAfter = 0;
1434 m_resumeScopingFromRange = 0;
1436 mainFrameImpl->m_framesScopingCount++;
1438 // Now, defer scoping until later to allow find operation to finish quickly.
1439 scopeStringMatchesSoon(
1443 false); // false=we just reset, so don't do it again.
1447 RefPtr<Range> searchRange(rangeOfContents(frame()->document()));
1449 ExceptionCode ec = 0, ec2 = 0;
1450 if (m_resumeScopingFromRange.get()) {
1451 // This is a continuation of a scoping operation that timed out and didn't
1452 // complete last time around, so we should start from where we left off.
1453 searchRange->setStart(m_resumeScopingFromRange->startContainer(),
1454 m_resumeScopingFromRange->startOffset(ec2) + 1,
1457 if (ec2) // A non-zero |ec| happens when navigating during search.
1458 ASSERT_NOT_REACHED();
1463 // This timeout controls how long we scope before releasing control. This
1464 // value does not prevent us from running for longer than this, but it is
1465 // periodically checked to see if we have exceeded our allocated time.
1466 const double maxScopingDuration = 0.1; // seconds
1469 bool timedOut = false;
1470 double startTime = currentTime();
1472 // Find next occurrence of the search string.
1473 // FIXME: (http://b/1088245) This WebKit operation may run for longer
1474 // than the timeout value, and is not interruptible as it is currently
1475 // written. We may need to rewrite it with interruptibility in mind, or
1476 // find an alternative.
1477 RefPtr<Range> resultRange(findPlainText(searchRange.get(),
1480 options.matchCase));
1481 if (resultRange->collapsed(ec)) {
1482 if (!resultRange->startContainer()->isInShadowTree())
1485 searchRange = rangeOfContents(frame()->document());
1486 searchRange->setStartAfter(
1487 resultRange->startContainer()->shadowAncestorNode(), ec);
1491 // A non-collapsed result range can in some funky whitespace cases still not
1492 // advance the range's start position (4509328). Break to avoid infinite
1493 // loop. (This function is based on the implementation of
1494 // Frame::markAllMatchesForText, which is where this safeguard comes from).
1495 VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM);
1496 if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM))
1499 // Only treat the result as a match if it is visible
1500 if (frame()->editor()->insideVisibleArea(resultRange.get())) {
1503 setStart(searchRange.get(), newStart);
1504 Node* shadowTreeRoot = searchRange->shadowTreeRootNode();
1505 if (searchRange->collapsed(ec) && shadowTreeRoot)
1506 searchRange->setEnd(shadowTreeRoot, shadowTreeRoot->childNodeCount(), ec);
1508 // Catch a special case where Find found something but doesn't know what
1509 // the bounding box for it is. In this case we set the first match we find
1510 // as the active rect.
1511 IntRect resultBounds = resultRange->boundingBox();
1512 IntRect activeSelectionRect;
1513 if (m_locatingActiveRect) {
1514 activeSelectionRect = m_activeMatch.get() ?
1515 m_activeMatch->boundingBox() : resultBounds;
1518 // If the Find function found a match it will have stored where the
1519 // match was found in m_activeSelectionRect on the current frame. If we
1520 // find this rect during scoping it means we have found the active
1522 bool foundActiveMatch = false;
1523 if (m_locatingActiveRect && (activeSelectionRect == resultBounds)) {
1524 // We have found the active tickmark frame.
1525 mainFrameImpl->m_activeMatchFrame = this;
1526 foundActiveMatch = true;
1527 // We also know which tickmark is active now.
1528 m_activeMatchIndex = matchCount - 1;
1529 // To stop looking for the active tickmark, we set this flag.
1530 m_locatingActiveRect = false;
1532 // Notify browser of new location for the selected rectangle.
1533 resultBounds.move(-frameView()->scrollOffset().width(),
1534 -frameView()->scrollOffset().height());
1535 reportFindInPageSelection(
1536 frame()->view()->convertToContainingWindow(resultBounds),
1537 m_activeMatchIndex + 1,
1541 addMarker(resultRange.get(), foundActiveMatch);
1544 m_resumeScopingFromRange = resultRange;
1545 timedOut = (currentTime() - startTime) >= maxScopingDuration;
1546 } while (!timedOut);
1548 // Remember what we search for last time, so we can skip searching if more
1549 // letters are added to the search string (and last outcome was 0).
1550 m_lastSearchString = searchText;
1552 if (matchCount > 0) {
1553 frame()->setMarkedTextMatchesAreHighlighted(true);
1555 m_lastMatchCount += matchCount;
1557 // Let the mainframe know how much we found during this pass.
1558 mainFrameImpl->increaseMatchCount(matchCount, identifier);
1562 // If we found anything during this pass, we should redraw. However, we
1563 // don't want to spam too much if the page is extremely long, so if we
1564 // reach a certain point we start throttling the redraw requests.
1566 invalidateIfNecessary();
1568 // Scoping effort ran out of time, lets ask for another time-slice.
1569 scopeStringMatchesSoon(
1573 false); // don't reset.
1574 return; // Done for now, resume work later.
1577 // This frame has no further scoping left, so it is done. Other frames might,
1578 // of course, continue to scope matches.
1579 m_scopingComplete = true;
1580 mainFrameImpl->m_framesScopingCount--;
1582 // If this is the last frame to finish scoping we need to trigger the final
1583 // update to be sent.
1584 if (!mainFrameImpl->m_framesScopingCount)
1585 mainFrameImpl->increaseMatchCount(0, identifier);
1587 // This frame is done, so show any scrollbar tickmarks we haven't drawn yet.
1588 invalidateArea(InvalidateScrollbar);
1591 void WebFrameImpl::cancelPendingScopingEffort()
1593 deleteAllValues(m_deferredScopingWork);
1594 m_deferredScopingWork.clear();
1596 m_activeMatchIndex = -1;
1599 void WebFrameImpl::increaseMatchCount(int count, int identifier)
1601 // This function should only be called on the mainframe.
1604 m_totalMatchCount += count;
1606 // Update the UI with the latest findings.
1608 client()->reportFindInPageMatchCount(identifier, m_totalMatchCount, !m_framesScopingCount);
1611 void WebFrameImpl::reportFindInPageSelection(const WebRect& selectionRect,
1612 int activeMatchOrdinal,
1615 // Update the UI with the latest selection rect.
1617 client()->reportFindInPageSelection(identifier, ordinalOfFirstMatchForFrame(this) + activeMatchOrdinal, selectionRect);
1620 void WebFrameImpl::resetMatchCount()
1622 m_totalMatchCount = 0;
1623 m_framesScopingCount = 0;
1626 WebString WebFrameImpl::contentAsText(size_t maxChars) const
1632 frameContentAsPlainText(maxChars, m_frame, &text);
1633 return String::adopt(text);
1636 WebString WebFrameImpl::contentAsMarkup() const
1638 return createFullMarkup(m_frame->document());
1641 WebString WebFrameImpl::renderTreeAsText() const
1643 return externalRepresentation(m_frame);
1646 WebString WebFrameImpl::counterValueForElementById(const WebString& id) const
1651 Element* element = m_frame->document()->getElementById(id);
1655 return counterValueForElement(element);
1658 int WebFrameImpl::pageNumberForElementById(const WebString& id,
1659 float pageWidthInPixels,
1660 float pageHeightInPixels) const
1665 Element* element = m_frame->document()->getElementById(id);
1669 FloatSize pageSize(pageWidthInPixels, pageHeightInPixels);
1670 return PrintContext::pageNumberForElement(element, pageSize);
1673 WebRect WebFrameImpl::selectionBoundsRect() const
1676 return IntRect(frame()->selectionBounds(false));
1681 // WebFrameImpl public ---------------------------------------------------------
1683 PassRefPtr<WebFrameImpl> WebFrameImpl::create(WebFrameClient* client)
1685 return adoptRef(new WebFrameImpl(client));
1688 WebFrameImpl::WebFrameImpl(WebFrameClient* client)
1689 : m_frameLoaderClient(this)
1691 , m_activeMatchFrame(0)
1692 , m_activeMatchIndex(-1)
1693 , m_locatingActiveRect(false)
1694 , m_resumeScopingFromRange(0)
1695 , m_lastMatchCount(-1)
1696 , m_totalMatchCount(-1)
1697 , m_framesScopingCount(-1)
1698 , m_scopingComplete(false)
1699 , m_nextInvalidateAfter(0)
1700 , m_animationController(this)
1702 ChromiumBridge::incrementStatsCounter(webFrameActiveCount);
1706 WebFrameImpl::~WebFrameImpl()
1708 ChromiumBridge::decrementStatsCounter(webFrameActiveCount);
1711 cancelPendingScopingEffort();
1712 clearPasswordListeners();
1715 void WebFrameImpl::initializeAsMainFrame(WebViewImpl* webViewImpl)
1717 RefPtr<Frame> frame = Frame::create(webViewImpl->page(), 0, &m_frameLoaderClient);
1718 m_frame = frame.get();
1720 // Add reference on behalf of FrameLoader. See comments in
1721 // WebFrameLoaderClient::frameLoaderDestroyed for more info.
1724 // We must call init() after m_frame is assigned because it is referenced
1729 PassRefPtr<Frame> WebFrameImpl::createChildFrame(
1730 const FrameLoadRequest& request, HTMLFrameOwnerElement* ownerElement)
1732 RefPtr<WebFrameImpl> webframe(adoptRef(new WebFrameImpl(m_client)));
1734 // Add an extra ref on behalf of the Frame/FrameLoader, which references the
1735 // WebFrame via the FrameLoaderClient interface. See the comment at the top
1736 // of this file for more info.
1739 RefPtr<Frame> childFrame = Frame::create(
1740 m_frame->page(), ownerElement, &webframe->m_frameLoaderClient);
1741 webframe->m_frame = childFrame.get();
1743 childFrame->tree()->setName(request.frameName());
1745 m_frame->tree()->appendChild(childFrame);
1747 // Frame::init() can trigger onload event in the parent frame,
1748 // which may detach this frame and trigger a null-pointer access
1749 // in FrameTree::removeChild. Move init() after appendChild call
1750 // so that webframe->mFrame is in the tree before triggering
1751 // onload event handler.
1752 // Because the event handler may set webframe->mFrame to null,
1753 // it is necessary to check the value after calling init() and
1754 // return without loading URL.
1756 childFrame->init(); // create an empty document
1757 if (!childFrame->tree()->parent())
1760 m_frame->loader()->loadURLIntoChildFrame(
1761 request.resourceRequest().url(),
1762 request.resourceRequest().httpReferrer(),
1765 // A synchronous navigation (about:blank) would have already processed
1766 // onload, so it is possible for the frame to have already been destroyed by
1767 // script in the page.
1768 if (!childFrame->tree()->parent())
1771 return childFrame.release();
1774 void WebFrameImpl::layout()
1776 // layout this frame
1777 FrameView* view = m_frame->view();
1779 view->layoutIfNeededRecursive();
1782 void WebFrameImpl::paintWithContext(GraphicsContext& gc, const WebRect& rect)
1784 IntRect dirtyRect(rect);
1786 if (m_frame->document() && frameView()) {
1788 frameView()->paint(&gc, dirtyRect);
1789 m_frame->page()->inspectorController()->drawNodeHighlight(gc);
1791 gc.fillRect(dirtyRect, Color::white, DeviceColorSpace);
1795 void WebFrameImpl::paint(WebCanvas* canvas, const WebRect& rect)
1800 GraphicsContext gc(canvas);
1801 LocalCurrentGraphicsContext localContext(&gc);
1802 #elif WEBKIT_USING_SKIA
1803 PlatformContextSkia context(canvas);
1805 // PlatformGraphicsContext is actually a pointer to PlatformContextSkia
1806 GraphicsContext gc(reinterpret_cast<PlatformGraphicsContext*>(&context));
1810 paintWithContext(gc, rect);
1813 void WebFrameImpl::createFrameView()
1815 ASSERT(m_frame); // If m_frame doesn't exist, we probably didn't init properly.
1817 Page* page = m_frame->page();
1819 ASSERT(page->mainFrame());
1821 bool isMainFrame = m_frame == page->mainFrame();
1822 if (isMainFrame && m_frame->view())
1823 m_frame->view()->setParentVisible(false);
1825 m_frame->setView(0);
1827 WebViewImpl* webView = viewImpl();
1829 RefPtr<FrameView> view;
1831 view = FrameView::create(m_frame, webView->size());
1833 view = FrameView::create(m_frame);
1835 m_frame->setView(view);
1837 if (webView->isTransparent())
1838 view->setTransparent(true);
1840 // FIXME: The Mac code has a comment about this possibly being unnecessary.
1841 // See installInFrame in WebCoreFrameBridge.mm
1842 if (m_frame->ownerRenderer())
1843 m_frame->ownerRenderer()->setWidget(view.get());
1845 if (HTMLFrameOwnerElement* owner = m_frame->ownerElement())
1846 view->setCanHaveScrollbars(owner->scrollingMode() != ScrollbarAlwaysOff);
1849 view->setParentVisible(true);
1852 WebFrameImpl* WebFrameImpl::fromFrame(Frame* frame)
1857 return static_cast<FrameLoaderClientImpl*>(frame->loader()->client())->webFrame();
1860 WebFrameImpl* WebFrameImpl::fromFrameOwnerElement(Element* element)
1863 || !element->isFrameOwnerElement()
1864 || (!element->hasTagName(HTMLNames::iframeTag)
1865 && !element->hasTagName(HTMLNames::frameTag)))
1868 HTMLFrameOwnerElement* frameElement =
1869 static_cast<HTMLFrameOwnerElement*>(element);
1870 return fromFrame(frameElement->contentFrame());
1873 WebViewImpl* WebFrameImpl::viewImpl() const
1878 return WebViewImpl::fromPage(m_frame->page());
1881 WebDataSourceImpl* WebFrameImpl::dataSourceImpl() const
1883 return static_cast<WebDataSourceImpl*>(dataSource());
1886 WebDataSourceImpl* WebFrameImpl::provisionalDataSourceImpl() const
1888 return static_cast<WebDataSourceImpl*>(provisionalDataSource());
1891 void WebFrameImpl::setFindEndstateFocusAndSelection()
1893 WebFrameImpl* mainFrameImpl = viewImpl()->mainFrameImpl();
1895 if (this == mainFrameImpl->activeMatchFrame() && m_activeMatch.get()) {
1896 // If the user has set the selection since the match was found, we
1897 // don't focus anything.
1898 VisibleSelection selection(frame()->selection()->selection());
1899 if (!selection.isNone())
1902 // Try to find the first focusable node up the chain, which will, for
1903 // example, focus links if we have found text within the link.
1904 Node* node = m_activeMatch->firstNode();
1905 while (node && !node->isFocusable() && node != frame()->document())
1906 node = node->parent();
1908 if (node && node != frame()->document()) {
1909 // Found a focusable parent node. Set focus to it.
1910 frame()->document()->setFocusedNode(node);
1914 // Iterate over all the nodes in the range until we find a focusable node.
1915 // This, for example, sets focus to the first link if you search for
1916 // text and text that is within one or more links.
1917 node = m_activeMatch->firstNode();
1918 while (node && node != m_activeMatch->pastLastNode()) {
1919 if (node->isFocusable()) {
1920 frame()->document()->setFocusedNode(node);
1923 node = node->traverseNextNode();
1926 // No node related to the active match was focusable, so set the
1927 // active match as the selection (so that when you end the Find session,
1928 // you'll have the last thing you found highlighted) and make sure that
1929 // we have nothing focused (otherwise you might have text selected but
1930 // a link focused, which is weird).
1931 frame()->selection()->setSelection(m_activeMatch.get());
1932 frame()->document()->setFocusedNode(0);
1936 void WebFrameImpl::didFail(const ResourceError& error, bool wasProvisional)
1940 WebURLError webError = error;
1942 client()->didFailProvisionalLoad(this, webError);
1944 client()->didFailLoad(this, webError);
1947 void WebFrameImpl::setCanHaveScrollbars(bool canHaveScrollbars)
1949 m_frame->view()->setCanHaveScrollbars(canHaveScrollbars);
1952 bool WebFrameImpl::registerPasswordListener(
1953 WebInputElement inputElement,
1954 WebPasswordAutocompleteListener* listener)
1956 RefPtr<HTMLInputElement> element = inputElement.operator PassRefPtr<HTMLInputElement>();
1957 if (!m_passwordListeners.add(element, listener).second) {
1964 WebPasswordAutocompleteListener* WebFrameImpl::getPasswordListener(
1965 HTMLInputElement* inputElement)
1967 return m_passwordListeners.get(RefPtr<HTMLInputElement>(inputElement));
1970 // WebFrameImpl private --------------------------------------------------------
1972 void WebFrameImpl::closing()
1977 void WebFrameImpl::invalidateArea(AreaToInvalidate area)
1979 ASSERT(frame() && frame()->view());
1980 FrameView* view = frame()->view();
1982 if ((area & InvalidateAll) == InvalidateAll)
1983 view->invalidateRect(view->frameRect());
1985 if ((area & InvalidateContentArea) == InvalidateContentArea) {
1986 IntRect contentArea(
1987 view->x(), view->y(), view->visibleWidth(), view->visibleHeight());
1988 IntRect frameRect = view->frameRect();
1989 contentArea.move(-frameRect.topLeft().x(), -frameRect.topLeft().y());
1990 view->invalidateRect(contentArea);
1993 if ((area & InvalidateScrollbar) == InvalidateScrollbar) {
1994 // Invalidate the vertical scroll bar region for the view.
1995 IntRect scrollBarVert(
1996 view->x() + view->visibleWidth(), view->y(),
1997 ScrollbarTheme::nativeTheme()->scrollbarThickness(),
1998 view->visibleHeight());
1999 IntRect frameRect = view->frameRect();
2000 scrollBarVert.move(-frameRect.topLeft().x(), -frameRect.topLeft().y());
2001 view->invalidateRect(scrollBarVert);
2006 void WebFrameImpl::addMarker(Range* range, bool activeMatch)
2008 // Use a TextIterator to visit the potentially multiple nodes the range
2010 TextIterator markedText(range);
2011 for (; !markedText.atEnd(); markedText.advance()) {
2012 RefPtr<Range> textPiece = markedText.range();
2015 DocumentMarker marker = {
2016 DocumentMarker::TextMatch,
2017 textPiece->startOffset(exception),
2018 textPiece->endOffset(exception),
2023 if (marker.endOffset > marker.startOffset) {
2024 // Find the node to add a marker to and add it.
2025 Node* node = textPiece->startContainer(exception);
2026 frame()->document()->addMarker(node, marker);
2028 // Rendered rects for markers in WebKit are not populated until each time
2029 // the markers are painted. However, we need it to happen sooner, because
2030 // the whole purpose of tickmarks on the scrollbar is to show where
2031 // matches off-screen are (that haven't been painted yet).
2032 Vector<DocumentMarker> markers = frame()->document()->markersForNode(node);
2033 frame()->document()->setRenderedRectForMarker(
2034 textPiece->startContainer(exception),
2035 markers[markers.size() - 1],
2036 range->boundingBox());
2041 void WebFrameImpl::setMarkerActive(Range* range, bool active)
2043 WebCore::ExceptionCode ec;
2044 if (!range || range->collapsed(ec))
2047 frame()->document()->setMarkersActive(range, active);
2050 int WebFrameImpl::ordinalOfFirstMatchForFrame(WebFrameImpl* frame) const
2053 WebFrameImpl* mainFrameImpl = viewImpl()->mainFrameImpl();
2054 // Iterate from the main frame up to (but not including) |frame| and
2055 // add up the number of matches found so far.
2056 for (WebFrameImpl* it = mainFrameImpl;
2058 it = static_cast<WebFrameImpl*>(it->traverseNext(true))) {
2059 if (it->m_lastMatchCount > 0)
2060 ordinal += it->m_lastMatchCount;
2065 bool WebFrameImpl::shouldScopeMatches(const String& searchText)
2067 // Don't scope if we can't find a frame or if the frame is not visible.
2068 // The user may have closed the tab/application, so abort.
2069 if (!frame() || !hasVisibleContent())
2072 ASSERT(frame()->document() && frame()->view());
2074 // If the frame completed the scoping operation and found 0 matches the last
2075 // time it was searched, then we don't have to search it again if the user is
2076 // just adding to the search string or sending the same search string again.
2077 if (m_scopingComplete && !m_lastSearchString.isEmpty() && !m_lastMatchCount) {
2078 // Check to see if the search string prefixes match.
2079 String previousSearchPrefix =
2080 searchText.substring(0, m_lastSearchString.length());
2082 if (previousSearchPrefix == m_lastSearchString)
2083 return false; // Don't search this frame, it will be fruitless.
2089 void WebFrameImpl::scopeStringMatchesSoon(int identifier, const WebString& searchText,
2090 const WebFindOptions& options, bool reset)
2092 m_deferredScopingWork.append(new DeferredScopeStringMatches(
2093 this, identifier, searchText, options, reset));
2096 void WebFrameImpl::callScopeStringMatches(DeferredScopeStringMatches* caller,
2097 int identifier, const WebString& searchText,
2098 const WebFindOptions& options, bool reset)
2100 m_deferredScopingWork.remove(m_deferredScopingWork.find(caller));
2102 scopeStringMatches(identifier, searchText, options, reset);
2104 // This needs to happen last since searchText is passed by reference.
2108 void WebFrameImpl::invalidateIfNecessary()
2110 if (m_lastMatchCount > m_nextInvalidateAfter) {
2111 // FIXME: (http://b/1088165) Optimize the drawing of the tickmarks and
2112 // remove this. This calculation sets a milestone for when next to
2113 // invalidate the scrollbar and the content area. We do this so that we
2114 // don't spend too much time drawing the scrollbar over and over again.
2115 // Basically, up until the first 500 matches there is no throttle.
2116 // After the first 500 matches, we set set the milestone further and
2117 // further out (750, 1125, 1688, 2K, 3K).
2118 static const int startSlowingDownAfter = 500;
2119 static const int slowdown = 750;
2120 int i = (m_lastMatchCount / startSlowingDownAfter);
2121 m_nextInvalidateAfter += i * slowdown;
2123 invalidateArea(InvalidateScrollbar);
2127 void WebFrameImpl::clearPasswordListeners()
2129 deleteAllValues(m_passwordListeners);
2130 m_passwordListeners.clear();
2133 void WebFrameImpl::loadJavaScriptURL(const KURL& url)
2135 // This is copied from ScriptController::executeIfJavaScriptURL.
2136 // Unfortunately, we cannot just use that method since it is private, and
2137 // it also doesn't quite behave as we require it to for bookmarklets. The
2138 // key difference is that we need to suppress loading the string result
2139 // from evaluating the JS URL if executing the JS URL resulted in a
2140 // location change. We also allow a JS URL to be loaded even if scripts on
2141 // the page are otherwise disabled.
2143 if (!m_frame->document() || !m_frame->page())
2146 String script = decodeURLEscapeSequences(url.string().substring(strlen("javascript:")));
2147 ScriptValue result = m_frame->script()->executeScript(script, true);
2149 String scriptResult;
2150 if (!result.getString(scriptResult))
2153 if (!m_frame->redirectScheduler()->locationChangePending())
2154 m_frame->loader()->writer()->replaceDocument(scriptResult);
2157 } // namespace WebKit