Closed Bug 506799 Opened 15 years ago Closed 15 years ago

nsIContentPrefService should accept string arguments for URI as well as nsIURI

Categories

(Toolkit :: Preferences, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla1.9.3a1

People

(Reporter: darktrojan, Assigned: darktrojan)

Details

(Keywords: dev-doc-complete)

Attachments

(1 file, 5 obsolete files)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1
Build Identifier: 

From bug 458299 comment 1:
> Perhaps we can make the service's methods polymorphically
> accept either a URI or a string representing the site.

myk, would the correct way to do this be to change the interface from nsIURI to nsIVariant, then throwing an exception if the argument is not string, nsIURI or null?

Reproducible: Always
Flags: wanted1.9.2?
Assignee: nobody → geoff
Status: UNCONFIRMED → NEW
Ever confirmed: true
(In reply to comment #0)
> From bug 458299 comment 1:
> > Perhaps we can make the service's methods polymorphically
> > accept either a URI or a string representing the site.
> 
> myk, would the correct way to do this be to change the interface from nsIURI to
> nsIVariant, then throwing an exception if the argument is not string, nsIURI or
> null?

That sounds reasonable to me, but cc:ing mconnor for confirmation on this API change and dolske to keep him apprised as it was his suggestion originally in bug 458299.
Status: NEW → ASSIGNED
Attached patch patch v1 (obsolete) — Splinter Review
Now that I've got the previous patch off my tree, here's what I've been working on.
Attachment #396331 - Flags: review?(myk)
Comment on attachment 396331 [details] [diff] [review]
patch v1

>diff --git a/toolkit/components/contentprefs/public/nsIContentPrefService.idl b/toolkit/components/contentprefs/public/nsIContentPrefService.idl

This IDL file should document that a string argument for an aURI parameter represents the group (i.e. site) to which the pref is restricted, while an nsIURI argument for that parameter represents the URI from which the service should derive the group.

In fact, we should probably change the name of these parameters to reflect this expanded use of them.  Techhnically, this value is the "group", and this parameter should be called "aGroup", even though the content pref service only derives site values from URIs.  Perhaps we can call it "aGroup" but document that it typically represents the site/hostname to which a preference applies.


>diff --git a/toolkit/components/contentprefs/src/nsContentPrefService.js b/toolkit/components/contentprefs/src/nsContentPrefService.js

>+    else if (aURI.QueryInterface && aURI.QueryInterface(Ci.nsIURI)) {

Use |aURI instanceof Ci.nsIURI| instead.  It's not only simpler, but it also doesn't throw, whereas aURI.QueryInterface(Ci.nsIURI) will throw NS_ERROR_NO_INTERFACE (preventing your code from throwing NS_ERROR_ILLEGAL_VALUE afterwards) if aURI doesn't implement that interface.


>diff --git a/toolkit/components/contentprefs/tests/unit/test_stringURIs.js b/toolkit/components/contentprefs/tests/unit/test_stringURIs.js

I'm not sure how important this is, but nsIVariant supports a number of string types, and I wonder if they all result in string primitives whose typeof == "string" when passed through an XPCOM interface.  I also wonder if passing a JavaScript String object (whose typeof == "object") results in a primitive string on the other side of an XPCOM interface.  It might be worth testing both these things.

Otherwise, this looks great!
Attachment #396331 - Flags: review?(myk) → review-
(In reply to comment #3)
> >diff --git a/toolkit/components/contentprefs/tests/unit/test_stringURIs.js b/toolkit/components/contentprefs/tests/unit/test_stringURIs.js
> 
> I'm not sure how important this is, but nsIVariant supports a number of string
> types, and I wonder if they all result in string primitives whose typeof ==
> "string" when passed through an XPCOM interface.  I also wonder if passing a
> JavaScript String object (whose typeof == "object") results in a primitive
> string on the other side of an XPCOM interface.  It might be worth testing both
> these things.

You're right, it's still an object on the other side. Any ideas on how to check for that? I couldn't find one that worked.
Maybe I should give up on this approach and have an nsIURI version and an AString version of each method.
(In reply to comment #4)
> You're right, it's still an object on the other side. Any ideas on how to check
> for that?

You should be able to check for aURI.constructor.name == "String".  This works for primitive strings too, since JS will transparently convert them to String objects before evaluating the conditional.

Note: you can't test for aURI.constructor == String because the String constructor in one JS context isn't the same as the String constructor from another one.


> Maybe I should give up on this approach and have an nsIURI version and an
> AString version of each method.

That's a significantly more complex API; I'd prefer sticking with your current approach.
Attached patch patch v2 (obsolete) — Splinter Review
Fixed up and tested String objects and renamed aURI to aGroup.
Attachment #396331 - Attachment is obsolete: true
Attachment #396913 - Flags: review?(myk)
Comment on attachment 396913 [details] [diff] [review]
patch v2

>diff --git a/toolkit/components/contentprefs/src/nsContentPrefService.js b/toolkit/components/contentprefs/src/nsContentPrefService.js
>+      return this._selectPref(aGroup.toString(), aName);

Nit: toString() is unnecessary, as JavaScript String objects can be used exactly the same way as string primitives (except for in "typeof" expressions).


>diff --git a/toolkit/components/contentprefs/tests/unit/test_stringURIs.js b/toolkit/components/contentprefs/tests/unit/test_stringURIs.js

Nit: this should be called test_stringGroups.js.

Otherwise this looks great and works fine in my testing.  r=myk and requesting sr from mconnor.
Attachment #396913 - Flags: superreview?(mconnor)
Attachment #396913 - Flags: review?(myk)
Attachment #396913 - Flags: review+
Comment on attachment 396913 [details] [diff] [review]
patch v2


> 
>-    if (aURI) {
>-      var group = this.grouper.group(aURI);
>+    if (aGroup == null) {
>+      return this._selectGlobalPref(aName);
>+    }
>+    else if (aGroup.constructor.name == "String") {
>+      return this._selectPref(aGroup.toString(), aName);
>+    }
>+    else if (aGroup instanceof Ci.nsIURI) {
>+      var group = this.grouper.group(aGroup);
>       return this._selectPref(group, aName);
>     }

else after return is wrong/useless, these should just be successive if statements, also, no braces around single lines please.

looks good otherwise.
Attachment #396913 - Flags: superreview?(mconnor) → superreview+
(In reply to comment #7)
> (From update of attachment 396913 [details] [diff] [review])
> >diff --git a/toolkit/components/contentprefs/src/nsContentPrefService.js b/toolkit/components/contentprefs/src/nsContentPrefService.js
> >+      return this._selectPref(aGroup.toString(), aName);
> 
> Nit: toString() is unnecessary, as JavaScript String objects can be used
> exactly the same way as string primitives (except for in "typeof" expressions).

Without it, NS_ERROR_UNEXPECTED is thrown trying to assign an object as a statement parameter. I always thought the Storage API handled that okay, but apparently not.
Attached patch patch v3 (obsolete) — Splinter Review
Now with less nits to pick.
Attachment #396913 - Attachment is obsolete: true
Comment on attachment 397139 [details] [diff] [review]
patch v3

(In reply to comment #9)
> (In reply to comment #7)
> > (From update of attachment 396913 [details] [diff] [review] [details])
> > >diff --git a/toolkit/components/contentprefs/src/nsContentPrefService.js b/toolkit/components/contentprefs/src/nsContentPrefService.js
> > >+      return this._selectPref(aGroup.toString(), aName);
> > 
> > Nit: toString() is unnecessary, as JavaScript String objects can be used
> > exactly the same way as string primitives (except for in "typeof" expressions).
> 
> Without it, NS_ERROR_UNEXPECTED is thrown trying to assign an object as a
> statement parameter. I always thought the Storage API handled that okay, but
> apparently not.

Ah, my bad. How bizarre. :-!

Note to committer: this is the patch you should be committing; mconnor's sr carries forward from the previous patch.
Attachment #397139 - Flags: review+
Landed (and forgot to update this bug), and backed out because of this:

http://tinderbox.mozilla.org/showlog.cgi?log=Firefox-Unittest/1251490034.1251491325.29668.gz
Keywords: checkin-needed
Attached patch patch v4 (obsolete) — Splinter Review
Found a hack in the private browsing code which is no longer necessary due to this patch anyway...
Attachment #397139 - Attachment is obsolete: true
Comment on attachment 397384 [details] [diff] [review]
patch v4

Ah sod, left some debug stuff in there.
Attachment #397384 - Attachment is obsolete: true
Attached patch patch v4 again (obsolete) — Splinter Review
Sorry about the bugmail spam!
Keywords: checkin-needed
Comment on attachment 397385 [details] [diff] [review]
patch v4 again

>         // The service only cares about the host of the URI, so we don't need a
>         // full nsIURI object here.
>-        let uri = { host: names[i]};
>+        let uri = names[i];

The comment also needs to be removed or adjusted.
So it does.
Attachment #397385 - Attachment is obsolete: true
http://hg.mozilla.org/mozilla-central/rev/d5a29930742a
Status: ASSIGNED → RESOLVED
Closed: 15 years ago
Keywords: checkin-needed
Resolution: --- → FIXED
Target Milestone: --- → mozilla1.9.3a1
Whiteboard: [doc-waiting-1.9.3]
Documentation updated:

https://developer.mozilla.org/en/nsIContentPrefService
Whiteboard: [doc-waiting-1.9.3]
Flags: wanted1.9.2?
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: