File: ppb_file_io.c

package info (click to toggle)
freshplayerplugin 0.3.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 5,324 kB
  • ctags: 13,257
  • sloc: ansic: 45,542; cpp: 26,176; yacc: 1,707; lex: 910; python: 176; sh: 87; makefile: 21
file content (355 lines) | stat: -rw-r--r-- 12,030 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright © 2013-2015  Rinat Ibragimov
 *
 * This file is part of FreshPlayerPlugin.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "ppb_file_io.h"
#include <stdlib.h>
#include <ppapi/c/pp_errors.h>
#include <inttypes.h>
#include "trace.h"
#include "tables.h"
#include "pp_resource.h"
#include "ppb_core.h"
#include "pp_interface.h"
#include "ppb_message_loop.h"


int32_t
ppb_file_io_request_os_file_handle(PP_Resource file_io, PP_FileHandle *handle,
                                   struct PP_CompletionCallback callback)
{
    struct pp_file_io_s *fio = pp_resource_acquire(file_io, PP_RESOURCE_FILE_IO);
    if (!fio) {
        trace_error("%s, bad resource\n", __func__);
        return PP_ERROR_BADRESOURCE;
    }

    *handle = fio->fd;

    ppb_message_loop_post_work_with_result(ppb_message_loop_get_current(), callback, 0, PP_OK, 0,
                                           __func__);
    pp_resource_release(file_io);
    return PP_OK;
}

PP_Resource
ppb_file_io_create(PP_Instance instance)
{
    struct pp_instance_s *pp_i = tables_get_pp_instance(instance);
    if (!pp_i) {
        trace_error("%s, bad instance\n", __func__);
        return 0;
    }
    PP_Resource file_io = pp_resource_allocate(PP_RESOURCE_FILE_IO, pp_i);
    struct pp_file_io_s *fio = pp_resource_acquire(file_io, PP_RESOURCE_FILE_IO);
    if (!fio) {
        trace_error("%s, resource allocation failure\n", __func__);
        return 0;
    }

    fio->fd = -1;

    pp_resource_release(file_io);
    return file_io;
}

static
void
ppb_file_io_destroy(void *p)
{
    struct pp_file_io_s *fio = p;

    if (fio->fd >= 0)
        close(fio->fd);
}

PP_Bool
ppb_file_io_is_file_io(PP_Resource resource)
{
    return pp_resource_get_type(resource) == PP_RESOURCE_FILE_IO;
}

int32_t
ppb_file_io_open(PP_Resource file_io, PP_Resource file_ref, int32_t open_flags,
                 struct PP_CompletionCallback callback)
{
    int32_t retval;
    struct pp_file_io_s *fio = pp_resource_acquire(file_io, PP_RESOURCE_FILE_IO);
    if (!fio) {
        trace_error("%s, bad resource\n", __func__);
        return PP_ERROR_BADRESOURCE;
    }

    struct pp_file_ref_s *fr = pp_resource_acquire(file_ref, PP_RESOURCE_FILE_REF);
    if (!fr) {
        trace_error("%s, bad resource\n", __func__);
        pp_resource_release(file_io);
        return PP_ERROR_BADRESOURCE;
    }

    switch (fr->type) {
    case PP_FILE_REF_TYPE_FD:
        fio->fd = dup(fr->fd);
        if (fio->fd >= 0) {
            if (lseek(fio->fd, 0, SEEK_SET) == (off_t) -1) {
                retval = PP_ERROR_FAILED;
                goto out;
            }
        } else {
            retval = PP_ERROR_FAILED;
            goto out;
        }
        break;
    default:
        trace_error("%s, fr->type not implemented\n", __func__);
        retval = PP_ERROR_FAILED;
        goto out;
    }

    ppb_message_loop_post_work_with_result(ppb_message_loop_get_current(), callback, 0, PP_OK, 0,
                                           __func__);
    retval = PP_OK_COMPLETIONPENDING;

out:
    pp_resource_release(file_io);
    pp_resource_release(file_ref);
    return retval;
}

