Last Comment Bug 647653 - It should be possible to create nsIDOMBlob in JavaScript XPCOM components
: It should be possible to create nsIDOMBlob in JavaScript XPCOM components
Status: RESOLVED FIXED
fixed-in-bs
: dev-doc-complete
Product: Core
Classification: Components
Component: XPCOM (show other bugs)
: unspecified
: All All
: -- normal (vote)
: mozilla8
Assigned To: Kyle Huey [:khuey] (khuey@mozilla.com)
:
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-04-04 00:51 PDT by Jan Gerber
Modified: 2013-11-04 18:29 PST (History)
6 users (show)
khuey: in‑testsuite+
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
An attempt to support nsIDOMBlob in an extension (4.45 KB, application/x-javascript)
2011-04-05 15:16 PDT, Jan Gerber
no flags Details
Patch (11.90 KB, patch)
2011-06-29 14:30 PDT, Kyle Huey [:khuey] (khuey@mozilla.com)
mrbkap: review+
Details | Diff | Review

Description Jan Gerber 2011-04-04 00:51:51 PDT
User-Agent:       Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0
Build Identifier: 

It would be nice to be able to return nsIDOMBlob objects or implement nsIDOMBlob inside a custom xpcom element for an extension to make files/data available inside the DOM. i.e. like this:
 f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
 f.initWithPath(..);
 f.QueryInterface(Ci.nsIDOMBlob)

Right now there is no way to create an nsIDOMBlob from an nsILocalFile.
Trying to implement a Javascript XPCOM element with nsIDOMBlob interface was also seams to be impossbile since i.e nsIPartialFileInputStream is not abailable:

 Cc["@mozilla.org/network/partial-file-input-stream;1"]
   .createInstance(Ci.nsIPartialFileInputStream);

Its is also not quite clear how one could add a moz-filedata: url to implement
getInternalUrl in Kavascript.



Reproducible: Always
Comment 1 Jan Gerber 2011-04-05 15:15:03 PDT
ongoing attempt to implement nsIDOMBlob in a JavaScript component,
it does not seam to call my getInternalUrl function. 
[Exception... "Component returned failure code: 0x804b000a (NS_ERROR_MALFORMED_URI) [nsIDOMFileReader.readAsBinaryString]" nsresult: "0x804b000a (NS_ERROR_MALFORMED_URI)"]

even if getInternalUrl would be called, it does not seam to be possible to add url to 
nsFileDataProtocolHandler from Javascript,
namely calling:
 nsFileDataProtocolHandler::AddFileDataEntry(url, this, aPrincipal);
Comment 2 Jan Gerber 2011-04-05 15:16:01 PDT
Created attachment 524096 [details]
An attempt to support nsIDOMBlob in an extension
Comment 3 Jonas Sicking (:sicking) PTO Until July 5th 2011-04-05 22:09:00 PDT
Is this a dupe of bug 607114?
Comment 4 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-04-05 23:34:56 PDT
Yes, accept Bug 607114 would have nicer syntax :-)

*** This bug has been marked as a duplicate of bug 607114 ***
Comment 5 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-05-12 06:40:08 PDT
We didn't do this for components ... maybe we should.
Comment 6 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-05-12 09:56:12 PDT
This shouldn't be too hard to do, but I don't know if I should be adding the code to nsXPConnect::InitClasses, nsXPConnect::InitClassesWithNewWrappedGlobal, or both.

Blake?
Comment 7 Blake Kaplan (:mrbkap) (please use needinfo!) 2011-05-18 10:10:03 PDT
On IRC, I floated the idea of just using a contract ID for this. But as that would be awkward, the answer to Kyle's question is that you need to add it to both.

You do need to be a little careful that you don't add it to Windows or ChromeWindows, though... I can't think of a clean way of doing that off the top of my head without checking the JSClass's name field :-/

Another option that might be cleaner would be to figure out a way to add the constructor in mozJSComponentLoader, the way that we add atob and dump to components' global objects.
Comment 8 Jan Gerber 2011-05-31 15:30:14 PDT
Was any decision made how to move this forward?
Comment 9 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-05-31 16:41:40 PDT
(In reply to comment #8)
> Was any decision made how to move this forward?

Yeah, it should be pretty straightforward to add a constructor to the JS component loader.  I just need to sit down and do it.
Comment 10 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-06-29 14:30:32 PDT
Created attachment 542950 [details] [diff] [review]
Patch
Comment 11 Blake Kaplan (:mrbkap) (please use needinfo!) 2011-07-05 17:50:19 PDT
Comment on attachment 542950 [details] [diff] [review]
Patch

Review of attachment 542950 [details] [diff] [review]:
-----------------------------------------------------------------

::: js/src/xpconnect/loader/mozJSComponentLoader.cpp
@@ +260,5 @@
> +        return JS_FALSE;
> +    }
> +
> +    nsCOMPtr<nsIXPConnect> xpc =
> +        do_GetService(kXPConnectServiceContractID, &rv);

