Closed Bug 1334782 Opened 7 years ago Closed 6 years ago

tabs.query should pattern match the title

Categories

(WebExtensions :: General, defect, P3)

defect

Tracking

(firefox59 fixed)

VERIFIED FIXED
mozilla59
Tracking Status
firefox59 --- fixed

People

(Reporter: evilpie, Assigned: tushararora, Mentored)

References

()

Details

(Keywords: good-first-bug, Whiteboard: triaged)

Attachments

(2 files)

Per our documentation and Chrome behavior the "title" attribute in tabs.query is pattern matched.
Keywords: good-first-bug
Whiteboard: triaged
Priority: -- → P3
Kris, could you please add some information on how to get started? Thanks!
Mentor: kmaglione+bmo
Flags: needinfo?(kmaglione+bmo)
If this is your first contribution, please see https://wiki.mozilla.org/Add-ons/Contribute
Can I get assigned to this bug? I am new to open source, so should I start by searching for the file and how should I submit a patch?
Can I get assigned to this bug? I am new to open source, so should I start by searching for the file and how should I submit a patch?
Hi Bharat, I've assigned the bug to you.  Check out the contribution guide on how to get the source code, make changes, compile, test, and finally submit the patch:

https://wiki.mozilla.org/Add-ons/Contribute

The code in question that does the matching is in ExtensionTabs.jsm:

http://searchfox.org/mozilla-central/source/toolkit/components/extensions/ExtensionTabs.jsm#431

Currently, the `title` attribute is treated the same as others that do exact matching.  You need to change that, for example, to convert the `queryInfo.title` param to a regex pattern, and test the tab's title against that.

Feel free to ask if you get stuck, either here or in #webextensions on irc for a faster response.
Assignee: nobody → bharatraghunthan9767
Status: NEW → ASSIGNED
Flags: needinfo?(kmaglione+bmo)
I will definitely ask if I get stuck and the solution is not available in the documentation or by simple Googling. Thanks!!!
What type of Regex Pattern must 'queryInfo.title' have? Can it allow all types of characters any number of times or must it be a regex that allows only starting with alphabets and strictly in Title Case?
Flags: needinfo?(tomica)
I have reverse-engineered Chrome's code and found that I have to write a MatchPattern() function. But, I need to know if some other pattern matching function or regex has already been implemented before I can go on to write a new one.Thanks in advance!!
You are right, we already have something similar:

http://searchfox.org/mozilla-central/source/toolkit/modules/addons/MatchPattern.jsm#26

You would need to add that function to exported symbols a few lines above.
Flags: needinfo?(tomica)
Does this code seem ok? I'm asking because I want to run some basic tests before submitting for review and currently I am not able to run my build of mozilla-central because of  this error:                             """ABORT: LoadSheetSync failed with error 80004005 loading built-in stylesheet 'chrome://global/content/minimal-xul.css':"""


In file http://searchfox.org/mozilla-central/source/toolkit/components/extensions/ExtensionTabs.jsm#431
const PROPS = ["active", "audible", "cookieStoreId", "highlighted", "index", "pinned", "status"]; //Title has been removed from this exact-checking list
 
var re=globToRegexp(this.title,true);		

    if (queryInfo.title){
      if (!re.test(queryInfo.title)) {
      	return false;
      }
    }
