| 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 #include "webkit/plugins/ppapi/resource_helper.h" |
| 23 | 23 |
| 24 using ppapi::HostResource; |
| 25 using ppapi::PPB_FileRef_CreateInfo; |
| 24 using ppapi::PPTimeToTime; | 26 using ppapi::PPTimeToTime; |
| 25 using ppapi::StringVar; | 27 using ppapi::StringVar; |
| 26 using ppapi::thunk::EnterResourceNoLock; | 28 using ppapi::thunk::EnterResourceNoLock; |
| 27 using ppapi::thunk::PPB_FileRef_API; | 29 using ppapi::thunk::PPB_FileRef_API; |
| 28 using ppapi::thunk::PPB_FileSystem_API; | 30 using ppapi::thunk::PPB_FileSystem_API; |
| 29 | 31 |
| 30 namespace webkit { | 32 namespace webkit { |
| 31 namespace ppapi { | 33 namespace ppapi { |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 return true; | 46 return true; |
| 45 } | 47 } |
| 46 | 48 |
| 47 void TrimTrailingSlash(std::string* path) { | 49 void TrimTrailingSlash(std::string* path) { |
| 48 // If this path ends with a slash, then normalize it away unless path is the | 50 // If this path ends with a slash, then normalize it away unless path is the |
| 49 // root path. | 51 // root path. |
| 50 if (path->size() > 1 && path->at(path->size() - 1) == '/') | 52 if (path->size() > 1 && path->at(path->size() - 1) == '/') |
| 51 path->erase(path->size() - 1, 1); | 53 path->erase(path->size() - 1, 1); |
| 52 } | 54 } |
| 53 | 55 |
| 56 std::string GetNameForExternalFilePath(const FilePath& in_path) { |
| 57 const FilePath::StringType& path = in_path.value(); |
| 58 size_t pos = path.rfind(FilePath::kSeparators[0]); |
| 59 CHECK(pos != FilePath::StringType::npos); |
| 60 #if defined(OS_WIN) |
| 61 return WideToUTF8(path.substr(pos + 1)); |
| 62 #elif defined(OS_POSIX) |
| 63 return path.substr(pos + 1); |
| 64 #else |
| 65 #error "Unsupported platform." |
| 66 #endif |
| 67 } |
| 68 |
| 69 std::string GetNameForVirtualFilePath(const std::string& path) { |
| 70 if (path.size() == 1 && path[0] == '/') |
| 71 return path; |
| 72 |
| 73 // There should always be a leading slash at least! |
| 74 size_t pos = path.rfind('/'); |
| 75 CHECK(pos != std::string::npos); |
| 76 return path.substr(pos + 1); |
| 77 } |
| 78 |
| 54 } // namespace | 79 } // namespace |
| 55 | 80 |
| 56 PPB_FileRef_Impl::PPB_FileRef_Impl() | 81 PPB_FileRef_Impl::PPB_FileRef_Impl(const PPB_FileRef_CreateInfo& info, |
| 57 : Resource(NULL), | 82 PPB_FileSystem_Impl* file_system) |
| 58 file_system_(NULL) { | 83 : FileRefImpl(FileRefImpl::InitAsImpl(), info), |
| 84 file_system_(file_system), |
| 85 system_path_() { |
| 59 } | 86 } |
| 60 | 87 |
| 61 PPB_FileRef_Impl::PPB_FileRef_Impl( | 88 PPB_FileRef_Impl::PPB_FileRef_Impl(const PPB_FileRef_CreateInfo& info, |
| 62 PP_Instance instance, | |
| 63 scoped_refptr<PPB_FileSystem_Impl> file_system, | |
| 64 const std::string& validated_path) | |
| 65 : Resource(instance), | |
| 66 file_system_(file_system), | |
| 67 virtual_path_(validated_path) { | |
| 68 } | |
| 69 | |
| 70 PPB_FileRef_Impl::PPB_FileRef_Impl(PP_Instance instance, | |
| 71 const FilePath& external_file_path) | 89 const FilePath& external_file_path) |
| 72 : Resource(instance), | 90 : FileRefImpl(FileRefImpl::InitAsImpl(), info), |
| 73 file_system_(NULL), | 91 file_system_(), |
| 74 system_path_(external_file_path) { | 92 system_path_(external_file_path) { |
| 75 } | 93 } |
| 76 | 94 |
| 77 PPB_FileRef_Impl::~PPB_FileRef_Impl() { | 95 PPB_FileRef_Impl::~PPB_FileRef_Impl() { |
| 78 } | 96 } |
| 79 | 97 |
| 80 // static | 98 // static |
| 81 PP_Resource PPB_FileRef_Impl::Create(PP_Resource pp_file_system, | 99 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateInternal(PP_Resource pp_file_system, |
| 82 const char* path) { | 100 const std::string& path) { |
| 83 EnterResourceNoLock<PPB_FileSystem_API> enter(pp_file_system, true); | 101 EnterResourceNoLock<PPB_FileSystem_API> enter(pp_file_system, true); |
| 84 if (enter.failed()) | 102 if (enter.failed()) |
| 85 return 0; | 103 return 0; |
| 86 | 104 |
| 87 PPB_FileSystem_Impl* file_system = | 105 PPB_FileSystem_Impl* file_system = |
| 88 static_cast<PPB_FileSystem_Impl*>(enter.object()); | 106 static_cast<PPB_FileSystem_Impl*>(enter.object()); |
| 89 if (!file_system->pp_instance()) | 107 if (!file_system->pp_instance()) |
| 90 return 0; | 108 return 0; |
| 91 | 109 |
| 92 if (file_system->type() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 110 if (file_system->type() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 93 file_system->type() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) | 111 file_system->type() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) |
| 94 return 0; | 112 return 0; |
| 95 | 113 |
| 96 std::string validated_path(path); | 114 PPB_FileRef_CreateInfo info; |
| 97 if (!IsValidLocalPath(validated_path)) | 115 info.resource = HostResource::MakeInstanceOnly( |
| 116 file_system->pp_instance()); |
| 117 info.file_system_type = file_system->type(); |
| 118 |
| 119 // Validate the path. |
| 120 info.path = path; |
| 121 if (!IsValidLocalPath(info.path)) |
| 98 return 0; | 122 return 0; |
| 99 TrimTrailingSlash(&validated_path); | 123 TrimTrailingSlash(&info.path); |
| 100 | 124 |
| 101 return (new PPB_FileRef_Impl(file_system->pp_instance(), | 125 info.name = GetNameForVirtualFilePath(info.path); |
| 102 file_system, validated_path))->GetReference(); | 126 |
| 127 return new PPB_FileRef_Impl(info, file_system); |
| 103 } | 128 } |
| 104 | 129 |
| 105 PPB_FileRef_API* PPB_FileRef_Impl::AsPPB_FileRef_API() { | 130 // static |
| 106 return this; | 131 PPB_FileRef_Impl* PPB_FileRef_Impl::CreateExternal( |
| 107 } | 132 PP_Instance instance, |
| 133 const FilePath& external_file_path) { |
| 134 PPB_FileRef_CreateInfo info; |
| 135 info.resource = HostResource::MakeInstanceOnly(instance); |
| 136 info.file_system_type = PP_FILESYSTEMTYPE_EXTERNAL; |
| 137 info.name = GetNameForExternalFilePath(external_file_path); |
| 108 | 138 |
| 109 PPB_FileRef_Impl* PPB_FileRef_Impl::AsPPB_FileRef_Impl() { | 139 return new PPB_FileRef_Impl(info, external_file_path); |
| 110 return this; | |
| 111 } | |
| 112 | |
| 113 PP_FileSystemType PPB_FileRef_Impl::GetFileSystemType() const { | |
| 114 // When the file ref exists but there's no explicit filesystem object | |
| 115 // associated with it, that means it's an "external" filesystem. | |
| 116 if (!file_system_) | |
| 117 return PP_FILESYSTEMTYPE_EXTERNAL; | |
| 118 return file_system_->type(); | |
| 119 } | |
| 120 | |
| 121 PP_Var PPB_FileRef_Impl::GetName() const { | |
| 122 std::string result; | |
| 123 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) { | |
| 124 FilePath::StringType path = system_path_.value(); | |
| 125 size_t pos = path.rfind(FilePath::kSeparators[0]); | |
| 126 DCHECK(pos != FilePath::StringType::npos); | |
| 127 #if defined(OS_WIN) | |
| 128 result = WideToUTF8(path.substr(pos + 1)); | |
| 129 #elif defined(OS_POSIX) | |
| 130 result = path.substr(pos + 1); | |
| 131 #else | |
| 132 #error "Unsupported platform." | |
| 133 #endif | |
| 134 } else if (virtual_path_.size() == 1 && virtual_path_[0] == '/') { | |
| 135 result = virtual_path_; | |
| 136 } else { | |
| 137 // There should always be a leading slash at least! | |
| 138 size_t pos = virtual_path_.rfind('/'); | |
| 139 DCHECK(pos != std::string::npos); | |
| 140 result = virtual_path_.substr(pos + 1); | |
| 141 } | |
| 142 | |
| 143 return StringVar::StringToPPVar( | |
| 144 ResourceHelper::GetPluginModule(this)->pp_module(), result); | |
| 145 } | |
| 146 | |
| 147 PP_Var PPB_FileRef_Impl::GetPath() const { | |
| 148 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
| 149 return PP_MakeUndefined(); | |
| 150 return StringVar::StringToPPVar( | |
| 151 ResourceHelper::GetPluginModule(this)->pp_module(), virtual_path_); | |
| 152 } | 140 } |
| 153 | 141 |
| 154 PP_Resource PPB_FileRef_Impl::GetParent() { | 142 PP_Resource PPB_FileRef_Impl::GetParent() { |
| 155 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | 143 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
| 156 return 0; | 144 return 0; |
| 157 | 145 |
| 146 const std::string& virtual_path = GetCreateInfo().path; |
| 147 |
| 158 // There should always be a leading slash at least! | 148 // There should always be a leading slash at least! |
| 159 size_t pos = virtual_path_.rfind('/'); | 149 size_t pos = virtual_path.rfind('/'); |
| 160 DCHECK(pos != std::string::npos); | 150 CHECK(pos != std::string::npos); |
| 161 | 151 |
| 162 // If the path is "/foo", then we want to include the slash. | 152 // If the path is "/foo", then we want to include the slash. |
| 163 if (pos == 0) | 153 if (pos == 0) |
| 164 pos++; | 154 pos++; |
| 165 std::string parent_path = virtual_path_.substr(0, pos); | 155 std::string parent_path = virtual_path.substr(0, pos); |
| 166 | 156 |
| 167 scoped_refptr<PPB_FileRef_Impl> parent_ref( | 157 scoped_refptr<PPB_FileRef_Impl> parent_ref( |
| 168 new PPB_FileRef_Impl(pp_instance(), file_system_, parent_path)); | 158 CreateInternal(file_system_->pp_resource(), parent_path)); |
| 159 if (!parent_ref.get()) |
| 160 return 0; |
| 169 return parent_ref->GetReference(); | 161 return parent_ref->GetReference(); |
| 170 } | 162 } |
| 171 | 163 |
| 172 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, | 164 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, |
| 173 PP_CompletionCallback callback) { | 165 PP_CompletionCallback callback) { |
| 174 if (!IsValidNonExternalFileSystem()) | 166 if (!IsValidNonExternalFileSystem()) |
| 175 return PP_ERROR_NOACCESS; | 167 return PP_ERROR_NOACCESS; |
| 176 | 168 |
| 177 PluginInstance* instance = ResourceHelper::GetPluginInstance(this); | 169 PluginInstance* instance = ResourceHelper::GetPluginInstance(this); |
| 178 if (!instance->delegate()->MakeDirectory( | 170 if (!instance->delegate()->MakeDirectory( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 233 } |
| 242 return system_path_; | 234 return system_path_; |
| 243 } | 235 } |
| 244 | 236 |
| 245 GURL PPB_FileRef_Impl::GetFileSystemURL() const { | 237 GURL PPB_FileRef_Impl::GetFileSystemURL() const { |
| 246 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 238 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
| 247 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { | 239 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { |
| 248 NOTREACHED(); | 240 NOTREACHED(); |
| 249 return GURL(); | 241 return GURL(); |
| 250 } | 242 } |
| 251 if (!virtual_path_.size()) | 243 |
| 252 return file_system_->root_url(); | 244 const std::string& virtual_path = GetCreateInfo().path; |
| 245 CHECK(!virtual_path.empty()); // Should always be at least "/". |
| 246 |
| 253 // Since |virtual_path_| starts with a '/', it looks like an absolute path. | 247 // Since |virtual_path_| starts with a '/', it looks like an absolute path. |
| 254 // We need to trim off the '/' before calling Resolve, as FileSystem URLs | 248 // We need to trim off the '/' before calling Resolve, as FileSystem URLs |
| 255 // start with a storage type identifier that looks like a path segment. | 249 // start with a storage type identifier that looks like a path segment. |
| 256 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand | 250 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand |
| 257 // FileSystem URLs. | 251 // FileSystem URLs. |
| 258 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); | 252 return GURL(file_system_->root_url().spec() + virtual_path.substr(1)); |
| 259 } | 253 } |
| 260 | 254 |
| 261 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { | 255 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { |
| 262 return file_system_ && file_system_->opened() && | 256 return file_system_ && file_system_->opened() && |
| 263 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; | 257 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; |
| 264 } | 258 } |
| 265 | 259 |
| 266 } // namespace ppapi | 260 } // namespace ppapi |
| 267 } // namespace webkit | 261 } // namespace webkit |
| OLD | NEW |