Why not use nsXPConnect::GetXPConnect()?

@@ +272,5 @@
> +        XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
> +        return JS_FALSE;
> +    }
> +
> +    JSObject* glob = JS_GetGlobalForObject(cx, scope);

You can just use JS_GetGlobalForScopeChain here.

@@ +278,5 @@
> +    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
> +    jsval retval;
> +    rv = xpc->WrapNativeToJSVal(cx, glob, native, nsnull,
> +                                &NS_GET_IID(nsISupports),
> +                                PR_FALSE, &retval, nsnull);

It looks like your passing PR_FALSE for aAllowWrapping. I think that should be PR_TRUE.
Comment 12 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-07-05 17:57:08 PDT
(In reply to comment #11)
> Comment on attachment 542950 [details] [diff] [review] [review]
> Patch
> 
> Review of attachment 542950 [details] [diff] [review] [review]:
> -----------------------------------------------------------------
> 
> ::: js/src/xpconnect/loader/mozJSComponentLoader.cpp
> @@ +260,5 @@
> > +        return JS_FALSE;
> > +    }
> > +
> > +    nsCOMPtr<nsIXPConnect> xpc =
> > +        do_GetService(kXPConnectServiceContractID, &rv);
> 
> Why not use nsXPConnect::GetXPConnect()?

Oh hey, that's neat.

> @@ +272,5 @@
> > +        XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
> > +        return JS_FALSE;
> > +    }
> > +
> > +    JSObject* glob = JS_GetGlobalForObject(cx, scope);
> 
> You can just use JS_GetGlobalForScopeChain here.

That's neat too.

> @@ +278,5 @@
> > +    nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
> > +    jsval retval;
> > +    rv = xpc->WrapNativeToJSVal(cx, glob, native, nsnull,
> > +                                &NS_GET_IID(nsISupports),
> > +                                PR_FALSE, &retval, nsnull);
> 
> It looks like your passing PR_FALSE for aAllowWrapping. I think that should
> be PR_TRUE.

ok.
Comment 13 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-07-06 09:37:09 PDT
http://hg.mozilla.org/projects/build-system/rev/e6220bfcfd17
Comment 14 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-07-06 18:15:40 PDT
http://hg.mozilla.org/mozilla-central/rev/e6220bfcfd17
Comment 15 Eric Shepherd [:sheppy] 2011-10-17 14:35:00 PDT
Am I correct that the change here is that you can now do "new File" in chrome code to create a DOM File object?
Comment 16 Kyle Huey [:khuey] (khuey@mozilla.com) 2011-10-17 14:38:45 PDT
You can do new File in component scopes to create a DOM File object.  This has worked in chrome window scopes previously.
Comment 17 Eric Shepherd [:sheppy] 2011-10-17 14:48:00 PDT
Documentation updated:

https://developer.mozilla.org/en/DOM/File#Gecko_notes
https://developer.mozilla.org/en/Extensions/Using_the_DOM_File_API_in_chrome_code#Notes

Also mentioned on Firefox 8 for developers.
Comment 18 steve.chung7 2013-11-04 18:18:47 PST
So how do you create an nsIDOMBlob object...?

Something like Cc["@mozilla.org/files/blob;1"].createInstance(Ci.nsIDOMBlob, [oFileBody], { type: "text/xml"});?

Thanks.
Comment 19 Kyle Huey [:khuey] (khuey@mozilla.com) 2013-11-04 18:19:56 PST
new Blob(arguments);
Comment 20 steve.chung7 2013-11-04 18:24:14 PST
Are you sure...?

I'm pretty sure I have to use XPCOM stuff...

[21:22:41.674] ReferenceError: Blob is not defined @ resource://gre/modules/XPIProvider.jsm -> jar:file:///Users/main/Library/Application%20Support/Firefox/Profiles/xfm2tq4r.default/extensions/jid0-AaE9xBnbY78IjyBB6jwVpaKiyKQ@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid0-aae9xbnby78ijybb6jwvpakiykq-at-jetpack/s12chung/lib/main.js:32
Comment 21 steve.chung7 2013-11-04 18:26:39 PST
funny. someone just answered it on stackoverflow: http://stackoverflow.com/questions/19780396/use-blob-on-firefox-add-on

thanks for getting back so quickly though :). happy monday.
Comment 22 Kyle Huey [:khuey] (khuey@mozilla.com) 2013-11-04 18:29:04 PST
Oh ... that's some weird SDK thing then.

Note You need to log in before you can comment on or make changes to this bug.