1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Java XPCOM Bindings.
*
* The Initial Developer of the Original Code is IBM Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* IBM Corporation. All Rights Reserved.
*
* Contributor(s):
* Javier Pedemonte (jhpedemonte@gmail.com)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsJavaInterfaces.h"
#include "nsJavaWrapper.h"
#include "nsJavaXPCOMBindingUtils.h"
#include "nsJavaXPTCStub.h"
#include "nsIComponentRegistrar.h"
#include "nsString.h"
#include "nsISimpleEnumerator.h"
#include "nsIInterfaceInfoManager.h"
#include "nsIInputStream.h"
#include "nsEnumeratorUtils.h"
#include "nsArray.h"
#include "nsAppFileLocProviderProxy.h"
#include "nsIEventQueueService.h"
#include "nsXULAppAPI.h"
#include "nsILocalFile.h"
#ifdef XP_MACOSX
#include "jawt.h"
#endif
// profile support
#include "nsIObserverService.h"
#include "nsIProfileChangeStatus.h"
#include "jsapi.h"
#include "nsIJSContextStack.h"
#include "nsIToolkitProfile.h"
PRBool profileNotified = PR_FALSE;
extern nsresult
NS_LockProfilePath(nsILocalFile* aPath, nsILocalFile* aTempPath,
nsIProfileUnlocker* *aUnlocker, nsIProfileLock* *aResult);
class ProfileChangeStatusImpl : public nsIProfileChangeStatus
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIPROFILECHANGESTATUS
ProfileChangeStatusImpl() { }
private:
~ProfileChangeStatusImpl() { }
};
extern "C" NS_EXPORT void JNICALL
MOZILLA_NATIVE(initialize) (JNIEnv* env, jobject)
{
if (!InitializeJavaGlobals(env)) {
jclass clazz =
env->FindClass("org/mozilla/xpcom/XPCOMInitializationException");
if (clazz) {
env->ThrowNew(clazz, "Failed to initialize JavaXPCOM");
}
}
}
nsresult
InitEmbedding_Impl(JNIEnv* env, jobject aLibXULDirectory,
jobject aAppDirectory, jobject aAppDirProvider)
{
nsresult rv;
// create an nsILocalFile from given java.io.File
nsCOMPtr<nsILocalFile> libXULDir;
if (aLibXULDirectory) {
rv = File_to_nsILocalFile(env, aLibXULDirectory, getter_AddRefs(libXULDir));
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsILocalFile> appDir;
if (aAppDirectory) {
rv = File_to_nsILocalFile(env, aAppDirectory, getter_AddRefs(appDir));
NS_ENSURE_SUCCESS(rv, rv);
}
// create nsAppFileLocProviderProxy from given Java object
nsCOMPtr<nsIDirectoryServiceProvider> provider;
if (aAppDirProvider) {
rv = NS_NewAppFileLocProviderProxy(aAppDirProvider,
getter_AddRefs(provider));
NS_ENSURE_SUCCESS(rv, rv);
}
// init libXUL
return XRE_InitEmbedding(libXULDir, appDir, provider, nsnull, 0);
}
extern "C" NS_EXPORT void JNICALL
GRE_NATIVE(initEmbedding) (JNIEnv* env, jobject, jobject aLibXULDirectory,
jobject aAppDirectory, jobject aAppDirProvider)
{
nsresult rv = InitEmbedding_Impl(env, aLibXULDirectory, aAppDirectory,
aAppDirProvider);
if (NS_FAILED(rv)) {
ThrowException(env, rv, "Failure in initEmbedding");
FreeJavaGlobals(env);
}
}
extern "C" NS_EXPORT void JNICALL
GRE_NATIVE(termEmbedding) (JNIEnv *env, jobject)
{
if (profileNotified) {
nsCOMPtr<nsIObserverService> obssvc
(do_GetService("@mozilla.org/observer-service;1"));
NS_ASSERTION(obssvc, "No observer service?");
if (obssvc) {
nsCOMPtr<nsIProfileChangeStatus> cs = new ProfileChangeStatusImpl();
static const PRUnichar kShutdownPersist[] =
{'s','h','u','t','d','o','w','n','-','p','e','r','s','i','s','t','\0'};
obssvc->NotifyObservers(cs, "profile-change-net-teardown",
kShutdownPersist);
obssvc->NotifyObservers(cs, "profile-change-teardown", kShutdownPersist);
// Now that things are torn down, force JS GC so that things
// which depend on resources which are about to go away in
// "profile-before-change" are destroyed first.
nsCOMPtr<nsIThreadJSContextStack> stack
(do_GetService("@mozilla.org/js/xpc/ContextStack;1"));
if (stack)
{
JSContext *cx = nsnull;
stack->GetSafeJSContext(&cx);
if (cx)
::JS_GC(cx);
}
// Notify observers of a profile change
obssvc->NotifyObservers(cs, "profile-before-change", kShutdownPersist);
}
profileNotified = PR_FALSE;
}
// Free globals before calling XRE_TermEmbedding(), since we need some
// XPCOM services.
FreeJavaGlobals(env);
XRE_TermEmbedding();
}
extern "C" NS_EXPORT jobject JNICALL
GRE_NATIVE(lockProfileDirectory) (JNIEnv* env, jobject, jobject aDirectory)
{
nsresult rv = NS_ERROR_FAILURE;
if (aDirectory) {
nsCOMPtr<nsILocalFile> profileDir;
rv = File_to_nsILocalFile(env, aDirectory, getter_AddRefs(profileDir));
if (NS_SUCCEEDED(rv)) {
nsIProfileLock* lock;
rv = NS_LockProfilePath(profileDir, nsnull, nsnull, &lock);
if (NS_SUCCEEDED(rv)) {
jclass clazz =
env->FindClass("org/mozilla/xpcom/ProfileLock");
if (clazz) {
jmethodID mid = env->GetMethodID(clazz, "<init>", "(J)V");
if (mid) {
return env->NewObject(clazz, mid, NS_REINTERPRET_CAST(jlong, lock));
}
}
// if we get here, then something failed
rv = NS_ERROR_FAILURE;
}
}
}
ThrowException(env, rv, "Failure in lockProfileDirectory");
return nsnull;
}
extern "C" NS_EXPORT void JNICALL
GRE_NATIVE(notifyProfile) (JNIEnv *env, jobject)
{
if (!profileNotified) {
nsCOMPtr<nsIObserverService> obsSvc
(do_GetService("@mozilla.org/observer-service;1"));
if (obsSvc) {
profileNotified = PR_TRUE;
static const PRUnichar kStartup[] = {'s','t','a','r','t','u','p','\0'};
obsSvc->NotifyObservers(nsnull, "profile-do-change", kStartup);
obsSvc->NotifyObservers(nsnull, "profile-after-change", kStartup);
}
}
}
nsresult
InitXPCOM_Impl(JNIEnv* env, jobject aMozBinDirectory,
jobject aAppFileLocProvider, jobject* aResult)
{
nsresult rv;
// create an nsILocalFile from given java.io.File
nsCOMPtr<nsILocalFile> directory;
if (aMozBinDirectory) {
rv = File_to_nsILocalFile(env, aMozBinDirectory, getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
}
// create nsAppFileLocProviderProxy from given Java object
nsCOMPtr<nsIDirectoryServiceProvider> provider;
if (aAppFileLocProvider) {
rv = NS_NewAppFileLocProviderProxy(aAppFileLocProvider,
getter_AddRefs(provider));
NS_ENSURE_SUCCESS(rv, rv);
}
// init XPCOM
nsCOMPtr<nsIServiceManager> servMan;
rv = NS_InitXPCOM2(getter_AddRefs(servMan), directory, provider);
NS_ENSURE_SUCCESS(rv, rv);
// init Event Queue
nsCOMPtr<nsIEventQueueService> eventQService =
do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = eventQService->CreateThreadEventQueue();
NS_ENSURE_SUCCESS(rv, rv);
// create Java proxy for service manager returned by NS_InitXPCOM2
return GetNewOrUsedJavaObject(env, servMan, NS_GET_IID(nsIServiceManager),
nsnull, aResult);
}
extern "C" NS_EXPORT jobject JNICALL
XPCOM_NATIVE(initXPCOM) (JNIEnv* env, jobject, jobject aMozBinDirectory,
jobject aAppFileLocProvider)
{
jobject servMan;
nsresult rv = InitXPCOM_Impl(env, aMozBinDirectory, aAppFileLocProvider,
&servMan);
if (NS_SUCCEEDED(rv))
return servMan;
ThrowException(env, rv, "Failure in initXPCOM");
FreeJavaGlobals(env);
return nsnull;
}
extern "C" NS_EXPORT void JNICALL
XPCOM_NATIVE(shutdownXPCOM) (JNIEnv *env, jobject, jobject aServMgr)
{
nsresult rv;
nsIServiceManager* servMgr = nsnull;
if (aServMgr) {
// Get native XPCOM instance
rv = GetNewOrUsedXPCOMObject(env, aServMgr, NS_GET_IID(nsIServiceManager),
(nsISupports**) &servMgr);
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get XPCOM obj for ServiceMgr.");
// Even if we failed to get the matching xpcom object, we don't abort this
// function. Just call NS_ShutdownXPCOM with a null service manager.
}
// Free globals before calling NS_ShutdownXPCOM(), since we need some
// XPCOM services.
FreeJavaGlobals(env);
rv = NS_ShutdownXPCOM(servMgr);
if (NS_FAILED(rv))
ThrowException(env, rv, "NS_ShutdownXPCOM failed");
}
extern "C" NS_EXPORT jobject JNICALL
XPCOM_NATIVE(newLocalFile) (JNIEnv *env, jobject, jstring aPath,
jboolean aFollowLinks)
{
// Create a Mozilla string from the jstring
const PRUnichar* buf = nsnull;
if (aPath) {
buf = env->GetStringChars(aPath, nsnull);
if (!buf)
return nsnull; // exception already thrown
}
nsAutoString path_str(buf);
env->ReleaseStringChars(aPath, buf);
// Make call to given function
nsCOMPtr<nsILocalFile> file;
nsresult rv = NS_NewLocalFile(path_str, aFollowLinks, getter_AddRefs(file));
if (NS_SUCCEEDED(rv)) {
jobject javaProxy;
rv = GetNewOrUsedJavaObject(env, file, NS_GET_IID(nsILocalFile),
nsnull, &javaProxy);
if (NS_SUCCEEDED(rv))
return javaProxy;
}
ThrowException(env, rv, "Failure in newLocalFile");
return nsnull;
}
extern "C" NS_EXPORT jobject JNICALL
XPCOM_NATIVE(getComponentManager) (JNIEnv *env, jobject)
{
// Call XPCOM method
nsCOMPtr<nsIComponentManager> cm;
nsresult rv = NS_GetComponentManager(getter_AddRefs(cm));
if (NS_SUCCEEDED(rv)) {
jobject javaProxy;
rv = GetNewOrUsedJavaObject(env, cm, NS_GET_IID(nsIComponentManager),
nsnull, &javaProxy);
if (NS_SUCCEEDED(rv))
return javaProxy;
}
ThrowException(env, rv, "Failure in getComponentManager");
return nsnull;
}
extern "C" NS_EXPORT jobject JNICALL
XPCOM_NATIVE(getComponentRegistrar) (JNIEnv *env, jobject)
{
// Call XPCOM method
nsCOMPtr<nsIComponentRegistrar> cr;
nsresult rv = NS_GetComponentRegistrar(getter_AddRefs(cr));
if (NS_SUCCEEDED(rv)) {
jobject javaProxy;
rv = GetNewOrUsedJavaObject(env, cr, NS_GET_IID(nsIComponentRegistrar),
nsnull, &javaProxy);
if (NS_SUCCEEDED(rv))
return javaProxy;
}
ThrowException(env, rv, "Failure in getComponentRegistrar");
return nsnull;
}
extern "C" NS_EXPORT jobject JNICALL
XPCOM_NATIVE(getServiceManager) (JNIEnv *env, jobject)
{
// Call XPCOM method
nsCOMPtr<nsIServiceManager> sm;
nsresult rv = NS_GetServiceManager(getter_AddRefs(sm));
if (NS_SUCCEEDED(rv)) {
jobject javaProxy;
rv = GetNewOrUsedJavaObject(env, sm, NS_GET_IID(nsIServiceManager),
nsnull, &javaProxy);
if (NS_SUCCEEDED(rv))
return javaProxy;
}
ThrowException(env, rv, "Failure in getServiceManager");
return nsnull;
}
#ifdef XP_MACOSX
extern PRUint64 GetPlatformHandle(JAWT_DrawingSurfaceInfo* dsi);
#endif
extern "C" NS_EXPORT jlong JNICALL
MOZILLA_NATIVE(getNativeHandleFromAWT) (JNIEnv* env, jobject clazz,
jobject widget)
{
PRUint64 handle = 0;
#ifdef XP_MACOSX
JAWT awt;
awt.version = JAWT_VERSION_1_4;
jboolean result = JAWT_GetAWT(env, &awt);
if (result == JNI_FALSE)
return 0;
JAWT_DrawingSurface* ds = awt.GetDrawingSurface(env, widget);
if (ds != nsnull) {
jint lock = ds->Lock(ds);
if (!(lock & JAWT_LOCK_ERROR)) {
JAWT_DrawingSurfaceInfo* dsi = ds->GetDrawingSurfaceInfo(ds);
if (dsi) {
handle = GetPlatformHandle(dsi);
ds->FreeDrawingSurfaceInfo(dsi);
}
ds->Unlock(ds);
}
awt.FreeDrawingSurface(ds);
}
#else
NS_WARNING("getNativeHandleFromAWT JNI method not implemented");
#endif
return handle;
}
extern "C" NS_EXPORT jlong JNICALL
JXUTILS_NATIVE(wrapJavaObject) (JNIEnv* env, jobject, jobject aJavaObject,
jstring aIID)
{
nsresult rv;
nsISupports* xpcomObject = nsnull;
if (!aJavaObject || !aIID) {
rv = NS_ERROR_NULL_POINTER;
} else {
const char* str = env->GetStringUTFChars(aIID, nsnull);
if (!str) {
rv = NS_ERROR_OUT_OF_MEMORY;
} else {
nsID iid;
if (iid.Parse(str)) {
rv = GetNewOrUsedXPCOMObject(env, aJavaObject, iid, &xpcomObject);
} else {
rv = NS_ERROR_INVALID_ARG;
}
env->ReleaseStringUTFChars(aIID, str);
}
}
if (NS_FAILED(rv)) {
ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
}
return NS_REINTERPRET_CAST(jlong, xpcomObject);
}
extern "C" NS_EXPORT jobject JNICALL
JXUTILS_NATIVE(wrapXPCOMObject) (JNIEnv* env, jobject, jlong aXPCOMObject,
jstring aIID)
{
nsresult rv;
jobject javaObject = nsnull;
nsISupports* xpcomObject = NS_REINTERPRET_CAST(nsISupports*, aXPCOMObject);
if (!xpcomObject || !aIID) {
rv = NS_ERROR_NULL_POINTER;
} else {
const char* str = env->GetStringUTFChars(aIID, nsnull);
if (!str) {
rv = NS_ERROR_OUT_OF_MEMORY;
} else {
nsID iid;
if (iid.Parse(str)) {
// XXX Should we be passing something other than NULL for aObjectLoader?
rv = GetNewOrUsedJavaObject(env, xpcomObject, iid, nsnull, &javaObject);
} else {
rv = NS_ERROR_INVALID_ARG;
}
env->ReleaseStringUTFChars(aIID, str);
}
}
if (NS_FAILED(rv)) {
ThrowException(env, rv, "Failed to create XPCOM proxy for Java object");
}
return javaObject;
}
|