Bug 1878401 - part 1 - Pass BrowsingContext to nsIFilePicker::Init instead of mozIDOMWindow r=geckoview-reviewers,win-reviewers,emilio,nika,m_kato,rkraesig

This will improve the situation in bug 1878336

Differential Revision: https://phabricator.services.mozilla.com/D200546
This commit is contained in:
Gregory Pappas 2024-02-28 21:29:37 +00:00
Родитель cbac415c3c
Коммит 91cace0cc3
15 изменённых файлов: 93 добавлений и 88 удалений

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

@ -779,8 +779,8 @@ nsresult HTMLInputElement::InitFilePicker(FilePickerType aType) {
// Get parent nsPIDOMWindow object.
nsCOMPtr<Document> doc = OwnerDoc();
nsCOMPtr<nsPIDOMWindowOuter> win = doc->GetWindow();
if (!win) {
RefPtr<BrowsingContext> bc = doc->GetBrowsingContext();
if (!bc) {
return NS_ERROR_FAILURE;
}
@ -793,15 +793,14 @@ nsresult HTMLInputElement::InitFilePicker(FilePickerType aType) {
nsAutoString okButtonLabel;
if (aType == FILE_PICKER_DIRECTORY) {
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
"DirectoryUpload", OwnerDoc(),
title);
"DirectoryUpload", doc, title);
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
"DirectoryPickerOkButtonLabel",
OwnerDoc(), okButtonLabel);
"DirectoryPickerOkButtonLabel", doc,
okButtonLabel);
} else {
nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
"FileUpload", OwnerDoc(), title);
"FileUpload", doc, title);
}
nsCOMPtr<nsIFilePicker> filePicker =
@ -818,8 +817,7 @@ nsresult HTMLInputElement::InitFilePicker(FilePickerType aType) {
mode = nsIFilePicker::modeOpen;
}
nsresult rv =
filePicker->Init(win, title, mode, OwnerDoc()->GetBrowsingContext());
nsresult rv = filePicker->Init(bc, title, mode);
NS_ENSURE_SUCCESS(rv, rv);
if (!okButtonLabel.IsEmpty()) {

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

@ -1322,8 +1322,20 @@ mozilla::ipc::IPCResult BrowserParent::RecvPDocAccessibleConstructor(
#endif
already_AddRefed<PFilePickerParent> BrowserParent::AllocPFilePickerParent(
const nsString& aTitle, const nsIFilePicker::Mode& aMode) {
return MakeAndAddRef<FilePickerParent>(aTitle, aMode);
const nsString& aTitle, const nsIFilePicker::Mode& aMode,
const MaybeDiscarded<BrowsingContext>& aBrowsingContext) {
RefPtr<CanonicalBrowsingContext> browsingContext =
[&]() -> CanonicalBrowsingContext* {
if (aBrowsingContext.IsNullOrDiscarded()) {
return nullptr;
}
if (!aBrowsingContext.get_canonical()->IsOwnedByProcess(
Manager()->ChildID())) {
return nullptr;
}
return aBrowsingContext.get_canonical();
}();
return MakeAndAddRef<FilePickerParent>(aTitle, aMode, browsingContext);
}
already_AddRefed<PSessionStoreParent>

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

@ -595,7 +595,8 @@ class BrowserParent final : public PBrowserParent,
const Maybe<DoubleTapToZoomMetrics>& aDoubleTapToZoomMetrics);
already_AddRefed<PFilePickerParent> AllocPFilePickerParent(
const nsString& aTitle, const nsIFilePicker::Mode& aMode);
const nsString& aTitle, const nsIFilePicker::Mode& aMode,
const MaybeDiscarded<BrowsingContext>& aBrowsingContext);
bool GetGlobalJSObject(JSContext* cx, JSObject** globalp);

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

