/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsWebBrowserFind.h" // Only need this for NS_FIND_CONTRACTID, // else we could use nsIDOMRange.h and nsIFind.h. #include "nsFind.h" #include "nsIComponentManager.h" #include "nsIScriptSecurityManager.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" #include "nsPIDOMWindow.h" #include "nsIURI.h" #include "nsIDocShell.h" #include "nsIPresShell.h" #include "nsPresContext.h" #include "nsIDocument.h" #include "nsIDOMDocument.h" #include "nsISelectionController.h" #include "nsISelection.h" #include "nsIFrame.h" #include "nsITextControlFrame.h" #include "nsReadableUtils.h" #include "nsIDOMHTMLElement.h" #include "nsIDOMHTMLDocument.h" #include "nsIContent.h" #include "nsContentCID.h" #include "nsIServiceManager.h" #include "nsIObserverService.h" #include "nsISupportsPrimitives.h" #include "nsFind.h" #include "nsError.h" #include "nsFocusManager.h" #include "mozilla/Services.h" #include "mozilla/dom/Element.h" #include "nsISimpleEnumerator.h" #include "nsContentUtils.h" #if DEBUG #include "nsIWebNavigation.h" #include "nsXPIDLString.h" #endif nsWebBrowserFind::nsWebBrowserFind() : mFindBackwards(false) , mWrapFind(false) , mEntireWord(false) , mMatchCase(false) , mSearchSubFrames(true) , mSearchParentFrames(true) { } nsWebBrowserFind::~nsWebBrowserFind() { } NS_IMPL_ISUPPORTS(nsWebBrowserFind, nsIWebBrowserFind, nsIWebBrowserFindInFrames) NS_IMETHODIMP nsWebBrowserFind::FindNext(bool* aResult) { NS_ENSURE_ARG_POINTER(aResult); *aResult = false; NS_ENSURE_TRUE(CanFindNext(), NS_ERROR_NOT_INITIALIZED); nsresult rv = NS_OK; nsCOMPtr searchFrame = do_QueryReferent(mCurrentSearchFrame); NS_ENSURE_TRUE(searchFrame, NS_ERROR_NOT_INITIALIZED); nsCOMPtr rootFrame = do_QueryReferent(mRootSearchFrame); NS_ENSURE_TRUE(rootFrame, NS_ERROR_NOT_INITIALIZED); // first, if there's a "cmd_findagain" observer around, check to see if it // wants to perform the find again command . If it performs the find again // it will return true, in which case we exit ::FindNext() early. // Otherwise, nsWebBrowserFind needs to perform the find again command itself // this is used by nsTypeAheadFind, which controls find again when it was // the last executed find in the current window. nsCOMPtr observerSvc = mozilla::services::GetObserverService(); if (observerSvc) { nsCOMPtr windowSupportsData = do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr searchWindowSupports = do_QueryInterface(rootFrame); windowSupportsData->SetData(searchWindowSupports); NS_NAMED_LITERAL_STRING(dnStr, "down"); NS_NAMED_LITERAL_STRING(upStr, "up"); observerSvc->NotifyObservers(windowSupportsData, "nsWebBrowserFind_FindAgain", mFindBackwards ? upStr.get() : dnStr.get()); windowSupportsData->GetData(getter_AddRefs(searchWindowSupports)); // findnext performed if search window data cleared out *aResult = searchWindowSupports == nullptr; if (*aResult) { return NS_OK; } } // next, look in the current frame. If found, return. // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } // if we are not searching other frames, return if (!mSearchSubFrames && !mSearchParentFrames) { return NS_OK; } nsIDocShell* rootDocShell = rootFrame->GetDocShell(); if (!rootDocShell) { return NS_ERROR_FAILURE; } int32_t enumDirection = mFindBackwards ? nsIDocShell::ENUMERATE_BACKWARDS : nsIDocShell::ENUMERATE_FORWARDS; nsCOMPtr docShellEnumerator; rv = rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeAll, enumDirection, getter_AddRefs(docShellEnumerator)); if (NS_FAILED(rv)) { return rv; } // remember where we started nsCOMPtr startingItem = do_QueryInterface(searchFrame->GetDocShell(), &rv); if (NS_FAILED(rv)) { return rv; } nsCOMPtr curItem; // XXX We should avoid searching in frameset documents here. // We also need to honour mSearchSubFrames and mSearchParentFrames. bool hasMore, doFind = false; while (NS_SUCCEEDED(docShellEnumerator->HasMoreElements(&hasMore)) && hasMore) { nsCOMPtr curSupports; rv = docShellEnumerator->GetNext(getter_AddRefs(curSupports)); if (NS_FAILED(rv)) { break; } curItem = do_QueryInterface(curSupports, &rv); if (NS_FAILED(rv)) { break; } if (doFind) { searchFrame = curItem->GetWindow(); if (!searchFrame) { break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } OnEndSearchFrame(searchFrame); } if (curItem.get() == startingItem.get()) { doFind = true; // start looking in frames after this one } } if (!mWrapFind) { // remember where we left off SetCurrentSearchFrame(searchFrame); return NS_OK; } // From here on, we're wrapping, first through the other frames, then finally // from the beginning of the starting frame back to the starting point. // because nsISimpleEnumerator is totally lame and isn't resettable, I have to // make a new one docShellEnumerator = nullptr; rv = rootDocShell->GetDocShellEnumerator(nsIDocShellTreeItem::typeAll, enumDirection, getter_AddRefs(docShellEnumerator)); if (NS_FAILED(rv)) { return rv; } while (NS_SUCCEEDED(docShellEnumerator->HasMoreElements(&hasMore)) && hasMore) { nsCOMPtr curSupports; rv = docShellEnumerator->GetNext(getter_AddRefs(curSupports)); if (NS_FAILED(rv)) { break; } curItem = do_QueryInterface(curSupports, &rv); if (NS_FAILED(rv)) { break; } searchFrame = curItem->GetWindow(); if (!searchFrame) { rv = NS_ERROR_FAILURE; break; } if (curItem.get() == startingItem.get()) { // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, true, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } break; } OnStartSearchFrame(searchFrame); // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. rv = SearchInFrame(searchFrame, false, aResult); if (NS_FAILED(rv)) { return rv; } if (*aResult) { return OnFind(searchFrame); // we are done } OnEndSearchFrame(searchFrame); } // remember where we left off SetCurrentSearchFrame(searchFrame); NS_ASSERTION(NS_SUCCEEDED(rv), "Something failed"); return rv; } NS_IMETHODIMP nsWebBrowserFind::GetSearchString(char16_t** aSearchString) { NS_ENSURE_ARG_POINTER(aSearchString); *aSearchString = ToNewUnicode(mSearchString); return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetSearchString(const char16_t* aSearchString) { mSearchString.Assign(aSearchString); return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetFindBackwards(bool* aFindBackwards) { NS_ENSURE_ARG_POINTER(aFindBackwards); *aFindBackwards = mFindBackwards; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetFindBackwards(bool aFindBackwards) { mFindBackwards = aFindBackwards; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetWrapFind(bool* aWrapFind) { NS_ENSURE_ARG_POINTER(aWrapFind); *aWrapFind = mWrapFind; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetWrapFind(bool aWrapFind) { mWrapFind = aWrapFind; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetEntireWord(bool* aEntireWord) { NS_ENSURE_ARG_POINTER(aEntireWord); *aEntireWord = mEntireWord; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetEntireWord(bool aEntireWord) { mEntireWord = aEntireWord; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::GetMatchCase(bool* aMatchCase) { NS_ENSURE_ARG_POINTER(aMatchCase); *aMatchCase = mMatchCase; return NS_OK; } NS_IMETHODIMP nsWebBrowserFind::SetMatchCase(bool aMatchCase) { mMatchCase = aMatchCase; return NS_OK; } static bool IsInNativeAnonymousSubtree(nsIContent* aContent) { while (aContent) { nsIContent* bindingParent = aContent->GetBindingParent(); if (bindingParent == aContent) { return true; } aContent = bindingParent; } return false; } void nsWebBrowserFind::SetSelectionAndScroll(nsPIDOMWindowOuter* aWindow, nsIDOMRange* aRange) { nsCOMPtr doc = aWindow->GetDoc(); if (!doc) { return; } nsIPresShell* presShell = doc->GetShell(); if (!presShell) { return; } nsCOMPtr node; aRange->GetStartContainer(getter_AddRefs(node)); nsCOMPtr content(do_QueryInterface(node)); nsIFrame* frame = content->GetPrimaryFrame(); if (!frame) { return; } nsCOMPtr selCon; frame->GetSelectionController(presShell->GetPresContext(), getter_AddRefs(selCon)); // since the match could be an anonymous textnode inside a //