And in file http://searchfox.org/mozilla-central/source/toolkit/modules/addons/MatchPattern.jsm#17 :
this.EXPORTED_SYMBOLS = ["MatchPattern", "MatchGlobs", "MatchURLFilters","globToRegexp"]; 
//Added function globToRegexp in symbols list
Flags: needinfo?(tomica)
How do I write some unit tests or manually test that I have implemented this feature correctly?          (I finally got the build to run)
(In reply to Bharat Raghunathan from comment #11)
> Does this code seem ok? 

Please use MozReview to submit code, even before you are ready to ask for review, it makes it easier to understand it in context.

http://reviewboard.mozilla.org/


> var re=globToRegexp(this.title,true);		

You should only construct the regex if the title param was passed.  The queryInfo title should serve as the basis for the pattern, and you should test it against the tab title, not the other way around.

Also, Please run ESLint on your code.

https://developer.mozilla.org/en-US/docs/ESLint


(In reply to Bharat Raghunathan from comment #12)
> How do I write some unit tests or manually test that I have implemented this
> feature correctly?

The tests for tabs.query are here:

http://searchfox.org/mozilla-central/source/browser/components/extensions/test/browser/browser_ext_tabs_query.js
Flags: needinfo?(tomica)
My college exams are coming up this week, so I shall resume work on the bug only after 28th Feb, 2017
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

This is unfinished and I just want you to see the changes made, lint check has been done.
Attachment #8839822 - Flags: feedback?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

Thanks for working on this Bharat, this is on right track overall.  I'll leave some code-specific comments in a followup.
Attachment #8839822 - Flags: feedback?(tomica) → feedback+
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

https://reviewboard.mozilla.org/r/114398/#review115938

That committ message is a bit on the verbose side, something like the bug title should be enough.

::: toolkit/components/extensions/ExtensionTabs.jsm:423
(Diff revision 1)
>     * @param {string} [queryInfo.status]
>     *        Matches against the exact value of the tab's `status` attribute.
>     * @param {string} [queryInfo.title]
>     *        Matches against the exact value of the tab's `title` attribute.
>     *
> -   *        Note: Per specification, this should perform a pattern match, rather
> +   *        Under Review: Per specification, this should perform a pattern match, rather

Just write the comment as if this already works, because once your patch lands, it will work.  :)

::: toolkit/components/extensions/ExtensionTabs.jsm:437
(Diff revision 1)
> -    const PROPS = ["active", "audible", "cookieStoreId", "highlighted", "index", "pinned", "status", "title"];
> +    const PROPS = ["active", "audible", "cookieStoreId", "highlighted", "index", "pinned", "status"];
> +
> +    if (queryInfo.title !== null) {
> +      // This function converts a glob pattern (containing * and ? as wildcards)
> +      // to a regular expression.
> +      let re = globToRegexp(queryInfo.title, true);

Turns out we already have a `MatchGlob` exported from "MatchPattern.jsm", so it might be better to convert the `title` param to an instance of `MatchGlob` in "ext-tab.js" [1] before passing it here, similar to what we do for the `uri` param.  And if we go with that, you would need to do the same in the android version of "ext-tabs.js".

1) http://searchfox.org/mozilla-central/source/browser/components/extensions/ext-tabs.js#514
Thanks for the feedback!! (My exams are over, started working again)
Where is the Android Port/Version of ext-tabs.js located?
Flags: needinfo?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

I put a comment on http://searchfox.org/mozilla-central/source/browser/components/extensions/test/browser/browser_ext_tabs_query.js#42 by mistake, but apart from it, is everything ok?
Attachment #8839822 - Flags: feedback?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

https://reviewboard.mozilla.org/r/114398/#review118546

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:42
(Diff revision 2)
>  
>          browser.test.assertEq(tabs[0].status, "complete", "tab 0 status correct");
>          browser.test.assertEq(tabs[1].status, "complete", "tab 1 status correct");
>  
>          browser.test.assertEq(tabs[0].title, "Gort! Klaatu barada nikto!", "tab 0 title correct");
> -
> +        // Unit testing for queryInfo.title done here?

Not exactly here, this is a basic test, for matching title, you need something similar to "match pattern" below.

::: mobile/android/components/extensions/ext-tabs.js:400
(Diff revision 2)
>              return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" parameter'});
>            }
>  
>            queryInfo = Object.assign({}, queryInfo);
>            queryInfo.url = new MatchPattern(queryInfo.url);
> +          queryInfo.title = new MatchGlobs(queryInfo.title);

This is not correct, you need to handle the title property separately from the url property, they can be set independently.