@ -219,25 +219,17 @@ void FilePickerParent::Done(nsIFilePicker::ResultCode aResult) {
}
bool FilePickerParent::CreateFilePicker() {
if (!mBrowsingContext) {
return false;
}
mFilePicker = do_CreateInstance("@mozilla.org/filepicker;1");
if (!mFilePicker) {
return false;
}
auto* browserParent = BrowserParent::GetFrom(Manager());
auto* browsingContext = browserParent->GetBrowsingContext();
Element* element = browserParent->GetOwnerElement();
if (!element) {
return false;
}
nsCOMPtr<mozIDOMWindowProxy> window = element->OwnerDoc()->GetWindow();
if (!window) {
return false;
}
return NS_SUCCEEDED(
mFilePicker->Init(window, mTitle, mMode, browsingContext));
return NS_SUCCEEDED(mFilePicker->Init(mBrowsingContext, mTitle, mMode));
}
mozilla::ipc::IPCResult FilePickerParent::RecvOpen(

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

@ -12,6 +12,7 @@
#include "nsCOMArray.h"
#include "nsThreadUtils.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/PFilePickerParent.h"
class nsIFile;
@ -20,8 +21,12 @@ namespace mozilla::dom {
class FilePickerParent : public PFilePickerParent {
public:
FilePickerParent(const nsString& aTitle, const nsIFilePicker::Mode& aMode)
: mTitle(aTitle), mMode(aMode), mResult(nsIFilePicker::returnOK) {}
FilePickerParent(const nsString& aTitle, const nsIFilePicker::Mode& aMode,
BrowsingContext* aBrowsingContext)
: mTitle(aTitle),
mMode(aMode),
mBrowsingContext(aBrowsingContext),
mResult(nsIFilePicker::returnOK) {}
private:
virtual ~FilePickerParent();
@ -93,6 +98,7 @@ class FilePickerParent : public PFilePickerParent {
nsString mTitle;
nsIFilePicker::Mode mMode;
RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext;
nsIFilePicker::ResultCode mResult;
};

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

@ -428,7 +428,7 @@ parent:
*/
async PColorPicker(nsString title, nsString initialColor, nsString[] defaultColors);
async PFilePicker(nsString aTitle, Mode aMode);
async PFilePicker(nsString aTitle, Mode aMode, MaybeDiscardedBrowsingContext aBrowsingContext);
/**
* Tells the containing widget whether the given input block results in a

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

@ -15,14 +15,14 @@ const { debug, warn } = GeckoViewUtils.initLogging("FilePickerDelegate");
export class FilePickerDelegate {
/* ---------- nsIFilePicker ---------- */
init(aParent, aTitle, aMode) {
init(aBrowsingContext, aTitle, aMode) {
if (
aMode === Ci.nsIFilePicker.modeGetFolder ||
aMode === Ci.nsIFilePicker.modeSave
) {
throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
}
this._prompt = new lazy.GeckoViewPrompter(aParent);
this._prompt = new lazy.GeckoViewPrompter(aBrowsingContext);
this._msg = {
type: "file",
title: aTitle,

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

@ -48,11 +48,11 @@ export var MockFilePicker = {
window: null,
pendingPromises: [],
init(window) {
this.window = window;
init(browsingContext) {
this.window = browsingContext.window;
this.reset();
this.factory = newFactory(window);
this.factory = newFactory(this.window);
if (!registrar.isCIDRegistered(newClassID)) {
oldClassID = registrar.contractIDToCID(CONTRACT_ID);
registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);

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

@ -18,6 +18,7 @@
#include "mozilla/dom/File.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/Components.h"
#include "mozilla/StaticPrefs_widget.h"
#include "WidgetUtils.h"
@ -156,17 +157,16 @@ nsBaseFilePicker::nsBaseFilePicker()
nsBaseFilePicker::~nsBaseFilePicker() = default;
NS_IMETHODIMP nsBaseFilePicker::Init(
mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
mozilla::dom::BrowsingContext* aBrowsingContext) {
MOZ_ASSERT(aParent,
"Null parent passed to filepicker, no file "
NS_IMETHODIMP nsBaseFilePicker::Init(BrowsingContext* aBrowsingContext,
const nsAString& aTitle,
nsIFilePicker::Mode aMode) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(aBrowsingContext,
"Null bc passed to filepicker, no file "
"picker for you!");
mParent = nsPIDOMWindowOuter::From(aParent);
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(mParent);
nsCOMPtr<nsIWidget> widget =
aBrowsingContext->Canonical()->GetParentProcessWidgetContaining();
NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
mBrowsingContext = aBrowsingContext;
@ -462,6 +462,8 @@ nsBaseFilePicker::GetOkButtonLabel(nsAString& aLabel) {
NS_IMETHODIMP
nsBaseFilePicker::GetDomFileOrDirectory(nsISupports** aValue) {
MOZ_ASSERT(XRE_IsParentProcess());
NS_ENSURE_ARG_POINTER(mBrowsingContext);
nsCOMPtr<nsIFile> localFile;
nsresult rv = GetFile(getter_AddRefs(localFile));
NS_ENSURE_SUCCESS(rv, rv);
@ -471,7 +473,10 @@ nsBaseFilePicker::GetDomFileOrDirectory(nsISupports** aValue) {
return NS_OK;
}
auto* innerParent = mParent ? mParent->GetCurrentInnerWindow() : nullptr;
auto* innerParent =
mBrowsingContext->GetDOMWindow()
? mBrowsingContext->GetDOMWindow()->GetCurrentInnerWindow()
: nullptr;
if (!innerParent) {
return NS_ERROR_FAILURE;
@ -485,11 +490,19 @@ NS_IMETHODIMP
nsBaseFilePicker::GetDomFileOrDirectoryEnumerator(
nsISimpleEnumerator** aValue) {
nsCOMPtr<nsISimpleEnumerator> iter;
MOZ_ASSERT(XRE_IsParentProcess());
NS_ENSURE_ARG_POINTER(mBrowsingContext);
nsresult rv = GetFiles(getter_AddRefs(iter));
NS_ENSURE_SUCCESS(rv, rv);
auto* parent = mBrowsingContext->GetDOMWindow();
if (!parent) {
return NS_ERROR_FAILURE;
}
RefPtr<nsBaseFilePickerEnumerator> retIter =
new nsBaseFilePickerEnumerator(mParent, iter, mMode);
new nsBaseFilePickerEnumerator(parent, iter, mMode);
retIter.forget(aValue);
return NS_OK;

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

@ -28,9 +28,8 @@ class nsBaseFilePicker : public nsIFilePicker {
nsBaseFilePicker();
virtual ~nsBaseFilePicker();
NS_IMETHOD Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
mozilla::dom::BrowsingContext* aBrowsingContext) override;
NS_IMETHOD Init(mozilla::dom::BrowsingContext* aBrowsingContext,
const nsAString& aTitle, nsIFilePicker::Mode aMode) override;
NS_IMETHOD IsModeSupported(nsIFilePicker::Mode aMode, JSContext* aCx,
mozilla::dom::Promise** aPromise) override;
#ifndef XP_WIN
@ -70,9 +69,6 @@ class nsBaseFilePicker : public nsIFilePicker {
nsCOMPtr<nsIFile> mDisplayDirectory;
nsString mDisplaySpecialDirectory;
nsCOMPtr<nsPIDOMWindowOuter> mParent;
// The BrowsingContext from which the file picker is being opened.
// Used for content analysis.
RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext;
nsIFilePicker::Mode mMode;
nsString mOkButtonLabel;

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

@ -25,19 +25,19 @@ nsFilePickerProxy::nsFilePickerProxy()
nsFilePickerProxy::~nsFilePickerProxy() = default;
NS_IMETHODIMP
nsFilePickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
BrowsingContext* aBrowsingContext) {
BrowserChild* browserChild = BrowserChild::GetFrom(aParent);
nsFilePickerProxy::Init(BrowsingContext* aBrowsingContext,
const nsAString& aTitle, nsIFilePicker::Mode aMode) {
BrowserChild* browserChild =
BrowserChild::GetFrom(aBrowsingContext->GetDocShell());
if (!browserChild) {
return NS_ERROR_FAILURE;
}
mParent = nsPIDOMWindowOuter::From(aParent);
mBrowsingContext = aBrowsingContext;
mMode = aMode;
browserChild->SendPFilePickerConstructor(this, aTitle, aMode);
browserChild->SendPFilePickerConstructor(this, aTitle, aMode,
aBrowsingContext);
mIPCActive = true;
return NS_OK;
@ -155,8 +155,9 @@ nsFilePickerProxy::Close() {
mozilla::ipc::IPCResult nsFilePickerProxy::Recv__delete__(
const MaybeInputData& aData, const nsIFilePicker::ResultCode& aResult) {
nsPIDOMWindowInner* inner =
mParent ? mParent->GetCurrentInnerWindow() : nullptr;
auto* inner = mBrowsingContext->GetDOMWindow()
? mBrowsingContext->GetDOMWindow()->GetCurrentInnerWindow()
: nullptr;
if (NS_WARN_IF(!inner)) {
return IPC_OK();

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

@ -34,9 +34,8 @@ class nsFilePickerProxy : public nsBaseFilePicker,
NS_DECL_ISUPPORTS
// nsIFilePicker (less what's in nsBaseFilePicker)
NS_IMETHOD Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
mozilla::dom::BrowsingContext* aBrowsingContext) override;
NS_IMETHOD Init(mozilla::dom::BrowsingContext* aBrowsingContext,
const nsAString& aTitle, nsIFilePicker::Mode aMode) override;
NS_IMETHOD AppendFilter(const nsAString& aTitle,
const nsAString& aFilter) override;
NS_IMETHOD GetCapture(nsIFilePicker::CaptureTarget* aCapture) override;

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

@ -66,19 +66,13 @@ interface nsIFilePicker : nsISupports
* Initialize the file picker widget. The file picker is not valid until this
* method is called.
*
* @param parent mozIDOMWindow parent. This dialog will be dependent
* on this parent. parent must be non-null.
* @param title The title for the file widget
* @param mode load, save, or get folder
* @param browsingContext [optional]
* The context in which the file picker is being shown. This is
* used for content analysis and can be omitted if chrome is
* showing the file picker.
* @param browsingContext The context in which the file picker is being
* shown, must be non-null.
* @param title The title for the file widget
* @param mode load, save, or get folder
*
*/
void init(in mozIDOMWindowProxy parent,
in AString title,
in nsIFilePicker_Mode mode,
[optional] in BrowsingContext browsingContext);
void init(in BrowsingContext browsingContext, in AString title, in nsIFilePicker_Mode mode);
/**
* Returns a Promise that resolves to true if the passed nsIFilePicker mode

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

@ -94,19 +94,14 @@ nsFilePicker::nsFilePicker() : mSelectedType(1) {}
NS_IMPL_ISUPPORTS(nsFilePicker, nsIFilePicker)
NS_IMETHODIMP nsFilePicker::Init(
mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
mozilla::dom::BrowsingContext* aBrowsingContext) {
mozilla::dom::BrowsingContext* aBrowsingContext, const nsAString& aTitle,
nsIFilePicker::Mode aMode) {
// Don't attempt to open a real file-picker in headless mode.
if (gfxPlatform::IsHeadless()) {
return nsresult::NS_ERROR_NOT_AVAILABLE;
}
nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aParent);
nsIDocShell* docShell = window ? window->GetDocShell() : nullptr;
mLoadContext = do_QueryInterface(docShell);
return nsBaseFilePicker::Init(aParent, aTitle, aMode, aBrowsingContext);
return nsBaseFilePicker::Init(aBrowsingContext, aTitle, aMode);
}
namespace mozilla::detail {
@ -1053,7 +1048,7 @@ void nsFilePicker::RememberLastUsedDirectory() {
}
bool nsFilePicker::IsPrivacyModeEnabled() {
return mLoadContext && mLoadContext->UsePrivateBrowsing();
return mBrowsingContext && mBrowsingContext->UsePrivateBrowsing();
}
bool nsFilePicker::IsDefaultPathLink() {

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

@ -66,9 +66,8 @@ class nsFilePicker final : public nsBaseWinFilePicker {
public:
nsFilePicker();
NS_IMETHOD Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle,
nsIFilePicker::Mode aMode,
mozilla::dom::BrowsingContext* aBrowsingContext) override;
NS_IMETHOD Init(mozilla::dom::BrowsingContext* aBrowsingContext,
const nsAString& aTitle, nsIFilePicker::Mode aMode) override;
NS_DECL_ISUPPORTS
@ -117,7 +116,6 @@ class nsFilePicker final : public nsBaseWinFilePicker {
bool IsDefaultPathLink();
bool IsDefaultPathHtml();
nsCOMPtr<nsILoadContext> mLoadContext;
nsCOMPtr<nsIWidget> mParentWidget;
nsString mTitle;
nsCString mFile;