~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/modules/plugin/base/src/nsPluginsDirOS2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public License
 
3
 * Version 1.1 (the "License"); you may not use this file except in
 
4
 * compliance with the License. You may obtain a copy of the License at
 
5
 * http://www.mozilla.org/MPL/
 
6
 *
 
7
 * Software distributed under the License is distributed on an "AS IS"
 
8
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 
9
 * License for the specific language governing rights and limitations
 
10
 * under the License.
 
11
 *
 
12
 * The Original Code is the Mozilla OS/2 libraries.
 
13
 *
 
14
 * The Initial Developer of the Original Code is John Fairhurst,
 
15
 * <john_fairhurst@iname.com>.  Portions created by John Fairhurst are
 
16
 * Copyright (C) 1998 John Fairhurst. All Rights Reserved.
 
17
 *
 
18
 * Contributor(s): 
 
19
 *
 
20
 */
 
21
 
 
22
// OS/2 plugin-loading code.
 
23
 
 
24
#define INCL_DOS
 
25
#define INCL_DOSERRORS
 
26
#include <os2.h>
 
27
 
 
28
#include "nsPluginsDir.h"
 
29
#include "prlink.h"
 
30
#include "plstr.h"
 
31
#include "prmem.h"
 
32
#include "nsPluginDefs.h"
 
33
 
 
34
#include "nsString.h"
 
35
 
 
36
/* Load a string stored as RCDATA in a resource segment */
 
37
/* Returned string needs to be PR_Free'd by caller */
 
38
static char *LoadRCDATAString( HMODULE hMod, ULONG resid)
 
39
{
 
40
   APIRET rc;
 
41
   ULONG  ulSize = 0;
 
42
   char  *string = 0;
 
43
 
 
44
   rc = DosQueryResourceSize( hMod, RT_RCDATA, resid, &ulSize);
 
45
 
 
46
   if( rc == NO_ERROR)
 
47
   {
 
48
      char *readOnlyString = 0;
 
49
      rc = DosGetResource( hMod, RT_RCDATA, resid, (void**) &readOnlyString);
 
50
 
 
51
      /* allow for 0-termination if user hasn't got it right */
 
52
      if( readOnlyString[ ulSize - 1] != '\0')
 
53
         ulSize++;
 
54
 
 
55
      if( rc == NO_ERROR)
 
56
      {
 
57
         /* copy string & zero-terminate */
 
58
         string = (char*) PR_Malloc( ulSize);
 
59
         memcpy( string, readOnlyString, ulSize - 1);
 
60
         string[ ulSize - 1] = '\0';
 
61
 
 
62
         DosFreeResource( readOnlyString);
 
63
      }
 
64
   }
 
65
 
 
66
   return string;
 
67
}
 
68
 
 
69
static PRUint32 CalculateVariantCount(char* mimeTypes)
 
70
{
 
71
  PRUint32 variants = 1;
 
72
 
 
73
  if(mimeTypes == nsnull)
 
74
    return 0;
 
75
 
 
76
  char* index = mimeTypes;
 
77
  while (*index)
 
78
  {
 
79
    if (*index == '|')
 
80
    variants++;
 
81
 
 
82
    ++index;
 
83
  }
 
84
  return variants;
 
85
}
 
86
 
 
87
static char** MakeStringArray(PRUint32 variants, char* data)
 
88
{
 
89
  if((variants <= 0) || (data == nsnull))
 
90
    return nsnull;
 
91
 
 
92
  char ** array = (char **)PR_Calloc(variants, sizeof(char *));
 
93
  if(array == nsnull)
 
94
    return nsnull;
 
95
 
 
96
  char * start = data;
 
97
  for(PRUint32 i = 0; i < variants; i++)
 
98
  {
 
99
    char * p = PL_strchr(start, '|');
 
100
    if(p != nsnull)
 
101
      *p = 0;
 
102
 
 
103
    array[i] = PL_strdup(start);
 
104
    start = ++p;
 
105
  }
 
106
  return array;
 
107
}
 
108
 
 
109
static void FreeStringArray(PRUint32 variants, char ** array)
 
110
{
 
111
  if((variants == 0) || (array == nsnull))
 
112
    return;
 
113
 
 
114
  for(PRUint32 i = 0; i < variants; i++)
 
115
  {
 
116
    if(array[i] != nsnull)
 
117
    {
 
118
      PL_strfree(array[i]);
 
119
      array[i] = nsnull;
 
120
    }
 
121
  }
 
122
  PR_Free(array);
 
123
}
 
124
 
 
125
// nsPluginsDir class
 
126
 
 
127
PRBool nsPluginsDir::IsPluginFile(nsIFile* file)
 
