Chromium Code Reviews| Index: ppapi/proxy/flash_file_resource.cc |
| diff --git a/ppapi/proxy/flash_file_resource.cc b/ppapi/proxy/flash_file_resource.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..710cfb4a430c09ebde0784b8c505153491159370 |
| --- /dev/null |
| +++ b/ppapi/proxy/flash_file_resource.cc |
| @@ -0,0 +1,234 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ppapi/proxy/flash_file_resource.h" |
| + |
| +#include "ipc/ipc_message.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/shared_impl/file_type_conversion.h" |
| +#include "ppapi/shared_impl/time_conversion.h" |
| +#include "ppapi/shared_impl/var.h" |
| +#include "ppapi/thunk/enter.h" |
| + |
| +namespace ppapi { |
| +namespace proxy { |
| + |
| +FlashFileResource::FlashFileResource(Connection connection, |
| + PP_Instance instance) |
| + : PluginResource(connection, instance) { |
| + SendCreate(BROWSER, PpapiHostMsg_FlashFile_Create()); |
| +} |
| + |
| +FlashFileResource::~FlashFileResource() { |
| +} |
| + |
| +thunk::PPB_Flash_File_API* FlashFileResource::AsPPB_Flash_File_API() { |
| + return this; |
| +} |
| + |
| +int32_t FlashFileResource::OpenFile(PP_Instance instance, |
| + const char* path, |
| + int32_t mode, |
| + PP_FileHandle* file) { |
| + return OpenFileHelper(instance, path, PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + mode, file); |
| +} |
| + |
| +int32_t FlashFileResource::RenameFile(PP_Instance instance, |
| + const char* path_from, |
| + const char* path_to) { |
| + PepperFilePath pepper_from(PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + FilePath::FromUTF8Unsafe(path_from)); |
|
yzshen1
2012/11/21 00:38:59
nit: explicitly include file_path.h
raymes
2012/11/21 22:44:53
Done.
|
| + PepperFilePath pepper_to(PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + FilePath::FromUTF8Unsafe(path_to)); |
| + |
| + int32_t error = SyncCall<IPC::Message>( |
| + BROWSER, PpapiHostMsg_FlashFile_RenameFile(pepper_from, pepper_to)); |
| + |
| + return error; |
| +} |
| + |
| +int32_t FlashFileResource::DeleteFileOrDir(PP_Instance instance, |
| + const char* path, |
| + PP_Bool recursive) { |
| + PepperFilePath pepper_path(PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + FilePath::FromUTF8Unsafe(path)); |
| + |
| + int32_t error = SyncCall<IPC::Message>( |
|
yzshen1
2012/11/21 00:38:59
wrong indent.
raymes
2012/11/21 22:44:53
Done.
|
| + BROWSER, PpapiHostMsg_FlashFile_DeleteFileOrDir(pepper_path, |
| + PP_ToBool(recursive))); |
| + |
| + return error; |
| +} |
| + |
| +int32_t FlashFileResource::CreateDir(PP_Instance instance, const char* path) { |
| + PepperFilePath pepper_path(PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + FilePath::FromUTF8Unsafe(path)); |
| + |
| + int32_t error = SyncCall<IPC::Message>(BROWSER, |
| + PpapiHostMsg_FlashFile_CreateDir(pepper_path)); |
| + |
| + return error; |
| +} |
| + |
| +int32_t FlashFileResource::QueryFile(PP_Instance instance, |
| + const char* path, |
| + PP_FileInfo* info) { |
| + return QueryFileHelper(instance, path, PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + info); |
| +} |
| + |
| +int32_t FlashFileResource::GetDirContents(PP_Instance instance, |
| + const char* path, |
| + PP_DirContents_Dev** contents) { |
| + ppapi::DirContents entries; |
| + PepperFilePath pepper_path(PepperFilePath::DOMAIN_MODULE_LOCAL, |
| + FilePath::FromUTF8Unsafe(path)); |
| + |
| + int32_t error = SyncCall<PpapiPluginMsg_FlashFile_GetDirContentsReply>( |
| + BROWSER, PpapiHostMsg_FlashFile_GetDirContents(pepper_path), &entries); |
| + |
| + if (error == PP_OK) { |
| + // Copy the serialized dir entries to the output struct. |
| + *contents = new PP_DirContents_Dev; |
| + (*contents)->count = static_cast<int32_t>(entries.size()); |
| + (*contents)->entries = new PP_DirEntry_Dev[entries.size()]; |
| + for (size_t i = 0; i < entries.size(); i++) { |
| + const ppapi::DirEntry& source = entries[i]; |
| + PP_DirEntry_Dev* dest = &(*contents)->entries[i]; |
| + std::string name = source.name.AsUTF8Unsafe(); |
| + char* name_copy = new char[name.size() + 1]; |
| + memcpy(name_copy, name.c_str(), name.size() + 1); |
| + dest->name = name_copy; |
| + dest->is_dir = PP_FromBool(source.is_dir); |
| + } |
| + } |
| + |
| + return error; |
| +} |
| + |
| +void FlashFileResource::FreeDirContents(PP_Instance instance, |
| + PP_DirContents_Dev* contents) { |
| + for (int32_t i = 0; i < contents->count; ++i) |
| + delete[] contents->entries[i].name; |
| + delete[] contents->entries; |
| + delete contents; |
| +} |
| + |
| +int32_t FlashFileResource::CreateTemporaryFile(PP_Instance instance, |
| + PP_FileHandle* file) { |
| + if (!file) |
| + return PP_ERROR_BADARGUMENT; |
| + |
| + IPC::Message unused; |
| + ResourceMessageReplyParams reply_params; |
| + int32_t error = GenericSyncCall(BROWSER, |
| + PpapiHostMsg_FlashFile_CreateTemporaryFile(), &unused, &reply_params); |
| + if (error != PP_OK) |
| + return error; |
| + |
| + IPC::PlatformFileForTransit transit_file; |
| + if (!reply_params.TakeFileHandleAtIndex(0, &transit_file)) |
| + return PP_ERROR_FAILED; |
| + |
| + *file = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| + return PP_OK; |
| +} |
| + |
| +int32_t FlashFileResource::OpenFileRef(PP_Instance instance, |
| + PP_Resource file_ref, |
| + int32_t mode, |
| + PP_FileHandle* file) { |
| + thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true); |
| + if (enter.failed()) |
| + return PP_ERROR_BADRESOURCE; |
| + if (enter.object()->GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) |
| + return PP_ERROR_BADARGUMENT; |
| + StringVar* string_var = |
| + StringVar::FromPPVar(enter.object()->GetAbsolutePath()); |
|
yzshen1
2012/11/21 00:38:59
the PP_Var is leaked.
raymes
2012/11/21 22:44:53
Done.
|
| + if (!string_var) |
| + return PP_ERROR_BADARGUMENT; |
| + std::string path = string_var->value(); |
| + |
| + return OpenFileHelper(instance, path, PepperFilePath::DOMAIN_ABSOLUTE, mode, |
| + file); |
| +} |
| + |
| +int32_t FlashFileResource::QueryFileRef(PP_Instance instance, |
| + PP_Resource file_ref, |
| + PP_FileInfo* info) { |
| + thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true); |
| + if (enter.failed()) |
| + return PP_ERROR_BADRESOURCE; |
| + if (enter.object()->GetFileSystemType() != PP_FILESYSTEMTYPE_EXTERNAL) |
| + return PP_ERROR_BADARGUMENT; |
| + StringVar* string_var = |
| + StringVar::FromPPVar(enter.object()->GetAbsolutePath()); |
|
yzshen1
2012/11/21 00:38:59
- the PP_Var is leaked.
- Is it possible the reuse
raymes
2012/11/21 22:44:53
Done.
|
| + if (!string_var) |
| + return PP_ERROR_BADARGUMENT; |
| + std::string path = string_var->value(); |
| + |
| + return QueryFileHelper(instance, path, PepperFilePath::DOMAIN_ABSOLUTE, info); |
| +} |
| + |
| +int32_t FlashFileResource::OpenFileHelper(PP_Instance instance, |
|
yzshen1
2012/11/21 00:38:59
instance is not used.
raymes
2012/11/21 22:44:53
Done.
|
| + const std::string& path, |
| + PepperFilePath::Domain domain_type, |
| + int32_t mode, |
| + PP_FileHandle* file) { |
| + int flags = 0; |
| + if (path.empty() || |
| + !ppapi::PepperFileOpenFlagsToPlatformFileFlags(mode, &flags) || |
| + !file) |
| + return PP_ERROR_BADARGUMENT; |
| + |
| + PepperFilePath pepper_path(domain_type, FilePath::FromUTF8Unsafe(path)); |
|
yzshen1
2012/11/21 00:38:59
[I don't know the answer. Just to make sure you ha
raymes
2012/11/21 22:44:53
If the plugin provides a non-UTF8 string, a best-e
|
| + |
| + IPC::Message unused; |
| + ResourceMessageReplyParams reply_params; |
| + int32_t error = GenericSyncCall(BROWSER, |
| + PpapiHostMsg_FlashFile_OpenFile(pepper_path, flags), &unused, |
| + &reply_params); |
| + if (error != PP_OK) |
| + return error; |
| + |
| + IPC::PlatformFileForTransit transit_file; |
| + if (!reply_params.TakeFileHandleAtIndex(0, &transit_file)) |
| + return PP_ERROR_FAILED; |
| + |
| + *file = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
| + return PP_OK; |
| +} |
| + |
| +int32_t FlashFileResource::QueryFileHelper(PP_Instance instance, |
|
yzshen1
2012/11/21 00:38:59
instance is not used.
raymes
2012/11/21 22:44:53
Done.
|
| + const std::string& path, |
| + PepperFilePath::Domain domain_type, |
| + PP_FileInfo* info) { |
| + if (path.empty() || !info) |
| + return PP_ERROR_BADARGUMENT; |
| + |
| + base::PlatformFileInfo file_info; |
| + PepperFilePath pepper_path(domain_type, FilePath::FromUTF8Unsafe(path)); |
| + |
| + int32_t error = SyncCall<PpapiPluginMsg_FlashFile_QueryFileReply>(BROWSER, |
| + PpapiHostMsg_FlashFile_QueryFile(pepper_path), &file_info); |
| + |
| + if (error == PP_OK) { |
| + info->size = file_info.size; |
| + info->creation_time = TimeToPPTime(file_info.creation_time); |
| + info->last_access_time = TimeToPPTime(file_info.last_accessed); |
| + info->last_modified_time = TimeToPPTime(file_info.last_modified); |
| + info->system_type = PP_FILESYSTEMTYPE_EXTERNAL; |
| + if (file_info.is_directory) |
| + info->type = PP_FILETYPE_DIRECTORY; |
| + else |
| + info->type = PP_FILETYPE_REGULAR; |
| + } |
| + |
| + return error; |
| +} |
| + |
| +} // namespace proxy |
| +} // namespace ppapi |