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_ref.h" 6 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" 12 13 namespace pp { 14 15 namespace { 16 17 template <> const char* interface_name<PPB_FileRef_1_0>() { 18 return PPB_FILEREF_INTERFACE_1_0; 19 } 20 21 template <> const char* interface_name<PPB_FileRef_1_1>() { 22 return PPB_FILEREF_INTERFACE_1_1; 23 } 24 25 } // namespace 26 27 FileRef::FileRef(PP_Resource resource) : Resource(resource) { 28 } 29 30 FileRef::FileRef(PassRef, PP_Resource resource) : Resource(PASS_REF, resource) { 31 } 32 33 FileRef::FileRef(const FileSystem& file_system, 34 const char* path) { 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)); 41 } 42 } 43 44 FileRef::FileRef(const FileRef& other) 45 : Resource(other) { 46 } 47 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; 54 } 55 56 Var FileRef::GetName() const { 57 if (has_interface<PPB_FileRef_1_1>()) { 58 return Var(PASS_REF, 59 get_interface<PPB_FileRef_1_1>()->GetName(pp_resource())); 60 } 61 if (has_interface<PPB_FileRef_1_0>()) { 62 return Var(PASS_REF, 63 get_interface<PPB_FileRef_1_0>()->GetName(pp_resource())); 64 } 65 return Var(); 66 } 67 68 Var FileRef::GetPath() const { 69 if (has_interface<PPB_FileRef_1_1>()) { 70 return Var(PASS_REF, 71 get_interface<PPB_FileRef_1_1>()->GetPath(pp_resource())); 72 } 73 if (has_interface<PPB_FileRef_1_0>()) { 74 return Var(PASS_REF, 75 get_interface<PPB_FileRef_1_0>()->GetPath(pp_resource())); 76 } 77 return Var(); 78 } 79 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())); 84 } 85 if (has_interface<PPB_FileRef_1_0>()) { 86 return FileRef(PASS_REF, 87 get_interface<PPB_FileRef_1_0>()->GetParent(pp_resource())); 88 } 89 return FileRef(); 90 } 91 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( 95 pp_resource(), 96 PP_FALSE, // make_ancestors 97 cc.pp_completion_callback()); 98 } 99 if (has_interface<PPB_FileRef_1_0>()) { 100 return get_interface<PPB_FileRef_1_0>()->MakeDirectory( 101 pp_resource(), 102 PP_FALSE, // make_ancestors 103 cc.pp_completion_callback()); 104 } 105 return cc.MayForce(PP_ERROR_NOINTERFACE); 106 } 107 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( 112 pp_resource(), 113 PP_TRUE, // make_ancestors 114 cc.pp_completion_callback()); 115 } 116 if (has_interface<PPB_FileRef_1_0>()) { 117 return get_interface<PPB_FileRef_1_0>()->MakeDirectory( 118 pp_resource(), 119 PP_TRUE, // make_ancestors 120 cc.pp_completion_callback()); 121 } 122 return cc.MayForce(PP_ERROR_NOINTERFACE); 123 } 124 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()); 132 } 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()); 137 } 138 return cc.MayForce(PP_ERROR_NOINTERFACE); 139 } 140 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()); 145 } 146 if (has_interface<PPB_FileRef_1_0>()) { 147 return get_interface<PPB_FileRef_1_0>()->Delete( 148 pp_resource(), cc.pp_completion_callback()); 149 } 150 return cc.MayForce(PP_ERROR_NOINTERFACE); 151 } 152 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()); 158 } 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()); 162 } 163 return cc.MayForce(PP_ERROR_NOINTERFACE); 164 } 165 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()); 171 } 172 173 int32_t FileRef::ReadDirectoryEntries( 174 const CompletionCallbackWithOutput<std::vector<DirectoryEntry> >& 175 callback) { 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()); 180 } 181 182 } // namespace pp 183