OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_file_io_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_file_io_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "ppapi/c/dev/ppb_file_io_dev.h" | 14 #include "ppapi/c/dev/ppb_file_io_dev.h" |
15 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" | 15 #include "ppapi/c/dev/ppb_file_io_trusted_dev.h" |
16 #include "ppapi/c/pp_completion_callback.h" | 16 #include "ppapi/c/pp_completion_callback.h" |
17 #include "ppapi/c/pp_errors.h" | 17 #include "ppapi/c/pp_errors.h" |
18 #include "ppapi/thunk/enter.h" | |
19 #include "ppapi/thunk/ppb_file_ref_api.h" | |
20 #include "webkit/plugins/ppapi/common.h" | 18 #include "webkit/plugins/ppapi/common.h" |
21 #include "webkit/plugins/ppapi/file_type_conversions.h" | 19 #include "webkit/plugins/ppapi/file_type_conversions.h" |
22 #include "webkit/plugins/ppapi/plugin_module.h" | 20 #include "webkit/plugins/ppapi/plugin_module.h" |
23 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 21 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
24 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" | 22 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
25 #include "webkit/plugins/ppapi/resource_tracker.h" | 23 #include "webkit/plugins/ppapi/resource_tracker.h" |
26 | 24 |
27 using ppapi::thunk::EnterResourceNoLock; | |
28 using ppapi::thunk::PPB_FileIO_API; | |
29 using ppapi::thunk::PPB_FileRef_API; | |
30 | |
31 namespace webkit { | 25 namespace webkit { |
32 namespace ppapi { | 26 namespace ppapi { |
33 | 27 |
| 28 namespace { |
| 29 |
| 30 PP_Resource Create(PP_Instance instance_id) { |
| 31 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
| 32 if (!instance) |
| 33 return 0; |
| 34 |
| 35 PPB_FileIO_Impl* file_io = new PPB_FileIO_Impl(instance); |
| 36 return file_io->GetReference(); |
| 37 } |
| 38 |
| 39 PP_Bool IsFileIO(PP_Resource resource) { |
| 40 return BoolToPPBool(!!Resource::GetAs<PPB_FileIO_Impl>(resource)); |
| 41 } |
| 42 |
| 43 int32_t Open(PP_Resource file_io_id, |
| 44 PP_Resource file_ref_id, |
| 45 int32_t open_flags, |
| 46 PP_CompletionCallback callback) { |
| 47 scoped_refptr<PPB_FileIO_Impl> |
| 48 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 49 if (!file_io) |
| 50 return PP_ERROR_BADRESOURCE; |
| 51 |
| 52 scoped_refptr<PPB_FileRef_Impl> file_ref( |
| 53 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); |
| 54 if (!file_ref) |
| 55 return PP_ERROR_BADRESOURCE; |
| 56 |
| 57 return file_io->Open(file_ref, open_flags, callback); |
| 58 } |
| 59 |
| 60 int32_t Query(PP_Resource file_io_id, |
| 61 PP_FileInfo_Dev* info, |
| 62 PP_CompletionCallback callback) { |
| 63 scoped_refptr<PPB_FileIO_Impl> |
| 64 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 65 if (!file_io) |
| 66 return PP_ERROR_BADRESOURCE; |
| 67 return file_io->Query(info, callback); |
| 68 } |
| 69 |
| 70 int32_t Touch(PP_Resource file_io_id, |
| 71 PP_Time last_access_time, |
| 72 PP_Time last_modified_time, |
| 73 PP_CompletionCallback callback) { |
| 74 scoped_refptr<PPB_FileIO_Impl> |
| 75 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 76 if (!file_io) |
| 77 return PP_ERROR_BADRESOURCE; |
| 78 return file_io->Touch(last_access_time, last_modified_time, callback); |
| 79 } |
| 80 |
| 81 int32_t Read(PP_Resource file_io_id, |
| 82 int64_t offset, |
| 83 char* buffer, |
| 84 int32_t bytes_to_read, |
| 85 PP_CompletionCallback callback) { |
| 86 scoped_refptr<PPB_FileIO_Impl> |
| 87 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 88 if (!file_io) |
| 89 return PP_ERROR_BADRESOURCE; |
| 90 return file_io->Read(offset, buffer, bytes_to_read, callback); |
| 91 } |
| 92 |
| 93 int32_t Write(PP_Resource file_io_id, |
| 94 int64_t offset, |
| 95 const char* buffer, |
| 96 int32_t bytes_to_write, |
| 97 PP_CompletionCallback callback) { |
| 98 scoped_refptr<PPB_FileIO_Impl> |
| 99 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 100 if (!file_io) |
| 101 return PP_ERROR_BADRESOURCE; |
| 102 return file_io->Write(offset, buffer, bytes_to_write, callback); |
| 103 } |
| 104 |
| 105 int32_t SetLength(PP_Resource file_io_id, |
| 106 int64_t length, |
| 107 PP_CompletionCallback callback) { |
| 108 scoped_refptr<PPB_FileIO_Impl> |
| 109 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 110 if (!file_io) |
| 111 return PP_ERROR_BADRESOURCE; |
| 112 return file_io->SetLength(length, callback); |
| 113 } |
| 114 |
| 115 int32_t Flush(PP_Resource file_io_id, |
| 116 PP_CompletionCallback callback) { |
| 117 scoped_refptr<PPB_FileIO_Impl> |
| 118 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 119 if (!file_io) |
| 120 return PP_ERROR_BADRESOURCE; |
| 121 return file_io->Flush(callback); |
| 122 } |
| 123 |
| 124 void Close(PP_Resource file_io_id) { |
| 125 scoped_refptr<PPB_FileIO_Impl> |
| 126 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 127 if (!file_io) |
| 128 return; |
| 129 file_io->Close(); |
| 130 } |
| 131 |
| 132 const PPB_FileIO_Dev ppb_fileio = { |
| 133 &Create, |
| 134 &IsFileIO, |
| 135 &Open, |
| 136 &Query, |
| 137 &Touch, |
| 138 &Read, |
| 139 &Write, |
| 140 &SetLength, |
| 141 &Flush, |
| 142 &Close |
| 143 }; |
| 144 |
| 145 int32_t GetOSFileDescriptor(PP_Resource file_io_id) { |
| 146 scoped_refptr<PPB_FileIO_Impl> |
| 147 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 148 if (!file_io) |
| 149 return PP_ERROR_BADRESOURCE; |
| 150 return file_io->GetOSFileDescriptor(); |
| 151 } |
| 152 |
| 153 int32_t WillWrite(PP_Resource file_io_id, |
| 154 int64_t offset, |
| 155 int32_t bytes_to_write, |
| 156 PP_CompletionCallback callback) { |
| 157 scoped_refptr<PPB_FileIO_Impl> |
| 158 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 159 if (!file_io) |
| 160 return PP_ERROR_BADRESOURCE; |
| 161 return file_io->WillWrite(offset, bytes_to_write, callback); |
| 162 } |
| 163 |
| 164 int32_t WillSetLength(PP_Resource file_io_id, |
| 165 int64_t length, |
| 166 PP_CompletionCallback callback) { |
| 167 scoped_refptr<PPB_FileIO_Impl> |
| 168 file_io(Resource::GetAs<PPB_FileIO_Impl>(file_io_id)); |
| 169 if (!file_io) |
| 170 return PP_ERROR_BADRESOURCE; |
| 171 return file_io->WillSetLength(length, callback); |
| 172 } |
| 173 |
| 174 const PPB_FileIOTrusted_Dev ppb_fileiotrusted = { |
| 175 &GetOSFileDescriptor, |
| 176 &WillWrite, |
| 177 &WillSetLength |
| 178 }; |
| 179 |
| 180 } // namespace |
| 181 |
34 PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) | 182 PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) |
35 : Resource(instance), | 183 : Resource(instance), |
36 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), | 184 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), |
37 file_(base::kInvalidPlatformFileValue), | 185 file_(base::kInvalidPlatformFileValue), |
38 callback_(), | 186 callback_(), |
39 info_(NULL), | 187 info_(NULL), |
40 read_buffer_(NULL) { | 188 read_buffer_(NULL) { |
41 } | 189 } |
42 | 190 |
43 PPB_FileIO_Impl::~PPB_FileIO_Impl() { | 191 PPB_FileIO_Impl::~PPB_FileIO_Impl() { |
44 Close(); | 192 Close(); |
45 } | 193 } |
46 | 194 |
47 // static | 195 // static |
48 PP_Resource PPB_FileIO_Impl::Create(PP_Instance pp_instance) { | 196 const PPB_FileIO_Dev* PPB_FileIO_Impl::GetInterface() { |
49 PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); | 197 return &ppb_fileio; |
50 if (!instance) | |
51 return 0; | |
52 PPB_FileIO_Impl* file_io = new PPB_FileIO_Impl(instance); | |
53 return file_io->GetReference(); | |
54 } | 198 } |
55 | 199 |
56 PPB_FileIO_API* PPB_FileIO_Impl::AsPPB_FileIO_API() { | 200 // static |
| 201 const PPB_FileIOTrusted_Dev* PPB_FileIO_Impl::GetTrustedInterface() { |
| 202 return &ppb_fileiotrusted; |
| 203 } |
| 204 |
| 205 PPB_FileIO_Impl* PPB_FileIO_Impl::AsPPB_FileIO_Impl() { |
57 return this; | 206 return this; |
58 } | 207 } |
59 | 208 |
60 int32_t PPB_FileIO_Impl::Open(PP_Resource pp_file_ref, | 209 int32_t PPB_FileIO_Impl::Open(PPB_FileRef_Impl* file_ref, |
61 int32_t open_flags, | 210 int32_t open_flags, |
62 PP_CompletionCallback callback) { | 211 PP_CompletionCallback callback) { |
63 EnterResourceNoLock<PPB_FileRef_API> enter(pp_file_ref, true); | |
64 if (enter.failed()) | |
65 return PP_ERROR_BADRESOURCE; | |
66 PPB_FileRef_Impl* file_ref = static_cast<PPB_FileRef_Impl*>(enter.object()); | |
67 | |
68 int32_t rv = CommonCallValidation(false, callback); | 212 int32_t rv = CommonCallValidation(false, callback); |
69 if (rv != PP_OK) | 213 if (rv != PP_OK) |
70 return rv; | 214 return rv; |
71 | 215 |
72 int flags = 0; | 216 int flags = 0; |
73 if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) | 217 if (!PepperFileOpenFlagsToPlatformFileFlags(open_flags, &flags)) |
74 return PP_ERROR_BADARGUMENT; | 218 return PP_ERROR_BADARGUMENT; |
75 | 219 |
76 file_system_type_ = file_ref->GetFileSystemType(); | 220 file_system_type_ = file_ref->GetFileSystemType(); |
77 switch (file_system_type_) { | 221 switch (file_system_type_) { |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, | 471 void PPB_FileIO_Impl::WriteCallback(base::PlatformFileError error_code, |
328 int bytes_written) { | 472 int bytes_written) { |
329 if (error_code != base::PLATFORM_FILE_OK) | 473 if (error_code != base::PLATFORM_FILE_OK) |
330 RunPendingCallback(PlatformFileErrorToPepperError(error_code)); | 474 RunPendingCallback(PlatformFileErrorToPepperError(error_code)); |
331 else | 475 else |
332 RunPendingCallback(bytes_written); | 476 RunPendingCallback(bytes_written); |
333 } | 477 } |
334 | 478 |
335 } // namespace ppapi | 479 } // namespace ppapi |
336 } // namespace webkit | 480 } // namespace webkit |
OLD | NEW |