lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / jvmfwk / plugins / sunmajor / pluginlib / util_cocoa.mm
blob5a5d7303a18a3b0dbbab4591fa62dcd450e82079
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 #include <rtl/ustring.hxx>
5 #include <premac.h>
6 #import <Foundation/Foundation.h>
7 #include <postmac.h>
9 #import "util_cocoa.hxx"
11 using namespace rtl;
13 bool JvmfwkUtil_isLoadableJVM( OUString const & aURL )
15     bool bRet = false;
17     if ( aURL.getLength() )
18     {
19         NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
21         NSString *pString = [NSString stringWithCharacters:reinterpret_cast<unichar const *>(aURL.getStr()) length:aURL.getLength()];
22         if ( pString )
23         {
24             NSURL *pURL = nil;
26             // Ignore all but Oracle's JDK as loading Apple's Java and Oracle's
27             // JRE will cause OS X's JavaVM framework to display a dialog and
28             // invoke exit() when loaded via JNI on OS X 10.10
29             NSURL *pTmpURL = [NSURL URLWithString:pString];
30             if ( pTmpURL )
31                 pTmpURL = [pTmpURL filePathURL];
32             if ( pTmpURL )
33                 pTmpURL = [pTmpURL URLByStandardizingPath];
34             if ( pTmpURL )
35                 pTmpURL = [pTmpURL URLByResolvingSymlinksInPath];
36             if ( pTmpURL )
37             {
38                 NSURL *pJVMsDirURL = [NSURL URLWithString:@"file:///Library/Java/JavaVirtualMachines/"];
39                 if ( pJVMsDirURL )
40                     pJVMsDirURL= [pJVMsDirURL filePathURL];
41                 if ( pJVMsDirURL )
42                     pJVMsDirURL = [pJVMsDirURL URLByStandardizingPath];
43                 // The JVM directory must not contain softlinks or the JavaVM
44                 // framework bug will occur so don't resolve softlinks in the
45                 // JVM directory
46                 if ( pJVMsDirURL )
47                 {
48                     NSString *pTmpURLString = [pTmpURL absoluteString];
49                     NSString *pJVMsDirURLString = [pJVMsDirURL absoluteString];
50                     if ( pTmpURLString && pJVMsDirURLString && [pJVMsDirURLString length] )
51                     {
52                         NSRange aJVMsDirURLRange = [pTmpURLString rangeOfString:pJVMsDirURLString];
53                         if ( !aJVMsDirURLRange.location && aJVMsDirURLRange.length )
54                             pURL = pTmpURL;
55                     }
56                 }
57             }
59             while ( pURL )
60             {
61                 // Check if this is a valid bundle
62                 NSNumber *pDir = nil;
63                 NSURL *pContentsURL = [pURL URLByAppendingPathComponent:@"Contents"];
64                 if ( pContentsURL && [pContentsURL getResourceValue:&pDir forKey:NSURLIsDirectoryKey error:nil] && pDir && [pDir boolValue] )
65                 {
66                     NSBundle *pBundle = [NSBundle bundleWithURL:pURL];
67                     if ( pBundle )
68                     {
69                         // Make sure that this bundle's Info.plist has the
70                         // proper JVM keys to supports loading via JNI. If
71                         // this bundle is a valid JVM and these keys
72                         // are missing, loading the JVM will cause OS X's
73                         // JavaVM framework to display a dialog and invoke
74                         // exit() when loaded via JNI on OS X 10.10.
75                         NSDictionary *pInfo = [pBundle infoDictionary];
76                         if ( pInfo )
77                         {
78                             NSDictionary *pJavaVM = [pInfo objectForKey:@"JavaVM"];
79                             if ( pJavaVM && [pJavaVM isKindOfClass:[NSDictionary class]] )
80                             {
81                                 NSArray *pJVMCapabilities = [pJavaVM objectForKey:@"JVMCapabilities"];
82                                 if ( pJVMCapabilities )
83                                 {
84                                     if ( [pJVMCapabilities indexOfObjectIdenticalTo:@"JNI"] == NSNotFound )
85                                     {
86                                         if ( [pJVMCapabilities isKindOfClass:[NSMutableArray class]] )
87                                         {
88                                             [static_cast<NSMutableArray *>(pJVMCapabilities) addObject:@"JNI"];
89                                             bRet = true;
90                                         }
91                                         else if ( [pJavaVM isKindOfClass:[NSMutableDictionary class]] )
92                                         {
93                                             NSMutableArray *pNewJVMCapabilities = [NSMutableArray arrayWithCapacity:[pJVMCapabilities count] + 1];
94                                             if ( pNewJVMCapabilities )
95                                             {
96                                                 [pNewJVMCapabilities addObject:@"JNI"];
97                                                 [static_cast<NSMutableDictionary *>(pJavaVM) setObject:pNewJVMCapabilities forKey:@"JVMCapabilities"];
98                                                 bRet = true;
99                                             }
100                                         }
101                                     }
102                                     else
103                                     {
104                                         bRet = true;
105                                     }
106                                 }
107                             }
108                         }
109                     }
110                 }
112                 NSURL *pOldURL = pURL;
113                 pURL = [pURL URLByDeletingLastPathComponent];
114                 if ( pURL )
115                 {
116                     pURL = [pURL URLByStandardizingPath];
117                     if ( pURL )
118                     {
119                         pURL = [pURL URLByResolvingSymlinksInPath];
120                         if ( pURL && [pURL isEqual:pOldURL] )
121                             pURL = nil;
122                     }
123                 }
124             }
125         }
127         [pPool release];
128     }
130     return bRet;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */