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/file_ref.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/cpp/completion_callback.h"
9 #include "ppapi/cpp/directory_entry.h"
10 #include "ppapi/cpp/file_system.h"
11 #include "ppapi/cpp/module_impl.h"
17 template <> const char* interface_name
<PPB_FileRef_1_0
>() {
18 return PPB_FILEREF_INTERFACE_1_0
;
21 template <> const char* interface_name
<PPB_FileRef_1_1
>() {
22 return PPB_FILEREF_INTERFACE_1_1
;
27 FileRef::FileRef(PP_Resource resource
) : Resource(resource
) {
30 FileRef::FileRef(PassRef
, PP_Resource resource
) : Resource(PASS_REF
, resource
) {
33 FileRef::FileRef(const FileSystem
& file_system
,
35 if (has_interface
<PPB_FileRef_1_1
>()) {
36 PassRefFromConstructor(get_interface
<PPB_FileRef_1_1
>()->Create(
37 file_system
.pp_resource(), path
));
38 } else if (has_interface
<PPB_FileRef_1_0
>()) {
39 PassRefFromConstructor(get_interface
<PPB_FileRef_1_0
>()->Create(
40 file_system
.pp_resource(), path
));
44 FileRef::FileRef(const FileRef
& other
)
48 PP_FileSystemType
FileRef::GetFileSystemType() const {
49 if (has_interface
<PPB_FileRef_1_1
>())
50 return get_interface
<PPB_FileRef_1_1
>()->GetFileSystemType(pp_resource());
51 if (has_interface
<PPB_FileRef_1_0
>())
52 return get_interface
<PPB_FileRef_1_0
>()->GetFileSystemType(pp_resource());
53 return PP_FILESYSTEMTYPE_EXTERNAL
;
56 Var
FileRef::GetName() const {
57 if (has_interface
<PPB_FileRef_1_1
>()) {
59 get_interface
<PPB_FileRef_1_1
>()->GetName(pp_resource()));
61 if (has_interface
<PPB_FileRef_1_0
>()) {
63 get_interface
<PPB_FileRef_1_0
>()->GetName(pp_resource()));
68 Var
FileRef::GetPath() const {
69 if (has_interface
<PPB_FileRef_1_1
>()) {
71 get_interface
<PPB_FileRef_1_1
>()->GetPath(pp_resource()));
73 if (has_interface
<PPB_FileRef_1_0
>()) {
75 get_interface
<PPB_FileRef_1_0
>()->GetPath(pp_resource()));
80 FileRef
FileRef::GetParent() const {
81 if (has_interface
<PPB_FileRef_1_1
>()) {
82 return FileRef(PASS_REF
,
83 get_interface
<PPB_FileRef_1_1
>()->GetParent(pp_resource()));
85 if (has_interface
<PPB_FileRef_1_0
>()) {
86 return FileRef(PASS_REF
,
87 get_interface
<PPB_FileRef_1_0
>()->GetParent(pp_resource()));
92 int32_t FileRef::MakeDirectory(const CompletionCallback
& cc
) {
93 if (has_interface
<PPB_FileRef_1_1
>()) {
94 return get_interface
<PPB_FileRef_1_1
>()->MakeDirectory(
96 PP_FALSE
, // make_ancestors
97 cc
.pp_completion_callback());
99 if (has_interface
<PPB_FileRef_1_0
>()) {
100 return get_interface
<PPB_FileRef_1_0
>()->MakeDirectory(
102 PP_FALSE
, // make_ancestors
103 cc
.pp_completion_callback());
105 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
108 int32_t FileRef::MakeDirectoryIncludingAncestors(
109 const CompletionCallback
& cc
) {
110 if (has_interface
<PPB_FileRef_1_1
>()) {
111 return get_interface
<PPB_FileRef_1_1
>()->MakeDirectory(
113 PP_TRUE
, // make_ancestors
114 cc
.pp_completion_callback());
116 if (has_interface
<PPB_FileRef_1_0
>()) {
117 return get_interface
<PPB_FileRef_1_0
>()->MakeDirectory(
119 PP_TRUE
, // make_ancestors
120 cc
.pp_completion_callback());
122 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
125 int32_t FileRef::Touch(PP_Time last_access_time
,
126 PP_Time last_modified_time
,
127 const CompletionCallback
& cc
) {
128 if (has_interface
<PPB_FileRef_1_1
>()) {
129 return get_interface
<PPB_FileRef_1_1
>()->Touch(
130 pp_resource(), last_access_time
, last_modified_time
,
131 cc
.pp_completion_callback());
133 if (has_interface
<PPB_FileRef_1_0
>()) {
134 return get_interface
<PPB_FileRef_1_0
>()->Touch(
135 pp_resource(), last_access_time
, last_modified_time
,
136 cc
.pp_completion_callback());
138 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
141 int32_t FileRef::Delete(const CompletionCallback
& cc
) {
142 if (has_interface
<PPB_FileRef_1_1
>()) {
143 return get_interface
<PPB_FileRef_1_1
>()->Delete(
144 pp_resource(), cc
.pp_completion_callback());
146 if (has_interface
<PPB_FileRef_1_0
>()) {
147 return get_interface
<PPB_FileRef_1_0
>()->Delete(
148 pp_resource(), cc
.pp_completion_callback());
150 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
153 int32_t FileRef::Rename(const FileRef
& new_file_ref
,
154 const CompletionCallback
& cc
) {
155 if (has_interface
<PPB_FileRef_1_1
>()) {
156 return get_interface
<PPB_FileRef_1_1
>()->Rename(
157 pp_resource(), new_file_ref
.pp_resource(), cc
.pp_completion_callback());
159 if (has_interface
<PPB_FileRef_1_0
>()) {
160 return get_interface
<PPB_FileRef_1_0
>()->Rename(
161 pp_resource(), new_file_ref
.pp_resource(), cc
.pp_completion_callback());
163 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
166 int32_t FileRef::Query(const CompletionCallbackWithOutput
<PP_FileInfo
>& cc
) {
167 if (!has_interface
<PPB_FileRef_1_1
>())
168 return cc
.MayForce(PP_ERROR_NOINTERFACE
);
169 return get_interface
<PPB_FileRef_1_1
>()->Query(
170 pp_resource(), cc
.output(), cc
.pp_completion_callback());
173 int32_t FileRef::ReadDirectoryEntries(
174 const CompletionCallbackWithOutput
<std::vector
<DirectoryEntry
> >&
176 if (!has_interface
<PPB_FileRef_1_1
>())
177 return callback
.MayForce(PP_ERROR_NOINTERFACE
);
178 return get_interface
<PPB_FileRef_1_1
>()->ReadDirectoryEntries(
179 pp_resource(), callback
.output(), callback
.pp_completion_callback());