::: toolkit/components/extensions/ExtensionTabs.jsm:436
(Diff revision 2)
> -    const PROPS = ["active", "audible", "cookieStoreId", "highlighted", "index", "pinned", "status", "title"];
> +    const PROPS = ["active", "audible", "cookieStoreId", "highlighted", "index", "pinned", "status"];
> +
> +    if (queryInfo.title !== null) {
> +      // This function converts a glob pattern (containing * and ? as wildcards)
> +      // to a regular expression.
> +      let re = globToRegexp(queryInfo.title, true);

You don't need this anymore, the `title` property is already a `MatchGlob` at this point, so you can just call `.match` on it, similar to the `url` property a few lines below.

::: toolkit/modules/addons/MatchPattern.jsm:19
(Diff revision 2)
>  XPCOMUtils.defineLazyModuleGetter(this, "Services",
>                                    "resource://gre/modules/Services.jsm");
>  
> -this.EXPORTED_SYMBOLS = ["MatchPattern", "MatchGlobs", "MatchURLFilters"];
> +this.EXPORTED_SYMBOLS = ["MatchPattern", "MatchGlobs", "MatchURLFilters", "globToRegexp"];
>  
> -/* globals MatchPattern, MatchGlobs */
> +/* globals MatchPattern, MatchGlobs,globToRegexp*/

Both of these changes are not needed anymore with the new approach, you should revert them.
Attachment #8839822 - Flags: feedback?(tomica)
Attachment #8839822 - Flags: feedback?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

Looks good.  I left a few nits, and remember that you still need tests.
Attachment #8839822 - Flags: feedback?(tomica) → feedback+
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

https://reviewboard.mozilla.org/r/114398/#review118736

::: browser/components/extensions/ext-tabs.js:526
(Diff revision 3)
> +          if (!extension.hasPermission("tabs")) {
> +            return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "title" parameter'});
> +          }
> +
> +          queryInfo = Object.assign({}, queryInfo);

This looks good, though it might be better to extract the common code outside the separate `if` blocks.

 - You can combine and throw one and the same message when the extension lacks "tabs" permissions and has either `url` or `title` property set.
 - Making the copy of queryInfo can happen in all cases.

::: toolkit/components/extensions/ExtensionTabs.jsm:21
(Diff revision 3)
>  XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
>                                    "resource://gre/modules/PrivateBrowsingUtils.jsm");
>  XPCOMUtils.defineLazyModuleGetter(this, "Services",
>                                    "resource://gre/modules/Services.jsm");
> +XPCOMUtils.defineLazyModuleGetter(this, "globToRegexp",
> +                                  "resource://gre/modules/MatchPattern.jsm");

This is also not needed anymore.

::: toolkit/modules/addons/MatchPattern.jsm:19
(Diff revision 3)
>  XPCOMUtils.defineLazyModuleGetter(this, "Services",
>                                    "resource://gre/modules/Services.jsm");
>  
>  this.EXPORTED_SYMBOLS = ["MatchPattern", "MatchGlobs", "MatchURLFilters"];
>  
> -/* globals MatchPattern, MatchGlobs */
> +/* globals MatchPattern, MatchGlobs*/

Please revert this whole file, no unneded changes.
I'm committing yet another diff without tests so that these files are first fixed.
Shall I start making a new commit for testing?
Also, by my understanding, http://searchfox.org/mozilla-central/source/browser/components/extensions/test/browser/browser_ext_tabs_query.js#55-57 is used to create Sample URLs for testing and then simple test queries are performed on them.
How do I create such Sample titles for simple test queries?
The syntax of 'gBrowser' and 'openNewForegroundTab' seem to have only URLs as valid arguments, so how do I proceed?
Sorry, if I've been disturbing you a lot..
Flags: needinfo?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

This is hopefully providing the final fix to all the needed files before going in for testing.
Attachment #8839822 - Flags: feedback?(tomica)
> I'm committing yet another diff without tests so that these files are first fixed.
> Shall I start making a new commit for testing?
You don't need a separate commit for tests, and no need to wait to get everything "perfect" before writing tests.  In fact, you are encouraged to write and run tests, that way you don't need me to tell you if something is correct and working, you can actually test it yourself.  ;)

> Also, by my understanding, [...] is used to create Sample URLs
> for testing and then simple test queries are performed on them.
> How do I create such Sample titles for simple test queries?
This doesn't "create URLs", it opens tabs from those URLs, which point to our test server, and which already have some titles.  Those three example URLs should be enough for your testing purposes for this bug, as at least third should have a different title from the others.  You can find out what the actual titles are by querying all tabs and printing out the .title property from each.


