| 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" | 11 #include "ppapi/thunk/enter.h" |
| 12 #include "ppapi/thunk/ppb_file_system_api.h" | 12 #include "ppapi/thunk/ppb_file_system_api.h" |
| 13 #include "ppapi/shared_impl/time_conversion.h" | 13 #include "ppapi/shared_impl/time_conversion.h" |
| 14 #include "ppapi/shared_impl/var.h" | 14 #include "ppapi/shared_impl/var.h" |
| 15 #include "webkit/plugins/ppapi/common.h" | 15 #include "webkit/plugins/ppapi/common.h" |
| 16 #include "webkit/plugins/ppapi/file_callbacks.h" | 16 #include "webkit/plugins/ppapi/file_callbacks.h" |
| 17 #include "webkit/plugins/ppapi/plugin_delegate.h" | 17 #include "webkit/plugins/ppapi/plugin_delegate.h" |
| 18 #include "webkit/plugins/ppapi/plugin_module.h" | 18 #include "webkit/plugins/ppapi/plugin_module.h" |
| 19 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 19 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 20 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" | 20 #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" |
| 21 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" | 21 #include "webkit/plugins/ppapi/ppb_file_system_impl.h" |
| 22 #include "webkit/plugins/ppapi/resource_helper.h" |
| 22 | 23 |
| 23 using ppapi::PPTimeToTime; | 24 using ppapi::PPTimeToTime; |
| 24 using ppapi::StringVar; | 25 using ppapi::StringVar; |
| 25 using ppapi::thunk::EnterResourceNoLock; | 26 using ppapi::thunk::EnterResourceNoLock; |
| 26 using ppapi::thunk::PPB_FileRef_API; | 27 using ppapi::thunk::PPB_FileRef_API; |
| 27 using ppapi::thunk::PPB_FileSystem_API; | 28 using ppapi::thunk::PPB_FileSystem_API; |
| 28 | 29 |
| 29 namespace webkit { | 30 namespace webkit { |
| 30 namespace ppapi { | 31 namespace ppapi { |
| 31 | 32 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 } | 52 } |
| 52 | 53 |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 PPB_FileRef_Impl::PPB_FileRef_Impl() | 56 PPB_FileRef_Impl::PPB_FileRef_Impl() |
| 56 : Resource(NULL), | 57 : Resource(NULL), |
| 57 file_system_(NULL) { | 58 file_system_(NULL) { |
| 58 } | 59 } |
| 59 | 60 |
| 60 PPB_FileRef_Impl::PPB_FileRef_Impl( | 61 PPB_FileRef_Impl::PPB_FileRef_Impl( |
| 61 PluginInstance* instance, | 62 PP_Instance instance, |
| 62 scoped_refptr<PPB_FileSystem_Impl> file_system, | 63 scoped_refptr<PPB_FileSystem_Impl> file_system, |
| 63 const std::string& validated_path) | 64 const std::string& validated_path) |
| 64 : Resource(instance), | 65 : Resource(instance), |
| 65 file_system_(file_system), | 66 file_system_(file_system), |
| 66 virtual_path_(validated_path) { | 67 virtual_path_(validated_path) { |
| 67 } | 68 } |
| 68 | 69 |
| 69 PPB_FileRef_Impl::PPB_FileRef_Impl(PluginInstance* instance, | 70 PPB_FileRef_Impl::PPB_FileRef_Impl(PP_Instance instance, |
| 70 const FilePath& external_file_path) | 71 const FilePath& external_file_path) |
| 71 : Resource(instance), | 72 : Resource(instance), |
| 72 file_system_(NULL), | 73 file_system_(NULL), |
| 73 system_path_(external_file_path) { | 74 system_path_(external_file_path) { |
| 74 } | 75 } |
| 75 | 76 |
| 76 PPB_FileRef_Impl::~PPB_FileRef_Impl() { | 77 PPB_FileRef_Impl::~PPB_FileRef_Impl() { |
| 77 } | 78 } |
| 78 | 79 |
| 79 // static | 80 // static |
| 80 PP_Resource PPB_FileRef_Impl::Create(PP_Resource pp_file_system, | 81 PP_Resource PPB_FileRef_Impl::Create(PP_Resource pp_file_system, |
| 81 const char* path) { | 82 const char* path) { |
| 82 EnterResourceNoLock<PPB_FileSystem_API> enter(pp_file_system, true); | 83 EnterResourceNoLock<PPB_FileSystem_API> enter(pp_file_system, true); |
| 83 if (enter.failed()) | 84 if (enter.failed()) |
| 84 return 0; | 85 return 0; |
| 85 | 86 |
| 86 PPB_FileSystem_Impl* file_system = | 87 PPB_FileSystem_Impl* file_system = |
| 87 static_cast<PPB_FileSystem_Impl*>(enter.object()); | 88 static_cast<PPB_FileSystem_Impl*>(enter.object()); |
| 88 if (!file_system->instance()) | 89 if (!file_system->pp_instance()) |
| 89 return 0; | 90 return 0; |
| 90 | 91 |
| 91 if (file_system->type() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 92 if (file_system->type() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 92 file_system->type() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) | 93 file_system->type() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) |
| 93 return 0; | 94 return 0; |
| 94 | 95 |
| 95 std::string validated_path(path); | 96 std::string validated_path(path); |
| 96 if (!IsValidLocalPath(validated_path)) | 97 if (!IsValidLocalPath(validated_path)) |
| 97 return 0; | 98 return 0; |
| 98 TrimTrailingSlash(&validated_path); | 99 TrimTrailingSlash(&validated_path); |
| 99 | 100 |
| 100 return (new PPB_FileRef_Impl(file_system->instance(), | 101 return (new PPB_FileRef_Impl(file_system->pp_instance(), |
| 101 file_system, validated_path))->GetReference(); | 102 file_system, validated_path))->GetReference(); |
| 102 } | 103 } |
| 103 | 104 |
| 104 PPB_FileRef_API* PPB_FileRef_Impl::AsPPB_FileRef_API() { | 105 PPB_FileRef_API* PPB_FileRef_Impl::AsPPB_FileRef_API() { |
| 105 return this; | 106 return this; |
| 106 } | 107 } |
| 107 | 108 |
| 108 PPB_FileRef_Impl* PPB_FileRef_Impl::AsPPB_FileRef_Impl() { | 109 PPB_FileRef_Impl* PPB_FileRef_Impl::AsPPB_FileRef_Impl() { |
| 109 return this; | 110 return this; |
| 110 } | 111 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 132 #endif | 133 #endif |
| 133 } else if (virtual_path_.size() == 1 && virtual_path_[0] == '/') { | 134 } else if (virtual_path_.size() == 1 && virtual_path_[0] == '/') { |
| 134 result = virtual_path_; | 135 result = virtual_path_; |
| 135 } else { | 136 } else { |
| 136 // There should always be a leading slash at least! | 137 // There should always be a leading slash at least! |
| 137 size_t pos = virtual_path_.rfind('/'); | 138 size_t pos = virtual_path_.rfind('/'); |
| 138 DCHECK(pos != std::string::npos); | 139 DCHECK(pos != std::string::npos); |
| 139 result = virtual_path_.substr(pos + 1); | 140 result = virtual_path_.substr(pos + 1); |
| 140 } | 141 } |
| 141 | 142 |
| 142 return StringVar::StringToPPVar(instance()->module()->pp_module(), result); | 143 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 144 if (!plugin_module) |
| 145 return PP_MakeUndefined(); |
| 146 return StringVar::StringToPPVar(plugin_module->pp_module(), result); |
| 143 } | 147 } |
| 144 | 148 |
| 145 PP_Var PPB_FileRef_Impl::GetPath() const { | 149 PP_Var PPB_FileRef_Impl::GetPath() const { |
| 146 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | 150 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 147 return PP_MakeUndefined(); | 151 return PP_MakeUndefined(); |
| 148 return StringVar::StringToPPVar(instance()->module()->pp_module(), | 152 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); |
| 149 virtual_path_); | 153 if (!plugin_module) |
| 154 return PP_MakeUndefined(); |
| 155 return StringVar::StringToPPVar(plugin_module->pp_module(), virtual_path_); |
| 150 } | 156 } |
| 151 | 157 |
| 152 PP_Resource PPB_FileRef_Impl::GetParent() { | 158 PP_Resource PPB_FileRef_Impl::GetParent() { |
| 153 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | 159 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 154 return 0; | 160 return 0; |
| 155 | 161 |
| 156 // There should always be a leading slash at least! | 162 // There should always be a leading slash at least! |
| 157 size_t pos = virtual_path_.rfind('/'); | 163 size_t pos = virtual_path_.rfind('/'); |
| 158 DCHECK(pos != std::string::npos); | 164 DCHECK(pos != std::string::npos); |
| 159 | 165 |
| 160 // If the path is "/foo", then we want to include the slash. | 166 // If the path is "/foo", then we want to include the slash. |
| 161 if (pos == 0) | 167 if (pos == 0) |
| 162 pos++; | 168 pos++; |
| 163 std::string parent_path = virtual_path_.substr(0, pos); | 169 std::string parent_path = virtual_path_.substr(0, pos); |
| 164 | 170 |
| 165 scoped_refptr<PPB_FileRef_Impl> parent_ref( | 171 scoped_refptr<PPB_FileRef_Impl> parent_ref( |
| 166 new PPB_FileRef_Impl(instance(), file_system_, parent_path)); | 172 new PPB_FileRef_Impl(pp_instance(), file_system_, parent_path)); |
| 167 return parent_ref->GetReference(); | 173 return parent_ref->GetReference(); |
| 168 } | 174 } |
| 169 | 175 |
| 170 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, | 176 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, |
| 171 PP_CompletionCallback callback) { | 177 PP_CompletionCallback callback) { |
| 172 if (!IsValidNonExternalFileSystem()) | 178 if (!IsValidNonExternalFileSystem()) |
| 173 return PP_ERROR_NOACCESS; | 179 return PP_ERROR_NOACCESS; |
| 174 if (!instance()->delegate()->MakeDirectory( | 180 |
| 181 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
| 182 if (!plugin_instance) |
| 183 return PP_ERROR_FAILED; |
| 184 if (!plugin_instance->delegate()->MakeDirectory( |
| 175 GetFileSystemURL(), PP_ToBool(make_ancestors), | 185 GetFileSystemURL(), PP_ToBool(make_ancestors), |
| 176 new FileCallbacks(instance()->module()->AsWeakPtr(), | 186 new FileCallbacks(plugin_instance->module()->AsWeakPtr(), |
| 177 pp_resource(), callback, NULL, NULL, NULL))) | 187 pp_resource(), callback, NULL, NULL, NULL))) |
| 178 return PP_ERROR_FAILED; | 188 return PP_ERROR_FAILED; |
| 179 return PP_OK_COMPLETIONPENDING; | 189 return PP_OK_COMPLETIONPENDING; |
| 180 } | 190 } |
| 181 | 191 |
| 182 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time, | 192 int32_t PPB_FileRef_Impl::Touch(PP_Time last_access_time, |
| 183 PP_Time last_modified_time, | 193 PP_Time last_modified_time, |
| 184 PP_CompletionCallback callback) { | 194 PP_CompletionCallback callback) { |
| 185 if (!IsValidNonExternalFileSystem()) | 195 if (!IsValidNonExternalFileSystem()) |
| 186 return PP_ERROR_NOACCESS; | 196 return PP_ERROR_NOACCESS; |
| 187 if (!instance()->delegate()->Touch( | 197 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
| 198 if (!plugin_instance) |
| 199 return PP_ERROR_FAILED; |
| 200 if (!plugin_instance->delegate()->Touch( |
| 188 GetFileSystemURL(), | 201 GetFileSystemURL(), |
| 189 PPTimeToTime(last_access_time), | 202 PPTimeToTime(last_access_time), |
| 190 PPTimeToTime(last_modified_time), | 203 PPTimeToTime(last_modified_time), |
| 191 new FileCallbacks(instance()->module()->AsWeakPtr(), | 204 new FileCallbacks(plugin_instance->module()->AsWeakPtr(), |
| 192 pp_resource(), callback, NULL, NULL, NULL))) | 205 pp_resource(), callback, NULL, NULL, NULL))) |
| 193 return PP_ERROR_FAILED; | 206 return PP_ERROR_FAILED; |
| 194 return PP_OK_COMPLETIONPENDING; | 207 return PP_OK_COMPLETIONPENDING; |
| 195 } | 208 } |
| 196 | 209 |
| 197 int32_t PPB_FileRef_Impl::Delete(PP_CompletionCallback callback) { | 210 int32_t PPB_FileRef_Impl::Delete(PP_CompletionCallback callback) { |
| 198 if (!IsValidNonExternalFileSystem()) | 211 if (!IsValidNonExternalFileSystem()) |
| 199 return PP_ERROR_NOACCESS; | 212 return PP_ERROR_NOACCESS; |
| 200 if (!instance()->delegate()->Delete( | 213 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
| 214 if (!plugin_instance) |
| 215 return PP_ERROR_FAILED; |
| 216 if (!plugin_instance->delegate()->Delete( |
| 201 GetFileSystemURL(), | 217 GetFileSystemURL(), |
| 202 new FileCallbacks(instance()->module()->AsWeakPtr(), | 218 new FileCallbacks(plugin_instance->module()->AsWeakPtr(), |
| 203 pp_resource(), callback, NULL, NULL, NULL))) | 219 pp_resource(), callback, NULL, NULL, NULL))) |
| 204 return PP_ERROR_FAILED; | 220 return PP_ERROR_FAILED; |
| 205 return PP_OK_COMPLETIONPENDING; | 221 return PP_OK_COMPLETIONPENDING; |
| 206 } | 222 } |
| 207 | 223 |
| 208 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref, | 224 int32_t PPB_FileRef_Impl::Rename(PP_Resource new_pp_file_ref, |
| 209 PP_CompletionCallback callback) { | 225 PP_CompletionCallback callback) { |
| 210 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true); | 226 EnterResourceNoLock<PPB_FileRef_API> enter(new_pp_file_ref, true); |
| 211 if (enter.failed()) | 227 if (enter.failed()) |
| 212 return PP_ERROR_BADRESOURCE; | 228 return PP_ERROR_BADRESOURCE; |
| 213 PPB_FileRef_Impl* new_file_ref = | 229 PPB_FileRef_Impl* new_file_ref = |
| 214 static_cast<PPB_FileRef_Impl*>(enter.object()); | 230 static_cast<PPB_FileRef_Impl*>(enter.object()); |
| 215 | 231 |
| 216 if (!IsValidNonExternalFileSystem() || | 232 if (!IsValidNonExternalFileSystem() || |
| 217 file_system_.get() != new_file_ref->file_system_.get()) | 233 file_system_.get() != new_file_ref->file_system_.get()) |
| 218 return PP_ERROR_NOACCESS; | 234 return PP_ERROR_NOACCESS; |
| 219 | 235 |
| 220 // TODO(viettrungluu): Also cancel when the new file ref is destroyed? | 236 // TODO(viettrungluu): Also cancel when the new file ref is destroyed? |
| 221 // http://crbug.com/67624 | 237 // http://crbug.com/67624 |
| 222 if (!instance()->delegate()->Rename( | 238 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
| 239 if (!plugin_instance) |
| 240 return PP_ERROR_FAILED; |
| 241 if (!plugin_instance->delegate()->Rename( |
| 223 GetFileSystemURL(), new_file_ref->GetFileSystemURL(), | 242 GetFileSystemURL(), new_file_ref->GetFileSystemURL(), |
| 224 new FileCallbacks(instance()->module()->AsWeakPtr(), | 243 new FileCallbacks(plugin_instance->module()->AsWeakPtr(), |
| 225 pp_resource(), callback, NULL, NULL, NULL))) | 244 pp_resource(), callback, NULL, NULL, NULL))) |
| 226 return PP_ERROR_FAILED; | 245 return PP_ERROR_FAILED; |
| 227 return PP_OK_COMPLETIONPENDING; | 246 return PP_OK_COMPLETIONPENDING; |
| 228 } | 247 } |
| 229 | 248 |
| 230 FilePath PPB_FileRef_Impl::GetSystemPath() const { | 249 FilePath PPB_FileRef_Impl::GetSystemPath() const { |
| 231 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { | 250 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { |
| 232 NOTREACHED(); | 251 NOTREACHED(); |
| 233 return FilePath(); | 252 return FilePath(); |
| 234 } | 253 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 251 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); | 270 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); |
| 252 } | 271 } |
| 253 | 272 |
| 254 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { | 273 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { |
| 255 return file_system_ && file_system_->opened() && | 274 return file_system_ && file_system_->opened() && |
| 256 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; | 275 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; |
| 257 } | 276 } |
| 258 | 277 |
| 259 } // namespace ppapi | 278 } // namespace ppapi |
| 260 } // namespace webkit | 279 } // namespace webkit |
| OLD | NEW |