1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "ppapi/c/ppb_file_io.h"
8 #include "ppapi/cpp/file_io.h"
9 #include "ppapi/cpp/file_ref.h"
10 #include "ppapi/cpp/instance.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/private/isolated_file_system_private.h"
13 #include "ppapi/utility/completion_callback_factory.h"
15 // When compiling natively on Windows, PostMessage can be #define-d to
21 // Buffer size for reading file.
22 const size_t kBufSize
= 1024;
24 class MyInstance
: public pp::Instance
{
26 explicit MyInstance(PP_Instance instance
)
27 : pp::Instance(instance
),
29 factory_
.Initialize(this);
31 virtual ~MyInstance() {
34 // Handler for the page sending us messages.
35 virtual void HandleMessage(const pp::Var
& message_data
);
38 void OpenCrxFsAndReadFile(const std::string
& filename
);
40 void CrxFileSystemCallback(int32_t pp_error
, pp::FileSystem file_system
);
41 void FileIOOpenCallback(int32_t pp_error
);
42 void FileIOReadCallback(int32_t pp_error
);
44 // Forwards the given string to the page.
45 void ReportResponse(const char* name
, int32_t pp_error
);
47 // Generates completion callbacks scoped to this class.
48 pp::CompletionCallbackFactory
<MyInstance
> factory_
;
50 pp::InstanceHandle handle_
;
51 pp::IsolatedFileSystemPrivate crxfs_
;
52 pp::FileRef file_ref_
;
54 std::string filename_
;
55 char read_buf_
[kBufSize
];
58 void MyInstance::HandleMessage(const pp::Var
& message_data
) {
59 if (!message_data
.is_string()) {
60 ReportResponse("HandleMessage: not a string", 0);
63 std::string filename
= message_data
.AsString();
64 OpenCrxFsAndReadFile(filename
);
67 void MyInstance::OpenCrxFsAndReadFile(const std::string
& filename
) {
70 pp::CompletionCallbackWithOutput
<pp::FileSystem
> callback
=
71 factory_
.NewCallbackWithOutput(&MyInstance::CrxFileSystemCallback
);
73 crxfs_
= pp::IsolatedFileSystemPrivate(
74 this, PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_CRX
);
75 int32_t rv
= crxfs_
.Open(callback
);
76 if (rv
!= PP_OK_COMPLETIONPENDING
)
77 ReportResponse("ExtCrxFileSystemPrivate::Open", rv
);
80 void MyInstance::CrxFileSystemCallback(int32_t pp_error
,
81 pp::FileSystem file_system
) {
82 if (pp_error
!= PP_OK
) {
83 ReportResponse("CrxFileSystemCallback", pp_error
);
87 file_io_
= pp::FileIO(handle_
);
88 file_ref_
= pp::FileRef(file_system
, filename_
.c_str());
89 int32_t rv
= file_io_
.Open(
90 file_ref_
, PP_FILEOPENFLAG_READ
,
91 factory_
.NewCallback(&MyInstance::FileIOOpenCallback
));
92 if (rv
!= PP_OK_COMPLETIONPENDING
)
93 ReportResponse("FileIO::Open", rv
);
96 void MyInstance::FileIOOpenCallback(int32_t pp_error
) {
97 if (pp_error
!= PP_OK
) {
98 ReportResponse("FileIOOpenCallback", pp_error
);
102 int32_t rv
= file_io_
.Read(0, read_buf_
, sizeof(read_buf_
),
103 factory_
.NewCallback(&MyInstance::FileIOReadCallback
));
104 if (rv
!= PP_OK_COMPLETIONPENDING
) {
105 ReportResponse("FileIO::Read", rv
);
110 void MyInstance::FileIOReadCallback(int32_t pp_error
) {
112 ReportResponse("FileIOReadCallback", pp_error
);
117 content
.append(read_buf_
, pp_error
);
118 PostMessage(pp::Var(content
));
121 void MyInstance::ReportResponse(const char* name
, int32_t rv
) {
122 std::ostringstream out
;
123 out
<< name
<< " failed, pp_error: " << rv
;
124 PostMessage(pp::Var(out
.str()));
127 // This object is the global object representing this plugin library as long
129 class MyModule
: public pp::Module
{
131 MyModule() : pp::Module() {}
132 virtual ~MyModule() {}
134 // Override CreateInstance to create your customized Instance object.
135 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
136 return new MyInstance(instance
);
142 // Factory function for your specialization of the Module object.
143 Module
* CreateModule() {
144 return new MyModule();