diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/ko/how_to_build_an_xpcom_component_in_javascript | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/ko/how_to_build_an_xpcom_component_in_javascript')
-rw-r--r-- | files/ko/how_to_build_an_xpcom_component_in_javascript/index.html | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/files/ko/how_to_build_an_xpcom_component_in_javascript/index.html b/files/ko/how_to_build_an_xpcom_component_in_javascript/index.html new file mode 100644 index 0000000000..a2f6f18b0c --- /dev/null +++ b/files/ko/how_to_build_an_xpcom_component_in_javascript/index.html @@ -0,0 +1,192 @@ +--- +title: How to Build an XPCOM Component in Javascript +slug: How_to_Build_an_XPCOM_Component_in_Javascript +tags: + - Add-ons + - Extensions + - XPCOM +--- +<p> +</p><p>본 문서는 자바스크립트에서 XPCOM 컴포넌트를 만드는 방법을 소개합니다. 이 문서에서는 XPCOM이 어떻게 움직이는지 혹은 그에 대한 코드는 다루지 않습니다. 자세한 사항은 <a href="ko/XPCOM">XPCOM</a>에서 아실 수 있습니다. 여기서는 실제로 이를 어떻게 <i>움직이게</i>하는 가에 달려 있습니다. +</p><p><br> +</p> +<h2 id=".EA.B5.AC.ED.98.84_.EB.B0.A9.EB.B2.95" name=".EA.B5.AC.ED.98.84_.EB.B0.A9.EB.B2.95"> 구현 방법 </h2> +<p>아래 예제는 "Hello World!"라는 메시지를 표시하는 간단한 방법입니다. +This example component will expose a single method, which returns the string "Hello World!". +</p> +<h3 id=".EC.9D.B8.ED.84.B0.ED.8E.98.EC.9D.B4.EC.8A.A4_.EC.9E.AC.EC.A0.95.EC.9D.98" name=".EC.9D.B8.ED.84.B0.ED.8E.98.EC.9D.B4.EC.8A.A4_.EC.9E.AC.EC.A0.95.EC.9D.98"> 인터페이스 재정의 </h3> +<p>If you want to use your component from JavaScript, or in other XPCOM components, you must define the interfaces that you want exposed (if you want to use your component <i>only</i> from JavaScript, you can use the <code><a href="ko/WrappedJSObject">wrappedJSObject</a></code> trick so that you don't need to generate an interface as described here). +</p><p>There are many interfaces already defined in Mozilla applications, so you may not need to define a new one. You can browse existing XPCOM interfaces at various locations in the Mozilla source code, or using <a class="external" href="http://xpcomviewer.mozdev.org/">XPCOMViewer</a>, a GUI for browsing registered interfaces and components. You can download an old version of XPCOMViewer that works with Firefox 1.5 from <a class="external" href="http://downloads.mozdev.org/xpcomviewer/">mozdev mirrors</a>. +</p><p>If an interface exists that meets your needs, then you do not need to write an IDL, or compile a typelib, and may skip to the <a href="#Creating_the_Component">next section</a>. +</p><p>If you don't find a suitable pre-existing interface, then you must define your own. XPCOM uses a dialect of IDL to define interfaces, called <a href="ko/XPIDL">XPIDL</a>. Here's the XPIDL definition for our HelloWorld component: +</p><p>HelloWorld.idl +</p> +<pre>#include "nsISupports.idl" + +[scriptable, uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)] +interface nsIHelloWorld : nsISupports +{ + string hello(); +}; +</pre> +<p>Note that you must generate a new UUID for each XPCOM component that you create. See <a href="ko/Generating_GUIDs">Generating GUIDs</a> for more information. +</p> +<h3 id="Compiling_the_Typelib" name="Compiling_the_Typelib"> Compiling the Typelib </h3> +<p>Your interface definition must be compiled into a binary format (XPT) in order to be registered and used within Mozilla applications. The compilation can be done using the Gecko SDK. You can learn how to get Mac, Linux, and Windows versions of the Gecko SDK by reading the article <a href="ko/Gecko_SDK">Gecko SDK</a>. +</p> +<div class="note"><b>Note:</b> On Windows if you download the Gecko SDK without the whole build tree, you will be missing some required DLLs for xpidl.exe and it will run with no errors but not do anything. To fix this download the <a class="external" href="http://ftp.mozilla.org/pub/mozilla.org/mozilla/source/wintools.zip">Mozilla build tools for Windows</a> and copy the DLLs from <tt>windows\bin\x86</tt> within the zip into the <tt>bin</tt> directory of the Gecko SDK.</div> +<div class="note"><b>Note:</b> The Mac version of the SDK provided for download is PowerPC-only. If you need an Intel version, you'll need to compile it yourself as described on that page.</div> +<p>Execute this command to compile the typelib. Here, <tt>{sdk_dir}</tt> is the directory in which you unpacked the Gecko SDK. +</p> +<pre class="eval">{sdk_dir}/bin/xpidl -m typelib -w -v -I {sdk_dir}/idl -e HelloWorld.xpt HelloWorld.idl +</pre> +<p><br> +</p> +<div class="note"><b>Note:</b> On Windows you must use forward slashes for the include path.</div> +<p>(The -I flag is an uppercase i, not a lowercase L.) This will create the typelib file HelloWorld.xpt in the current working directory. +</p> +<h3 id="Creating_the_Component" name="Creating_the_Component"> Creating the Component </h3> +<p>HelloWorld.js +</p> +<pre>/*********************************************************** +constants +***********************************************************/ + +// reference to the interface defined in nsIHelloWorld.idl +const nsIHelloWorld = Components.interfaces.nsIHelloWorld; + +// reference to the required base interface that all components must support +const nsISupports = Components.interfaces.nsISupports; + +// UUID uniquely identifying our component +// You can get from: http://kruithof.xs4all.nl/uuid/uuidgen here +const CLASS_ID = Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}"); + +// description +const CLASS_NAME = "My Hello World Javascript XPCOM Component"; + +// textual unique identifier +const CONTRACT_ID = "@dietrich.ganx4.com/helloworld;1"; + +/*********************************************************** +class definition +***********************************************************/ + +//class constructor +function HelloWorld() { +}; + +// class definition +HelloWorld.prototype = { + + // define the function we want to expose in our interface + hello: function() { + return "Hello World!"; + }, + + QueryInterface: function(aIID) + { + if (!aIID.equals(nsIHelloWorld) && + !aIID.equals(nsISupports)) + throw Components.results.NS_ERROR_NO_INTERFACE; + return this; + } +}; + +/*********************************************************** +class factory + +This object is a member of the global-scope Components.classes. +It is keyed off of the contract ID. Eg: + +myHelloWorld = Components.classes["@dietrich.ganx4.com/helloworld;1"]. + createInstance(Components.interfaces.nsIHelloWorld); + +***********************************************************/ +var HelloWorldFactory = { + createInstance: function (aOuter, aIID) + { + if (aOuter != null) + throw Components.results.NS_ERROR_NO_AGGREGATION; + return (new HelloWorld()).QueryInterface(aIID); + } +}; + +/*********************************************************** +module definition (xpcom registration) +***********************************************************/ +var HelloWorldModule = { + registerSelf: function(aCompMgr, aFileSpec, aLocation, aType) + { + aCompMgr = aCompMgr. + QueryInterface(Components.interfaces.nsIComponentRegistrar); + aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, + CONTRACT_ID, aFileSpec, aLocation, aType); + }, + + unregisterSelf: function(aCompMgr, aLocation, aType) + { + aCompMgr = aCompMgr. + QueryInterface(Components.interfaces.nsIComponentRegistrar); + aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation); + }, + + getClassObject: function(aCompMgr, aCID, aIID) + { + if (!aIID.equals(Components.interfaces.nsIFactory)) + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + + if (aCID.equals(CLASS_ID)) + return HelloWorldFactory; + + throw Components.results.NS_ERROR_NO_INTERFACE; + }, + + canUnload: function(aCompMgr) { return true; } +}; + +/*********************************************************** +module initialization + +When the application registers the component, this function +is called. +***********************************************************/ +function NSGetModule(aCompMgr, aFileSpec) { return HelloWorldModule; } + +</pre> +<h2 id="Installation" name="Installation"> Installation </h2> +<h3 id="For_extensions:" name="For_extensions:"> For extensions: </h3> +<ol><li> Copy HelloWorld.js and HelloWorld.xpt to {extensiondir}/components/ +</li><li> Delete compreg.dat and xpti.dat from your profile directory. +</li><li> Restart application +</li></ol> +<h3 id="For_Firefox" name="For_Firefox"> For Firefox </h3> +<ol><li> Copy HelloWorld.js and HelloWorld.xpt to the {objdir}/dist/bin/components directory, if running from the source. +</li><li> Delete compreg.dat and xpti.dat from the components directory. +</li><li> Delete compreg.dat and xpti.dat from your profile directory. +</li><li> Restart application +</li></ol> +<h2 id="Using_Your_Component" name="Using_Your_Component"> Using Your Component </h2> +<pre>try { + // this is needed to generally allow usage of components in javascript + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + + var myComponent = Components.classes['@dietrich.ganx4.com/helloworld;1'] + .createInstance(Components.interfaces.nsIHelloWorld); + + alert(myComponent.hello()); +} catch (anError) { + dump("ERROR: " + anError); +} +</pre> +<h2 id="Other_resources" name="Other_resources"> Other resources </h2> +<ul><li> Two mozillazine forums threads about implementing XPCOM components in JS with some explanations, example code, and troubleshooting tips: +<ul><li> <a class=" external" href="http://forums.mozillazine.org/viewtopic.php?t=308369" rel="freelink">http://forums.mozillazine.org/viewtopic.php?t=308369</a> +</li><li> <a class=" external" href="http://forums.mozillazine.org/viewtopic.php?t=367298" rel="freelink">http://forums.mozillazine.org/viewtopic.php?t=367298</a> +</li></ul> +</li><li> <a class="external" href="http://kb.mozillazine.org/Implementing_XPCOM_components_in_JavaScript">Implementing XPCOM components in JavaScript</a> at kb.mozillazine.org +</li><li> <a class="external" href="http://www.mozilla.org/scriptable/avoiding-leaks.html">Using XPCOM in JavaScript without leaking</a> - A must-read. +</li><li> <a class="external" href="http://lxr.mozilla.org/seamonkey/source/xpcom/sample/nsSample.js">An example component</a> +</li><li> <a class="external" href="http://www.mozilla.org/scriptable/js-components-status.html">Older JS+XPCOM notes</a> - includes some wrappedJSObject information. +</li></ul> +{{ languages( { "en": "en/How_to_Build_an_XPCOM_Component_in_Javascript", "ja": "ja/How_to_Build_an_XPCOM_Component_in_Javascript" } ) }} |