Chrome extension crashs on function pointer call

44 views
Skip to first unread message

Michael Maier

unread,
Dec 3, 2014, 8:59:50 AM12/3/14
to chromium-...@chromium.org
This chrome extension crashs on startup:

manifest.json
{
  "manifest_version": 2,

  "name": "My-Extension",
  "description": "This extension crashs on startup.",
  "version": "1.0",
  "background": {
    "persistent": true,
    "scripts": ["background.js"]
  }
}

background.js
var onMessage = chrome.runtime.onMessage.addListener;
onMessage
(function(message) {
  console
.log('received message');
});



It is not crashing, if the function is called directly

chrome.runtime.onMessage.addListener(function(message) {
  console
.log('received message');
});

or like this

var onMessage = function(listener) {
  chrome
.runtime.onMessage.addListener(listener);
};

onMessage
(function(message) {
  console
.log('received message');
});


Why is this causing a crash? Is it a bug or a feature?

PhistucK

unread,
Dec 3, 2014, 9:58:42 AM12/3/14
to Michael Maier, Chromium-extensions
Does the entire extension crash (there is a notification that says that the extension has crashed)?
If so, the crash is definitely a bug - you can look for an existing similar issue at crbug.com and if you did not found any, you can create one using the "New issue" link there. You can reply with a link to the found or created issue.

Regarding your goal of shortening the call - in JavaScript, when you assign a method of an object instance/namespace to another object (in this case, the global one - window), you change its context, its this. So onMessage is probably expected not to work, because it requires the right context (chrome.runtime, not window). However -
var onMessage = chrome.runtime.onMessage.addListener.bind(chrome.runtime.onMessage);
Should work in theory, because the context stays intact (this is the purpose of bind - you bind a function to a certain context, instead of the calling context).


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/ae627d44-e35f-46a0-9def-1b00318ea97b%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Michael Maier

unread,
Dec 8, 2014, 2:18:02 PM12/8/14
to PhistucK, Chromium-extensions
Thanks. That's the point. If I don't bind the function to the object, the extension crashs and a notification is shown. Thus, it was my fault. Your suggested solution works:
var onMessage = chrome.runtime.onMessage.addListener.bind(chrome.runtime.onMessage);

PhistucK

unread,
Dec 8, 2014, 2:20:51 PM12/8/14
to Michael Maier, Chromium-extensions
I am glad that works for you. Would you, please, file an issue at crbug.com regardless? A crash is always a bug. :(

Thank you!


PhistucK

Rob Wu

unread,
Dec 8, 2014, 5:22:16 PM12/8/14
to PhistucK, Michael Maier, Chromium-extensions
Never mind the bug report, I've already submitted a patch: https://codereview.chromium.org/783333002/

Next time, please open a ticket at https://crbug.com/new instead of posting on the mailing list.

Kind regards,
 Rob
 https://robwu.nl

PhistucK

unread,
Dec 9, 2014, 1:56:36 AM12/9/14
to Rob Wu, Michael Maier, Chromium-extensions
Thank you very much!


PhistucK

Michael Maier

unread,
Dec 14, 2014, 1:21:20 PM12/14/14
to Rob Wu, PhistucK, Chromium-extensions
Thank you very much. I just did not have the time to create a ticket yet.
Reply all
Reply to author
Forward
0 new messages