Bug 1653219 - Use BrowserTabEnumerator to enumerate tabs; r=ng

Differential Revision: https://phabricator.services.mozilla.com/D84592
This commit is contained in:
Dan Minor 2020-08-10 15:57:59 +00:00
Родитель fd59610aaf
Коммит 2db7834f68
1 изменённых файлов: 46 добавлений и 46 удалений

Просмотреть файл

@ -24,23 +24,23 @@
#include "rtc_base/logging.h"
#include "rtc_base/scoped_ref_ptr.h"
#include "nsThreadUtils.h"
#include "nsIBrowserWindowTracker.h"
#include "nsIDocShellTreeOwner.h"
#include "nsImportModule.h"
#include "mozilla/dom/BrowserHost.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/ImageBitmapBinding.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/StaticPrefs_media.h"
#include "MediaUtils.h"
mozilla::LazyLogModule gTabShareLog("TabShare");
using namespace mozilla::dom;
// XXX switch once we have UI
#define NO_TABSHARE_UI 1
namespace mozilla {
TabCapturer::TabCapturer(const webrtc::DesktopCaptureOptions& options)
@ -65,39 +65,6 @@ bool TabCapturer::FocusOnSelectedSource() { return true; }
nsresult TabCapturer::StartRunnable::Run() {
MOZ_ASSERT(NS_IsMainThread());
// We use BrowserId to identify a tab since it's the only stable id; we
// need the top-level browserid for a tab.
#ifdef NO_TABSHARE_UI
// XXX Since we have no UI to feed us a browserid, grab "a" browserid for a
// tab. This is a temporary hack until we have UI (bug 1646597)
nsCOMPtr<nsPIDOMWindowOuter> chromeWindow =
nsContentUtils::GetMostRecentNonPBWindow();
if (!chromeWindow) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIDocShell> docShell = chromeWindow->GetDocShell();
if (!docShell) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIDocShellTreeOwner> owner;
docShell->GetTreeOwner(getter_AddRefs(owner));
if (!owner) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIRemoteTab> primaryRemoteTab;
owner->GetPrimaryRemoteTab(getter_AddRefs(primaryRemoteTab));
if (!primaryRemoteTab) {
return NS_ERROR_FAILURE;
}
RefPtr<BrowsingContext> context =
BrowserHost::GetFrom(primaryRemoteTab)->GetBrowsingContext();
if (!context) {
return NS_ERROR_FAILURE;
}
mVideoSource->mBrowserId = context->BrowserId();
#endif
MOZ_LOG(gTabShareLog, LogLevel::Debug,
("TabShare: Start, id=%" PRIu64, mVideoSource->mBrowserId));
@ -255,16 +222,49 @@ webrtc::DesktopCapturer::CreateRawTabCapturer(
}
void webrtc::DesktopDeviceInfoImpl::InitializeTabList() {
if (mozilla::StaticPrefs::media_getusermedia_browser_enabled()) {
DesktopTab* desktop_tab = new DesktopTab;
if (desktop_tab) {
desktop_tab->setTabBrowserId(0);
desktop_tab->setTabName("dummy tab");
desktop_tab->setUniqueIdName("dummy tab 0");
desktop_tab->setTabCount(1);
desktop_tab_list_[desktop_tab->getTabBrowserId()] = desktop_tab;
}
if (!mozilla::StaticPrefs::media_getusermedia_browser_enabled()) {
return;
}
// This is a sync dispatch to main thread, which is unfortunate. To
// call JavaScript we have to be on main thread, but the remaining
// DesktopCapturer very much wants to be off main thread. This might
// be solvable by calling this method earlier on while we're still on
// main thread and plumbing the information down to here.
NS_DispatchToMainThread(
mozilla::media::NewRunnableFrom([this]() -> nsresult {
nsresult rv;
nsCOMPtr<nsIBrowserWindowTracker> bwt =
do_ImportModule("resource:///modules/BrowserWindowTracker.jsm",
"BrowserWindowTracker", &rv);
if (NS_SUCCEEDED(rv)) {
nsTArray<RefPtr<nsIVisibleTab>> tabArray;
rv = bwt->GetAllVisibleTabs(tabArray);
if (NS_SUCCEEDED(rv)) {
for (const auto& browserTab : tabArray) {
nsString contentTitle;
browserTab->GetContentTitle(contentTitle);
int64_t browserId;
browserTab->GetBrowserId(&browserId);
DesktopTab* desktopTab = new DesktopTab;
if (desktopTab) {
char* contentTitleUTF8 = ToNewUTF8String(contentTitle);
desktopTab->setTabBrowserId(browserId);
desktopTab->setTabName(contentTitleUTF8);
std::ostringstream uniqueId;
uniqueId << browserId;
desktopTab->setUniqueIdName(uniqueId.str().c_str());
desktop_tab_list_[desktopTab->getTabBrowserId()] = desktopTab;
free(contentTitleUTF8);
}
}
}
}
return NS_OK;
}),
nsIEventTarget::DISPATCH_SYNC);
}
} // namespace webrtc