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.
5 #include "ppapi/cpp/private/flash_file.h"
7 #include "ppapi/c/pp_bool.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/cpp/file_ref.h"
10 #include "ppapi/cpp/instance_handle.h"
11 #include "ppapi/cpp/module_impl.h"
15 // FileModuleLocal -------------------------------------------------------------
19 template <> const char* interface_name
<PPB_Flash_File_ModuleLocal_3_0
>() {
20 return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0
;
27 static FileModuleLocal::DirEntry
ConvertDirEntry(const PP_DirEntry_Dev
& entry
) {
28 FileModuleLocal::DirEntry rv
= { entry
.name
, PP_ToBool(entry
.is_dir
) };
33 bool FileModuleLocal::IsAvailable() {
34 return has_interface
<PPB_Flash_File_ModuleLocal_3_0
>();
38 PP_FileHandle
FileModuleLocal::OpenFile(const InstanceHandle
& instance
,
39 const std::string
& path
,
41 PP_FileHandle file_handle
= PP_kInvalidFileHandle
;
42 int32_t result
= PP_ERROR_FAILED
;
43 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
44 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
45 OpenFile(instance
.pp_instance(), path
.c_str(), mode
, &file_handle
);
47 return (result
== PP_OK
) ? file_handle
: PP_kInvalidFileHandle
;
51 bool FileModuleLocal::RenameFile(const InstanceHandle
& instance
,
52 const std::string
& path_from
,
53 const std::string
& path_to
) {
54 int32_t result
= PP_ERROR_FAILED
;
55 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
56 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
57 RenameFile(instance
.pp_instance(), path_from
.c_str(), path_to
.c_str());
59 return result
== PP_OK
;
63 bool FileModuleLocal::DeleteFileOrDir(const InstanceHandle
& instance
,
64 const std::string
& path
,
66 int32_t result
= PP_ERROR_FAILED
;
67 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
68 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
69 DeleteFileOrDir(instance
.pp_instance(), path
.c_str(),
70 PP_FromBool(recursive
));
72 return result
== PP_OK
;
76 bool FileModuleLocal::CreateDir(const InstanceHandle
& instance
,
77 const std::string
& path
) {
78 int32_t result
= PP_ERROR_FAILED
;
79 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
80 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
81 CreateDir(instance
.pp_instance(), path
.c_str());
83 return result
== PP_OK
;
87 bool FileModuleLocal::QueryFile(const InstanceHandle
& instance
,
88 const std::string
& path
,
90 int32_t result
= PP_ERROR_FAILED
;
91 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
92 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
93 QueryFile(instance
.pp_instance(), path
.c_str(), info
);
95 return result
== PP_OK
;
99 bool FileModuleLocal::GetDirContents(
100 const InstanceHandle
& instance
,
101 const std::string
& path
,
102 std::vector
<DirEntry
>* dir_contents
) {
103 dir_contents
->clear();
105 int32_t result
= PP_ERROR_FAILED
;
106 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
107 PP_DirContents_Dev
* contents
= NULL
;
108 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
109 GetDirContents(instance
.pp_instance(), path
.c_str(), &contents
);
110 if (result
== PP_OK
&& contents
) {
111 for (int32_t i
= 0; i
< contents
->count
; i
++)
112 dir_contents
->push_back(ConvertDirEntry(contents
->entries
[i
]));
115 get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
116 FreeDirContents(instance
.pp_instance(), contents
);
119 return result
== PP_OK
;
123 PP_FileHandle
FileModuleLocal::CreateTemporaryFile(
124 const InstanceHandle
& instance
) {
125 PP_FileHandle file_handle
= PP_kInvalidFileHandle
;
126 int32_t result
= PP_ERROR_FAILED
;
127 if (has_interface
<PPB_Flash_File_ModuleLocal_3_0
>()) {
128 result
= get_interface
<PPB_Flash_File_ModuleLocal_3_0
>()->
129 CreateTemporaryFile(instance
.pp_instance(), &file_handle
);
131 return (result
== PP_OK
) ? file_handle
: PP_kInvalidFileHandle
;
136 // FileFileRef -----------------------------------------------------------------
140 template <> const char* interface_name
<PPB_Flash_File_FileRef
>() {
141 return PPB_FLASH_FILE_FILEREF_INTERFACE
;
149 bool FileFileRef::IsAvailable() {
150 return has_interface
<PPB_Flash_File_FileRef
>();
154 PP_FileHandle
FileFileRef::OpenFile(const pp::FileRef
& resource
,
156 PP_FileHandle file_handle
= PP_kInvalidFileHandle
;
157 int32_t result
= PP_ERROR_FAILED
;
158 if (has_interface
<PPB_Flash_File_FileRef
>()) {
159 result
= get_interface
<PPB_Flash_File_FileRef
>()->
160 OpenFile(resource
.pp_resource(), mode
, &file_handle
);
162 return (result
== PP_OK
) ? file_handle
: PP_kInvalidFileHandle
;
166 bool FileFileRef::QueryFile(const pp::FileRef
& resource
,
168 int32_t result
= PP_ERROR_FAILED
;
169 if (has_interface
<PPB_Flash_File_FileRef
>()) {
170 result
= get_interface
<PPB_Flash_File_FileRef
>()->
171 QueryFile(resource
.pp_resource(), info
);
173 return result
== PP_OK
;