| 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_ref_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/thunk/enter.h" |
| 12 #include "ppapi/thunk/ppb_file_system_api.h" |
| 11 #include "webkit/plugins/ppapi/common.h" | 13 #include "webkit/plugins/ppapi/common.h" |
| 12 #include "webkit/plugins/ppapi/file_callbacks.h" | 14 #include "webkit/plugins/ppapi/file_callbacks.h" |
| 13 #include "webkit/plugins/ppapi/plugin_delegate.h" | 15 #include "webkit/plugins/ppapi/plugin_delegate.h" |
| 14 #include "webkit/plugins/ppapi/plugin_module.h" | 16 #include "webkit/plugins/ppapi/plugin_module.h" |
| 15 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 17 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 16 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" | 18 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" |
| 17 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" | 19 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" |
| 18 #include "webkit/plugins/ppapi/var.h" | 20 #include "webkit/plugins/ppapi/var.h" |
| 19 | 21 |
| 22 using ppapi::thunk::EnterResourceNoLock; |
| 23 using ppapi::thunk::PPB_FileRef_API; |
| 24 using ppapi::thunk::PPB_FileSystem_API; |
| 25 |
| 20 namespace webkit { | 26 namespace webkit { |
| 21 namespace ppapi { | 27 namespace ppapi { |
| 22 | 28 |
| 23 namespace { | 29 namespace { |
| 24 | 30 |
| 25 bool IsValidLocalPath(const std::string& path) { | 31 bool IsValidLocalPath(const std::string& path) { |
| 26 // The path must start with '/' | 32 // The path must start with '/' |
| 27 if (path.empty() || path[0] != '/') | 33 if (path.empty() || path[0] != '/') |
| 28 return false; | 34 return false; |
| 29 | 35 |
| 30 // The path must contain valid UTF-8 characters. | 36 // The path must contain valid UTF-8 characters. |
| 31 if (!IsStringUTF8(path)) | 37 if (!IsStringUTF8(path)) |
| 32 return false; | 38 return false; |
| 33 | 39 |
| 34 return true; | 40 return true; |
| 35 } | 41 } |
| 36 | 42 |
| 37 void TrimTrailingSlash(std::string* path) { | 43 void TrimTrailingSlash(std::string* path) { |
| 38 // If this path ends with a slash, then normalize it away unless path is the | 44 // If this path ends with a slash, then normalize it away unless path is the |
| 39 // root path. | 45 // root path. |
| 40 if (path->size() > 1 && path->at(path->size() - 1) == '/') | 46 if (path->size() > 1 && path->at(path->size() - 1) == '/') |
| 41 path->erase(path->size() - 1, 1); | 47 path->erase(path->size() - 1, 1); |
| 42 } | 48 } |
| 43 | 49 |
| 44 PP_Resource Create(PP_Resource file_system_id, const char* path) { | |
| 45 scoped_refptr<PPB_FileSystem_Impl> file_system( | |
| 46 Resource::GetAs<PPB_FileSystem_Impl>(file_system_id)); | |
| 47 if (!file_system) | |
| 48 return 0; | |
| 49 | |
| 50 if (!file_system->instance()) | |
| 51 return 0; | |
| 52 | |
| 53 std::string validated_path(path); | |
| 54 if (!IsValidLocalPath(validated_path)) | |
| 55 return 0; | |
| 56 TrimTrailingSlash(&validated_path); | |
| 57 | |
| 58 PPB_FileRef_Impl* file_ref = | |
| 59 new PPB_FileRef_Impl(file_system->instance(), | |
| 60 file_system, validated_path); | |
| 61 return file_ref->GetReference(); | |
| 62 } | |
| 63 | |
| 64 PP_Bool IsFileRef(PP_Resource resource) { | |
| 65 return BoolToPPBool(!!Resource::GetAs<PPB_FileRef_Impl>(resource)); | |
| 66 } | |
| 67 | |
| 68 PP_FileSystemType_Dev GetFileSystemType(PP_Resource file_ref_id) { | |
| 69 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 70 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 71 if (!file_ref) | |
| 72 return PP_FILESYSTEMTYPE_INVALID; | |
| 73 return file_ref->GetFileSystemType(); | |
| 74 } | |
| 75 | |
| 76 PP_Var GetName(PP_Resource file_ref_id) { | |
| 77 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 78 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 79 if (!file_ref) | |
| 80 return PP_MakeUndefined(); | |
| 81 return StringVar::StringToPPVar(file_ref->instance()->module(), | |
| 82 file_ref->GetName()); | |
| 83 } | |
| 84 | |
| 85 PP_Var GetPath(PP_Resource file_ref_id) { | |
| 86 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 87 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 88 if (!file_ref) | |
| 89 return PP_MakeUndefined(); | |
| 90 | |
| 91 if (file_ref->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 92 return PP_MakeUndefined(); | |
| 93 | |
| 94 return StringVar::StringToPPVar(file_ref->instance()->module(), | |
| 95 file_ref->GetPath()); | |
| 96 } | |
| 97 | |
| 98 PP_Resource GetParent(PP_Resource file_ref_id) { | |
| 99 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 100 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 101 if (!file_ref) | |
| 102 return 0; | |
| 103 | |
| 104 if (file_ref->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 105 return 0; | |
| 106 | |
| 107 scoped_refptr<PPB_FileRef_Impl> parent_ref(file_ref->GetParent()); | |
| 108 if (!parent_ref) | |
| 109 return 0; | |
| 110 | |
| 111 return parent_ref->GetReference(); | |
| 112 } | |
| 113 | |
| 114 int32_t MakeDirectory(PP_Resource directory_ref_id, | |
| 115 PP_Bool make_ancestors, | |
| 116 PP_CompletionCallback callback) { | |
| 117 scoped_refptr<PPB_FileRef_Impl> directory_ref( | |
| 118 Resource::GetAs<PPB_FileRef_Impl>(directory_ref_id)); | |
| 119 if (!directory_ref) | |
| 120 return PP_ERROR_BADRESOURCE; | |
| 121 | |
| 122 scoped_refptr<PPB_FileSystem_Impl> file_system = | |
| 123 directory_ref->GetFileSystem(); | |
| 124 if (!file_system || !file_system->opened() || | |
| 125 (file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL)) | |
| 126 return PP_ERROR_NOACCESS; | |
| 127 | |
| 128 PluginInstance* instance = file_system->instance(); | |
| 129 if (!instance->delegate()->MakeDirectory( | |
| 130 directory_ref->GetFileSystemURL(), PPBoolToBool(make_ancestors), | |
| 131 new FileCallbacks(instance->module()->AsWeakPtr(), directory_ref_id, | |
| 132 callback, NULL, NULL, NULL))) | |
| 133 return PP_ERROR_FAILED; | |
| 134 | |
| 135 return PP_OK_COMPLETIONPENDING; | |
| 136 } | |
| 137 | |
| 138 int32_t Touch(PP_Resource file_ref_id, | |
| 139 PP_Time last_access_time, | |
| 140 PP_Time last_modified_time, | |
| 141 PP_CompletionCallback callback) { | |
| 142 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 143 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 144 if (!file_ref) | |
| 145 return PP_ERROR_BADRESOURCE; | |
| 146 | |
| 147 scoped_refptr<PPB_FileSystem_Impl> file_system = file_ref->GetFileSystem(); | |
| 148 if (!file_system || !file_system->opened() || | |
| 149 (file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL)) | |
| 150 return PP_ERROR_NOACCESS; | |
| 151 | |
| 152 PluginInstance* instance = file_system->instance(); | |
| 153 if (!instance->delegate()->Touch( | |
| 154 file_ref->GetFileSystemURL(), | |
| 155 base::Time::FromDoubleT(last_access_time), | |
| 156 base::Time::FromDoubleT(last_modified_time), | |
| 157 new FileCallbacks(instance->module()->AsWeakPtr(), file_ref_id, | |
| 158 callback, NULL, NULL, NULL))) | |
| 159 return PP_ERROR_FAILED; | |
| 160 | |
| 161 return PP_OK_COMPLETIONPENDING; | |
| 162 } | |
| 163 | |
| 164 int32_t Delete(PP_Resource file_ref_id, | |
| 165 PP_CompletionCallback callback) { | |
| 166 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 167 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 168 if (!file_ref) | |
| 169 return PP_ERROR_BADRESOURCE; | |
| 170 | |
| 171 scoped_refptr<PPB_FileSystem_Impl> file_system = file_ref->GetFileSystem(); | |
| 172 if (!file_system || !file_system->opened() || | |
| 173 (file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL)) | |
| 174 return PP_ERROR_NOACCESS; | |
| 175 | |
| 176 PluginInstance* instance = file_system->instance(); | |
| 177 if (!instance->delegate()->Delete( | |
| 178 file_ref->GetFileSystemURL(), | |
| 179 new FileCallbacks(instance->module()->AsWeakPtr(), file_ref_id, | |
| 180 callback, NULL, NULL, NULL))) | |
| 181 return PP_ERROR_FAILED; | |
| 182 | |
| 183 return PP_OK_COMPLETIONPENDING; | |
| 184 } | |
| 185 | |
| 186 int32_t Rename(PP_Resource file_ref_id, | |
| 187 PP_Resource new_file_ref_id, | |
| 188 PP_CompletionCallback callback) { | |
| 189 scoped_refptr<PPB_FileRef_Impl> file_ref( | |
| 190 Resource::GetAs<PPB_FileRef_Impl>(file_ref_id)); | |
| 191 if (!file_ref) | |
| 192 return PP_ERROR_BADRESOURCE; | |
| 193 | |
| 194 scoped_refptr<PPB_FileRef_Impl> new_file_ref( | |
| 195 Resource::GetAs<PPB_FileRef_Impl>(new_file_ref_id)); | |
| 196 if (!new_file_ref) | |
| 197 return PP_ERROR_BADRESOURCE; | |
| 198 | |
| 199 scoped_refptr<PPB_FileSystem_Impl> file_system = file_ref->GetFileSystem(); | |
| 200 if (!file_system || !file_system->opened() || | |
| 201 (file_system != new_file_ref->GetFileSystem()) || | |
| 202 (file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL)) | |
| 203 return PP_ERROR_NOACCESS; | |
| 204 | |
| 205 // TODO(viettrungluu): Also cancel when the new file ref is destroyed? | |
| 206 // http://crbug.com/67624 | |
| 207 PluginInstance* instance = file_system->instance(); | |
| 208 if (!instance->delegate()->Rename( | |
| 209 file_ref->GetFileSystemURL(), new_file_ref->GetFileSystemURL(), | |
| 210 new FileCallbacks(instance->module()->AsWeakPtr(), file_ref_id, | |
| 211 callback, NULL, NULL, NULL))) | |
| 212 return PP_ERROR_FAILED; | |
| 213 | |
| 214 return PP_OK_COMPLETIONPENDING; | |
| 215 } | |
| 216 | |
| 217 const PPB_FileRef_Dev ppb_fileref = { | |
| 218 &Create, | |
| 219 &IsFileRef, | |
| 220 &GetFileSystemType, | |
| 221 &GetName, | |
| 222 &GetPath, | |
| 223 &GetParent, | |
| 224 &MakeDirectory, | |
| 225 &Touch, | |
| 226 &Delete, | |
| 227 &Rename | |
| 228 }; | |
| 229 | |
| 230 } // namespace | 50 } // namespace |
| 231 | 51 |
| 232 PPB_FileRef_Impl::PPB_FileRef_Impl() | 52 PPB_FileRef_Impl::PPB_FileRef_Impl() |
| 233 : Resource(NULL), | 53 : Resource(NULL), |
| 234 file_system_(NULL) { | 54 file_system_(NULL) { |
| 235 } | 55 } |
| 236 | 56 |
| 237 PPB_FileRef_Impl::PPB_FileRef_Impl( | 57 PPB_FileRef_Impl::PPB_FileRef_Impl( |
| 238 PluginInstance* instance, | 58 PluginInstance* instance, |
| 239 scoped_refptr<PPB_FileSystem_Impl> file_system, | 59 scoped_refptr<PPB_FileSystem_Impl> file_system, |
| 240 const std::string& validated_path) | 60 const std::string& validated_path) |
| 241 : Resource(instance), | 61 : Resource(instance), |
| 242 file_system_(file_system), | 62 file_system_(file_system), |
| 243 virtual_path_(validated_path) { | 63 virtual_path_(validated_path) { |
| 244 } | 64 } |
| 245 | 65 |
| 246 PPB_FileRef_Impl::PPB_FileRef_Impl(PluginInstance* instance, | 66 PPB_FileRef_Impl::PPB_FileRef_Impl(PluginInstance* instance, |
| 247 const FilePath& external_file_path) | 67 const FilePath& external_file_path) |
| 248 : Resource(instance), | 68 : Resource(instance), |
| 249 file_system_(NULL), | 69 file_system_(NULL), |
| 250 system_path_(external_file_path) { | 70 system_path_(external_file_path) { |
| 251 } | 71 } |
| 252 | 72 |
| 253 PPB_FileRef_Impl::~PPB_FileRef_Impl() { | 73 PPB_FileRef_Impl::~PPB_FileRef_Impl() { |
| 254 } | 74 } |
| 255 | 75 |
| 256 // static | 76 // static |
| 257 const PPB_FileRef_Dev* PPB_FileRef_Impl::GetInterface() { | 77 PP_Resource PPB_FileRef_Impl::Create(PP_Resource pp_file_system, |
| 258 return &ppb_fileref; | 78 const char* path) { |
| 79 EnterResourceNoLock<PPB_FileSystem_API> enter(pp_file_system, true); |
| 80 if (enter.failed()) |
| 81 return 0; |
| 82 |
| 83 PPB_FileSystem_Impl* file_system = |
| 84 static_cast<PPB_FileSystem_Impl*>(enter.object()); |
| 85 if (!file_system->instance()) |
| 86 return 0; |
| 87 |
| 88 std::string validated_path(path); |
| 89 if (!IsValidLocalPath(validated_path)) |
| 90 return 0; |
| 91 TrimTrailingSlash(&validated_path); |
| 92 |
| 93 PPB_FileRef_Impl* file_ref = |
| 94 new PPB_FileRef_Impl(file_system->instance(), |
| 95 file_system, validated_path); |
| 96 return file_ref->GetReference(); |
| 97 } |
| 98 |
| 99 PPB_FileRef_API* PPB_FileRef_Impl::AsPPB_FileRef_API() { |
| 100 return this; |
| 259 } | 101 } |
| 260 | 102 |
| 261 PPB_FileRef_Impl* PPB_FileRef_Impl::AsPPB_FileRef_Impl() { | 103 PPB_FileRef_Impl* PPB_FileRef_Impl::AsPPB_FileRef_Impl() { |
| 262 return this; | 104 return this; |
| 263 } | 105 } |
| 264 | 106 |
| 265 std::string PPB_FileRef_Impl::GetName() const { | 107 PP_FileSystemType_Dev PPB_FileRef_Impl::GetFileSystemType() const { |
| 108 // When the file ref exists but there's no explicit filesystem object |
| 109 // associated with it, that means it's an "external" filesystem. |
| 110 if (!file_system_) |
| 111 return PP_FILESYSTEMTYPE_EXTERNAL; |
| 112 return file_system_->type(); |
| 113 } |
| 114 |
| 115 PP_Var PPB_FileRef_Impl::GetName() const { |
| 116 std::string result; |
| 266 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) { | 117 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) { |
| 267 FilePath::StringType path = system_path_.value(); | 118 FilePath::StringType path = system_path_.value(); |
| 268 size_t pos = path.rfind(FilePath::kSeparators[0]); | 119 size_t pos = path.rfind(FilePath::kSeparators[0]); |
| 269 DCHECK(pos != FilePath::StringType::npos); | 120 DCHECK(pos != FilePath::StringType::npos); |
| 270 #if defined(OS_WIN) | 121 #if defined(OS_WIN) |
| 271 return WideToUTF8(path.substr(pos + 1)); | 122 result = WideToUTF8(path.substr(pos + 1)); |
| 272 #elif defined(OS_POSIX) | 123 #elif defined(OS_POSIX) |
| 273 return path.substr(pos + 1); | 124 result = path.substr(pos + 1); |
| 274 #else | 125 #else |
| 275 #error "Unsupported platform." | 126 #error "Unsupported platform." |
| 276 #endif | 127 #endif |
| 128 } else if (virtual_path_.size() == 1 && virtual_path_[0] == '/') { |
| 129 result = virtual_path_; |
| 130 } else { |
| 131 // There should always be a leading slash at least! |
| 132 size_t pos = virtual_path_.rfind('/'); |
| 133 DCHECK(pos != std::string::npos); |
| 134 result = virtual_path_.substr(pos + 1); |
| 277 } | 135 } |
| 278 | 136 |
| 279 if (virtual_path_.size() == 1 && virtual_path_[0] == '/') | 137 return StringVar::StringToPPVar(instance()->module(), result); |
| 280 return virtual_path_; | 138 } |
| 139 |
| 140 PP_Var PPB_FileRef_Impl::GetPath() const { |
| 141 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 142 return PP_MakeUndefined(); |
| 143 return StringVar::StringToPPVar(instance()->module(), virtual_path_); |
| 144 } |
| 145 |
| 146 PP_Resource PPB_FileRef_Impl::GetParent() { |
| 147 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 148 return 0; |
| 281 | 149 |
| 282 // There should always be a leading slash at least! | 150 // There should always be a leading slash at least! |
| 283 size_t pos = virtual_path_.rfind('/'); | 151 size_t pos = virtual_path_.rfind('/'); |
| 284 DCHECK(pos != std::string::npos); | |
| 285 | |
| 286 return virtual_path_.substr(pos + 1); | |
| 287 } | |
| 288 | |
| 289 scoped_refptr<PPB_FileRef_Impl> PPB_FileRef_Impl::GetParent() { | |
| 290 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 291 return new PPB_FileRef_Impl(); | |
| 292 | |
| 293 // There should always be a leading slash at least! | |
| 294 size_t pos = virtual_path_.rfind('/'); | |
| 295 DCHECK(pos != std::string::npos); | 152 DCHECK(pos != std::string::npos); |
| 296 | 153 |
| 297 // If the path is "/foo", then we want to include the slash. | 154 // If the path is "/foo", then we want to include the slash. |
| 298 if (pos == 0) | 155 if (pos == 0) |
| 299 pos++; | 156 pos++; |
| 300 std::string parent_path = virtual_path_.substr(0, pos); | 157 std::string parent_path = virtual_path_.substr(0, pos); |
| 301 | 158 |
| 302 PPB_FileRef_Impl* parent_ref = new PPB_FileRef_Impl(instance(), file_system_, | 159 scoped_refptr<PPB_FileRef_Impl> parent_ref( |
| 303 parent_path); | 160 new PPB_FileRef_Impl(instance(), file_system_, parent_path)); |
| 304 return parent_ref; | 161 return parent_ref->GetReference(); |
| 305 } | 162 } |
| 306 | 163 |
| 307 scoped_refptr<PPB_FileSystem_Impl> PPB_FileRef_Impl::GetFileSystem() const { | 164 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, |
| 308 return file_system_; | 165 PP_CompletionCallback callback) { |
| 166 if (!IsValidNonExternalFileSystem()) |
| 167 return PP_ERROR_NOACCESS; |
| 168 if (!instance()->delegate()->MakeDirectory( |
| 169 GetFileSystemURL(), PP_ToBool(make_ancestors), |
| 170 new FileCallbacks(instance()->module()->AsWeakPtr(), |
| 171 GetReferenceNoAddRef(), callback, |
| 172 NULL, NULL, NULL))) |
| 173 return PP_ERROR_FAILED; |
| 174 return PP_OK_COMPLETIONPENDING; |
| 309 } | 175 } |
| 310 | 176 |
| 311 PP_FileSystemType_Dev PPB_FileRef_Impl::GetFileSystemType() const { | 177 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time, |
| 312 // When the file ref exists but there's no explicit filesystem object | 178 PP_Time last_modified_time, |
| 313 // associated with it, that means it's an "external" filesystem. | 179 PP_CompletionCallback callback) { |
| 314 if (!file_system_) | 180 if (!IsValidNonExternalFileSystem()) |
| 315 return PP_FILESYSTEMTYPE_EXTERNAL; | 181 return PP_ERROR_NOACCESS; |
| 316 | 182 if (!instance()->delegate()->Touch( |
| 317 return file_system_->type(); | 183 GetFileSystemURL(), |
| 184 base::Time::FromDoubleT(last_access_time), |
| 185 base::Time::FromDoubleT(last_modified_time), |
| 186 new FileCallbacks(instance()->module()->AsWeakPtr(), |
| 187 GetReferenceNoAddRef(), callback, |
| 188 NULL, NULL, NULL))) |
| 189 return PP_ERROR_FAILED; |
| 190 return PP_OK_COMPLETIONPENDING; |
| 318 } | 191 } |
| 319 | 192 |
| 320 std::string PPB_FileRef_Impl::GetPath() const { | 193 int32_t PPB_FileRef_Impl::Delete(PP_CompletionCallback callback) { |
| 321 return virtual_path_; | 194 if (!IsValidNonExternalFileSystem()) |
| 195 return PP_ERROR_NOACCESS; |
| 196 if (!instance()->delegate()->Delete( |
| 197 GetFileSystemURL(), |
| 198 new FileCallbacks(instance()->module()->AsWeakPtr(), |
| 199 GetReferenceNoAddRef(), callback, |
| 200 NULL, NULL, NULL))) |
| 201 return PP_ERROR_FAILED; |
| 202 return PP_OK_COMPLETIONPENDING; |
| 203 } |
| 204 |
| 205 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref, |
| 206 PP_CompletionCallback callback) { |
| 207 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true); |
| 208 if (enter.failed()) |
| 209 return PP_ERROR_BADRESOURCE; |
| 210 PPB_FileRef_Impl* new_file_ref = |
| 211 static_cast<PPB_FileRef_Impl*>(enter.object()); |
| 212 |
| 213 if (!IsValidNonExternalFileSystem() || |
| 214 file_system_.get() != new_file_ref->file_system_.get()) |
| 215 return PP_ERROR_NOACCESS; |
| 216 |
| 217 // TODO(viettrungluu): Also cancel when the new file ref is destroyed? |
| 218 // http://crbug.com/67624 |
| 219 if (!instance()->delegate()->Rename( |
| 220 GetFileSystemURL(), new_file_ref->GetFileSystemURL(), |
| 221 new FileCallbacks(instance()->module()->AsWeakPtr(), |
| 222 GetReferenceNoAddRef(), callback, |
| 223 NULL, NULL, NULL))) |
| 224 return PP_ERROR_FAILED; |
| 225 return PP_OK_COMPLETIONPENDING; |
| 322 } | 226 } |
| 323 | 227 |
| 324 FilePath PPB_FileRef_Impl::GetSystemPath() const { | 228 FilePath PPB_FileRef_Impl::GetSystemPath() const { |
| 325 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { | 229 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 326 NOTREACHED(); | 230 NOTREACHED(); |
| 327 return FilePath(); | 231 return FilePath(); |
| 328 } | 232 } |
| 329 return system_path_; | 233 return system_path_; |
| 330 } | 234 } |
| 331 | 235 |
| 332 GURL PPB_FileRef_Impl::GetFileSystemURL() const { | 236 GURL PPB_FileRef_Impl::GetFileSystemURL() const { |
| 333 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 237 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 334 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { | 238 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { |
| 335 NOTREACHED(); | 239 NOTREACHED(); |
| 336 return GURL(); | 240 return GURL(); |
| 337 } | 241 } |
| 338 if (!virtual_path_.size()) | 242 if (!virtual_path_.size()) |
| 339 return file_system_->root_url(); | 243 return file_system_->root_url(); |
| 340 // Since |virtual_path_| starts with a '/', it looks like an absolute path. | 244 // Since |virtual_path_| starts with a '/', it looks like an absolute path. |
| 341 // We need to trim off the '/' before calling Resolve, as FileSystem URLs | 245 // We need to trim off the '/' before calling Resolve, as FileSystem URLs |
| 342 // start with a storage type identifier that looks like a path segment. | 246 // start with a storage type identifier that looks like a path segment. |
| 343 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand | 247 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand |
| 344 // FileSystem URLs. | 248 // FileSystem URLs. |
| 345 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); | 249 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); |
| 346 } | 250 } |
| 347 | 251 |
| 252 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { |
| 253 return file_system_ && file_system_->opened() && |
| 254 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; |
| 255 } |
| 256 |
| 348 } // namespace ppapi | 257 } // namespace ppapi |
| 349 } // namespace webkit | 258 } // namespace webkit |
| OLD | NEW |