diff options
-rw-r--r-- | ppapi/cpp/private/flash_file.cc | 237 | ||||
-rw-r--r-- | ppapi/cpp/private/flash_file.h | 74 | ||||
-rw-r--r-- | ppapi/ppapi_sources.gypi | 2 | ||||
-rw-r--r-- | ppapi/tests/test_flash_file.cc | 36 | ||||
-rw-r--r-- | ppapi/tests/test_flash_file.h | 5 |
5 files changed, 330 insertions, 24 deletions
diff --git a/ppapi/cpp/private/flash_file.cc b/ppapi/cpp/private/flash_file.cc new file mode 100644 index 0000000..0f0ee07 --- /dev/null +++ b/ppapi/cpp/private/flash_file.cc @@ -0,0 +1,237 @@ +// 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/cpp/private/flash_file.h" + +#include "ppapi/c/pp_bool.h" +#include "ppapi/c/pp_errors.h" +#include "ppapi/cpp/file_ref.h" +#include "ppapi/cpp/instance_handle.h" +#include "ppapi/cpp/module_impl.h" + +namespace pp { + +// FileModuleLocal ------------------------------------------------------------- + +namespace { + +template <> const char* interface_name<PPB_Flash_File_ModuleLocal_3_0>() { + return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0; +} + +template <> const char* interface_name<PPB_Flash_File_ModuleLocal_2_0>() { + return PPB_FLASH_FILE_MODULELOCAL_INTERFACE_2_0; +} + +} // namespace + +namespace flash { + +// static +bool FileModuleLocal::IsAvailable() { + return has_interface<PPB_Flash_File_ModuleLocal_3_0>() || + has_interface<PPB_Flash_File_ModuleLocal_2_0>(); +} + +// static +bool FileModuleLocal::CreateThreadAdapterForInstance( + const InstanceHandle& instance) { + bool rv = false; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + rv = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + CreateThreadAdapterForInstance(instance.pp_instance()); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + rv = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + CreateThreadAdapterForInstance( instance.pp_instance()); + } + return rv; +} + +// static +void FileModuleLocal::ClearThreadAdapterForInstance( + const InstanceHandle& instance) { + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + ClearThreadAdapterForInstance(instance.pp_instance()); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + ClearThreadAdapterForInstance(instance.pp_instance()); + } +} + +// static +PP_FileHandle FileModuleLocal::OpenFile(const InstanceHandle& instance, + const std::string& path, + int32_t mode) { + PP_FileHandle file_handle = PP_kInvalidFileHandle; + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + OpenFile(instance.pp_instance(), path.c_str(), mode, &file_handle); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + OpenFile(instance.pp_instance(), path.c_str(), mode, &file_handle); + } + return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle; +} + +// static +bool FileModuleLocal::RenameFile(const InstanceHandle& instance, + const std::string& path_from, + const std::string& path_to) { + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + RenameFile(instance.pp_instance(), path_from.c_str(), path_to.c_str()); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + RenameFile(instance.pp_instance(), path_from.c_str(), path_to.c_str()); + } + return result == PP_OK; +} + +// static +bool FileModuleLocal::DeleteFileOrDir(const InstanceHandle& instance, + const std::string& path, + bool recursive) { + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + DeleteFileOrDir(instance.pp_instance(), path.c_str(), + PP_FromBool(recursive)); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + DeleteFileOrDir(instance.pp_instance(), path.c_str(), + PP_FromBool(recursive)); + } + return result == PP_OK; +} + +// static +bool FileModuleLocal::CreateDir(const InstanceHandle& instance, + const std::string& path) { + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + CreateDir(instance.pp_instance(), path.c_str()); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + CreateDir(instance.pp_instance(), path.c_str()); + } + return result == PP_OK; +} + +// static +bool FileModuleLocal::QueryFile(const InstanceHandle& instance, + const std::string& path, + PP_FileInfo* info) { + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + QueryFile(instance.pp_instance(), path.c_str(), info); + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + QueryFile(instance.pp_instance(), path.c_str(), info); + } + return result == PP_OK; +} + +// static +bool FileModuleLocal::GetDirContents( + const InstanceHandle& instance, + const std::string& path, + std::vector<PP_DirEntry_Dev>* dir_contents) { + dir_contents->clear(); + + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + PP_DirContents_Dev* contents = NULL; + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + GetDirContents(instance.pp_instance(), path.c_str(), &contents); + if (result == PP_OK && contents) { + for (int32_t i = 0; i < contents->count; i++) + dir_contents->push_back(contents->entries[i]); + } + if (contents) { + get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + FreeDirContents(instance.pp_instance(), contents); + } + } else if (has_interface<PPB_Flash_File_ModuleLocal_2_0>()) { + PP_DirContents_Dev* contents = NULL; + result = get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + GetDirContents(instance.pp_instance(), path.c_str(), &contents); + if (result == PP_OK && contents) { + for (int32_t i = 0; i < contents->count; i++) + dir_contents->push_back(contents->entries[i]); + } + if (contents) { + get_interface<PPB_Flash_File_ModuleLocal_2_0>()-> + FreeDirContents(instance.pp_instance(), contents); + } + } + return result == PP_OK; +} + +// static +bool FileModuleLocal::IsCreateTemporaryFileAvailable() { + return has_interface<PPB_Flash_File_ModuleLocal_3_0>(); +} + +// static +PP_FileHandle FileModuleLocal::CreateTemporaryFile( + const InstanceHandle& instance) { + PP_FileHandle file_handle = PP_kInvalidFileHandle; + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_ModuleLocal_3_0>()) { + result = get_interface<PPB_Flash_File_ModuleLocal_3_0>()-> + CreateTemporaryFile(instance.pp_instance(), &file_handle); + } + return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle; +} + +} // namespace flash + +// FileFileRef ----------------------------------------------------------------- + +namespace { + +template <> const char* interface_name<PPB_Flash_File_FileRef>() { + return PPB_FLASH_FILE_FILEREF_INTERFACE; +} + +} // namespace + +namespace flash { + +// static +bool FileFileRef::IsAvailable() { + return has_interface<PPB_Flash_File_FileRef>(); +} + +// static +PP_FileHandle FileFileRef::OpenFile(const pp::FileRef& resource, + int32_t mode) { + PP_FileHandle file_handle = PP_kInvalidFileHandle; + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_FileRef>()) { + result = get_interface<PPB_Flash_File_FileRef>()-> + OpenFile(resource.pp_resource(), mode, &file_handle); + } + return (result == PP_OK) ? file_handle : PP_kInvalidFileHandle; +} + +// static +bool FileFileRef::QueryFile(const pp::FileRef& resource, + PP_FileInfo* info) { + int32_t result = PP_ERROR_FAILED; + if (has_interface<PPB_Flash_File_FileRef>()) { + result = get_interface<PPB_Flash_File_FileRef>()-> + QueryFile(resource.pp_resource(), info); + } + return result == PP_OK; +} + +} // namespace flash + +} // namespace pp diff --git a/ppapi/cpp/private/flash_file.h b/ppapi/cpp/private/flash_file.h new file mode 100644 index 0000000..1c19c9d --- /dev/null +++ b/ppapi/cpp/private/flash_file.h @@ -0,0 +1,74 @@ +// 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. + +#ifndef PPAPI_CPP_PRIVATE_FLASH_FILE_H_ +#define PPAPI_CPP_PRIVATE_FLASH_FILE_H_ + +#include <string> +#include <vector> + +#include "ppapi/c/private/ppb_flash_file.h" + +namespace pp { + +class FileRef; +class InstanceHandle; + +namespace flash { + +// FileModuleLocal ------------------------------------------------------------- + +class FileModuleLocal { + public: + // Returns true if the required interface is available. + static bool IsAvailable(); + + static bool CreateThreadAdapterForInstance(const InstanceHandle& instance); + static void ClearThreadAdapterForInstance(const InstanceHandle& instance); + + // Returns |PP_kInvalidFileHandle| on error. + static PP_FileHandle OpenFile(const InstanceHandle& instance, + const std::string& path, + int32_t mode); + static bool RenameFile(const InstanceHandle& instance, + const std::string& path_from, + const std::string& path_to); + static bool DeleteFileOrDir(const InstanceHandle& instance, + const std::string& path, + bool recursive); + static bool CreateDir(const InstanceHandle& instance, + const std::string& path); + static bool QueryFile(const InstanceHandle& instance, + const std::string& path, + PP_FileInfo* info); + // Note that, unlike the C interface, no |FreeDirContents()| is needed. + static bool GetDirContents(const InstanceHandle& instance, + const std::string& path, + std::vector<PP_DirEntry_Dev>* dir_contents); + + // Returns true if |CreateTemporaryFile()| is supported. + // TODO(viettrungluu): Remove this sometime after M21 ships to Stable? + static bool IsCreateTemporaryFileAvailable(); + // Returns |PP_kInvalidFileHandle| on error. + static PP_FileHandle CreateTemporaryFile(const InstanceHandle& instance); +}; + +// FileFileRef ----------------------------------------------------------------- + +class FileFileRef { + public: + // Returns true if the required interface is available. + static bool IsAvailable(); + + // Returns |PP_kInvalidFileHandle| on error. + static PP_FileHandle OpenFile(const pp::FileRef& resource, + int32_t mode); + static bool QueryFile(const pp::FileRef& resource, + PP_FileInfo* info); +}; + +} // namespace flash +} // namespace pp + +#endif // PPAPI_CPP_PRIVATE_FLASH_FILE_H_ diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index 25af3e6..8cdbcba 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -257,6 +257,8 @@ 'cpp/private/flash_clipboard.h', 'cpp/private/flash_device_id.cc', 'cpp/private/flash_device_id.h', + 'cpp/private/flash_file.cc', + 'cpp/private/flash_file.h', 'cpp/private/flash_fullscreen.cc', 'cpp/private/flash_fullscreen.h', 'cpp/private/flash_menu.cc', diff --git a/ppapi/tests/test_flash_file.cc b/ppapi/tests/test_flash_file.cc index b7baeae..f0c4d42 100644 --- a/ppapi/tests/test_flash_file.cc +++ b/ppapi/tests/test_flash_file.cc @@ -5,6 +5,7 @@ #include "ppapi/tests/test_flash_file.h" #include "ppapi/cpp/module.h" +#include "ppapi/cpp/private/flash_file.h" #include "ppapi/tests/testing_instance.h" #include "ppapi/tests/test_utils.h" @@ -15,6 +16,8 @@ #include <unistd.h> #endif +using pp::flash::FileModuleLocal; + namespace { void CloseFileHandle(PP_FileHandle file_handle) { @@ -76,17 +79,14 @@ bool ReadFile(PP_FileHandle file_handle, std::string* contents) { REGISTER_TEST_CASE(FlashFile); TestFlashFile::TestFlashFile(TestingInstance* instance) - : TestCase(instance), module_local_interface_(NULL) { + : TestCase(instance) { } TestFlashFile::~TestFlashFile() { } bool TestFlashFile::Init() { - module_local_interface_ = static_cast<const PPB_Flash_File_ModuleLocal*>( - pp::Module::Get()->GetBrowserInterface( - PPB_FLASH_FILE_MODULELOCAL_INTERFACE)); - return !!module_local_interface_; + return FileModuleLocal::IsAvailable(); } void TestFlashFile::RunTests(const std::string& filter) { @@ -94,16 +94,16 @@ void TestFlashFile::RunTests(const std::string& filter) { } std::string TestFlashFile::TestCreateTemporaryFile() { + ASSERT_TRUE(FileModuleLocal::IsCreateTemporaryFileAvailable()); + // Make sure that the root directory exists. - module_local_interface_->CreateDir(instance_->pp_instance(), ""); + FileModuleLocal::CreateDir(instance_, std::string()); - int32_t before_create = 0; + size_t before_create = 0; ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&before_create)); - PP_FileHandle file_handle = PP_kInvalidFileHandle; - int32_t result = module_local_interface_->CreateTemporaryFile( - instance_->pp_instance(), &file_handle); - ASSERT_EQ(result, PP_OK); + PP_FileHandle file_handle = FileModuleLocal::CreateTemporaryFile(instance_); + ASSERT_NE(PP_kInvalidFileHandle, file_handle); std::string contents = "This is a temp file."; ASSERT_TRUE(WriteFile(file_handle, contents)); @@ -113,7 +113,7 @@ std::string TestFlashFile::TestCreateTemporaryFile() { CloseFileHandle(file_handle); - int32_t after_close = 0; + size_t after_close = 0; ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); ASSERT_EQ(before_create, after_close); @@ -121,13 +121,9 @@ std::string TestFlashFile::TestCreateTemporaryFile() { } std::string TestFlashFile::GetItemCountUnderModuleLocalRoot( - int32_t* item_count) { - PP_DirContents_Dev* contents = NULL; - int32_t result = module_local_interface_->GetDirContents( - instance_->pp_instance(), "", &contents); - ASSERT_EQ(result, PP_OK); - - *item_count = contents->count; - module_local_interface_->FreeDirContents(instance_->pp_instance(), contents); + size_t* item_count) { + std::vector<PP_DirEntry_Dev> contents; + ASSERT_TRUE(FileModuleLocal::GetDirContents(instance_, "", &contents)); + *item_count = contents.size(); PASS(); } diff --git a/ppapi/tests/test_flash_file.h b/ppapi/tests/test_flash_file.h index 36adfae..b58890d 100644 --- a/ppapi/tests/test_flash_file.h +++ b/ppapi/tests/test_flash_file.h @@ -7,7 +7,6 @@ #include <string> -#include "ppapi/c/private/ppb_flash_file.h" #include "ppapi/tests/test_case.h" class TestFlashFile: public TestCase { @@ -26,9 +25,7 @@ class TestFlashFile: public TestCase { // Gets the number of files and directories under the module-local root // directory. - std::string GetItemCountUnderModuleLocalRoot(int32_t* item_count); - - const PPB_Flash_File_ModuleLocal* module_local_interface_; + std::string GetItemCountUnderModuleLocalRoot(size_t* item_count); }; #endif // PPAPI_TESTS_TEST_FLASH_FILE_H_ |