summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_file_io_impl.cc
blob: 7fa69bcf7753615260a112dc71c55c1b2dfe55f8 (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
// 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/bind.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/ppb_file_io.h"
#include "ppapi/c/trusted/ppb_file_io_trusted.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/file_type_conversion.h"
#include "ppapi/shared_impl/time_conversion.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/ppb_file_ref_api.h"
#include "webkit/plugins/ppapi/common.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/quota_file_io.h"
#include "webkit/plugins/ppapi/resource_helper.h"

using ppapi::PPTimeToTime;
using ppapi::TimeToPPTime;
using ppapi::thunk::PPB_FileRef_API;

namespace webkit {
namespace ppapi {

PPB_FileIO_Impl::PPB_FileIO_Impl(PP_Instance instance)
    : ::ppapi::PPB_FileIO_Shared(instance),
      file_(base::kInvalidPlatformFileValue),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
}

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

int32_t PPB_FileIO_Impl::OpenValidated(PP_Resource file_ref_resource,
                                       PPB_FileRef_API* file_ref_api,
                                       int32_t open_flags,
                                       PP_CompletionCallback callback) {
  PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(file_ref_api);

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

  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_BADARGUMENT;

  if (file_ref->HasValidFileSystem()) {
    file_system_url_ = file_ref->GetFileSystemURL();
    if (!plugin_delegate->AsyncOpenFileSystemURL(
            file_system_url_, flags,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  } else {
    if (file_system_type_ != PP_FILESYSTEMTYPE_EXTERNAL)
      return PP_ERROR_FAILED;
    if (!plugin_delegate->AsyncOpenFile(
            file_ref->GetSystemPath(), flags,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformOpenFileCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  }

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::QueryValidated(PP_FileInfo* info,
                                        PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::GetFileInfoFromPlatformFile(
          plugin_delegate->GetFileThreadMessageLoopProxy(), file_,
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformQueryCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, info);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::TouchValidated(PP_Time last_access_time,
                                        PP_Time last_modified_time,
                                        PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::Touch(
          plugin_delegate->GetFileThreadMessageLoopProxy(),
          file_, PPTimeToTime(last_access_time),
          PPTimeToTime(last_modified_time),
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::ReadValidated(int64_t offset,
                                       char* buffer,
                                       int32_t bytes_to_read,
                                       PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::Read(
          plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset,
          bytes_to_read,
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformReadCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_READ, callback, buffer, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::WriteValidated(int64_t offset,
                                        const char* buffer,
                                        int32_t bytes_to_write,
                                        PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (quota_file_io_.get()) {
    if (!quota_file_io_->Write(
            offset, buffer, bytes_to_write,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  } else {
    if (!base::FileUtilProxy::Write(
            plugin_delegate->GetFileThreadMessageLoopProxy(), file_, offset,
            buffer, bytes_to_write,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformWriteCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  }

  RegisterCallback(OPERATION_WRITE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::SetLengthValidated(int64_t length,
                                            PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (quota_file_io_.get()) {
    if (!quota_file_io_->SetLength(
            length,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  } else {
    if (!base::FileUtilProxy::Truncate(
            plugin_delegate->GetFileThreadMessageLoopProxy(), file_, length,
            base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback,
                       weak_factory_.GetWeakPtr())))
      return PP_ERROR_FAILED;
  }

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_FileIO_Impl::FlushValidated(PP_CompletionCallback callback) {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (!plugin_delegate)
    return PP_ERROR_FAILED;

  if (!base::FileUtilProxy::Flush(
          plugin_delegate->GetFileThreadMessageLoopProxy(), file_,
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

void PPB_FileIO_Impl::Close() {
  PluginDelegate* plugin_delegate = GetPluginDelegate();
  if (file_ != base::kInvalidPlatformFileValue && plugin_delegate) {
    base::FileUtilProxy::Close(
        plugin_delegate->GetFileThreadMessageLoopProxy(), file_,
        base::FileUtilProxy::StatusCallback());
    file_ = base::kInvalidPlatformFileValue;
    quota_file_io_.reset();
  }
}

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) {
  int32_t rv = CommonCallValidation(true, OPERATION_EXCLUSIVE, callback);
  if (rv != PP_OK)
    return rv;

  if (!quota_file_io_.get())
    return PP_OK;

  if (!quota_file_io_->WillWrite(
          offset, bytes_to_write,
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformWillWriteCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

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

  if (!quota_file_io_.get())
    return PP_OK;

  if (!quota_file_io_->WillSetLength(
          length,
          base::Bind(&PPB_FileIO_Impl::ExecutePlatformGeneralCallback,
                     weak_factory_.GetWeakPtr())))
    return PP_ERROR_FAILED;

  RegisterCallback(OPERATION_EXCLUSIVE, callback, NULL, NULL);
  return PP_OK_COMPLETIONPENDING;
}

PluginDelegate* PPB_FileIO_Impl::GetPluginDelegate() {
  return ResourceHelper::GetPluginDelegate(this);
}

void PPB_FileIO_Impl::ExecutePlatformGeneralCallback(
    base::PlatformFileError error_code) {
  ExecuteGeneralCallback(::ppapi::PlatformFileErrorToPepperError(error_code));
}

void PPB_FileIO_Impl::ExecutePlatformOpenFileCallback(
    base::PlatformFileError error_code,
    base::PassPlatformFile file) {
  DCHECK(file_ == base::kInvalidPlatformFileValue);
  file_ = file.ReleaseValue();

  DCHECK(!quota_file_io_.get());
  if (file_ != base::kInvalidPlatformFileValue &&
      (file_system_type_ == PP_FILESYSTEMTYPE_LOCALTEMPORARY ||
       file_system_type_ == PP_FILESYSTEMTYPE_LOCALPERSISTENT)) {
    quota_file_io_.reset(new QuotaFileIO(
        pp_instance(), file_, file_system_url_, file_system_type_));
  }

  ExecuteOpenFileCallback(::ppapi::PlatformFileErrorToPepperError(error_code));
}

void PPB_FileIO_Impl::ExecutePlatformQueryCallback(
    base::PlatformFileError error_code,
    const base::PlatformFileInfo& file_info) {
  PP_FileInfo pp_info;
  pp_info.size = file_info.size;
  pp_info.creation_time = TimeToPPTime(file_info.creation_time);
  pp_info.last_access_time = TimeToPPTime(file_info.last_accessed);
  pp_info.last_modified_time = TimeToPPTime(file_info.last_modified);
  pp_info.system_type = file_system_type_;
  if (file_info.is_directory)
    pp_info.type = PP_FILETYPE_DIRECTORY;
  else
    pp_info.type = PP_FILETYPE_REGULAR;

  ExecuteQueryCallback(::ppapi::PlatformFileErrorToPepperError(error_code),
                       pp_info);
}

void PPB_FileIO_Impl::ExecutePlatformReadCallback(
    base::PlatformFileError error_code,
    const char* data, int bytes_read) {
  // Map the error code, OK getting mapped to the # of bytes read.
  int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code);
  pp_result = pp_result == PP_OK ? bytes_read : pp_result;
  ExecuteReadCallback(pp_result, data);
}

void PPB_FileIO_Impl::ExecutePlatformWriteCallback(
    base::PlatformFileError error_code,
    int bytes_written) {
  int32_t pp_result = ::ppapi::PlatformFileErrorToPepperError(error_code);
  ExecuteGeneralCallback(pp_result == PP_OK ? bytes_written : pp_result);
}

void PPB_FileIO_Impl::ExecutePlatformWillWriteCallback(
    base::PlatformFileError error_code,
    int bytes_written) {
  if (pending_op_ != OPERATION_EXCLUSIVE || callbacks_.empty()) {
    NOTREACHED();
    return;
  }

  if (error_code != base::PLATFORM_FILE_OK) {
    RunAndRemoveFirstPendingCallback(
        ::ppapi::PlatformFileErrorToPepperError(error_code));
  } else {
    RunAndRemoveFirstPendingCallback(bytes_written);
  }
}

}  // namespace ppapi
}  // namespace webkit