summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/ppb_file_io_proxy.cc
blob: 6858655a3ecf14e3ac88cf28fd178e2aa02bc08b (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
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppb_file_io_proxy.h"

#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/enter_proxy.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppb_file_ref_proxy.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppb_file_io_shared.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/resource_tracker.h"

using ppapi::thunk::PPB_FileIO_API;
using ppapi::thunk::PPB_FileRef_API;

namespace ppapi {
namespace proxy {

namespace {

// The maximum size we'll support reading in one chunk. The renderer process
// must allocate a buffer sized according to the request of the plugin. To
// keep things from getting out of control, we cap the read size to this value.
// This should generally be OK since the API specifies that it may perform a
// partial read.
static const int32_t kMaxReadSize = 33554432;  // 32MB

typedef EnterHostFromHostResourceForceCallback<PPB_FileIO_API> EnterHostFileIO;
typedef EnterPluginFromHostResource<PPB_FileIO_API> EnterPluginFileIO;

class FileIO : public PPB_FileIO_Shared {
 public:
  explicit FileIO(const HostResource& host_resource);
  virtual ~FileIO();

  // PPB_FileIO_API implementation (not provided by FileIOImpl).
  virtual void Close() OVERRIDE;
  virtual int32_t GetOSFileDescriptor() OVERRIDE;
  virtual int32_t WillWrite(int64_t offset,
                            int32_t bytes_to_write,
                            scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t WillSetLength(
      int64_t length,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;

 private:
  // FileIOImpl overrides.
  virtual int32_t OpenValidated(
      PP_Resource file_ref_resource,
      PPB_FileRef_API* file_ref_api,
      int32_t open_flags,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t QueryValidated(
      PP_FileInfo* info,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t TouchValidated(
      PP_Time last_access_time,
      PP_Time last_modified_time,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t ReadValidated(
      int64_t offset,
      char* buffer,
      int32_t bytes_to_read,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t WriteValidated(
      int64_t offset,
      const char* buffer,
      int32_t bytes_to_write,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t SetLengthValidated(
      int64_t length,
      scoped_refptr<TrackedCallback> callback) OVERRIDE;
  virtual int32_t FlushValidated(
      scoped_refptr<TrackedCallback> callback) OVERRIDE;

  PluginDispatcher* GetDispatcher() const {
    return PluginDispatcher::GetForResource(this);
  }

  static const ApiID kApiID = API_ID_PPB_FILE_IO;

  DISALLOW_IMPLICIT_CONSTRUCTORS(FileIO);
};

FileIO::FileIO(const HostResource& host_resource)
    : PPB_FileIO_Shared(host_resource) {
}

FileIO::~FileIO() {
  Close();
}

void FileIO::Close() {
  if (file_open_) {
    GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Close(kApiID,
                                                           host_resource()));
  }
}

int32_t FileIO::GetOSFileDescriptor() {
  return -1;
}

int32_t FileIO::WillWrite(int64_t offset,
                          int32_t bytes_to_write,
                          scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_WillWrite(
      kApiID, host_resource(), offset, bytes_to_write));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::WillSetLength(int64_t length,
                              scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_WillSetLength(
      kApiID, host_resource(), length));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::OpenValidated(PP_Resource file_ref_resource,
                              PPB_FileRef_API* file_ref_api,
                              int32_t open_flags,
                              scoped_refptr<TrackedCallback> callback) {
  Resource* file_ref_object =
      PpapiGlobals::Get()->GetResourceTracker()->GetResource(file_ref_resource);

  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Open(
      kApiID, host_resource(), file_ref_object->host_resource(), open_flags));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::QueryValidated(PP_FileInfo* info,
                               scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Query(
      kApiID, host_resource()));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::TouchValidated(PP_Time last_access_time,
                               PP_Time last_modified_time,
                               scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Touch(
      kApiID, host_resource(), last_access_time, last_modified_time));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::ReadValidated(int64_t offset,
                              char* buffer,
                              int32_t bytes_to_read,
                              scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Read(
      kApiID, host_resource(), offset, bytes_to_read));
  RegisterCallback(OPERATION_READ, callback, buffer, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::WriteValidated(int64_t offset,
                               const char* buffer,
                               int32_t bytes_to_write,
                               scoped_refptr<TrackedCallback> callback) {
  // TODO(brettw) it would be nice to use a shared memory buffer for large
  // writes rather than having to copy to a string (which will involve a number
  // of extra copies to serialize over IPC).
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Write(
      kApiID, host_resource(), offset, std::string(buffer, bytes_to_write)));
  RegisterCallback(OPERATION_WRITE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::SetLengthValidated(int64_t length,
                                   scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_SetLength(
      kApiID, host_resource(), length));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t FileIO::FlushValidated(scoped_refptr<TrackedCallback> callback) {
  GetDispatcher()->Send(new PpapiHostMsg_PPBFileIO_Flush(
      kApiID, host_resource()));
  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

}  // namespace

// -----------------------------------------------------------------------------

PPB_FileIO_Proxy::PPB_FileIO_Proxy(Dispatcher* dispatcher)
    : InterfaceProxy(dispatcher),
      callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}

PPB_FileIO_Proxy::~PPB_FileIO_Proxy() {
}

// static
PP_Resource PPB_FileIO_Proxy::CreateProxyResource(PP_Instance instance) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return 0;

  HostResource result;
  dispatcher->Send(new PpapiHostMsg_PPBFileIO_Create(kApiID, instance,
                                                     &result));
  if (result.is_null())
    return 0;
  return (new FileIO(result))->GetReference();
}

bool PPB_FileIO_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_FileIO_Proxy, msg)
    // Plugin -> host message.
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Create, OnHostMsgCreate)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Open, OnHostMsgOpen)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Close, OnHostMsgClose)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Query, OnHostMsgQuery)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Touch, OnHostMsgTouch)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Read, OnHostMsgRead)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Write, OnHostMsgWrite)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_SetLength, OnHostMsgSetLength)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_Flush, OnHostMsgFlush)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_WillWrite, OnHostMsgWillWrite)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFileIO_WillSetLength,
                        OnHostMsgWillSetLength)

    // Host -> plugin messages.
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_GeneralComplete,
                        OnPluginMsgGeneralComplete)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_OpenFileComplete,
                        OnPluginMsgOpenFileComplete)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_QueryComplete,
                        OnPluginMsgQueryComplete)
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBFileIO_ReadComplete,
                        OnPluginMsgReadComplete)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void PPB_FileIO_Proxy::OnHostMsgCreate(PP_Instance instance,
                                       HostResource* result) {
  thunk::EnterResourceCreation enter(instance);
  if (enter.succeeded()) {
    result->SetHostResource(instance,
                            enter.functions()->CreateFileIO(instance));
  }
}

