Pepper_47_C++_interfaces
file_io.cc
Go to the documentation of this file.
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ppapi/cpp/file_io.h"
6 
7 #include <string.h> // memcpy
8 
9 #include "ppapi/c/ppb_file_io.h"
10 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/cpp/file_ref.h"
14 #include "ppapi/cpp/module.h"
15 #include "ppapi/cpp/module_impl.h"
16 
17 namespace pp {
18 
19 namespace {
20 
21 template <> const char* interface_name<PPB_FileIO_1_0>() {
22  return PPB_FILEIO_INTERFACE_1_0;
23 }
24 
25 template <> const char* interface_name<PPB_FileIO_1_1>() {
26  return PPB_FILEIO_INTERFACE_1_1;
27 }
28 
29 } // namespace
30 
32 }
33 
34 FileIO::FileIO(const InstanceHandle& instance) {
35  if (has_interface<PPB_FileIO_1_1>()) {
36  PassRefFromConstructor(get_interface<PPB_FileIO_1_1>()->Create(
37  instance.pp_instance()));
38  } else if (has_interface<PPB_FileIO_1_0>()) {
39  PassRefFromConstructor(get_interface<PPB_FileIO_1_0>()->Create(
40  instance.pp_instance()));
41  }
42 }
43 
44 FileIO::FileIO(const FileIO& other)
45  : Resource(other) {
46 }
47 
48 int32_t FileIO::Open(const FileRef& file_ref,
49  int32_t open_flags,
50  const CompletionCallback& cc) {
51  if (has_interface<PPB_FileIO_1_1>()) {
52  return get_interface<PPB_FileIO_1_1>()->Open(
53  pp_resource(), file_ref.pp_resource(), open_flags,
55  } else if (has_interface<PPB_FileIO_1_0>()) {
56  return get_interface<PPB_FileIO_1_0>()->Open(
57  pp_resource(), file_ref.pp_resource(), open_flags,
59  }
60  return cc.MayForce(PP_ERROR_NOINTERFACE);
61 }
62 
63 int32_t FileIO::Query(PP_FileInfo* result_buf,
64  const CompletionCallback& cc) {
65  if (has_interface<PPB_FileIO_1_1>()) {
66  return get_interface<PPB_FileIO_1_1>()->Query(
67  pp_resource(), result_buf, cc.pp_completion_callback());
68  } else if (has_interface<PPB_FileIO_1_0>()) {
69  return get_interface<PPB_FileIO_1_0>()->Query(
70  pp_resource(), result_buf, cc.pp_completion_callback());
71  }
72  return cc.MayForce(PP_ERROR_NOINTERFACE);
73 }
74 
75 int32_t FileIO::Touch(PP_Time last_access_time,
76  PP_Time last_modified_time,
77  const CompletionCallback& cc) {
78  if (has_interface<PPB_FileIO_1_1>()) {
79  return get_interface<PPB_FileIO_1_1>()->Touch(
80  pp_resource(), last_access_time, last_modified_time,
82  } else if (has_interface<PPB_FileIO_1_0>()) {
83  return get_interface<PPB_FileIO_1_0>()->Touch(
84  pp_resource(), last_access_time, last_modified_time,
86  }
87  return cc.MayForce(PP_ERROR_NOINTERFACE);
88 }
89 
90 int32_t FileIO::Read(int64_t offset,
91  char* buffer,
92  int32_t bytes_to_read,
93  const CompletionCallback& cc) {
94  if (has_interface<PPB_FileIO_1_1>()) {
95  return get_interface<PPB_FileIO_1_1>()->Read(pp_resource(),
96  offset, buffer, bytes_to_read, cc.pp_completion_callback());
97  } else if (has_interface<PPB_FileIO_1_0>()) {
98  return get_interface<PPB_FileIO_1_0>()->Read(pp_resource(),
99  offset, buffer, bytes_to_read, cc.pp_completion_callback());
100  }
101  return cc.MayForce(PP_ERROR_NOINTERFACE);
102 }
103 
105  int32_t offset,
106  int32_t max_read_length,
107  const CompletionCallbackWithOutput< std::vector<char> >& cc) {
108  if (has_interface<PPB_FileIO_1_1>()) {
109  PP_ArrayOutput array_output = cc.output();
110  return get_interface<PPB_FileIO_1_1>()->ReadToArray(pp_resource(),
111  offset, max_read_length, &array_output,
112  cc.pp_completion_callback());
113  } else if (has_interface<PPB_FileIO_1_0>()) {
114  // Data for our callback wrapper. The callback handler will delete it and
115  // temp_buffer.
116  CallbackData1_0* data = new CallbackData1_0;
117  data->output = cc.output();
118  data->temp_buffer = max_read_length >= 0 ? new char[max_read_length] : NULL;
119  data->original_callback = cc.pp_completion_callback();
120 
121  // Actual returned bytes might not equals to max_read_length. We need to
122  // read to a temporary buffer first and copy later to make sure the array
123  // buffer has correct size.
124  return get_interface<PPB_FileIO_1_0>()->Read(
125  pp_resource(), offset, data->temp_buffer, max_read_length,
126  PP_MakeCompletionCallback(&CallbackConverter, data));
127  }
128  return cc.MayForce(PP_ERROR_NOINTERFACE);
129 }
130 
131 int32_t FileIO::Write(int64_t offset,
132  const char* buffer,
133  int32_t bytes_to_write,
134  const CompletionCallback& cc) {
135  if (has_interface<PPB_FileIO_1_1>()) {
136  return get_interface<PPB_FileIO_1_1>()->Write(
137  pp_resource(), offset, buffer, bytes_to_write,
139  } else if (has_interface<PPB_FileIO_1_0>()) {
140  return get_interface<PPB_FileIO_1_0>()->Write(
141  pp_resource(), offset, buffer, bytes_to_write,
143  }
144  return cc.MayForce(PP_ERROR_NOINTERFACE);
145 }
146 
147 int32_t FileIO::SetLength(int64_t length,
148  const CompletionCallback& cc) {
149  if (has_interface<PPB_FileIO_1_1>()) {
150  return get_interface<PPB_FileIO_1_1>()->SetLength(
151  pp_resource(), length, cc.pp_completion_callback());
152  } else if (has_interface<PPB_FileIO_1_0>()) {
153  return get_interface<PPB_FileIO_1_0>()->SetLength(
154  pp_resource(), length, cc.pp_completion_callback());
155  }
156  return cc.MayForce(PP_ERROR_NOINTERFACE);
157 }
158 
160  if (has_interface<PPB_FileIO_1_1>()) {
161  return get_interface<PPB_FileIO_1_1>()->Flush(
163  } else if (has_interface<PPB_FileIO_1_0>()) {
164  return get_interface<PPB_FileIO_1_0>()->Flush(
166  }
167  return cc.MayForce(PP_ERROR_NOINTERFACE);
168 }
169 
171  if (has_interface<PPB_FileIO_1_1>())
172  get_interface<PPB_FileIO_1_1>()->Close(pp_resource());
173  else if (has_interface<PPB_FileIO_1_0>())
174  get_interface<PPB_FileIO_1_0>()->Close(pp_resource());
175 }
176 
177 // static
178 void FileIO::CallbackConverter(void* user_data, int32_t result) {
179  CallbackData1_0* data = static_cast<CallbackData1_0*>(user_data);
180 
181  if (result >= 0) {
182  // Copy to the destination buffer owned by the callback.
183  char* buffer = static_cast<char*>(data->output.GetDataBuffer(
184  data->output.user_data, result, sizeof(char)));
185  memcpy(buffer, data->temp_buffer, result);
186  delete[] data->temp_buffer;
187  }
188 
189  // Now execute the original callback.
190  PP_RunCompletionCallback(&data->original_callback, result);
191  delete data;
192 }
193 
194 } // namespace pp
void PassRefFromConstructor(PP_Resource resource)
Definition: resource.cc:50
The FileIO class represents a regular file.
Definition: file_io.h:23
int32_t MayForce(int32_t result) const
const PP_CompletionCallback & pp_completion_callback() const
int32_t Query(PP_FileInfo *result_buf, const CompletionCallback &cc)
Definition: file_io.cc:63
int32_t Touch(PP_Time last_access_time, PP_Time last_modified_time, const CompletionCallback &cc)
Definition: file_io.cc:75
void Close()
Definition: file_io.cc:170
int32_t Flush(const CompletionCallback &cc)
Definition: file_io.cc:159
int32_t Write(int64_t offset, const char *buffer, int32_t bytes_to_write, const CompletionCallback &cc)
Definition: file_io.cc:131
int32_t Read(int64_t offset, char *buffer, int32_t bytes_to_read, const CompletionCallback &cc)
Definition: file_io.cc:90
int32_t SetLength(int64_t length, const CompletionCallback &cc)
Definition: file_io.cc:147
int32_t Open(const FileRef &file_ref, int32_t open_flags, const CompletionCallback &cc)
Definition: file_io.cc:48
PP_Resource pp_resource() const
Definition: resource.h:47
PP_Instance pp_instance() const
A reference counted module resource.
Definition: resource.h:20