OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <stdio.h> | |
6 #include "ppapi/c/ppb_file_io.h" | |
7 #include "ppapi/cpp/file_io.h" | |
8 #include "ppapi/cpp/instance.h" | |
9 #include "ppapi/cpp/module.h" | |
10 #include "ppapi/cpp/private/ext_crx_file_ref_private.h" | |
11 #include "ppapi/cpp/private/ext_crx_file_system_private.h" | |
12 #include "ppapi/utility/completion_callback_factory.h" | |
13 | |
14 // When compiling natively on Windows, PostMessage can be #define-d to | |
15 // something else. | |
16 #ifdef PostMessage | |
17 #undef PostMessage | |
18 #endif | |
19 | |
20 // Buffer size for reading network data. | |
21 const int kBufSize = 1024; | |
22 | |
23 class MyInstance : public pp::Instance { | |
24 public: | |
25 explicit MyInstance(PP_Instance instance) | |
26 : pp::Instance(instance), | |
27 handle_(instance) { | |
28 factory_.Initialize(this); | |
29 } | |
30 virtual ~MyInstance() { | |
31 } | |
32 | |
33 // Handler for the page sending us messages. | |
34 virtual void HandleMessage(const pp::Var& message_data); | |
35 | |
36 private: | |
37 void OpenCrxFsAndReadFile(const std::string& filename); | |
38 | |
39 void CrxFileSystemCallback(int32_t pp_error); | |
40 void FileIOOpenCallback(int32_t pp_error); | |
41 void FileIOReadCallback(int32_t pp_error); | |
42 | |
43 // Forwards the given string to the page. | |
44 void ReportResponse(const char* name, int32_t pp_error); | |
45 | |
46 // Generates completion callbacks scoped to this class. | |
47 pp::CompletionCallbackFactory<MyInstance> factory_; | |
48 | |
49 pp::InstanceHandle handle_; | |
50 pp::ExtCrxFileSystemPrivate crxfs_; | |
51 pp::ExtCrxFileRefPrivate file_ref_; | |
52 pp::FileIO file_io_; | |
53 std::string filename_; | |
54 char read_buf_[8192]; | |
55 }; | |
56 | |
57 void MyInstance::HandleMessage(const pp::Var& message_data) { | |
58 if (!message_data.is_string()) { | |
59 ReportResponse("HandleMessage: not a string", 0); | |
60 return; | |
61 } | |
62 std::string filename = message_data.AsString(); | |
63 OpenCrxFsAndReadFile(filename); | |
64 } | |
65 | |
66 void MyInstance::OpenCrxFsAndReadFile(const std::string& filename) { | |
67 filename_ = filename; | |
68 | |
69 crxfs_ = pp::ExtCrxFileSystemPrivate(this); | |
70 int32_t rv = crxfs_.Open( | |
71 factory_.NewCallback(&MyInstance::CrxFileSystemCallback)); | |
72 if (rv != PP_OK_COMPLETIONPENDING) | |
73 ReportResponse("ExtCrxFileSystemPrivate::Open", rv); | |
74 } | |
75 | |
76 void MyInstance::CrxFileSystemCallback(int32_t pp_error) { | |
77 if (pp_error != PP_OK) { | |
78 ReportResponse("CrxFileSystemCallback", pp_error); | |
79 return; | |
80 } | |
81 | |
82 file_io_ = pp::FileIO(handle_); | |
83 file_ref_ = pp::ExtCrxFileRefPrivate(crxfs_, filename_.c_str()); | |
84 int32_t rv = file_io_.Open( | |
85 file_ref_, PP_FILEOPENFLAG_READ, | |
86 factory_.NewCallback(&MyInstance::FileIOOpenCallback)); | |
87 if (rv != PP_OK_COMPLETIONPENDING) | |
88 ReportResponse("FileIO::Open", rv); | |
89 } | |
90 | |
91 void MyInstance::FileIOOpenCallback(int32_t pp_error) { | |
92 if (pp_error != PP_OK) { | |
93 ReportResponse("FileIOOpenCallback", pp_error); | |
94 return; | |
95 } | |
96 | |
97 int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_), | |
98 factory_.NewCallback(&MyInstance::FileIOReadCallback)); | |
99 if (rv != PP_OK_COMPLETIONPENDING) { | |
100 ReportResponse("FileIO::Read", rv); | |
101 return; | |
102 } | |
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.
| |
103 } | |
104 | |
105 void MyInstance::FileIOReadCallback(int32_t pp_error) { | |
106 if (pp_error < 0) { | |
107 ReportResponse("FileIOReadCallback", pp_error); | |
108 return; | |
109 } | |
110 | |
111 std::string content; | |
112 content.append(read_buf_, pp_error); | |
113 PostMessage(pp::Var(content)); | |
114 } | |
115 | |
116 void MyInstance::ReportResponse(const char* name, int32_t rv) { | |
117 char buf[1024]; | |
118 snprintf(buf, sizeof(buf), "%s failed, pp_error: %d", name, rv); | |
119 PostMessage(pp::Var(buf)); | |
120 } | |
121 | |
122 // This object is the global object representing this plugin library as long | |
123 // as it is loaded. | |
124 class MyModule : public pp::Module { | |
125 public: | |
126 MyModule() : pp::Module() {} | |
127 virtual ~MyModule() {} | |
128 | |
129 // Override CreateInstance to create your customized Instance object. | |
130 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
131 return new MyInstance(instance); | |
132 } | |
133 }; | |
134 | |
135 namespace pp { | |
136 | |
137 // Factory function for your specialization of the Module object. | |
138 Module* CreateModule() { | |
139 return new MyModule(); | |
140 } | |
141 | |
142 } // namespace pp | |
143 | |
OLD | NEW |