void PPB_FileIO_Proxy::OnHostMsgOpen(const HostResource& host_resource,
                                     const HostResource& file_ref_resource,
                                     int32_t open_flags) {
  EnterHostFileIO enter(host_resource, callback_factory_,
      &PPB_FileIO_Proxy::OpenFileCallbackCompleteInHost, host_resource);
  if (enter.succeeded()) {
    enter.SetResult(enter.object()->Open(
        file_ref_resource.host_resource(), open_flags, enter.callback()));
  }
}

void PPB_FileIO_Proxy::OnHostMsgClose(const HostResource& host_resource) {
  EnterHostFromHostResource<PPB_FileIO_API> enter(host_resource);
  if (enter.succeeded())
    enter.object()->Close();
}

void PPB_FileIO_Proxy::OnHostMsgQuery(const HostResource& host_resource) {
  // The callback will take charge of deleting the FileInfo. The contents must
  // be defined so we don't send garbage to the plugin in the failure case.
  PP_FileInfo* info = new PP_FileInfo;
  memset(info, 0, sizeof(PP_FileInfo));
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::QueryCallbackCompleteInHost,
                        host_resource, info);
  if (enter.succeeded())
    enter.SetResult(enter.object()->Query(info, enter.callback()));
}

void PPB_FileIO_Proxy::OnHostMsgTouch(const HostResource& host_resource,
                                      PP_Time last_access_time,
                                      PP_Time last_modified_time) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded()) {
    enter.SetResult(enter.object()->Touch(last_access_time, last_modified_time,
                                          enter.callback()));
  }
}

void PPB_FileIO_Proxy::OnHostMsgRead(const HostResource& host_resource,
                                     int64_t offset,
                                     int32_t bytes_to_read) {
  // Validate bytes_to_read before allocating below. This value is coming from
  // the untrusted plugin.
  bytes_to_read = std::min(bytes_to_read, kMaxReadSize);
  if (bytes_to_read < 0) {
    ReadCallbackCompleteInHost(PP_ERROR_FAILED, host_resource,
                               new std::string());
    return;
  }

  // The callback will take charge of deleting the string.
  std::string* dest = new std::string;
  dest->resize(bytes_to_read);
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::ReadCallbackCompleteInHost,
                        host_resource, dest);
  if (enter.succeeded()) {
    enter.SetResult(enter.object()->Read(offset,
                                         bytes_to_read > 0 ? &(*dest)[0] : NULL,
                                         bytes_to_read, enter.callback()));
  }
}

