Skip to content

Commit

Permalink
Bug 1813148 - Don't return already_AddRefed in nsPresContext::GetRoot…
Browse files Browse the repository at this point in the history
…Widget. r=dholbert

Let the caller addref it if needed.

I wrote this because I wanted to make some code dealing with it
thread-safe, but I ended up writing a less sketchy solution. However I
still think this is worth it.

It seems this only returns an already_AddRefed because before it used to
be an XPCOM-ish thing where the widget was returned as an out-param.

For now it doesn't change behavior but there are some callers that would
benefit from having less addref/release calls if they only need to read
simple stuff from the widget.

Differential Revision: https://phabricator.services.mozilla.com/D168141
  • Loading branch information
emilio committed Jan 27, 2023
1 parent 11c961f commit 61b953b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
5 changes: 4 additions & 1 deletion dom/events/TextComposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class TextComposition final {
TextRangeArray* GetRanges() const { return mRanges; }
// Returns the widget which is proper to call NotifyIME().
already_AddRefed<nsIWidget> GetWidget() const {
return mPresContext ? mPresContext->GetRootWidget() : nullptr;
if (!mPresContext) {
return nullptr;
}
return do_AddRef(mPresContext->GetRootWidget());
}
// Returns the tab parent which has this composition in its remote process.
BrowserParent* GetBrowserParent() const { return mBrowserParent; }
Expand Down
2 changes: 1 addition & 1 deletion dom/ipc/BrowserParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ already_AddRefed<nsPIDOMWindowOuter> BrowserParent::GetParentWindowOuter() {
already_AddRefed<nsIWidget> BrowserParent::GetTopLevelWidget() {
if (RefPtr<Element> element = mFrameElement) {
if (PresShell* presShell = element->OwnerDoc()->GetPresShell()) {
return presShell->GetViewManager()->GetRootWidget();
return do_AddRef(presShell->GetViewManager()->GetRootWidget());
}
}
return nullptr;
Expand Down
3 changes: 1 addition & 2 deletions layout/base/nsPresContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,13 +1167,12 @@ nsIWidget* nsPresContext::GetNearestWidget(nsPoint* aOffset) {
return rootView->GetNearestWidget(aOffset);
}

already_AddRefed<nsIWidget> nsPresContext::GetRootWidget() const {
nsIWidget* nsPresContext::GetRootWidget() const {
NS_ENSURE_TRUE(mPresShell, nullptr);
nsViewManager* vm = mPresShell->GetViewManager();
if (!vm) {
return nullptr;
}

return vm->GetRootWidget();
}

Expand Down
4 changes: 2 additions & 2 deletions layout/base/nsPresContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
/**
* Returns the root widget for this.
*/
already_AddRefed<nsIWidget> GetRootWidget() const;
nsIWidget* GetRootWidget() const;

/**
* Returns the widget which may have native focus and handles text input
* like keyboard input, IME, etc.
*/
already_AddRefed<nsIWidget> GetTextInputHandlingWidget() const {
nsIWidget* GetTextInputHandlingWidget() const {
// Currently, root widget for each PresContext handles text input.
return GetRootWidget();
}
Expand Down
19 changes: 10 additions & 9 deletions view/nsViewManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,16 +895,17 @@ void nsViewManager::DecrementDisableRefreshCount() {
NS_ASSERTION(mRefreshDisableCount >= 0, "Invalid refresh disable count!");
}

already_AddRefed<nsIWidget> nsViewManager::GetRootWidget() {
nsCOMPtr<nsIWidget> rootWidget;
if (mRootView) {
if (mRootView->HasWidget()) {
rootWidget = mRootView->GetWidget();
} else if (mRootView->GetParent()) {
rootWidget = mRootView->GetParent()->GetViewManager()->GetRootWidget();
}
nsIWidget* nsViewManager::GetRootWidget() const {
if (!mRootView) {
return nullptr;
}
if (mRootView->HasWidget()) {
return mRootView->GetWidget();
}
if (mRootView->GetParent()) {
return mRootView->GetParent()->GetViewManager()->GetRootWidget();
}
return rootWidget.forget();
return nullptr;
}

LayoutDeviceIntRect nsViewManager::ViewToWidget(nsView* aView,
Expand Down
2 changes: 1 addition & 1 deletion view/nsViewManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class nsViewManager final {
* Retrieve the widget at the root of the nearest enclosing
* view manager whose root view has a widget.
*/
already_AddRefed<nsIWidget> GetRootWidget();
nsIWidget* GetRootWidget() const;

/**
* Indicate whether the viewmanager is currently painting
Expand Down

0 comments on commit 61b953b

Please sign in to comment.