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