diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-15 22:56:53 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-15 22:56:53 +0000 |
commit | 481a4b7f34565ef087c8aa44c8e04f9f0e466b2d (patch) | |
tree | 58a27508ef38cecf35d478710ee9039aa9a2cea0 /ppapi | |
parent | e5d5b0e8f45962bbb5cba1a6dd9a3b31eadb9429 (diff) | |
download | chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.zip chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.tar.gz chromium_src-481a4b7f34565ef087c8aa44c8e04f9f0e466b2d.tar.bz2 |
Add CreateTemporaryFile to PPB_Flash_File_ModuleLocal.
BUG=129807
TEST=None
Review URL: https://chromiumcodereview.appspot.com/10534045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142512 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/c/private/ppb_flash_file.h | 45 | ||||
-rw-r--r-- | ppapi/ppapi_sources.gypi | 2 | ||||
-rw-r--r-- | ppapi/proxy/pepper_file_messages.h | 4 | ||||
-rw-r--r-- | ppapi/proxy/ppb_flash_proxy.cc | 19 | ||||
-rw-r--r-- | ppapi/proxy/ppb_flash_proxy.h | 2 | ||||
-rw-r--r-- | ppapi/tests/test_flash_file.cc | 133 | ||||
-rw-r--r-- | ppapi/tests/test_flash_file.h | 34 | ||||
-rw-r--r-- | ppapi/thunk/interfaces_ppb_private_flash.h | 7 | ||||
-rw-r--r-- | ppapi/thunk/ppb_flash_api.h | 2 | ||||
-rw-r--r-- | ppapi/thunk/ppb_flash_file_modulelocal_thunk.cc | 40 |
10 files changed, 275 insertions, 13 deletions
diff --git a/ppapi/c/private/ppb_flash_file.h b/ppapi/c/private/ppb_flash_file.h index ac8c0fb..521ab39 100644 --- a/ppapi/c/private/ppb_flash_file.h +++ b/ppapi/c/private/ppb_flash_file.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// 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. @@ -34,13 +34,15 @@ struct PP_DirContents_Dev { }; // PPB_Flash_File_ModuleLocal -------------------------------------------------- - -#define PPB_FLASH_FILE_MODULELOCAL_INTERFACE "PPB_Flash_File_ModuleLocal;2" +#define PPB_FLASH_FILE_MODULELOCAL_INTERFACE_2_0 "PPB_Flash_File_ModuleLocal;2" +#define PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0 "PPB_Flash_File_ModuleLocal;3" +#define PPB_FLASH_FILE_MODULELOCAL_INTERFACE \ + PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0 // This interface provides (for Flash) synchronous access to module-local files. // Module-local file paths are '/'-separated UTF-8 strings, relative to a // module-specific root. -struct PPB_Flash_File_ModuleLocal { +struct PPB_Flash_File_ModuleLocal_3_0 { // Does initialization necessary for proxying this interface on background // threads. You must always call this function before using any other // function in this interface for a given instance ID. @@ -97,8 +99,43 @@ struct PPB_Flash_File_ModuleLocal { // Frees the data allocated by GetDirContents. void (*FreeDirContents)(PP_Instance instance, struct PP_DirContents_Dev* contents); + + // Creates a temporary file. The file will be automatically deleted when all + // handles to it are closed. + // Returns PP_OK if successful, one of the PP_ERROR_* values in case of + // failure. + // + // If successful, |file| is set to a file descriptor (posix) or a HANDLE + // (win32) to the file. If failed, |file| is not touched. + int32_t (*CreateTemporaryFile)(PP_Instance instance, PP_FileHandle* file); +}; + +struct PPB_Flash_File_ModuleLocal_2_0 { + bool (*CreateThreadAdapterForInstance)(PP_Instance instance); + void (*ClearThreadAdapterForInstance)(PP_Instance instance); + int32_t (*OpenFile)(PP_Instance instance, + const char* path, + int32_t mode, + PP_FileHandle* file); + int32_t (*RenameFile)(PP_Instance instance, + const char* path_from, + const char* path_to); + int32_t (*DeleteFileOrDir)(PP_Instance instance, + const char* path, + PP_Bool recursive); + int32_t (*CreateDir)(PP_Instance instance, const char* path); + int32_t (*QueryFile)(PP_Instance instance, + const char* path, + struct PP_FileInfo* info); + int32_t (*GetDirContents)(PP_Instance instance, + const char* path, + struct PP_DirContents_Dev** contents); + void (*FreeDirContents)(PP_Instance instance, + struct PP_DirContents_Dev* contents); }; +typedef struct PPB_Flash_File_ModuleLocal_3_0 PPB_Flash_File_ModuleLocal; + // PPB_Flash_File_FileRef ------------------------------------------------------ #define PPB_FLASH_FILE_FILEREF_INTERFACE "PPB_Flash_File_FileRef;2" diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index 9e494a9..155da5a 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -414,6 +414,8 @@ 'tests/test_flash.h', 'tests/test_flash_clipboard.cc', 'tests/test_flash_clipboard.h', + 'tests/test_flash_file.cc', + 'tests/test_flash_file.h', 'tests/test_flash_fullscreen.cc', 'tests/test_flash_fullscreen.h', 'tests/test_flash_message_loop.cc', diff --git a/ppapi/proxy/pepper_file_messages.h b/ppapi/proxy/pepper_file_messages.h index c36e7b6..3edd490 100644 --- a/ppapi/proxy/pepper_file_messages.h +++ b/ppapi/proxy/pepper_file_messages.h @@ -76,3 +76,7 @@ IPC_SYNC_MESSAGE_CONTROL1_2(PepperFileMsg_GetDirContents, ppapi::DirContents, /* contents */ base::PlatformFileError /* error_code */) +// Create a temporary file. +IPC_SYNC_MESSAGE_CONTROL0_2(PepperFileMsg_CreateTemporaryFile, + base::PlatformFileError /* error_code */, + IPC::PlatformFileForTransit /* file */) diff --git a/ppapi/proxy/ppb_flash_proxy.cc b/ppapi/proxy/ppb_flash_proxy.cc index a6b7e62..28444e9 100644 --- a/ppapi/proxy/ppb_flash_proxy.cc +++ b/ppapi/proxy/ppb_flash_proxy.cc @@ -498,6 +498,25 @@ int32_t PPB_Flash_Proxy::GetDirContents(PP_Instance, return ppapi::PlatformFileErrorToPepperError(error); } +int32_t PPB_Flash_Proxy::CreateTemporaryFile(PP_Instance instance, + PP_FileHandle* file) { + if (!file) + return PP_ERROR_BADARGUMENT; + + base::PlatformFileError error; + IPC::PlatformFileForTransit transit_file; + + if (PluginGlobals::Get()->plugin_proxy_delegate()->SendToBrowser( + new PepperFileMsg_CreateTemporaryFile(&error, &transit_file))) { + *file = IPC::PlatformFileForTransitToPlatformFile(transit_file); + } else { + error = base::PLATFORM_FILE_ERROR_FAILED; + *file = base::kInvalidPlatformFileValue; + } + + return ppapi::PlatformFileErrorToPepperError(error); +} + int32_t PPB_Flash_Proxy::OpenFileRef(PP_Instance instance, PP_Resource file_ref_id, int32_t mode, diff --git a/ppapi/proxy/ppb_flash_proxy.h b/ppapi/proxy/ppb_flash_proxy.h index be48c14..c441455 100644 --- a/ppapi/proxy/ppb_flash_proxy.h +++ b/ppapi/proxy/ppb_flash_proxy.h @@ -107,6 +107,8 @@ class PPB_Flash_Proxy : public InterfaceProxy, public PPB_Flash_Shared { virtual int32_t GetDirContents(PP_Instance instance, const char* path, PP_DirContents_Dev** contents) OVERRIDE; + virtual int32_t CreateTemporaryFile(PP_Instance instance, + PP_FileHandle* file) OVERRIDE; virtual int32_t OpenFileRef(PP_Instance instance, PP_Resource file_ref, int32_t mode, diff --git a/ppapi/tests/test_flash_file.cc b/ppapi/tests/test_flash_file.cc new file mode 100644 index 0000000..b7baeae --- /dev/null +++ b/ppapi/tests/test_flash_file.cc @@ -0,0 +1,133 @@ +// 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/tests/test_flash_file.h" + +#include "ppapi/cpp/module.h" +#include "ppapi/tests/testing_instance.h" +#include "ppapi/tests/test_utils.h" + +#if defined(PPAPI_OS_WIN) +#include <windows.h> +#else +#include <errno.h> +#include <unistd.h> +#endif + +namespace { + +void CloseFileHandle(PP_FileHandle file_handle) { +#if defined(PPAPI_OS_WIN) + CloseHandle(file_handle); +#else + close(file_handle); +#endif +} + +bool WriteFile(PP_FileHandle file_handle, const std::string& contents) { +#if defined(PPAPI_OS_WIN) + DWORD bytes_written = 0; + BOOL result = ::WriteFile(file_handle, contents.c_str(), contents.size(), + &bytes_written, NULL); + return result && bytes_written == static_cast<DWORD>(contents.size()); +#else + ssize_t bytes_written = 0; + do { + bytes_written = write(file_handle, contents.c_str(), contents.size()); + } while (bytes_written == -1 && errno == EINTR); + return bytes_written == static_cast<ssize_t>(contents.size()); +#endif +} + +bool ReadFile(PP_FileHandle file_handle, std::string* contents) { + static const size_t kBufferSize = 1024; + char* buffer = new char[kBufferSize]; + bool result = false; + contents->clear(); + +#if defined(PPAPI_OS_WIN) + SetFilePointer(file_handle, 0, NULL, FILE_BEGIN); + DWORD bytes_read = 0; + do { + result = !!::ReadFile(file_handle, buffer, kBufferSize, &bytes_read, NULL); + if (result && bytes_read > 0) + contents->append(buffer, bytes_read); + } while (result && bytes_read > 0); +#else + lseek(file_handle, 0, SEEK_SET); + ssize_t bytes_read = 0; + do { + do { + bytes_read = read(file_handle, buffer, kBufferSize); + } while (bytes_read == -1 && errno == EINTR); + result = bytes_read != -1; + if (bytes_read > 0) + contents->append(buffer, bytes_read); + } while (bytes_read > 0); +#endif + + delete[] buffer; + return result; +} + +} // namespace + +REGISTER_TEST_CASE(FlashFile); + +TestFlashFile::TestFlashFile(TestingInstance* instance) + : TestCase(instance), module_local_interface_(NULL) { +} + +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_; +} + +void TestFlashFile::RunTests(const std::string& filter) { + RUN_TEST(CreateTemporaryFile, filter); +} + +std::string TestFlashFile::TestCreateTemporaryFile() { + // Make sure that the root directory exists. + module_local_interface_->CreateDir(instance_->pp_instance(), ""); + + int32_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); + + std::string contents = "This is a temp file."; + ASSERT_TRUE(WriteFile(file_handle, contents)); + std::string read_contents; + ASSERT_TRUE(ReadFile(file_handle, &read_contents)); + ASSERT_EQ(contents, read_contents); + + CloseFileHandle(file_handle); + + int32_t after_close = 0; + ASSERT_SUBTEST_SUCCESS(GetItemCountUnderModuleLocalRoot(&after_close)); + ASSERT_EQ(before_create, after_close); + + PASS(); +} + +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); + PASS(); +} diff --git a/ppapi/tests/test_flash_file.h b/ppapi/tests/test_flash_file.h new file mode 100644 index 0000000..36adfae --- /dev/null +++ b/ppapi/tests/test_flash_file.h @@ -0,0 +1,34 @@ +// 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 PAPPI_TESTS_TEST_FLASH_FILE_H_ +#define PPAPI_TESTS_TEST_FLASH_FILE_H_ + +#include <string> + +#include "ppapi/c/private/ppb_flash_file.h" +#include "ppapi/tests/test_case.h" + +class TestFlashFile: public TestCase { + public: + explicit TestFlashFile(TestingInstance* instance); + virtual ~TestFlashFile(); + + // TestCase implementation. + virtual bool Init(); + virtual void RunTests(const std::string& filter); + + private: + // TODO(yzshen): Add more test cases for PPB_Flash_File_ModuleLocal and + // PPB_Flash_File_FileRef. + std::string TestCreateTemporaryFile(); + + // 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_; +}; + +#endif // PPAPI_TESTS_TEST_FLASH_FILE_H_ diff --git a/ppapi/thunk/interfaces_ppb_private_flash.h b/ppapi/thunk/interfaces_ppb_private_flash.h index c0bd0df..fb5817b 100644 --- a/ppapi/thunk/interfaces_ppb_private_flash.h +++ b/ppapi/thunk/interfaces_ppb_private_flash.h @@ -34,8 +34,11 @@ PROXIED_IFACE(PPB_Flash, PPB_FLASH_CLIPBOARD_INTERFACE_4_0, PPB_Flash_Clipboard_4_0) PROXIED_IFACE(PPB_Flash, - PPB_FLASH_FILE_MODULELOCAL_INTERFACE, - PPB_Flash_File_ModuleLocal) + PPB_FLASH_FILE_MODULELOCAL_INTERFACE_2_0, + PPB_Flash_File_ModuleLocal_2_0) +PROXIED_IFACE(PPB_Flash, + PPB_FLASH_FILE_MODULELOCAL_INTERFACE_3_0, + PPB_Flash_File_ModuleLocal_3_0) PROXIED_IFACE(PPB_Flash, PPB_FLASH_FILE_FILEREF_INTERFACE, PPB_Flash_File_FileRef) diff --git a/ppapi/thunk/ppb_flash_api.h b/ppapi/thunk/ppb_flash_api.h index 0fdcc68..823740e 100644 --- a/ppapi/thunk/ppb_flash_api.h +++ b/ppapi/thunk/ppb_flash_api.h @@ -82,6 +82,8 @@ class PPAPI_THUNK_EXPORT PPB_Flash_API { PP_DirContents_Dev** contents) = 0; virtual void FreeDirContents(PP_Instance instance, PP_DirContents_Dev* contents) = 0; + virtual int32_t CreateTemporaryFile(PP_Instance instance, + PP_FileHandle* file) = 0; // FlashFile_FileRef. virtual int32_t OpenFileRef(PP_Instance instance, diff --git a/ppapi/thunk/ppb_flash_file_modulelocal_thunk.cc b/ppapi/thunk/ppb_flash_file_modulelocal_thunk.cc index 6338779..7e9678a 100644 --- a/ppapi/thunk/ppb_flash_file_modulelocal_thunk.cc +++ b/ppapi/thunk/ppb_flash_file_modulelocal_thunk.cc @@ -87,13 +87,20 @@ int32_t GetDirContents(PP_Instance instance, void FreeDirContents(PP_Instance instance, PP_DirContents_Dev* contents) { EnterInstance enter(instance); - if (enter.succeeded()) { - return enter.functions()->GetFlashAPI()->FreeDirContents(instance, - contents); - } + if (enter.succeeded()) + enter.functions()->GetFlashAPI()->FreeDirContents(instance, contents); +} + +int32_t CreateTemporaryFile(PP_Instance instance, PP_FileHandle* file) { + EnterInstance enter(instance); + if (enter.failed()) + return PP_ERROR_BADARGUMENT; + + *file = PP_kInvalidFileHandle; + return enter.functions()->GetFlashAPI()->CreateTemporaryFile(instance, file); } -const PPB_Flash_File_ModuleLocal g_ppb_flash_file_modulelocal_thunk = { +const PPB_Flash_File_ModuleLocal_2_0 g_ppb_flash_file_modulelocal_thunk_2_0 = { &CreateThreadAdapterForInstance, &ClearThreadAdapterForInstance, &OpenFile, @@ -105,10 +112,29 @@ const PPB_Flash_File_ModuleLocal g_ppb_flash_file_modulelocal_thunk = { &FreeDirContents }; +const PPB_Flash_File_ModuleLocal_3_0 g_ppb_flash_file_modulelocal_thunk_3_0 = { + &CreateThreadAdapterForInstance, + &ClearThreadAdapterForInstance, + &OpenFile, + &RenameFile, + &DeleteFileOrDir, + &CreateDir, + &QueryFile, + &GetDirContents, + &FreeDirContents, + &CreateTemporaryFile +}; + } // namespace -const PPB_Flash_File_ModuleLocal* GetPPB_Flash_File_ModuleLocal_Thunk() { - return &g_ppb_flash_file_modulelocal_thunk; +const PPB_Flash_File_ModuleLocal_2_0* + GetPPB_Flash_File_ModuleLocal_2_0_Thunk() { + return &g_ppb_flash_file_modulelocal_thunk_2_0; +} + +const PPB_Flash_File_ModuleLocal_3_0* + GetPPB_Flash_File_ModuleLocal_3_0_Thunk() { + return &g_ppb_flash_file_modulelocal_thunk_3_0; } } // namespace thunk |