Chromium Code Reviews| Index: ppapi/examples/crxfs/crxfs.cc |
| diff --git a/ppapi/examples/crxfs/crxfs.cc b/ppapi/examples/crxfs/crxfs.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7eff4171cab1b5317aa974168b04dfe5ff7c102d |
| --- /dev/null |
| +++ b/ppapi/examples/crxfs/crxfs.cc |
| @@ -0,0 +1,143 @@ |
| +// Copyright (c) 2013 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 <stdio.h> |
| +#include "ppapi/c/ppb_file_io.h" |
| +#include "ppapi/cpp/file_io.h" |
| +#include "ppapi/cpp/instance.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/cpp/private/ext_crx_file_ref_private.h" |
| +#include "ppapi/cpp/private/ext_crx_file_system_private.h" |
| +#include "ppapi/utility/completion_callback_factory.h" |
| + |
| +// When compiling natively on Windows, PostMessage can be #define-d to |
| +// something else. |
| +#ifdef PostMessage |
| +#undef PostMessage |
| +#endif |
| + |
| +// Buffer size for reading network data. |
| +const int kBufSize = 1024; |
| + |
| +class MyInstance : public pp::Instance { |
| + public: |
| + explicit MyInstance(PP_Instance instance) |
| + : pp::Instance(instance), |
| + handle_(instance) { |
| + factory_.Initialize(this); |
| + } |
| + virtual ~MyInstance() { |
| + } |
| + |
| + // Handler for the page sending us messages. |
| + virtual void HandleMessage(const pp::Var& message_data); |
| + |
| + private: |
| + void OpenCrxFsAndReadFile(const std::string& filename); |
| + |
| + void CrxFileSystemCallback(int32_t pp_error); |
| + void FileIOOpenCallback(int32_t pp_error); |
| + void FileIOReadCallback(int32_t pp_error); |
| + |
| + // Forwards the given string to the page. |
| + void ReportResponse(const char* name, int32_t pp_error); |
| + |
| + // Generates completion callbacks scoped to this class. |
| + pp::CompletionCallbackFactory<MyInstance> factory_; |
| + |
| + pp::InstanceHandle handle_; |
| + pp::ExtCrxFileSystemPrivate crxfs_; |
| + pp::ExtCrxFileRefPrivate file_ref_; |
| + pp::FileIO file_io_; |
| + std::string filename_; |
| + char read_buf_[8192]; |
| +}; |
| + |
| +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; |
| + |
| + crxfs_ = pp::ExtCrxFileSystemPrivate(this); |
| + int32_t rv = crxfs_.Open( |
| + factory_.NewCallback(&MyInstance::CrxFileSystemCallback)); |
| + if (rv != PP_OK_COMPLETIONPENDING) |
| + ReportResponse("ExtCrxFileSystemPrivate::Open", rv); |
| +} |
| + |
| +void MyInstance::CrxFileSystemCallback(int32_t pp_error) { |
| + if (pp_error != PP_OK) { |
| + ReportResponse("CrxFileSystemCallback", pp_error); |
| + return; |
| + } |
| + |
| + file_io_ = pp::FileIO(handle_); |
| + file_ref_ = pp::ExtCrxFileRefPrivate(crxfs_, 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; |
| + } |
|
kinuko
2013/04/22 15:12:36
Probably it doesn't necessarily be in this example
victorhsieh
2013/04/22 17:51:39
Yes, it fails.
|
| +} |
| + |
| +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) { |
| + char buf[1024]; |
| + snprintf(buf, sizeof(buf), "%s failed, pp_error: %d", name, rv); |
| + PostMessage(pp::Var(buf)); |
| +} |
| + |
| +// This object is the global object representing this plugin library as long |
| +// as it is loaded. |
| +class MyModule : public pp::Module { |
| + public: |
| + MyModule() : pp::Module() {} |
| + virtual ~MyModule() {} |
| + |
| + // Override CreateInstance to create your customized Instance object. |
| + virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| + return new MyInstance(instance); |
| + } |
| +}; |
| + |
| +namespace pp { |
| + |
| +// Factory function for your specialization of the Module object. |
| +Module* CreateModule() { |
| + return new MyModule(); |
| +} |
| + |
| +} // namespace pp |
| + |