diff options
author | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-15 00:51:20 +0000 |
---|---|---|
committer | binji@chromium.org <binji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-15 00:51:20 +0000 |
commit | 30965c8349f8478bed8cb7d67e5cea4efd19da8c (patch) | |
tree | 026c9476946f2866802fda33a4deb934dd08661a /native_client_sdk | |
parent | 802ecdb9cee0d66fe546bdf24e98150f8f716ad8 (diff) | |
download | chromium_src-30965c8349f8478bed8cb7d67e5cea4efd19da8c.zip chromium_src-30965c8349f8478bed8cb7d67e5cea4efd19da8c.tar.gz chromium_src-30965c8349f8478bed8cb7d67e5cea4efd19da8c.tar.bz2 |
[NaCl SDK] PPAPI interface to nacl_mounts.
BUG=none
TBR=noelallen@chromium.org
NOTRY=true
Review URL: https://codereview.chromium.org/11583012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173250 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'native_client_sdk')
21 files changed, 593 insertions, 30 deletions
diff --git a/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.cc b/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.cc index 940f5b7..336ff4c 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.cc +++ b/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.cc @@ -4,6 +4,8 @@ */ #include "nacl_mounts/kernel_intercept.h" #include "nacl_mounts/kernel_proxy.h" +#include "nacl_mounts/pepper_interface.h" +#include "nacl_mounts/real_pepper_interface.h" #include "utils/macros.h" FORCE_LINK_THAT(kernel_wrap) @@ -11,9 +13,20 @@ FORCE_LINK_THAT(kernel_wrap) static KernelProxy* s_kp; void ki_init(void* kp) { + ki_init_ppapi(kp, 0, NULL); +} + +void ki_init_ppapi(void* kp, + PP_Instance instance, + PPB_GetInterface get_browser_interface) { if (kp == NULL) kp = new KernelProxy(); s_kp = static_cast<KernelProxy*>(kp); - s_kp->Init(); + + PepperInterface* ppapi = NULL; + if (instance && get_browser_interface) + ppapi = new RealPepperInterface(instance, get_browser_interface); + + s_kp->Init(ppapi); } int ki_is_initialized() { diff --git a/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.h b/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.h index a966c4d..330df66 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.h +++ b/native_client_sdk/src/libraries/nacl_mounts/kernel_intercept.h @@ -5,6 +5,8 @@ #ifndef LIBRARIES_NACL_MOUNTS_KERNEL_INTERCEPT_H_ #define LIBRARIES_NACL_MOUNTS_KERNEL_INTERCEPT_H_ +#include <ppapi/c/ppb.h> +#include <ppapi/c/pp_instance.h> #include "nacl_mounts/ostypes.h" #include "utils/macros.h" @@ -17,6 +19,9 @@ EXTERN_C_BEGIN // with NULL will instantiate a default kernel proxy object. ki_init must // be called before any other ki_XXX function can be used. void ki_init(void* kernel_proxy); +void ki_init_ppapi(void* kernel_proxy, + PP_Instance instance, + PPB_GetInterface get_browser_interface); int ki_is_initialized(); int ki_chdir(const char* path); diff --git a/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.cc b/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.cc index 9c94f94..e8e2440 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.cc +++ b/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.cc @@ -17,6 +17,7 @@ #include "nacl_mounts/mount_node.h" #include "nacl_mounts/osstat.h" #include "nacl_mounts/path.h" +#include "nacl_mounts/pepper_interface.h" #include "utils/auto_lock.h" #include "utils/ref_object.h" @@ -29,10 +30,17 @@ #define GRP_ID 1003 -KernelProxy::KernelProxy() : dev_(0) {} -KernelProxy::~KernelProxy() {} +KernelProxy::KernelProxy() + : dev_(0), + ppapi_(NULL) { +} + +KernelProxy::~KernelProxy() { + delete ppapi_; +} -void KernelProxy::Init() { +void KernelProxy::Init(PepperInterface* ppapi) { + ppapi_ = ppapi; cwd_ = "/"; dev_ = 1; @@ -41,10 +49,9 @@ void KernelProxy::Init() { // Create memory mount at root StringMap_t smap; - mounts_["/"] = MountMem::Create<MountMem>(dev_++, smap); + mounts_["/"] = MountMem::Create<MountMem>(dev_++, smap, ppapi_); } - int KernelProxy::open(const char *path, int oflags) { Path rel; @@ -225,7 +232,7 @@ int KernelProxy::mount(const char *source, const char *target, free(str); } - Mount* mnt = factory->second(dev_++, smap); + Mount* mnt = factory->second(dev_++, smap, ppapi_); if (mnt) { mounts_[abs_targ] = mnt; return 0; diff --git a/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.h b/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.h index b77df83..d21cce3 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.h +++ b/native_client_sdk/src/libraries/nacl_mounts/kernel_proxy.h @@ -5,31 +5,36 @@ #ifndef LIBRARIES_NACL_MOUNTS_KERNEL_PROXY_H_ #define LIBRARIES_NACL_MOUNTS_KERNEL_PROXY_H_ +#include <ppapi/c/pp_instance.h> +#include <ppapi/c/ppb.h> #include <pthread.h> #include <map> #include <string> #include <vector> -#include "nacl_mounts/path.h" #include "nacl_mounts/kernel_object.h" #include "nacl_mounts/mount.h" #include "nacl_mounts/ostypes.h" +#include "nacl_mounts/path.h" class KernelHandle; class Mount; class MountNode; +class PepperInterface; // KernelProxy provide one-to-one mapping for libc kernel calls. Calls to the // proxy will result in IO access to the provided Mount and MountNode objects. class KernelProxy : protected KernelObject { public: - typedef Mount* (*MountFactory_t)(int, StringMap_t&); + typedef Mount* (*MountFactory_t)(int, StringMap_t&, PepperInterface*); typedef std::map<std::string, std::string> StringMap_t; typedef std::map<std::string, MountFactory_t> MountFactoryMap_t; KernelProxy(); virtual ~KernelProxy(); - virtual void Init(); + // Takes ownership of |ppapi|. + // |ppapi| may be NULL. If so, no mount that uses pepper calls can be mounted. + virtual void Init(PepperInterface* ppapi); // KernelHandle and FD allocation and manipulation functions. virtual int open(const char *path, int oflag); @@ -83,6 +88,7 @@ class KernelProxy : protected KernelObject { protected: MountFactoryMap_t factories_; int dev_; + PepperInterface* ppapi_; static KernelProxy *s_instance_; diff --git a/native_client_sdk/src/libraries/nacl_mounts/library.dsc b/native_client_sdk/src/libraries/nacl_mounts/library.dsc index 8b1d1e4..0c6f094 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/library.dsc +++ b/native_client_sdk/src/libraries/nacl_mounts/library.dsc @@ -24,6 +24,8 @@ "mount_node_dir.cc", "mount_node_mem.cc", "path.cc", + "pepper_interface.cc", + "real_pepper_interface.cc", ], } ], @@ -44,7 +46,9 @@ "osdirent.h", "osstat.h", "ostypes.h", - "path.h" + "path.h", + "pepper_interface.h", + "real_pepper_interface.h", ], 'DEST': 'include/nacl_mounts', }, diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount.cc b/native_client_sdk/src/libraries/nacl_mounts/mount.cc index 2841e3a..e92f748 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount.cc +++ b/native_client_sdk/src/libraries/nacl_mounts/mount.cc @@ -22,8 +22,9 @@ Mount::Mount() Mount::~Mount() {} -bool Mount::Init(int dev, StringMap_t& args) { +bool Mount::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { dev_ = dev; + ppapi_ = ppapi; return true; } diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount.h b/native_client_sdk/src/libraries/nacl_mounts/mount.h index 0e3296a..700097a 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount.h +++ b/native_client_sdk/src/libraries/nacl_mounts/mount.h @@ -14,6 +14,7 @@ #include "utils/ref_object.h" class MountNode; +class PepperInterface; typedef std::map<std::string, std::string> StringMap_t; @@ -27,7 +28,8 @@ class Mount : public RefObject { // Init must be called by the factory before the mount is used. // This function must assign a root node, or replace FindNode. - virtual bool Init(int dev, StringMap_t& args); + // |ppapi| can be NULL. If so, this mount cannot make any pepper calls. + virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi); // Destroy is called when the reference count reaches zero, // just before the destructor is called. @@ -35,7 +37,9 @@ class Mount : public RefObject { public: template <class M> - static Mount* Create(int dev, StringMap_t& args); + static Mount* Create(int dev, StringMap_t& args, PepperInterface* ppapi); + + PepperInterface* ppapi() { return ppapi_; } // All paths are expected to containing a leading "/" virtual void AcquireNode(MountNode* node); @@ -59,6 +63,7 @@ class Mount : public RefObject { protected: // Device number for the mount. int dev_; + PepperInterface* ppapi_; // Weak reference. private: // May only be called by the KernelProxy when the Kernel's @@ -74,14 +79,13 @@ class Mount : public RefObject { template <class M> /*static*/ -Mount* Mount::Create(int dev, StringMap_t& args) { +Mount* Mount::Create(int dev, StringMap_t& args, PepperInterface* ppapi) { Mount* mnt = new M(); - if (mnt->Init(dev, args) == false) { + if (mnt->Init(dev, args, ppapi) == false) { delete mnt; return NULL; } return mnt; } - #endif // LIBRARIES_NACL_MOUNTS_MOUNT_H_ diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount_dev.cc b/native_client_sdk/src/libraries/nacl_mounts/mount_dev.cc index 97100f0..5d43989 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount_dev.cc +++ b/native_client_sdk/src/libraries/nacl_mounts/mount_dev.cc @@ -180,8 +180,10 @@ MountDev::MountDev() random_node_(NULL) { } -bool MountDev::Init(int dev, StringMap_t& args) { - dev_ = dev; +bool MountDev::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { + if (!Mount::Init(dev, args, ppapi)) + return false; + root_ = new MountNodeDir(this, 1, dev_); null_node_ = new NullNode(this, 2, dev_); root_->AddChild("/null", null_node_); diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount_dev.h b/native_client_sdk/src/libraries/nacl_mounts/mount_dev.h index 9e92199..8cac9ef 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount_dev.h +++ b/native_client_sdk/src/libraries/nacl_mounts/mount_dev.h @@ -24,7 +24,7 @@ class MountDev : public Mount { protected: MountDev(); - virtual bool Init(int dev, StringMap_t& args); + virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi); virtual void Destroy(); private: diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc b/native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc index 78d89d9..e0a35ca 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc +++ b/native_client_sdk/src/libraries/nacl_mounts/mount_mem.cc @@ -26,8 +26,8 @@ MountMem::MountMem() max_ino_(0) { } -bool MountMem::Init(int dev, StringMap_t& args) { - dev_ = dev; +bool MountMem::Init(int dev, StringMap_t& args, PepperInterface* ppapi) { + Mount::Init(dev, args, ppapi); root_ = AllocatePath(S_IREAD | S_IWRITE); return (bool) (root_ != NULL); } diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount_mem.h b/native_client_sdk/src/libraries/nacl_mounts/mount_mem.h index 7f2240f..742ac17 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount_mem.h +++ b/native_client_sdk/src/libraries/nacl_mounts/mount_mem.h @@ -14,7 +14,7 @@ class MountMem : public Mount { protected: MountMem(); - virtual bool Init(int dev, StringMap_t& args); + virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi); virtual void Destroy(); // The protected functions are only used internally and will not diff --git a/native_client_sdk/src/libraries/nacl_mounts/mount_node.h b/native_client_sdk/src/libraries/nacl_mounts/mount_node.h index 46bf0e2..028f75a 100644 --- a/native_client_sdk/src/libraries/nacl_mounts/mount_node.h +++ b/native_client_sdk/src/libraries/nacl_mounts/mount_node.h @@ -13,7 +13,6 @@ struct dirent; struct stat; class Mount; -class MountNode; class MountNode : public RefObject { protected: diff --git a/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.cc b/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.cc new file mode 100644 index 0000000..22ab4f5 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.cc @@ -0,0 +1,21 @@ +/* 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 "nacl_mounts/pepper_interface.h" + +ScopedResource::ScopedResource(PepperInterface* ppapi, PP_Resource resource) + : ppapi_(ppapi), + resource_(resource) { + ppapi_->AddRefResource(resource_); +} + +ScopedResource::ScopedResource(PepperInterface* ppapi, PP_Resource resource, + NoAddRef) + : ppapi_(ppapi), + resource_(resource) { +} + +ScopedResource::~ScopedResource() { + ppapi_->ReleaseResource(resource_); +} diff --git a/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.h b/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.h new file mode 100644 index 0000000..484ff8d --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts/pepper_interface.h @@ -0,0 +1,99 @@ +/* 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 LIBRARIES_NACL_MOUNTS_PEPPER_INTERFACE_H_ +#define LIBRARIES_NACL_MOUNTS_PEPPER_INTERFACE_H_ + +#include <ppapi/c/dev/ppb_directory_reader_dev.h> +#include <ppapi/c/pp_completion_callback.h> +#include <ppapi/c/pp_instance.h> +#include <ppapi/c/pp_resource.h> +#include <ppapi/c/pp_var.h> +#include <utils/macros.h> + +class DirectoryReaderInterface; +class FileIoInterface; +class FileRefInterface; +class FileSystemInterface; +class VarInterface; + +class PepperInterface { + public: + virtual ~PepperInterface() {} + virtual PP_Instance GetInstance() = 0; + virtual void AddRefResource(PP_Resource) = 0; + virtual void ReleaseResource(PP_Resource) = 0; + virtual FileSystemInterface* GetFileSystemInterface() = 0; + virtual FileRefInterface* GetFileRefInterface() = 0; + virtual FileIoInterface* GetFileIoInterface() = 0; + virtual DirectoryReaderInterface* GetDirectoryReaderInterface() = 0; + virtual VarInterface* GetVarInterface() = 0; +}; + +class FileSystemInterface { + public: + virtual ~FileSystemInterface() {} + virtual PP_Resource Create(PP_Instance, PP_FileSystemType) = 0; + virtual int32_t Open(PP_Resource, int64_t, PP_CompletionCallback) = 0; +}; + +class FileRefInterface { + public: + virtual ~FileRefInterface() {} + virtual PP_Resource Create(PP_Resource, const char*) = 0; + virtual int32_t Delete(PP_Resource, PP_CompletionCallback) = 0; + virtual PP_Var GetName(PP_Resource) = 0; + virtual int32_t MakeDirectory(PP_Resource, PP_Bool, + PP_CompletionCallback) = 0; +}; + +class FileIoInterface { + public: + virtual ~FileIoInterface() {} + virtual void Close(PP_Resource) = 0; + virtual PP_Resource Create(PP_Instance) = 0; + virtual int32_t Flush(PP_Resource, PP_CompletionCallback) = 0; + virtual int32_t Open(PP_Resource, PP_Resource, int32_t, + PP_CompletionCallback) = 0; + virtual int32_t Query(PP_Resource, PP_FileInfo*, + PP_CompletionCallback) = 0; + virtual int32_t Read(PP_Resource, int64_t, char*, int32_t, + PP_CompletionCallback) = 0; + virtual int32_t SetLength(PP_Resource, int64_t, + PP_CompletionCallback) = 0; + virtual int32_t Write(PP_Resource, int64_t, const char*, int32_t, + PP_CompletionCallback) = 0; +}; + +class DirectoryReaderInterface { + public: + virtual ~DirectoryReaderInterface() {} + virtual PP_Resource Create(PP_Resource) = 0; + virtual int32_t GetNextEntry(PP_Resource, PP_DirectoryEntry_Dev*, + PP_CompletionCallback) = 0; +}; + +class VarInterface { + public: + virtual ~VarInterface() {} + virtual const char* VarToUtf8(PP_Var, uint32_t*) = 0; +}; + + +class ScopedResource { + public: + struct NoAddRef {}; + + ScopedResource(PepperInterface* ppapi, PP_Resource resource); + ScopedResource(PepperInterface* ppapi, PP_Resource resource, NoAddRef); + ~ScopedResource(); + + private: + PepperInterface* ppapi_; + PP_Resource resource_; + + DISALLOW_COPY_AND_ASSIGN(ScopedResource); +}; + +#endif // LIBRARIES_NACL_MOUNTS_PEPPER_INTERFACE_H_ diff --git a/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.cc b/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.cc new file mode 100644 index 0000000..bffbb49 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.cc @@ -0,0 +1,230 @@ +/* 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 "nacl_mounts/real_pepper_interface.h" +#include <assert.h> +#include <ppapi/c/pp_errors.h> +#include <ppapi/c/ppb_file_io.h> +#include <ppapi/c/ppb_file_ref.h> +#include <ppapi/c/ppb_file_system.h> +#include <ppapi/c/ppb_var.h> +#include <stdio.h> + +#define DEFINE_CONSTRUCTOR(Class, Interface) \ + Class::Class(const Interface* interface) : interface_(interface) {} + +#define DEFINE_METHOD1(Class, ReturnType, MethodName, Type0) \ + ReturnType Class::MethodName(Type0 arg0) { \ + return interface_->MethodName(arg0); \ + } + +#define DEFINE_METHOD2(Class, ReturnType, MethodName, Type0, Type1) \ + ReturnType Class::MethodName(Type0 arg0, Type1 arg1) { \ + return interface_->MethodName(arg0, arg1); \ + } + +#define DEFINE_METHOD3(Class, ReturnType, MethodName, Type0, Type1, Type2) \ + ReturnType Class::MethodName(Type0 arg0, Type1 arg1, Type2 arg2) { \ + return interface_->MethodName(arg0, arg1, arg2); \ + } + +#define DEFINE_METHOD4(Class, ReturnType, MethodName, Type0, Type1, Type2, \ + Type3) \ + ReturnType Class::MethodName(Type0 arg0, Type1 arg1, Type2 arg2, \ + Type3 arg3) { \ + return interface_->MethodName(arg0, arg1, arg2, arg3); \ + } + +#define DEFINE_METHOD5(Class, ReturnType, MethodName, Type0, Type1, Type2, \ + Type3, Type4) \ + ReturnType Class::MethodName(Type0 arg0, Type1 arg1, Type2 arg2, \ + Type3 arg3, Type4 arg4) { \ + return interface_->MethodName(arg0, arg1, arg2, arg3, arg4); \ + } + + +class RealFileSystemInterface : public FileSystemInterface { + public: + explicit RealFileSystemInterface(const PPB_FileSystem* filesystem_interface); + virtual PP_Resource Create(PP_Instance, PP_FileSystemType); + virtual int32_t Open(PP_Resource, int64_t, PP_CompletionCallback); + + private: + const PPB_FileSystem* interface_; +}; +DEFINE_CONSTRUCTOR(RealFileSystemInterface, PPB_FileSystem) +DEFINE_METHOD2(RealFileSystemInterface, PP_Resource, Create, PP_Instance, + PP_FileSystemType) +DEFINE_METHOD3(RealFileSystemInterface, int32_t, Open, PP_Resource, int64_t, + PP_CompletionCallback) + + +class RealFileRefInterface : public FileRefInterface { + public: + explicit RealFileRefInterface(const PPB_FileRef* fileref_interface); + + virtual PP_Resource Create(PP_Resource, const char*); + virtual int32_t Delete(PP_Resource, PP_CompletionCallback); + virtual PP_Var GetName(PP_Resource); + virtual int32_t MakeDirectory(PP_Resource, PP_Bool, + PP_CompletionCallback); + + private: + const PPB_FileRef* interface_; +}; +DEFINE_CONSTRUCTOR(RealFileRefInterface, PPB_FileRef) +DEFINE_METHOD2(RealFileRefInterface, PP_Resource, Create, PP_Resource, + const char*) +DEFINE_METHOD2(RealFileRefInterface, int32_t, Delete, PP_Resource, + PP_CompletionCallback) +DEFINE_METHOD1(RealFileRefInterface, PP_Var, GetName, PP_Resource) +DEFINE_METHOD3(RealFileRefInterface, int32_t, MakeDirectory, PP_Resource, + PP_Bool, PP_CompletionCallback); + + +class RealFileIoInterface : public FileIoInterface { + public: + explicit RealFileIoInterface(const PPB_FileIO* fileio_interface); + + virtual void Close(PP_Resource); + virtual PP_Resource Create(PP_Instance); + virtual int32_t Flush(PP_Resource, PP_CompletionCallback); + virtual int32_t Open(PP_Resource, PP_Resource, int32_t, + PP_CompletionCallback); + virtual int32_t Query(PP_Resource, PP_FileInfo*, PP_CompletionCallback); + virtual int32_t Read(PP_Resource, int64_t, char*, int32_t, + PP_CompletionCallback); + virtual int32_t SetLength(PP_Resource, int64_t, PP_CompletionCallback); + virtual int32_t Write(PP_Resource, int64_t, const char*, int32_t, + PP_CompletionCallback); + + private: + const PPB_FileIO* interface_; +}; +DEFINE_CONSTRUCTOR(RealFileIoInterface, PPB_FileIO) +DEFINE_METHOD1(RealFileIoInterface, void, Close, PP_Resource) +DEFINE_METHOD1(RealFileIoInterface, PP_Resource, Create, PP_Resource) +DEFINE_METHOD2(RealFileIoInterface, int32_t, Flush, PP_Resource, + PP_CompletionCallback) +DEFINE_METHOD4(RealFileIoInterface, int32_t, Open, PP_Resource, PP_Resource, + int32_t, PP_CompletionCallback); +DEFINE_METHOD3(RealFileIoInterface, int32_t, Query, PP_Resource, PP_FileInfo*, + PP_CompletionCallback); +DEFINE_METHOD5(RealFileIoInterface, int32_t, Read, PP_Resource, int64_t, char*, + int32_t, PP_CompletionCallback); +DEFINE_METHOD3(RealFileIoInterface, int32_t, SetLength, PP_Resource, int64_t, + PP_CompletionCallback); +DEFINE_METHOD5(RealFileIoInterface, int32_t, Write, PP_Resource, int64_t, + const char*, int32_t, PP_CompletionCallback); + + +class RealDirectoryReaderInterface : public DirectoryReaderInterface { + public: + explicit RealDirectoryReaderInterface( + const PPB_DirectoryReader_Dev* directory_reader_interface); + + virtual PP_Resource Create(PP_Resource); + virtual int32_t GetNextEntry(PP_Resource, PP_DirectoryEntry_Dev*, + PP_CompletionCallback); + + private: + const PPB_DirectoryReader_Dev* interface_; +}; +DEFINE_CONSTRUCTOR(RealDirectoryReaderInterface, PPB_DirectoryReader_Dev) +DEFINE_METHOD1(RealDirectoryReaderInterface, PP_Resource, Create, PP_Resource) +DEFINE_METHOD3(RealDirectoryReaderInterface, int32_t, GetNextEntry, PP_Resource, + PP_DirectoryEntry_Dev*, PP_CompletionCallback) + +class RealVarInterface : public VarInterface { + public: + explicit RealVarInterface(const PPB_Var* var_interface); + + virtual const char* VarToUtf8(PP_Var, uint32_t*); + + private: + const PPB_Var* interface_; +}; +DEFINE_CONSTRUCTOR(RealVarInterface, PPB_Var) +DEFINE_METHOD2(RealVarInterface, const char*, VarToUtf8, PP_Var, uint32_t*) + + +RealPepperInterface::RealPepperInterface(PP_Instance instance, + PPB_GetInterface get_browser_interface) + : instance_(instance), + core_interface_(NULL), + message_loop_interface_(NULL) { + core_interface_ = static_cast<const PPB_Core*>( + get_browser_interface(PPB_CORE_INTERFACE)); + message_loop_interface_ = static_cast<const PPB_MessageLoop*>( + get_browser_interface(PPB_MESSAGELOOP_INTERFACE)); + assert(core_interface_); + assert(message_loop_interface_); + + directory_reader_interface_ = new RealDirectoryReaderInterface( + static_cast<const PPB_DirectoryReader_Dev*>(get_browser_interface( + PPB_DIRECTORYREADER_DEV_INTERFACE))); + fileio_interface_ = new RealFileIoInterface(static_cast<const PPB_FileIO*>( + get_browser_interface(PPB_FILEIO_INTERFACE))); + fileref_interface_ = new RealFileRefInterface(static_cast<const PPB_FileRef*>( + get_browser_interface(PPB_FILEREF_INTERFACE))); + filesystem_interface_ = new RealFileSystemInterface( + static_cast<const PPB_FileSystem*>(get_browser_interface( + PPB_FILESYSTEM_INTERFACE))); + var_interface_= new RealVarInterface( + static_cast<const PPB_Var*>(get_browser_interface( + PPB_VAR_INTERFACE))); +} + +PP_Instance RealPepperInterface::GetInstance() { + return instance_; +} + +void RealPepperInterface::AddRefResource(PP_Resource resource) { + if (resource) + core_interface_->AddRefResource(resource); +} + +void RealPepperInterface::ReleaseResource(PP_Resource resource) { + if (resource) + core_interface_->ReleaseResource(resource); +} + +FileSystemInterface* RealPepperInterface::GetFileSystemInterface() { + return filesystem_interface_; +} + +FileRefInterface* RealPepperInterface::GetFileRefInterface() { + return fileref_interface_; +} + +FileIoInterface* RealPepperInterface::GetFileIoInterface() { + return fileio_interface_; +} + +DirectoryReaderInterface* RealPepperInterface::GetDirectoryReaderInterface() { + return directory_reader_interface_; +} + +VarInterface* RealPepperInterface::GetVarInterface() { + return var_interface_; +} + +int32_t RealPepperInterface::InitializeMessageLoop() { + int32_t result; + PP_Resource message_loop = 0; + if (core_interface_->IsMainThread()) { + // TODO(binji): Spin up the main thread's ppapi work thread. + assert(0); + } else { + message_loop = message_loop_interface_->GetCurrent(); + if (!message_loop) { + message_loop = message_loop_interface_->Create(instance_); + result = message_loop_interface_->AttachToCurrentThread(message_loop); + assert(result == PP_OK); + } + } + + return PP_OK; +} diff --git a/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.h b/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.h new file mode 100644 index 0000000..b0a4a52 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts/real_pepper_interface.h @@ -0,0 +1,46 @@ +/* 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 LIBRARIES_NACL_MOUNTS_REAL_PEPPER_INTERFACE_H_ +#define LIBRARIES_NACL_MOUNTS_REAL_PEPPER_INTERFACE_H_ + +#include <ppapi/c/ppb.h> +#include <ppapi/c/ppb_core.h> +#include <ppapi/c/ppb_message_loop.h> +#include "pepper_interface.h" + +class RealDirectoryReaderInterface; +class RealFileIoInterface; +class RealFileRefInterface; +class RealFileSystemInterface; +class RealVarInterface; + +class RealPepperInterface : public PepperInterface { + public: + RealPepperInterface(PP_Instance instance, + PPB_GetInterface get_browser_interface); + + virtual PP_Instance GetInstance(); + virtual void AddRefResource(PP_Resource); + virtual void ReleaseResource(PP_Resource); + virtual FileSystemInterface* GetFileSystemInterface(); + virtual FileRefInterface* GetFileRefInterface(); + virtual FileIoInterface* GetFileIoInterface(); + virtual DirectoryReaderInterface* GetDirectoryReaderInterface(); + virtual VarInterface* GetVarInterface(); + + int32_t InitializeMessageLoop(); + + private: + PP_Instance instance_; + const PPB_Core* core_interface_; + const PPB_MessageLoop* message_loop_interface_; + RealDirectoryReaderInterface* directory_reader_interface_; + RealFileIoInterface* fileio_interface_; + RealFileRefInterface* fileref_interface_; + RealFileSystemInterface* filesystem_interface_; + RealVarInterface* var_interface_; +}; + +#endif // LIBRARIES_NACL_MOUNTS_REAL_PEPPER_INTERFACE_H_ diff --git a/native_client_sdk/src/libraries/nacl_mounts_test/example.dsc b/native_client_sdk/src/libraries/nacl_mounts_test/example.dsc index 894f329..ed86526 100644 --- a/native_client_sdk/src/libraries/nacl_mounts_test/example.dsc +++ b/native_client_sdk/src/libraries/nacl_mounts_test/example.dsc @@ -17,6 +17,8 @@ 'mount_node_test.cc', 'mount_test.cc', 'path_test.cc', + 'pepper_interface_mock.cc', + 'pepper_interface_mock.h', ], 'LIBS': ['ppapi', 'pthread', 'gtest', 'gmock', 'nacl_mounts', 'ppapi_cpp', 'gtest_ppapi'] } diff --git a/native_client_sdk/src/libraries/nacl_mounts_test/kernel_proxy_test.cc b/native_client_sdk/src/libraries/nacl_mounts_test/kernel_proxy_test.cc index 58eaca2..771a429 100644 --- a/native_client_sdk/src/libraries/nacl_mounts_test/kernel_proxy_test.cc +++ b/native_client_sdk/src/libraries/nacl_mounts_test/kernel_proxy_test.cc @@ -145,7 +145,7 @@ StringMap_t g_StringMap; class MountMockInit : public MountMem { public: - bool Init(int dev, StringMap_t& args) { + virtual bool Init(int dev, StringMap_t& args, PepperInterface* ppapi) { g_StringMap = args; if (args.find("false") != args.end()) return false; @@ -154,8 +154,8 @@ class MountMockInit : public MountMem { }; class KernelProxyMountMock : public KernelProxy { - void Init() { - KernelProxy::Init(); + virtual void Init(PepperInterface* ppapi) { + KernelProxy::Init(NULL); factories_["initfs"] = MountMockInit::Create<MountMockInit>; } }; diff --git a/native_client_sdk/src/libraries/nacl_mounts_test/mount_test.cc b/native_client_sdk/src/libraries/nacl_mounts_test/mount_test.cc index fc8cafa..a167866 100644 --- a/native_client_sdk/src/libraries/nacl_mounts_test/mount_test.cc +++ b/native_client_sdk/src/libraries/nacl_mounts_test/mount_test.cc @@ -5,10 +5,10 @@ #include <errno.h> #include <fcntl.h> +#include <string.h> #include <string> #include <sys/stat.h> - #include "nacl_mounts/mount.h" #include "nacl_mounts/mount_dev.h" #include "nacl_mounts/mount_mem.h" @@ -24,7 +24,7 @@ class MountMemMock : public MountMem { : MountMem(), nodes_(0) { StringMap_t map; - Init(1, map); + Init(1, map, NULL); }; MountNode* AllocateData(int mode) { @@ -45,7 +45,7 @@ class MountDevMock : public MountDev { public: MountDevMock() : MountDev() { StringMap_t map; - Init(1, map); + Init(1, map, NULL); } }; diff --git a/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.cc b/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.cc new file mode 100644 index 0000000..0fbfc04 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.cc @@ -0,0 +1,42 @@ +/* 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 "pepper_interface_mock.h" + +PepperInterfaceMock::PepperInterfaceMock() + : filesystem_interface_(new FileSystemInterfaceMock), + fileref_interface_(new FileRefInterfaceMock), + fileio_interface_(new FileIoInterfaceMock), + directory_reader_interface_(new DirectoryReaderInterfaceMock), + var_interface_(new VarInterfaceMock) { +} + +PepperInterfaceMock::~PepperInterfaceMock() { + delete filesystem_interface_; + delete fileref_interface_; + delete fileio_interface_; + delete directory_reader_interface_; + delete var_interface_; +} + +FileSystemInterface* PepperInterfaceMock::GetFileSystemInterface() { + return filesystem_interface_; +} + +FileRefInterface* PepperInterfaceMock::GetFileRefInterface() { + return fileref_interface_; +} + +FileIoInterface* PepperInterfaceMock::GetFileIoInterface() { + return fileio_interface_; +} + +DirectoryReaderInterface* PepperInterfaceMock::GetDirectoryReaderInterface() { + return directory_reader_interface_; +} + +VarInterface* PepperInterfaceMock::GetVarInterface() { + return var_interface_; +} diff --git a/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.h b/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.h new file mode 100644 index 0000000..2a8fe13 --- /dev/null +++ b/native_client_sdk/src/libraries/nacl_mounts_test/pepper_interface_mock.h @@ -0,0 +1,82 @@ +/* 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 LIBRARIES_NACL_MOUNTS_TEST_PEPPER_INTERFACE_MOCK_H_ +#define LIBRARIES_NACL_MOUNTS_TEST_PEPPER_INTERFACE_MOCK_H_ + +#include "gmock/gmock.h" +#include "nacl_mounts/pepper_interface.h" + +class FileSystemInterfaceMock; +class FileRefInterfaceMock; +class FileIoInterfaceMock; +class DirectoryReaderInterfaceMock; +class VarInterfaceMock; + +class PepperInterfaceMock : public PepperInterface { + public: + PepperInterfaceMock(); + ~PepperInterfaceMock(); + + MOCK_METHOD0(GetInstance, PP_Instance()); + MOCK_METHOD1(AddRefResource, void(PP_Resource)); + MOCK_METHOD1(ReleaseResource, void(PP_Resource)); + virtual FileSystemInterface* GetFileSystemInterface(); + virtual FileRefInterface* GetFileRefInterface(); + virtual FileIoInterface* GetFileIoInterface(); + virtual DirectoryReaderInterface* GetDirectoryReaderInterface(); + virtual VarInterface* GetVarInterface(); + + private: + FileSystemInterfaceMock* filesystem_interface_; + FileRefInterfaceMock* fileref_interface_; + FileIoInterfaceMock* fileio_interface_; + DirectoryReaderInterfaceMock* directory_reader_interface_; + VarInterfaceMock* var_interface_; +}; + +class FileSystemInterfaceMock : public FileSystemInterface { + public: + MOCK_METHOD2(Create, PP_Resource(PP_Instance, PP_FileSystemType)); + MOCK_METHOD3(Open, int32_t(PP_Resource, int64_t, PP_CompletionCallback)); +}; + +class FileRefInterfaceMock : public FileRefInterface { + public: + MOCK_METHOD2(Create, PP_Resource(PP_Resource, const char*)); + MOCK_METHOD2(Delete, int32_t(PP_Resource, PP_CompletionCallback)); + MOCK_METHOD1(GetName, PP_Var(PP_Resource)); + MOCK_METHOD3(MakeDirectory, int32_t(PP_Resource, PP_Bool, + PP_CompletionCallback)); +}; + +class FileIoInterfaceMock : public FileIoInterface { + public: + MOCK_METHOD1(Close, void(PP_Resource)); + MOCK_METHOD1(Create, PP_Resource(PP_Instance)); + MOCK_METHOD2(Flush, int32_t(PP_Resource, PP_CompletionCallback)); + MOCK_METHOD4(Open, int32_t(PP_Resource, PP_Resource, int32_t, + PP_CompletionCallback)); + MOCK_METHOD3(Query, int32_t(PP_Resource, PP_FileInfo*, + PP_CompletionCallback)); + MOCK_METHOD5(Read, int32_t(PP_Resource, int64_t, char*, int32_t, + PP_CompletionCallback)); + MOCK_METHOD3(SetLength, int32_t(PP_Resource, int64_t, PP_CompletionCallback)); + MOCK_METHOD5(Write, int32_t(PP_Resource, int64_t, const char*, int32_t, + PP_CompletionCallback)); +}; + +class DirectoryReaderInterfaceMock : public DirectoryReaderInterface { + public: + MOCK_METHOD1(Create, PP_Resource(PP_Resource)); + MOCK_METHOD3(GetNextEntry, int32_t(PP_Resource, PP_DirectoryEntry_Dev*, + PP_CompletionCallback)); +}; + +class VarInterfaceMock : public VarInterface { + public: + MOCK_METHOD2(VarToUtf8, const char*(PP_Var, uint32_t*)); +}; + +#endif // LIBRARIES_NACL_MOUNTS_TEST_PEPPER_INTERFACE_MOCK_H_ |