128
{
 
129
    nsCAutoString leaf;
 
130
    if (NS_FAILED(file->GetNativeLeafName(leaf)))
 
131
        return PR_FALSE;
 
132
 
 
133
    const char *leafname = leaf.get();
 
134
    
 
135
    if( nsnull != leafname)
 
136
    {
 
137
      int len = strlen( leafname);
 
138
      if( len > 6 &&                 // np*.dll
 
139
          (0 == strnicmp( &(leafname[len - 4]), ".dll", 4)) &&
 
140
          (0 == strnicmp( leafname, "np", 2)))
 
141
      {
 
142
        return PR_TRUE;
 
143
      }
 
144
    }
 
145
    return PR_FALSE;
 
146
}
 
147
 
 
148
// nsPluginFile implementation
 
149
 
 
150
nsPluginFile::nsPluginFile(nsIFile* file)
 
151
: mPlugin(file)
 
152
{}
 
153
 
 
154
nsPluginFile::~nsPluginFile()
 
155
{}
 
156
 
 
157
// Loads the plugin into memory using NSPR's shared-library loading
 
158
nsresult nsPluginFile::LoadPlugin( PRLibrary *&outLibrary)
 
159
{
 
160
    if (!mPlugin)
 
161
      return NS_ERROR_NULL_POINTER;
 
162
   
 
163
    nsCAutoString temp;
 
164
    mPlugin->GetNativePath(temp);
 
165
 
 
166
    outLibrary = PR_LoadLibrary(temp.get());
 
167
    return outLibrary == nsnull ? NS_ERROR_FAILURE : NS_OK;
 
168
}
 
169
 
 
170
// Obtains all of the information currently available for this plugin.
 
171
nsresult nsPluginFile::GetPluginInfo( nsPluginInfo &info)
 
172
{
 
173
   nsresult   rc = NS_ERROR_FAILURE;
 
174
   HMODULE    hPlug = 0; // Need a HMODULE to query resource statements
 
175
   char       failure[ CCHMAXPATH] = "";
 
176
   APIRET     ret;
 
177
 
 
178
   const char* path;
 
179
   nsCAutoString temp;
 
180
   mPlugin->GetNativePath(temp);
 
181
   path = temp.get();
 
182
   ret = DosLoadModule( failure, CCHMAXPATH, path, &hPlug);
 
183
 
 
184
   while( ret == NO_ERROR)
 
185
   {
 
186
      info.fPluginInfoSize = sizeof( nsPluginInfo);
 
187
 
 
188
      info.fName = LoadRCDATAString( hPlug, NS_INFO_ProductName);
 
189
 
 
190
      // get description (doesn't matter if it's missing)...
 
191
      info.fDescription = LoadRCDATAString( hPlug, NS_INFO_FileDescription);
 
192
 
 
193
      char * mimeType = LoadRCDATAString( hPlug, NS_INFO_MIMEType);
 
194
      if( nsnull == mimeType) break;
 
195
 
 
196
      char * mimeDescription = LoadRCDATAString( hPlug, NS_INFO_FileOpenName);
 
197
      if( nsnull == mimeDescription) break;
 
198
 
 
199
      char * extensions = LoadRCDATAString( hPlug, NS_INFO_FileExtents);
 
200
      if( nsnull == extensions) break;
 
201
 
 
202
      info.fVariantCount = CalculateVariantCount(mimeType);
 
203
 
 
204
      info.fMimeTypeArray = MakeStringArray(info.fVariantCount, mimeType);
 
205
      if( info.fMimeTypeArray == nsnull) break;
 
206
 
 
207
      info.fMimeDescriptionArray = MakeStringArray(info.fVariantCount, mimeDescription);
 
208
      if( nsnull == info.fMimeDescriptionArray) break;
 
209
 
 
210
      info.fExtensionArray = MakeStringArray(info.fVariantCount, extensions);
 
211
      if( nsnull == info.fExtensionArray) break;
 
212
 
 
213
      info.fFileName = PL_strdup(path);
 
214
 
 
215
      rc = NS_OK;
 
216
      break;
 
217
   }
 
218
 
 
219
   if( 0 != hPlug)
 
220
      DosFreeModule( hPlug);
 
221
 
 
222
   return rc;
 
223
}
 
224
 
 
225
nsresult nsPluginFile::FreePluginInfo(nsPluginInfo& info)
 
226
{
 
227
   if(info.fName != nsnull)
 
228
     PL_strfree(info.fName);
 
229
 
 
230
   if(info.fDescription != nsnull)
 
231
     PL_strfree(info.fDescription);
 
232
 
 
233
   if(info.fMimeTypeArray != nsnull)
 
234
     FreeStringArray(info.fVariantCount, info.fMimeTypeArray);
 
235
 
 
236
   if(info.fMimeDescriptionArray != nsnull)
 
237
     FreeStringArray(info.fVariantCount, info.fMimeDescriptionArray);
 
238
 
 
239
   if(info.fExtensionArray != nsnull)
 
240
     FreeStringArray(info.fVariantCount, info.fExtensionArray);
 
241
 
 
242
   memset((void *)&info, 0, sizeof(info));
 
243
 
 
244
   return NS_OK;
 
245
}