> Sorry, if I've been disturbing you a lot..
You are not disturbing me, asking questions is fine when you get stuck.  But if you wanna try and get some understanding faster, you should try running the tests yourself, modifying them slightly and seeing what happens.  It's a good way to learn on your own, rather than waiting for my answers each time.
Flags: needinfo?(tomica)
Comment on attachment 8839822 [details]
Bug 1334782 - Convert title to MatchGlob and title param to regex - cleanup code

https://reviewboard.mozilla.org/r/114398/#review118946

::: mobile/android/components/extensions/ext-tabs.js:400
(Diff revision 4)
> -            return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" parameter'});
> +            return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" or "title" parameter'});
>            }
>  
>            queryInfo = Object.assign({}, queryInfo);
>            queryInfo.url = new MatchPattern(queryInfo.url);
> +          queryInfo.title = new MatchGlobs(queryInfo.title);

This is not actually what I had in mind, but I probably didn't explain it clearly.  Let's go to the previous version of this code, at least it should work (even if it isn't optimal), and it should allow you to write tests.

Once you have test coverage, you should be able to make these kind of changes to code easily, while testing that things still work as intended.
Attachment #8839822 - Flags: feedback?(tomica)
I am stuck in writing tests and other university work has accumulated, so unassigning myself until I or someone else makes the next submission.
Status: ASSIGNED → NEW
Assignee: bharatraghunthan9767 → nobody
I'd like to work on this, if it is available!
Hi Gabrielle, yes, it is available.  I've assigned the bug to you.

There is info about what needs to be done starting from comment #5 above, but feel free to ask if you don't understand something.
Assignee: nobody → gabrielle.singhcadieux
Hi, I'm having some trouble locating the code that was formerly in [1]!

[1] http://searchfox.org/mozilla-central/source/browser/components/extensions/ext-tabs.j
Flags: needinfo?(tomica)
Yeah, sorry, the code moves a bit over time.  Here's a permalink instead:
http://searchfox.org/mozilla-central/rev/596d188f6dbc8cb023e625f0a4310d184875f8fc/browser/components/extensions/ext-tabs.js#474
Flags: needinfo?(tomica)
I'm still a little confused - do I need to change to a different revision? I'm working with the latest one, and this code doesn't seem to be there: https://hg.mozilla.org/mozilla-central/file/tip//toolkit/components/extensions/
Flags: needinfo?(tomica)
Oh, the ext-tabs.js is in the browser directory, look here:
https://hg.mozilla.org/mozilla-central/file/tip//browser/components/extensions/

They are similar directories, but the split goes like this:
toolkit/components/extensions/          - extension files shared between both desktop and mobile
browser/components/extensions/          - desktop-specific extension API implementations
mobile/android/components/extensions/   - android-specific
Flags: needinfo?(tomica)
Hey Gabrielle! Just wanted to check in and see how you are doing with this bug.
I really apologize - the bugfix was going smoothly, but things got very busy at work and it fell by the wayside. I will get back on this ASAP (this week)!
No worries at all! We just wanted to check in and see if you were still interested in working on it. :)
Hey Gabrielle, we haven't heard from you in a while, so we are going to unassign this bug for now, so someone else can take it up. If you have a patch ready, feel free to submit it and we'll reassign it to you!
Assignee: gabrielle.singhcadieux → nobody
Hi, I'm a beginner and would like to work on this as my first bug. Can I take it up?
Hey Apporva! Go for it! :zombie is mentoring this bug -- feel free to needinfo him if you need any help!
Hey Tomislav! Can you please review the code I just submitted?
Just wanted to work on a good first bug you know, couldn't resist myself.
Flags: needinfo?(tomica)
Thanks Tushar, I've assigned the bug to you, and I'll review it today (next time you can ask for review directly instead of ni?).
Assignee: nobody → tushararora.cs
Flags: needinfo?(tomica)
Comment on attachment 8920571 [details]
Bug 1334782 - tabs.query should pattern match the title

https://reviewboard.mozilla.org/r/191582/#review197240

::: browser/components/extensions/ext-tabs.js:520
(Diff revision 2)
>  
>          async query(queryInfo) {
> -          if (queryInfo.url !== null) {
> -            if (!extension.hasPermission("tabs")) {
> +          if (!extension.hasPermission("tabs")) {
> -              return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" parameter'});
> +            if (queryInfo.url !== null || queryInfo.title !== null) {
> +              return Promise.reject({message: 'The "tabs" permission is required to use the query API with the "url" or "title" parameter'});

nit: "parameters"

::: browser/components/extensions/ext-tabs.js:530
(Diff revision 2)
> +
> +          if (queryInfo.url !== null) {
>              queryInfo.url = new MatchPatternSet([].concat(queryInfo.url));
>            }
> +          if (queryInfo.title !== null) {
> +            queryInfo.title = new MatchGlob([].concat(queryInfo.title));

`title` can only be a string (not an array), and MatchGlob only takes a string, so drop the `concat` here (and on the android side).

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:142
(Diff revision 2)
> +    manifest: {
> +      "permissions": ["tabs"],
> +    },
> +
> +    background: function() {
> +      browser.tabs.query({

nit: let's make the background function async, and just use await instead of callbacks.

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:166
(Diff revision 2)
> +    manifest: {
> +      "permissions": ["tabs"],
> +    },
> +
> +    background: function() {
> +      browser.tabs.query({

This can go in the same extension as the previous test, no need to separate it.

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:261
(Diff revision 2)
>      manifest: {
>        "permissions": [],
>      },
>  
>      async background() {
>        await browser.test.assertRejects(

Please also add a test that querying with the `title` rejects as well.
This is pretty close, though I noted a few nits that I would like to get fixed.
Attachment #8920571 - Flags: review?(tomica)
Comment on attachment 8920571 [details]
Bug 1334782 - tabs.query should pattern match the title

https://reviewboard.mozilla.org/r/191582/#review198200

Thanks, looks great (a few more nits).

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:141
(Diff revision 3)
> +  extension = ExtensionTestUtils.loadExtension({
> +    manifest: {
> +      "permissions": ["tabs"],
> +    },
> +
> +    background: async function() {

nit: this can be just `async background() {`

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:157
(Diff revision 3)
> +        title: "?ochitest index /*",
> +      });
> +
> +      browser.test.assertEq(tabs.length, 3, "should have three tabs");
> +
> +      tabs.sort((tab1, tab2) => tab1.index - tab2.index);

You should really sort in the test above as well.

::: browser/components/extensions/test/browser/browser_ext_tabs_query.js:269
(Diff revision 3)
> +    manifest: {
> +      "permissions": [],
> +    },
> +
> +    async background() {
> +      await browser.test.assertRejects(

nit: this can be part of the previous test as well.
Attachment #8920571 - Flags: review?(tomica) → review+
Attachment #8920571 - Flags: review+ → review?(tomica)
Comment on attachment 8920571 [details]
Bug 1334782 - tabs.query should pattern match the title

https://reviewboard.mozilla.org/r/191582/#review207488

Great, thanks (and sorry for the delay).

I've started a Try test run, and if everything is ok, you can mark all issues in reviewboard as resolved, and add a checkin-needed keyword.
Attachment #8920571 - Flags: review?(tomica) → review+
Hi Tomislav!

The failed tests don't seem to be related to this bug. What are the next steps? Should we go ahead or do a rerun?

https://treeherder-manifest.herokuapp.com/?repo=try&revision=354393e1573a4437085979fe6404334a8f550db1
Flags: needinfo?(tomica)
Yeah, seem unrelated, and no need for a rerun, just mark all issues as resolved, and add `checking-needed` keyword.
Flags: needinfo?(tomica)
Keywords: checkin-needed
Pushed by ryanvm@gmail.com:
https://hg.mozilla.org/integration/autoland/rev/7f14b8ca27cf
tabs.query should pattern match the title r=zombie
Keywords: checkin-needed
https://hg.mozilla.org/mozilla-central/rev/7f14b8ca27cf
Status: NEW → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla59
Is manual testing required on this bug? If yes, please provide some STR and the proper extension(if required) or set the “qe-verify -“ flag.

Thanks!
Flags: needinfo?(tomica)
Not needed for this bug, thanks.
Flags: needinfo?(tomica)
Status: RESOLVED → VERIFIED
Flags: qe-verify-
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: