Bug 1932512 - Pass BrowsingContext to nsIColorPicker::Init; r=geckoview-reviewers,win-reviewers,mac-reviewers,emilio,spohl,m_kato,rkraesig

Differential Revision: https://phabricator.services.mozilla.com/D229682
This commit is contained in:
Edgar Chen
2024-11-25 14:03:25 +00:00
parent 4d1ec66bf7
commit 997fb832b3
14 changed files with 79 additions and 49 deletions

View File

@@ -736,8 +736,8 @@ nsresult HTMLInputElement::InitColorPicker() {
nsCOMPtr<Document> doc = OwnerDoc(); nsCOMPtr<Document> doc = OwnerDoc();
nsCOMPtr<nsPIDOMWindowOuter> win = doc->GetWindow(); RefPtr<BrowsingContext> bc = doc->GetBrowsingContext();
if (!win) { if (!bc) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
@@ -759,7 +759,7 @@ nsresult HTMLInputElement::InitColorPicker() {
nsAutoString initialValue; nsAutoString initialValue;
GetNonFileValueInternal(initialValue); GetNonFileValueInternal(initialValue);
nsTArray<nsString> colors = GetColorsFromList(); nsTArray<nsString> colors = GetColorsFromList();
nsresult rv = colorPicker->Init(win, title, initialValue, colors); nsresult rv = colorPicker->Init(bc, title, initialValue, colors);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIColorPickerShownCallback> callback = nsCOMPtr<nsIColorPickerShownCallback> callback =

View File

@@ -3522,10 +3522,22 @@ BrowserParent::GetAuthPrompt(uint32_t aPromptReason, const nsIID& iid,
} }
already_AddRefed<PColorPickerParent> BrowserParent::AllocPColorPickerParent( already_AddRefed<PColorPickerParent> BrowserParent::AllocPColorPickerParent(
const MaybeDiscarded<BrowsingContext>& aBrowsingContext,
const nsString& aTitle, const nsString& aInitialColor, const nsString& aTitle, const nsString& aInitialColor,
const nsTArray<nsString>& aDefaultColors) { const nsTArray<nsString>& aDefaultColors) {
return MakeAndAddRef<ColorPickerParent>(aTitle, aInitialColor, RefPtr<CanonicalBrowsingContext> browsingContext =
aDefaultColors); [&]() -> CanonicalBrowsingContext* {
if (aBrowsingContext.IsNullOrDiscarded()) {
return nullptr;
}
if (!aBrowsingContext.get_canonical()->IsOwnedByProcess(
Manager()->ChildID())) {
return nullptr;
}
return aBrowsingContext.get_canonical();
}();
return MakeAndAddRef<ColorPickerParent>(browsingContext, aTitle,
aInitialColor, aDefaultColors);
} }
already_AddRefed<nsFrameLoader> BrowserParent::GetFrameLoader( already_AddRefed<nsFrameLoader> BrowserParent::GetFrameLoader(

View File

@@ -426,6 +426,7 @@ class BrowserParent final : public PBrowserParent,
const int32_t& aAppUnitsPerDevPixel); const int32_t& aAppUnitsPerDevPixel);
already_AddRefed<PColorPickerParent> AllocPColorPickerParent( already_AddRefed<PColorPickerParent> AllocPColorPickerParent(
const MaybeDiscarded<BrowsingContext>& aBrowsingContext,
const nsString& aTitle, const nsString& aInitialColor, const nsString& aTitle, const nsString& aInitialColor,
const nsTArray<nsString>& aDefaultColors); const nsTArray<nsString>& aDefaultColors);

View File

@@ -38,23 +38,17 @@ void ColorPickerParent::ColorPickerShownCallback::Destroy() {
} }
bool ColorPickerParent::CreateColorPicker() { bool ColorPickerParent::CreateColorPicker() {
if (!mBrowsingContext) {
return false;
}
mPicker = do_CreateInstance("@mozilla.org/colorpicker;1"); mPicker = do_CreateInstance("@mozilla.org/colorpicker;1");
if (!mPicker) { if (!mPicker) {
return false; return false;
} }
Element* ownerElement = BrowserParent::GetFrom(Manager())->GetOwnerElement();
if (!ownerElement) {
return false;
}
nsCOMPtr<nsPIDOMWindowOuter> window = ownerElement->OwnerDoc()->GetWindow();
if (!window) {
return false;
}
return NS_SUCCEEDED( return NS_SUCCEEDED(
mPicker->Init(window, mTitle, mInitialColor, mDefaultColors)); mPicker->Init(mBrowsingContext, mTitle, mInitialColor, mDefaultColors));
} }
mozilla::ipc::IPCResult ColorPickerParent::RecvOpen() { mozilla::ipc::IPCResult ColorPickerParent::RecvOpen() {

View File

@@ -14,9 +14,11 @@ namespace mozilla::dom {
class ColorPickerParent : public PColorPickerParent { class ColorPickerParent : public PColorPickerParent {
public: public:
ColorPickerParent(const nsString& aTitle, const nsString& aInitialColor, ColorPickerParent(BrowsingContext* aBrowsingContext, const nsString& aTitle,
const nsString& aInitialColor,
const nsTArray<nsString>& aDefaultColors) const nsTArray<nsString>& aDefaultColors)
: mTitle(aTitle), : mBrowsingContext(aBrowsingContext),
mTitle(aTitle),
mInitialColor(aInitialColor), mInitialColor(aInitialColor),
mDefaultColors(aDefaultColors.Clone()) {} mDefaultColors(aDefaultColors.Clone()) {}
@@ -45,6 +47,7 @@ class ColorPickerParent : public PColorPickerParent {
bool CreateColorPicker(); bool CreateColorPicker();
RefPtr<BrowsingContext> mBrowsingContext;
RefPtr<ColorPickerShownCallback> mCallback; RefPtr<ColorPickerShownCallback> mCallback;
nsCOMPtr<nsIColorPicker> mPicker; nsCOMPtr<nsIColorPicker> mPicker;

View File

@@ -413,7 +413,9 @@ parent:
* Create an asynchronous color picker on the parent side, * Create an asynchronous color picker on the parent side,
* but don't open it yet. * but don't open it yet.
*/ */
async PColorPicker(nsString title, nsString initialColor, nsString[] defaultColors); async PColorPicker(MaybeDiscardedBrowsingContext aBrowsingContext,
nsString title, nsString initialColor,
nsString[] defaultColors);
async PFilePicker(nsString aTitle, Mode aMode, MaybeDiscardedBrowsingContext aBrowsingContext); async PFilePicker(nsString aTitle, Mode aMode, MaybeDiscardedBrowsingContext aBrowsingContext);

View File

@@ -47,3 +47,8 @@
# undef False # undef False
# define X11False 0 # define X11False 0
#endif #endif
#ifdef DestroyAll
# undef DestroyAll
# define X11DestroyAll 0
#endif

View File

@@ -14,8 +14,8 @@ const { debug, warn } = GeckoViewUtils.initLogging("ColorPickerDelegate");
export class ColorPickerDelegate { export class ColorPickerDelegate {
// TODO(bug 1805397): Implement default colors // TODO(bug 1805397): Implement default colors
init(aParent, aTitle, aInitialColor, aDefaultColors) { init(aBrowsingContext, aTitle, aInitialColor, aDefaultColors) {
this._prompt = new lazy.GeckoViewPrompter(aParent); this._prompt = new lazy.GeckoViewPrompter(aBrowsingContext);
this._msg = { this._msg = {
type: "color", type: "color",
title: aTitle, title: aTitle,

View File

@@ -70,13 +70,13 @@ function MockColorPickerInstance(window) {
} }
MockColorPickerInstance.prototype = { MockColorPickerInstance.prototype = {
QueryInterface: ChromeUtils.generateQI(["nsIColorPicker"]), QueryInterface: ChromeUtils.generateQI(["nsIColorPicker"]),
init(aParent, aTitle, aInitialColor, aDefaultColors) { init(aBrowsingContext, aTitle, aInitialColor, aDefaultColors) {
this.parent = aParent; this.browsingContext = aBrowsingContext;
this.initialColor = aInitialColor; this.initialColor = aInitialColor;
this.defaultColors = aDefaultColors; this.defaultColors = aDefaultColors;
}, },
initialColor: "", initialColor: "",
parent: null, browsingContext: null,
open(aColorPickerShownCallback) { open(aColorPickerShownCallback) {
MockColorPicker.showing = true; MockColorPicker.showing = true;
MockColorPicker.shown = true; MockColorPicker.shown = true;

View File

@@ -96,8 +96,8 @@ nsColorPicker::~nsColorPicker() {
// TODO(bug 1805397): Implement default colors // TODO(bug 1805397): Implement default colors
NS_IMETHODIMP NS_IMETHODIMP
nsColorPicker::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle, nsColorPicker::Init(dom::BrowsingContext* aBrowsingContext,
const nsAString& aInitialColor, const nsAString& aTitle, const nsAString& aInitialColor,
const nsTArray<nsString>& aDefaultColors) { const nsTArray<nsString>& aDefaultColors) {
MOZ_ASSERT(NS_IsMainThread(), MOZ_ASSERT(NS_IsMainThread(),
"Color pickers can only be opened from main thread currently"); "Color pickers can only be opened from main thread currently");

View File

@@ -5,7 +5,13 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#ifdef MOZ_X11
# include "X11UndefineNone.h"
#endif
#include "mozilla/Maybe.h" #include "mozilla/Maybe.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/HTMLInputElement.h" #include "mozilla/dom/HTMLInputElement.h"
#include "nsColor.h" #include "nsColor.h"
#include "nsColorPicker.h" #include "nsColorPicker.h"
@@ -61,12 +67,13 @@ GtkColorSelection* nsColorPicker::WidgetGetColorSelection(GtkWidget* widget) {
} }
#endif #endif
NS_IMETHODIMP nsColorPicker::Init(mozIDOMWindowProxy* aParent, NS_IMETHODIMP nsColorPicker::Init(
const nsAString& title, mozilla::dom::BrowsingContext* aBrowsingContext, const nsAString& title,
const nsAString& initialColor, const nsAString& initialColor, const nsTArray<nsString>& aDefaultColors) {
const nsTArray<nsString>& aDefaultColors) { MOZ_ASSERT(aBrowsingContext, "Null browsingContext passed to color picker!");
auto* parent = nsPIDOMWindowOuter::From(aParent);
mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent); mParentWidget =
aBrowsingContext->Canonical()->GetParentProcessWidgetContaining();
mTitle = title; mTitle = title;
mInitialColor = initialColor; mInitialColor = initialColor;
mDefaultColors.Assign(aDefaultColors); mDefaultColors.Assign(aDefaultColors);

View File

@@ -13,16 +13,18 @@ using namespace mozilla::dom;
NS_IMPL_ISUPPORTS(nsColorPickerProxy, nsIColorPicker) NS_IMPL_ISUPPORTS(nsColorPickerProxy, nsIColorPicker)
NS_IMETHODIMP NS_IMETHODIMP
nsColorPickerProxy::Init(mozIDOMWindowProxy* aParent, const nsAString& aTitle, nsColorPickerProxy::Init(BrowsingContext* aBrowsingContext,
const nsAString& aTitle,
const nsAString& aInitialColor, const nsAString& aInitialColor,
const nsTArray<nsString>& aDefaultColors) { const nsTArray<nsString>& aDefaultColors) {
BrowserChild* browserChild = BrowserChild::GetFrom(aParent); BrowserChild* browserChild =
BrowserChild::GetFrom(aBrowsingContext->GetDocShell());
if (!browserChild) { if (!browserChild) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
browserChild->SendPColorPickerConstructor(this, aTitle, aInitialColor, browserChild->SendPColorPickerConstructor(this, aBrowsingContext, aTitle,
aDefaultColors); aInitialColor, aDefaultColors);
return NS_OK; return NS_OK;
} }

View File

@@ -6,7 +6,7 @@
#include "nsISupports.idl" #include "nsISupports.idl"
interface mozIDOMWindowProxy; webidl BrowsingContext;
/** /**
* nsIColorPicker is representing colors as strings because the internal * nsIColorPicker is representing colors as strings because the internal
@@ -55,14 +55,15 @@ interface nsIColorPicker : nsISupports
* one used by the underlying backend or an arbitrary one. The backend could * one used by the underlying backend or an arbitrary one. The backend could
* also assert. * also assert.
* *
* @param parent nsIDOMWindow parent. This dialog will be dependent * @param browsingContext The context in which the color picker is being
* on this parent. parent must be non-null. * shown, must be non-null.
* @param title The title for the color picker widget. * @param title The title for the color picker widget.
* @param initialColor The color to show when the widget is opened. The * @param initialColor The color to show when the widget is opened. The
* parameter has to follow the format specified on top * parameter has to follow the format specified on
* of this file. * top of this file.
*/ */
void init(in mozIDOMWindowProxy parent, in AString title, in AString initialColor, in Array<AString> defaultColors); void init(in BrowsingContext browsingContext, in AString title,
in AString initialColor, in Array<AString> defaultColors);
/** /**
* Opens the color dialog asynchrounously. * Opens the color dialog asynchrounously.

View File

@@ -11,6 +11,8 @@
#include "mozilla/ArrayUtils.h" #include "mozilla/ArrayUtils.h"
#include "mozilla/AutoRestore.h" #include "mozilla/AutoRestore.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "nsIWidget.h" #include "nsIWidget.h"
#include "nsString.h" #include "nsString.h"
#include "WidgetUtils.h" #include "WidgetUtils.h"
@@ -188,13 +190,14 @@ nsColorPicker::~nsColorPicker() {}
NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker) NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker)
NS_IMETHODIMP NS_IMETHODIMP
nsColorPicker::Init(mozIDOMWindowProxy* parent, const nsAString& title, nsColorPicker::Init(mozilla::dom::BrowsingContext* aBrowsingContext,
const nsAString& aInitialColor, const nsAString& title, const nsAString& aInitialColor,
const nsTArray<nsString>& aDefaultColors) { const nsTArray<nsString>& aDefaultColors) {
MOZ_ASSERT(parent, MOZ_ASSERT(
"Null parent passed to colorpicker, no color picker for you!"); aBrowsingContext,
"Null browsingContext passed to colorpicker, no color picker for you!");
mParentWidget = mParentWidget =
WidgetUtils::DOMWindowToWidget(nsPIDOMWindowOuter::From(parent)); aBrowsingContext->Canonical()->GetParentProcessWidgetContaining();
mInitialColor = ColorStringToRGB(aInitialColor); mInitialColor = ColorStringToRGB(aInitialColor);
mDefaultColors.Assign(aDefaultColors); mDefaultColors.Assign(aDefaultColors);
return NS_OK; return NS_OK;