void PPB_FileIO_Proxy::OnHostMsgWrite(const HostResource& host_resource,
                                      int64_t offset,
                                      const std::string& data) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded()) {
    enter.SetResult(enter.object()->Write(offset, data.data(), data.size(),
                                          enter.callback()));
  }
}

void PPB_FileIO_Proxy::OnHostMsgSetLength(const HostResource& host_resource,
                                          int64_t length) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded())
    enter.SetResult(enter.object()->SetLength(length, enter.callback()));
}

void PPB_FileIO_Proxy::OnHostMsgFlush(const HostResource& host_resource) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded())
    enter.SetResult(enter.object()->Flush(enter.callback()));
}

void PPB_FileIO_Proxy::OnHostMsgWillWrite(const HostResource& host_resource,
                                          int64_t offset,
                                          int32_t bytes_to_write) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded()) {
    enter.SetResult(enter.object()->WillWrite(offset, bytes_to_write,
                                              enter.callback()));
  }
}

void PPB_FileIO_Proxy::OnHostMsgWillSetLength(const HostResource& host_resource,
                                              int64_t length) {
  EnterHostFileIO enter(host_resource, callback_factory_,
                        &PPB_FileIO_Proxy::GeneralCallbackCompleteInHost,
                        host_resource);
  if (enter.succeeded())
    enter.SetResult(enter.object()->WillSetLength(length, enter.callback()));
}

void PPB_FileIO_Proxy::OnPluginMsgGeneralComplete(
    const HostResource& host_resource,
    int32_t result) {
  EnterPluginFileIO enter(host_resource);
  if (enter.succeeded())
    static_cast<FileIO*>(enter.object())->ExecuteGeneralCallback(result);
}

void PPB_FileIO_Proxy::OnPluginMsgOpenFileComplete(
    const HostResource& host_resource,
    int32_t result) {
  EnterPluginFileIO enter(host_resource);
  if (enter.succeeded())
    static_cast<FileIO*>(enter.object())->ExecuteOpenFileCallback(result);
}

void PPB_FileIO_Proxy::OnPluginMsgQueryComplete(
    const HostResource& host_resource,
    int32_t result,
    const PP_FileInfo& info) {
  EnterPluginFileIO enter(host_resource);
  if (enter.succeeded())
    static_cast<FileIO*>(enter.object())->ExecuteQueryCallback(result, info);
}

void PPB_FileIO_Proxy::OnPluginMsgReadComplete(
    const HostResource& host_resource,
    int32_t result,
    const std::string& data) {
  EnterPluginFileIO enter(host_resource);
  if (enter.succeeded()) {
    // The result code should contain the data size if it's positive.
    DCHECK((result < 0 && data.size() == 0) ||
           result == static_cast<int32_t>(data.size()));
    static_cast<FileIO*>(enter.object())->ExecuteReadCallback(result,
                                                              data.data());
  }
}

void PPB_FileIO_Proxy::GeneralCallbackCompleteInHost(
    int32_t pp_error,
    const HostResource& host_resource) {
  Send(new PpapiMsg_PPBFileIO_GeneralComplete(kApiID, host_resource, pp_error));
}

void PPB_FileIO_Proxy::OpenFileCallbackCompleteInHost(
    int32_t pp_error,
    const HostResource& host_resource) {
  Send(new PpapiMsg_PPBFileIO_OpenFileComplete(kApiID, host_resource,
                                               pp_error));
}

void PPB_FileIO_Proxy::QueryCallbackCompleteInHost(
    int32_t pp_error,
    const HostResource& host_resource,
    PP_FileInfo* info) {
  Send(new PpapiMsg_PPBFileIO_QueryComplete(kApiID, host_resource, pp_error,
                                            *info));
  delete info;
}

void PPB_FileIO_Proxy::ReadCallbackCompleteInHost(
    int32_t pp_error,
    const HostResource& host_resource,
    std::string* data) {
  // Only send the amount of data in the string that was actually read.
  if (pp_error >= 0) {
    DCHECK(pp_error <= static_cast<int32_t>(data->size()));
    data->resize(pp_error);
  }
  Send(new PpapiMsg_PPBFileIO_ReadComplete(kApiID, host_resource, pp_error,
                                           *data));
  delete data;
}

}  // namespace proxy
}  // namespace ppapi