summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_file_io_impl.cc
blob: b68da1b72191ac0d693991ed2031e22652b8841d (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
// Copyright (c) 2011 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 "webkit/plugins/ppapi/ppb_file_io_impl.h"

#include "base/callback.h"
#include "base/file_util.h"
#include "base/file_util_proxy.h"
#include "base/message_loop_proxy.h"
#include "base/platform_file.h"
#include "base/logging.h"
#include "base/time.h"
#include "ppapi/c/dev/ppb_file_io_dev.h"
#include "ppapi/c/dev/ppb_file_io_trusted_dev.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_file_ref_api.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/file_type_conversions.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
#include "webkit/plugins/ppapi/resource_tracker.h"

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

namespace webkit {
namespace ppapi {

PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance)
    : Resource(instance),
      ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
      file_(base::kInvalidPlatformFileValue),
      callback_(),
      info_(NULL),
      read_buffer_(NULL) {
}

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

// static
PP_Resource PPB_FileIO_Impl::Create(PP_Instance pp_instance) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance);
  if (!instance)
    return 0;
  PPB_FileIO_Impl* file_io = new PPB_FileIO_Impl(instance);
  return file_io->GetReference();
}

PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() {
  return this;
}

int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref,
                              int32_t open_flags,
                              PP_CompletionCallback callback) {
  EnterResourceNoLock<PPB_FileRef_API> enter(pp_file_ref, true);
  if (enter.failed())
    return PP_ERROR_BADRESOURCE;
  PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object());

  int32_t rv = CommonCallValidation(false, callback);
  if (rv != PP_OK)
    return rv;

  int flags = 0;
  if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags))
    return PP_ERROR_BADARGUMENT;

  file_system_type_ = file_ref->GetFileSystemType();
  switch (file_system_type_) {
    case PP_FILESYSTEMTYPE_EXTERNAL:
      if (!instance()->delegate()->AsyncOpenFile(
              file_ref->GetSystemPath(), flags,
              callback_factory_.NewCallback(
                  &PPB_FileIO_Impl::AsyncOpenFileCallback)))
        return PP_ERROR_FAILED;
      break;
    case PP_FILESYSTEMTYPE_LOCALPERSISTENT:
    case PP_FILESYSTEMTYPE_LOCALTEMPORARY:
      if (!instance()->delegate()->AsyncOpenFileSystemURL(
              file_ref->GetFileSystemURL(), flags,
              callback_factory_.NewCallback(
                  &PPB_FileIO_Impl::AsyncOpenFileCallback)))
        return PP_ERROR_FAILED;
      break;
    default:
      return PP_ERROR_FAILED;
  }

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::Query(PP_FileInfo_Dev* info,
                               PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  if (!info)
    return PP_ERROR_BADARGUMENT;

  DCHECK(!info_);  // If |info_|, a callback should be pending (caught above).
  info_ = info;

  if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(
          instance()->delegate()->GetFileThreadMessageLoopProxy(), file_,
          callback_factory_.NewCallback(&PPB_FileIO_Impl::QueryInfoCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time,
                      PP_Time last_modified_time,
                      PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  if (!base::FileUtilProxy::Touch(
          instance()->delegate()->GetFileThreadMessageLoopProxy(),
          file_, base::Time::FromDoubleT(last_access_time),
          base::Time::FromDoubleT(last_modified_time),
          callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::Read(int64_t offset,
                              char* buffer,
                              int32_t bytes_to_read,
                              PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  // If |read_buffer__|, a callback should be pending (caught above).
  DCHECK(!read_buffer_);
  read_buffer_ = buffer;

  if (!base::FileUtilProxy::Read(
          instance()->delegate()->GetFileThreadMessageLoopProxy(),
          file_, offset, bytes_to_read,
          callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::Write(int64_t offset,
                               const char* buffer,
                               int32_t bytes_to_write,
                               PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  if (!base::FileUtilProxy::Write(
          instance()->delegate()->GetFileThreadMessageLoopProxy(),
          file_, offset, buffer, bytes_to_write,
          callback_factory_.NewCallback(&PPB_FileIO_Impl::WriteCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::SetLength(int64_t length,
                          PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  if (!base::FileUtilProxy::Truncate(
          instance()->delegate()->GetFileThreadMessageLoopProxy(),
          file_, length,
          callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) {
  int32_t rv = CommonCallValidation(true, callback);
  if (rv != PP_OK)
    return rv;

  if (!base::FileUtilProxy::Flush(
          instance()->delegate()->GetFileThreadMessageLoopProxy(), file_,
          callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback)))
    return PP_ERROR_FAILED;

  RegisterCallback(callback);
  return PP_OK_COMPLETIONPENDING;
}

void PPB_FileIO_Impl::Close() {
  if (file_ != base::kInvalidPlatformFileValue) {
    base::FileUtilProxy::Close(
        instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, NULL);
    file_ = base::kInvalidPlatformFileValue;
  }
}

int32_t PPB_FileIO_Impl::GetOSFileDescriptor() {
#if defined(OS_POSIX)
  return file_;
#elif defined(OS_WIN)
  return reinterpret_cast<uintptr_t>(file_);
#else
#error "Platform not supported."
#endif
}

int32_t PPB_FileIO_Impl::WillWrite(int64_t offset,
                                   int32_t bytes_to_write,
                                   PP_CompletionCallback callback) {
  // TODO(dumi): implement me
  return PP_OK;
}

int32_t PPB_FileIO_Impl::WillSetLength(int64_t length,
                                       PP_CompletionCallback callback) {
  // TODO(dumi): implement me
  return PP_OK;
}

int32_t PPB_FileIO_Impl::CommonCallValidation(bool should_be_open,
                                              PP_CompletionCallback callback) {
  // Only asynchronous operation is supported.
  if (!callback.func) {
    NOTIMPLEMENTED();
    return PP_ERROR_BADARGUMENT;
  }

  if (should_be_open) {
    if (file_ == base::kInvalidPlatformFileValue)
      return PP_ERROR_FAILED;
  } else {
    if (file_ != base::kInvalidPlatformFileValue)
      return PP_ERROR_FAILED;
  }

  if (callback_.get() && !callback_->completed())
    return PP_ERROR_INPROGRESS;

  return PP_OK;
}

void PPB_FileIO_Impl::RegisterCallback(PP_CompletionCallback callback) {
  DCHECK(callback.func);
  DCHECK(!callback_.get() || callback_->completed());

  PP_Resource resource_id = GetReferenceNoAddRef();
  CHECK(resource_id);
  callback_ = new TrackedCompletionCallback(
      instance()->module()->GetCallbackTracker(), resource_id, callback);
}

void PPB_FileIO_Impl::RunPendingCallback(int32_t result) {
  scoped_refptr<TrackedCompletionCallback> callback;
  callback.swap(callback_);
  callback->Run(result);  // Will complete abortively if necessary.
}

void PPB_FileIO_Impl::StatusCallback(base::PlatformFileError error_code) {
  RunPendingCallback(PlatformFileErrorToPepperError(error_code));
}

void PPB_FileIO_Impl::AsyncOpenFileCallback(
    base::PlatformFileError error_code,
    base::PlatformFile file) {
  DCHECK(file_ == base::kInvalidPlatformFileValue);
  file_ = file;
  RunPendingCallback(PlatformFileErrorToPepperError(error_code));
}

void PPB_FileIO_Impl::QueryInfoCallback(
    base::PlatformFileError error_code,
    const base::PlatformFileInfo& file_info) {
  DCHECK(info_);
  if (error_code == base::PLATFORM_FILE_OK) {
    info_->size = file_info.size;
    info_->creation_time = file_info.creation_time.ToDoubleT();
    info_->last_access_time = file_info.last_accessed.ToDoubleT();
    info_->last_modified_time = file_info.last_modified.ToDoubleT();
    info_->system_type = file_system_type_;
    if (file_info.is_directory)
      info_->type = PP_FILETYPE_DIRECTORY;
    else
      info_->type = PP_FILETYPE_REGULAR;
  }
  info_ = NULL;
  RunPendingCallback(PlatformFileErrorToPepperError(error_code));
}

void PPB_FileIO_Impl::ReadCallback(base::PlatformFileError error_code,
                                   const char* data, int bytes_read) {
  DCHECK(data);
  DCHECK(read_buffer_);

  int rv;
  if (error_code == base::PLATFORM_FILE_OK) {
    rv = bytes_read;
    if (file_ != base::kInvalidPlatformFileValue)
      memcpy(read_buffer_, data, bytes_read);
  } else
    rv = PlatformFileErrorToPepperError(error_code);

  read_buffer_ = NULL;
  RunPendingCallback(rv);
}

void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code,
                                    int bytes_written) {
  if (error_code != base::PLATFORM_FILE_OK)
    RunPendingCallback(PlatformFileErrorToPepperError(error_code));
  else
    RunPendingCallback(bytes_written);
}

}  // namespace ppapi
}  // namespace webkit