This source file includes following definitions.
- handle_
- HandleMessage
- OpenCrxFsAndReadFile
- CrxFileSystemCallback
- FileIOOpenCallback
- FileIOReadCallback
- ReportResponse
- CreateInstance
- CreateModule
#include <sstream>
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/isolated_file_system_private.h"
#include "ppapi/utility/completion_callback_factory.h"
#ifdef PostMessage
#undef PostMessage
#endif
const size_t kBufSize = 1024;
class MyInstance : public pp::Instance {
public:
explicit MyInstance(PP_Instance instance)
: pp::Instance(instance),
handle_(instance) {
factory_.Initialize(this);
}
virtual ~MyInstance() {
}
virtual void HandleMessage(const pp::Var& message_data);
private:
void OpenCrxFsAndReadFile(const std::string& filename);
void CrxFileSystemCallback(int32_t pp_error, pp::FileSystem file_system);
void FileIOOpenCallback(int32_t pp_error);
void FileIOReadCallback(int32_t pp_error);
void ReportResponse(const char* name, int32_t pp_error);
pp::CompletionCallbackFactory<MyInstance> factory_;
pp::InstanceHandle handle_;
pp::IsolatedFileSystemPrivate crxfs_;
pp::FileRef file_ref_;
pp::FileIO file_io_;
std::string filename_;
char read_buf_[kBufSize];
};
void MyInstance::HandleMessage(const pp::Var& message_data) {
if (!message_data.is_string()) {
ReportResponse("HandleMessage: not a string", 0);
return;
}
std::string filename = message_data.AsString();
OpenCrxFsAndReadFile(filename);
}
void MyInstance::OpenCrxFsAndReadFile(const std::string& filename) {
filename_ = filename;
pp::CompletionCallbackWithOutput<pp::FileSystem> callback =
factory_.NewCallbackWithOutput(&MyInstance::CrxFileSystemCallback);
crxfs_ = pp::IsolatedFileSystemPrivate(
this, PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_CRX);
int32_t rv = crxfs_.Open(callback);
if (rv != PP_OK_COMPLETIONPENDING)
ReportResponse("ExtCrxFileSystemPrivate::Open", rv);
}
void MyInstance::CrxFileSystemCallback(int32_t pp_error,
pp::FileSystem file_system) {
if (pp_error != PP_OK) {
ReportResponse("CrxFileSystemCallback", pp_error);
return;
}
file_io_ = pp::FileIO(handle_);
file_ref_ = pp::FileRef(file_system, filename_.c_str());
int32_t rv = file_io_.Open(
file_ref_, PP_FILEOPENFLAG_READ,
factory_.NewCallback(&MyInstance::FileIOOpenCallback));
if (rv != PP_OK_COMPLETIONPENDING)
ReportResponse("FileIO::Open", rv);
}
void MyInstance::FileIOOpenCallback(int32_t pp_error) {
if (pp_error != PP_OK) {
ReportResponse("FileIOOpenCallback", pp_error);
return;
}
int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_),
factory_.NewCallback(&MyInstance::FileIOReadCallback));
if (rv != PP_OK_COMPLETIONPENDING) {
ReportResponse("FileIO::Read", rv);
return;
}
}
void MyInstance::FileIOReadCallback(int32_t pp_error) {
if (pp_error < 0) {
ReportResponse("FileIOReadCallback", pp_error);
return;
}
std::string content;
content.append(read_buf_, pp_error);
PostMessage(pp::Var(content));
}
void MyInstance::ReportResponse(const char* name, int32_t rv) {
std::ostringstream out;
out << name << " failed, pp_error: " << rv;
PostMessage(pp::Var(out.str()));
}
class MyModule : public pp::Module {
public:
MyModule() : pp::Module() {}
virtual ~MyModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new MyInstance(instance);
}
};
namespace pp {
Module* CreateModule() {
return new MyModule();
}
}