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(0), | 82 PPB_FileSystem_Impl* file_system) |
58 file_system_() { | 83 : FileRefImpl(FileRefImpl::InitAsImpl(), info), |
84 file_system_(file_system), | |
85 external_file_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 external_file_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()); | |
bbudge
2011/08/25 19:50:37
This fits on a single line.
| |
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 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | |
144 if (!plugin_module) | |
145 return PP_MakeUndefined(); | |
146 return StringVar::StringToPPVar(plugin_module->pp_module(), result); | |
147 } | |
148 | |
149 PP_Var PPB_FileRef_Impl::GetPath() const { | |
150 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | |
151 return PP_MakeUndefined(); | |
152 PluginModule* plugin_module = ResourceHelper::GetPluginModule(this); | |
153 if (!plugin_module) | |
154 return PP_MakeUndefined(); | |
155 return StringVar::StringToPPVar(plugin_module->pp_module(), virtual_path_); | |
156 } | 140 } |
157 | 141 |
158 PP_Resource PPB_FileRef_Impl::GetParent() { | 142 PP_Resource PPB_FileRef_Impl::GetParent() { |
159 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) | 143 if (GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) |
160 return 0; | 144 return 0; |
161 | 145 |
146 const std::string& virtual_path = GetCreateInfo().path; | |
147 | |
162 // There should always be a leading slash at least! | 148 // There should always be a leading slash at least! |
163 size_t pos = virtual_path_.rfind('/'); | 149 size_t pos = virtual_path.rfind('/'); |
164 DCHECK(pos != std::string::npos); | 150 CHECK(pos != std::string::npos); |
165 | 151 |
166 // 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. |
167 if (pos == 0) | 153 if (pos == 0) |
168 pos++; | 154 pos++; |
169 std::string parent_path = virtual_path_.substr(0, pos); | 155 std::string parent_path = virtual_path.substr(0, pos); |
170 | 156 |
171 scoped_refptr<PPB_FileRef_Impl> parent_ref( | 157 scoped_refptr<PPB_FileRef_Impl> parent_ref( |
172 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; | |
173 return parent_ref->GetReference(); | 161 return parent_ref->GetReference(); |
174 } | 162 } |
175 | 163 |
176 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, | 164 int32_t PPB_FileRef_Impl::MakeDirectory(PP_Bool make_ancestors, |
177 PP_CompletionCallback callback) { | 165 PP_CompletionCallback callback) { |
178 if (!IsValidNonExternalFileSystem()) | 166 if (!IsValidNonExternalFileSystem()) |
179 return PP_ERROR_NOACCESS; | 167 return PP_ERROR_NOACCESS; |
180 | 168 |
181 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); | 169 PluginInstance* plugin_instance = ResourceHelper::GetPluginInstance(this); |
182 if (!plugin_instance) | 170 if (!plugin_instance) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 pp_resource(), callback, NULL, NULL, NULL))) | 232 pp_resource(), callback, NULL, NULL, NULL))) |
245 return PP_ERROR_FAILED; | 233 return PP_ERROR_FAILED; |
246 return PP_OK_COMPLETIONPENDING; | 234 return PP_OK_COMPLETIONPENDING; |
247 } | 235 } |
248 | 236 |
249 FilePath PPB_FileRef_Impl::GetSystemPath() const { | 237 FilePath PPB_FileRef_Impl::GetSystemPath() const { |
250 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { | 238 if (GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) { |
251 NOTREACHED(); | 239 NOTREACHED(); |
252 return FilePath(); | 240 return FilePath(); |
253 } | 241 } |
254 return system_path_; | 242 return external_file_system_path_; |
255 } | 243 } |
256 | 244 |
257 GURL PPB_FileRef_Impl::GetFileSystemURL() const { | 245 GURL PPB_FileRef_Impl::GetFileSystemURL() const { |
258 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && | 246 if (GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT && |
259 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { | 247 GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY) { |
260 NOTREACHED(); | 248 NOTREACHED(); |
261 return GURL(); | 249 return GURL(); |
262 } | 250 } |
263 if (!virtual_path_.size()) | 251 |
264 return file_system_->root_url(); | 252 const std::string& virtual_path = GetCreateInfo().path; |
253 CHECK(!virtual_path.empty()); // Should always be at least "/". | |
254 | |
265 // Since |virtual_path_| starts with a '/', it looks like an absolute path. | 255 // Since |virtual_path_| starts with a '/', it looks like an absolute path. |
266 // We need to trim off the '/' before calling Resolve, as FileSystem URLs | 256 // We need to trim off the '/' before calling Resolve, as FileSystem URLs |
267 // start with a storage type identifier that looks like a path segment. | 257 // start with a storage type identifier that looks like a path segment. |
268 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand | 258 // TODO(ericu): Switch this to use Resolve after fixing GURL to understand |
269 // FileSystem URLs. | 259 // FileSystem URLs. |
270 return GURL(file_system_->root_url().spec() + virtual_path_.substr(1)); | 260 return GURL(file_system_->root_url().spec() + virtual_path.substr(1)); |
271 } | 261 } |
272 | 262 |
273 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { | 263 bool PPB_FileRef_Impl::IsValidNonExternalFileSystem() const { |
274 return file_system_ && file_system_->opened() && | 264 return file_system_ && file_system_->opened() && |
275 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; | 265 file_system_->type() != PP_FILESYSTEMTYPE_EXTERNAL; |
276 } | 266 } |
277 | 267 |
278 } // namespace ppapi | 268 } // namespace ppapi |
279 } // namespace webkit | 269 } // namespace webkit |
OLD | NEW |