Bug 1651705: Part 7 - Update MediaSessionSupport to work with NativeWeakPtr; r=geckoview-reviewers,agi

This patch is similar to part 4 but for MediaSessionSupport.

Conversions over to `NativeWeakPtr` are pretty straight forward thanks to the
type system. Basically we take a `NativeWeakPtr`, call `Access()` on it, and
if the accessor is truthy, then we call whatever methods we need to call.

Creation of new pointers is done using `NativeWeakPtrHolder::Attach()` and
detaching of strong references is done by `NativeWeakPtr::Detach()`.

Differential Revision: https://phabricator.services.mozilla.com/D88088
This commit is contained in:
Aaron Klotz 2020-09-21 21:49:02 +00:00
parent ee56e6a4b2
commit fd928bd229
3 changed files with 62 additions and 20 deletions

View File

@ -89,6 +89,14 @@ class GeckoViewSupport final
void PassExternalResponse(java::WebResponse::Param aResponse);
void AttachMediaSessionController(
const java::GeckoSession::Window::LocalRef& inst,
jni::Object::Param aController, const int64_t aId);
void DetachMediaSessionController(
const java::GeckoSession::Window::LocalRef& inst,
jni::Object::Param aController);
void OnWeakNonIntrusiveDetach(already_AddRefed<Runnable> aDisposer) {
RefPtr<Runnable> disposer(aDisposer);
disposer->Run();

View File

@ -175,15 +175,14 @@ namespace widget {
using WindowPtr = jni::NativeWeakPtr<GeckoViewSupport>;
class nsWindow::MediaSessionSupport final
class MediaSessionSupport final
: public mozilla::java::MediaSession::Controller::Natives<
MediaSessionSupport> {
using LockedWindowPtr = WindowPtr<MediaSessionSupport>::Locked;
using MediaKeysArray = nsTArray<MediaControlKey>;
typedef RefPtr<mozilla::dom::MediaController> ControllerPtr;
WindowPtr<MediaSessionSupport> mWindow;
WindowPtr mWindow;
mozilla::java::MediaSession::Controller::WeakRef mJavaController;
ControllerPtr mMediaController;
MediaEventListener mMetadataChangedListener;
@ -196,18 +195,30 @@ class nsWindow::MediaSessionSupport final
using Base::DisposeNative;
MediaSessionSupport(
NativePtr<MediaSessionSupport>* aPtr, nsWindow* aWindow,
WindowPtr aWindow,
const java::MediaSession::Controller::LocalRef& aController)
: mWindow(aPtr, aWindow),
: mWindow(aWindow),
mJavaController(aController),
mMediaController(nullptr) {
MOZ_ASSERT(mWindow);
#if defined(DEBUG)
auto win(mWindow.Access());
MOZ_ASSERT(!!win);
#endif // defined(DEBUG)
}
bool Dispatch(const char16_t aType[],
java::GeckoBundle::Param aBundle = nullptr) {
widget::EventDispatcher* dispatcher = mWindow->GetEventDispatcher();
auto win = mWindow.Access();
if (!win) {
return false;
}
nsWindow* gkWindow = win->GetNsWindow();
if (!gkWindow) {
return false;
}
widget::EventDispatcher* dispatcher = gkWindow->GetEventDispatcher();
if (!dispatcher) {
return false;
}
@ -335,7 +346,7 @@ class nsWindow::MediaSessionSupport final
}
}
void OnDetach(already_AddRefed<Runnable> aDisposer) {
void OnWeakNonIntrusiveDetach(already_AddRefed<Runnable> aDisposer) {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<Runnable> disposer = aDisposer;
@ -513,10 +524,6 @@ class nsWindow::MediaSessionSupport final
}
};
template <>
const char nsWindow::NativePtr<nsWindow::MediaSessionSupport>::sName[] =
"MediaSessionSupport";
/**
* PanZoomController handles its native calls on the UI thread, so make
* it separate from GeckoViewSupport.
@ -1557,11 +1564,6 @@ GeckoViewSupport::~GeckoViewSupport() {
if (mWindow) {
mWindow->DetachNatives();
}
if (window.mMediaSessionSupport) {
window.mMediaSessionSupport.Detach(
window.mMediaSessionSupport->GetJavaController());
}
}
/* static */
@ -1771,6 +1773,36 @@ void GeckoViewSupport::PassExternalResponse(
response] { window->PassExternalWebResponse(response); });
}
void GeckoViewSupport::AttachMediaSessionController(
const GeckoSession::Window::LocalRef& inst, jni::Object::Param aController,
const int64_t aId) {
auto controller = java::MediaSession::Controller::LocalRef(
jni::GetGeckoThreadEnv(),
java::MediaSession::Controller::Ref::From(aController));
mWindow->mMediaSessionSupport =
jni::NativeWeakPtrHolder<MediaSessionSupport>::Attach(
controller, mWindow->mGeckoViewSupport, controller);
RefPtr<BrowsingContext> bc = BrowsingContext::Get(aId);
RefPtr<dom::MediaController> nativeController =
bc->Canonical()->GetMediaController();
MOZ_ASSERT(nativeController);
if (auto acc = mWindow->mMediaSessionSupport.Access()) {
acc->SetNativeController(nativeController);
}
DispatchToUiThread("GeckoViewSupport::AttachMediaSessionController",
[controller = java::MediaSession::Controller::GlobalRef(
controller)] { controller->OnAttached(); });
}
void GeckoViewSupport::DetachMediaSessionController(
const GeckoSession::Window::LocalRef& inst,
jni::Object::Param aController) {
mWindow->mMediaSessionSupport.Detach();
}
} // namespace widget
} // namespace mozilla
@ -1778,8 +1810,8 @@ void nsWindow::InitNatives() {
jni::InitConversionStatics();
mozilla::widget::GeckoViewSupport::Base::Init();
mozilla::widget::LayerViewSupport::Init();
mozilla::widget::MediaSessionSupport::Init();
mozilla::widget::NPZCSupport::Init();
nsWindow::MediaSessionSupport::Init();
mozilla::widget::GeckoEditableSupport::Init();
a11y::SessionAccessibility::Init();
@ -1791,6 +1823,7 @@ void nsWindow::DetachNatives() {
mNPZCSupport.Detach();
mLayerViewSupport.Detach();
mSessionAccessibility.Detach();
mMediaSessionSupport.Detach();
}
/* static */

View File

@ -38,6 +38,7 @@ class AndroidView;
class GeckoEditableSupport;
class GeckoViewSupport;
class LayerViewSupport;
class MediaSessionSupport;
class NPZCSupport;
} // namespace widget
@ -95,8 +96,8 @@ class nsWindow final : public nsBaseWidget {
mozilla::jni::NativeWeakPtr<mozilla::a11y::SessionAccessibility>
mSessionAccessibility;
class MediaSessionSupport;
NativePtr<MediaSessionSupport> mMediaSessionSupport;
mozilla::jni::NativeWeakPtr<mozilla::widget::MediaSessionSupport>
mMediaSessionSupport;
// Object that implements native GeckoView calls and associated states.
// nullptr for nsWindows that were not opened from GeckoView.