int32_t
ppb_file_io_query(PP_Resource file_io, struct PP_FileInfo *info,
                  struct PP_CompletionCallback callback)
{
    return PP_OK;
}

int32_t
ppb_file_io_touch(PP_Resource file_io, PP_Time last_access_time, PP_Time last_modified_time,
                  struct PP_CompletionCallback callback)
{
    return PP_OK;
}

int32_t
ppb_file_io_read(PP_Resource file_io, int64_t offset, char *buffer, int32_t bytes_to_read,
                 struct PP_CompletionCallback callback)
{
    return PP_OK;
}

int32_t
ppb_file_io_write(PP_Resource file_io, int64_t offset, const char *buffer, int32_t bytes_to_write,
                  struct PP_CompletionCallback callback)
{
    return PP_OK;
}

int32_t
ppb_file_io_set_length(PP_Resource file_io, int64_t length, struct PP_CompletionCallback callback)
{
    return PP_OK;
}

int32_t
ppb_file_io_flush(PP_Resource file_io, struct PP_CompletionCallback callback)
{
    return PP_OK;
}

void
ppb_file_io_close(PP_Resource file_io)
{
}

int32_t
ppb_file_io_read_to_array(PP_Resource file_io, int64_t offset, int32_t max_read_length,
                          struct PP_ArrayOutput *output, struct PP_CompletionCallback callback)
{
    return PP_OK;
}


// trace wrappers
TRACE_WRAPPER
int32_t
trace_ppb_file_io_request_os_file_handle(PP_Resource file_io, PP_FileHandle *handle,
                                         struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {full} %s file_io=%d, callback={.func=%p, .user_data=%p, .flags=%u}\n",
               __func__+6, file_io, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_request_os_file_handle(file_io, handle, callback);
}

TRACE_WRAPPER
PP_Resource
trace_ppb_file_io_create(PP_Instance instance)
{
    trace_info("[PPB] {full} %s instance=%d\n", __func__+6, instance);
    return ppb_file_io_create(instance);
}

