summaryrefslogtreecommitdiff
path: root/riscos/plugin.c
blob: 2497294b7b1745d0f81edcc98d260c1aec49bf10 (plain)
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
/**
 * $Id: plugin.c,v 1.10 2003/06/17 19:24:21 bursa Exp $
 */

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "netsurf/content/content.h"
#include "netsurf/render/html.h"
#include "netsurf/riscos/plugin.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"

#include "oslib/mimemap.h"

char* create_mime_from_ext(char* data);
char* create_sysvar(char* mime);
void plugin_fetch(struct plugin_object* po,
                  char* alias_sysvar/* vars here */);

/**
 * plugin_decode
 * Processes the contents of the plugin_object struct (defined in plugin.h)
 * in order to work out if NetSurf can handle the object.
 * For more information, read
 * http://www.ecs.soton.ac.uk/~jmb202/riscos/acorn/browse-plugins.html
 * as this code is based heavily on that description.
 */
void plugin_decode(struct content* content, char* url, struct box* box,
                  struct plugin_object* po) {


        content_type mime_type;
        bool can_handle = TRUE;
        char* alias_sysvar;


        if (po->data != NULL) {

                if (po->type != NULL) {

                        /* acquire NS mime type from actual mime type */
                        mime_type = content_lookup((const char*)po->type);
                }
                else {

                        /* create actual mime type - if we're wrong,
                         * it doesn't matter as the HTTP content-type
                         * header should override whatever we think.
                         * however, checking the header hasn't been
                         * implemented yet so it will just b0rk :(
                         */
                        po->type = strdup(create_mime_from_ext(po->data));

                        if (po->type != NULL)
                          mime_type = content_lookup((const char*)po->type);

                        else {

                          /* failed to create mime type, clean up and exit */
                          xfree(po);
                          can_handle = FALSE;
                        }

                }
        }
        else {

                /* no data so try using classid instead */

                if (po->classid != NULL) {

                        po->data = strdup(po->classid);

                        if (strnicmp(po->data,"clsid:",6) == 0) {

                                /* We can't handle ActiveX objects */
                                LOG(("Can't Handle ActiveX"));
                                xfree(po);
                                can_handle = FALSE;
                        }
                        else {

                                if (po->codetype != NULL) {

                                /* use codetype instead of type if we can */
                                        po->type = strdup(po->codetype);
                                        mime_type = content_lookup(
                                                 (const char*)po->codetype);
                                }
                                else {

                                /* try ye olde file extension munging */
                                        po->codetype = strdup(
                                            create_mime_from_ext(po->data));

                                        if (po->codetype != NULL) {

                                        /* well, it appeared to work... */
                                                mime_type = content_lookup(
                                                 (const char*)po->codetype);
                                                 po->type = strdup(
                                                              po->codetype);
                                        }
                                        else {

                                                  /* arse, failed. oh well */
                                                  xfree(po);
                                                  can_handle = FALSE;
                                        }
                                }
                        }
                }
                else {

                        /* we don't have sufficient data to handle this
                         * object :(
                         * TODO: start fetch anyway and check header.
                         *       if we can handle the content, continue
                         *       fetch and carry on as if the proper HTML
                         *       was written.
                         *       if we can't handle the content, stop fetch
                         *       and clean up.
                         */
                        xfree(po);
                        can_handle = FALSE;
                }
        }


        /* so, you think you can handle it do you? */
        if (can_handle == TRUE) {

                /* We think we can handle this object. Now check that
                 * we can.
                 * 1) Is it an image? Yes - send to image handler
                 *                    No - continue checking
                 * 2) Is a suitable Alias$... System Variable set?
                 *    Yes - invoke plugin
                 *    No - we can't handle it. Display alternative HTML
                 */


                /* TODO: There must be a better way than this...
                 *       Perhaps checking if the mime type begins "image/"
                 *       would be better?
                 */
                if (mime_type == CONTENT_JPEG || mime_type == CONTENT_PNG
                   || mime_type == CONTENT_GIF) {

                    /* OK, we have an image. Let's make the image handler
                     * deal with it.
                     */
                        xfree(po);
                        LOG(("sending data to image handler"));
                        html_fetch_object(content, url, box);
                        return;
                }
                else {  /* not an image; is sys var set? */

                        /* Create Alias variable */
                        alias_sysvar = create_sysvar(po->type);
                        if (alias_sysvar == NULL) {

                                /* oh dear, you can't handle it */
                                xfree(po);
                                xfree(alias_sysvar);
                                can_handle = FALSE;
                        }
                        else {

                                /* Right, we have a variable.
                                 * Does it actually exist?
                                 */
                                 int used;
                                 xos_read_var_val_size(
                                              (const char*)alias_sysvar,
                                              0, os_VARTYPE_STRING,
                                              &used, 0, os_VARTYPE_STRING);

                                if (used == 0) {

                                        /* no, doesn't exist */
                                        xfree(po);
                                        xfree(alias_sysvar);
                                        can_handle = FALSE;
                                }
                                else {
                                        /* yes, it exists */
                                        LOG(("%s exists", alias_sysvar));
                                        plugin_fetch(po, alias_sysvar/* insert vars here */);
                                }
                        }
                }
        }

        if (can_handle == FALSE) {

                /* Get alternative HTML as we can't handle the object */
                return;
        }
}

/**
 * create_mime_from_ext
 * attempts to create a mime type from the filename extension.
 * returns NULL if it fails.
 */

char* create_mime_from_ext(char* data){

        char* ret;
        os_error *e;

        LOG(("Creating Mime Type from File Extension"));

        ret = xcalloc(90, sizeof(char));
        ret = strrchr(data, '.');
        LOG(("Extension = %s", ret));

        /* Let's make the mime map module do the work for us */
        e = xmimemaptranslate_extension_to_mime_type((const char*)ret,
                                                      ret);
        LOG(("Mime Type = %s", ret));

        if (e != NULL) ret = NULL;

        return ret;
}

/**
 * create_sysvar
 * attempts to create a system variable of the form Alias$@PlugInType_XXX
 * where XXX is the filetype.
 * returns NULL if unsuccessful.
 */

char* create_sysvar(char* mime) {

        char* ret;
        char* ft;
        unsigned int* fv;
        os_error *e;

        LOG(("Creating System Variable from Mime Type"));

        ret = xcalloc(22, sizeof(char));
        ft = xcalloc(10, sizeof(char));
        strcpy(ret, "Alias$@PlugInType_");

        LOG(("Mime Type: %s", mime));

        e = xmimemaptranslate_mime_type_to_filetype((const char*)mime,
                                                     (unsigned int*)&fv);
        if (e != NULL) ret = NULL;

        else {

                sprintf(ft, "%x", (int)fv);
                strcat(ret, ft);
                LOG(("Alias Var: %s", ret));
        }

        xfree(ft);
        return ret;
}

/**
 * plugin_fetch
 * attempts to negotiate with the plugin.
 * also fetches the object for the plugin to handle.
 */
void plugin_fetch (struct plugin_object* po,
                   char* alias_sysvar/* insert vars here */) {

        LOG(("Entering plugin_fetch"));
        xfree(po);
        xfree(alias_sysvar);
        return;
}