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
|
// 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/cpp/private/flash_file.h"
#include "ppapi/c/pp_bool.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/module_impl.h"
namespace pp {
// FileModuleLocal -------------------------------------------------------------
namespace {
template <> const char* interface_name<PPB_Flash_File_ModuleLocal_3_0>() {
return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0;
}
template <> const char* interface_name<PPB_Flash_File_ModuleLocal_2_0>() {
return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_2_0;
}
} // namespace
namespace flash {
static FileModuleLocal::DirEntry ConvertDirEntry(const PP_DirEntry_Dev& entry) {
FileModuleLocal::DirEntry rv = { entry.name, PP_ToBool(entry.is_dir) };
return rv;
}
// static
bool FileModuleLocal::IsAvailable() {
return has_interface<PPB_Flash_File_ModuleLocal_3_0>() ||
has_interface<PPB_Flash_File_ModuleLocal_2_0>();
}
// static
bool FileModuleLocal::CreateThreadAdapterForInstance(
const InstanceHandle& instance) {
bool rv = false;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
rv = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
CreateThreadAdapterForInstance(instance.pp_instance());
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
rv = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
CreateThreadAdapterForInstance( instance.pp_instance());
}
return rv;
}
// static
void FileModuleLocal::ClearThreadAdapterForInstance(
const InstanceHandle& instance) {
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
ClearThreadAdapterForInstance(instance.pp_instance());
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
ClearThreadAdapterForInstance(instance.pp_instance());
}
}
// static
PP_FileHandle FileModuleLocal::OpenFile(const InstanceHandle& instance,
const std::string& path,
int32_t mode) {
PP_FileHandle file_handle = PP_kInvalidFileHandle;
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
OpenFile(instance.pp_instance(), path.c_str(), mode, &file_handle);
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
OpenFile(instance.pp_instance(), path.c_str(), mode, &file_handle);
}
return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
}
// static
bool FileModuleLocal::RenameFile(const InstanceHandle& instance,
const std::string& path_from,
const std::string& path_to) {
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
RenameFile(instance.pp_instance(), path_from.c_str(), path_to.c_str());
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
RenameFile(instance.pp_instance(), path_from.c_str(), path_to.c_str());
}
return result == PP_OK;
}
// static
bool FileModuleLocal::DeleteFileOrDir(const InstanceHandle& instance,
const std::string& path,
bool recursive) {
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
DeleteFileOrDir(instance.pp_instance(), path.c_str(),
PP_FromBool(recursive));
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
DeleteFileOrDir(instance.pp_instance(), path.c_str(),
PP_FromBool(recursive));
}
return result == PP_OK;
}
// static
bool FileModuleLocal::CreateDir(const InstanceHandle& instance,
const std::string& path) {
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
CreateDir(instance.pp_instance(), path.c_str());
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
CreateDir(instance.pp_instance(), path.c_str());
}
return result == PP_OK;
}
// static
bool FileModuleLocal::QueryFile(const InstanceHandle& instance,
const std::string& path,
PP_FileInfo* info) {
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
QueryFile(instance.pp_instance(), path.c_str(), info);
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
QueryFile(instance.pp_instance(), path.c_str(), info);
}
return result == PP_OK;
}
// static
bool FileModuleLocal::GetDirContents(
const InstanceHandle& instance,
const std::string& path,
std::vector<DirEntry>* dir_contents) {
dir_contents->clear();
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
PP_DirContents_Dev* contents = NULL;
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
GetDirContents(instance.pp_instance(), path.c_str(), &contents);
if (result == PP_OK && contents) {
for (int32_t i = 0; i < contents->count; i++)
dir_contents->push_back(ConvertDirEntry(contents->entries[i]));
}
if (contents) {
get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
FreeDirContents(instance.pp_instance(), contents);
}
} else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) {
PP_DirContents_Dev* contents = NULL;
result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
GetDirContents(instance.pp_instance(), path.c_str(), &contents);
if (result == PP_OK && contents) {
for (int32_t i = 0; i < contents->count; i++)
dir_contents->push_back(ConvertDirEntry(contents->entries[i]));
}
if (contents) {
get_interface<PPB_Flash_File_ModuleLocal_2_0>()->
FreeDirContents(instance.pp_instance(), contents);
}
}
return result == PP_OK;
}
// static
bool FileModuleLocal::IsCreateTemporaryFileAvailable() {
return has_interface<PPB_Flash_File_ModuleLocal_3_0>();
}
// static
PP_FileHandle FileModuleLocal::CreateTemporaryFile(
const InstanceHandle& instance) {
PP_FileHandle file_handle = PP_kInvalidFileHandle;
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) {
result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()->
CreateTemporaryFile(instance.pp_instance(), &file_handle);
}
return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
}
} // namespace flash
// FileFileRef -----------------------------------------------------------------
namespace {
template <> const char* interface_name<PPB_Flash_File_FileRef>() {
return PPB_FLASH_FILE_FILEREF_INTERFACE;
}
} // namespace
namespace flash {
// static
bool FileFileRef::IsAvailable() {
return has_interface<PPB_Flash_File_FileRef>();
}
// static
PP_FileHandle FileFileRef::OpenFile(const pp::FileRef& resource,
int32_t mode) {
PP_FileHandle file_handle = PP_kInvalidFileHandle;
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_FileRef>()) {
result = get_interface<PPB_Flash_File_FileRef>()->
OpenFile(resource.pp_resource(), mode, &file_handle);
}
return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle;
}
// static
bool FileFileRef::QueryFile(const pp::FileRef& resource,
PP_FileInfo* info) {
int32_t result = PP_ERROR_FAILED;
if (has_interface<PPB_Flash_File_FileRef>()) {
result = get_interface<PPB_Flash_File_FileRef>()->
QueryFile(resource.pp_resource(), info);
}
return result == PP_OK;
}
} // namespace flash
} // namespace pp
|