TRACE_WRAPPER
PP_Bool
trace_ppb_file_io_is_file_io(PP_Resource resource)
{
    trace_info("[PPB] {full} %s resource=%d\n", __func__+6, resource);
    return ppb_file_io_is_file_io(resource);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_open(PP_Resource file_io, PP_Resource file_ref, int32_t open_flags,
                       struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {full} %s file_io=%d, file_ref=%d, open_flags=%u, callback={.func=%p, "
               ".user_data=%p, .flags=%u}\n", __func__+6, file_io, file_ref, open_flags,
               callback.func, callback.user_data, callback.flags);
    return ppb_file_io_open(file_io, file_ref, open_flags, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_query(PP_Resource file_io, struct PP_FileInfo *info,
                        struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, callback={.func=%p, .user_data=%p, .flags=%u}\n",
               __func__+6, file_io, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_query(file_io, info, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_touch(PP_Resource file_io, PP_Time last_access_time, PP_Time last_modified_time,
                        struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, last_access_time=%f, last_modified_time=%f, "
               "callback={.func=%p, .user_data=%p, .flags=%u}\n", __func__+6, file_io,
               last_access_time, last_modified_time, callback.func, callback.user_data,
               callback.flags);
    return ppb_file_io_touch(file_io, last_access_time, last_modified_time, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_read(PP_Resource file_io, int64_t offset, char *buffer, int32_t bytes_to_read,
                       struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, offset=%"PRId64", bytes_to_read=%d, "
               "callback={.func=%p, .user_data=%p, .flags=%u}\n", __func__+6, file_io, offset,
               bytes_to_read, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_read(file_io, offset, buffer, bytes_to_read, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_write(PP_Resource file_io, int64_t offset, const char *buffer,
                        int32_t bytes_to_write, struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, offset=%"PRId64", bytes_to_write=%d, "
               "callback={.func=%p, .user_data=%p, .flags=%u}\n", __func__+6, file_io, offset,
               bytes_to_write, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_write(file_io, offset, buffer, bytes_to_write, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_set_length(PP_Resource file_io, int64_t length,
                             struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, length=%"PRId64", callback={.func=%p, .user_data=%p, "
               ".flags=%u}\n", __func__+6, file_io, length, callback.func, callback.user_data,
               callback.flags);
    return ppb_file_io_set_length(file_io, length, callback);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_flush(PP_Resource file_io, struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, callback={.func=%p, .user_data=%p, .flags=%u}\n",
               __func__+6, file_io, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_flush(file_io, callback);
}

TRACE_WRAPPER
void
trace_ppb_file_io_close(PP_Resource file_io)
{
    trace_info("[PPB] {zilch} %s file_io=%d\n", __func__+6, file_io);
    return ppb_file_io_close(file_io);
}

TRACE_WRAPPER
int32_t
trace_ppb_file_io_read_to_array(PP_Resource file_io, int64_t offset, int32_t max_read_length,
                                struct PP_ArrayOutput *output,
                                struct PP_CompletionCallback callback)
{
    trace_info("[PPB] {zilch} %s file_io=%d, offset=%"PRId64", max_read_length=%d, "
               "callback={.func=%p, .user_data=%p, .flags=%u}\n", __func__+6, file_io, offset,
               max_read_length, callback.func, callback.user_data, callback.flags);
    return ppb_file_io_read_to_array(file_io, offset, max_read_length, output, callback);
}


const struct PPB_FileIO_Private_0_1 ppb_file_io_private_interface_0_1 = {
    .RequestOSFileHandle = TWRAPF(ppb_file_io_request_os_file_handle),
};

const struct PPB_FileIO_1_1 ppb_file_io_interface_1_1 = {
    .Create =       TWRAPF(ppb_file_io_create),
    .IsFileIO =     TWRAPF(ppb_file_io_is_file_io),
    .Open =         TWRAPF(ppb_file_io_open),
    .Query =        TWRAPZ(ppb_file_io_query),
    .Touch =        TWRAPZ(ppb_file_io_touch),
    .Read =         TWRAPZ(ppb_file_io_read),
    .Write =        TWRAPZ(ppb_file_io_write),
    .SetLength =    TWRAPZ(ppb_file_io_set_length),
    .Flush =        TWRAPZ(ppb_file_io_flush),
    .Close =        TWRAPZ(ppb_file_io_close),
    .ReadToArray =  TWRAPZ(ppb_file_io_read_to_array),
};

const struct PPB_FileIO_1_0 ppb_file_io_interface_1_0 = {
    .Create =       TWRAPF(ppb_file_io_create),
    .IsFileIO =     TWRAPF(ppb_file_io_is_file_io),
    .Open =         TWRAPF(ppb_file_io_open),
    .Query =        TWRAPZ(ppb_file_io_query),
    .Touch =        TWRAPZ(ppb_file_io_touch),
    .Read =         TWRAPZ(ppb_file_io_read),
    .Write =        TWRAPZ(ppb_file_io_write),
    .SetLength =    TWRAPZ(ppb_file_io_set_length),
    .Flush =        TWRAPZ(ppb_file_io_flush),
    .Close =        TWRAPZ(ppb_file_io_close),
};

static
void
__attribute__((constructor))
constructor_ppb_file_io(void)
{
    register_interface(PPB_FILEIO_PRIVATE_INTERFACE_0_1, &ppb_file_io_private_interface_0_1);
    register_interface(PPB_FILEIO_INTERFACE_1_0, &ppb_file_io_interface_1_0);
    register_interface(PPB_FILEIO_INTERFACE_1_1, &ppb_file_io_interface_1_1);
    register_resource(PP_RESOURCE_FILE_IO, ppb_file_io_destroy);
}