OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <sstream> | 5 #include <sstream> |
6 | 6 |
7 #include "ppapi/c/ppb_file_io.h" | 7 #include "ppapi/c/ppb_file_io.h" |
8 #include "ppapi/cpp/file_io.h" | 8 #include "ppapi/cpp/file_io.h" |
9 #include "ppapi/cpp/file_ref.h" | 9 #include "ppapi/cpp/file_ref.h" |
10 #include "ppapi/cpp/instance.h" | 10 #include "ppapi/cpp/instance.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 handle_(instance) { | 28 handle_(instance) { |
29 factory_.Initialize(this); | 29 factory_.Initialize(this); |
30 } | 30 } |
31 virtual ~MyInstance() { | 31 virtual ~MyInstance() { |
32 } | 32 } |
33 | 33 |
34 // Handler for the page sending us messages. | 34 // Handler for the page sending us messages. |
35 virtual void HandleMessage(const pp::Var& message_data); | 35 virtual void HandleMessage(const pp::Var& message_data); |
36 | 36 |
37 private: | 37 private: |
38 void OpenCrxFsAndReadFile(const std::string& filename); | 38 void OpenPluginFsAndReadFile(const std::string& filename); |
39 | 39 |
40 void CrxFileSystemCallback(int32_t pp_error, pp::FileSystem file_system); | 40 void PluginFileSystemCallback(int32_t pp_error, pp::FileSystem file_system); |
41 void FileIOOpenCallback(int32_t pp_error); | 41 void FileIOOpenCallback(int32_t pp_error); |
| 42 void FileIOWriteCallback(int32_t pp_error); |
42 void FileIOReadCallback(int32_t pp_error); | 43 void FileIOReadCallback(int32_t pp_error); |
43 | 44 |
| 45 void OpenFile(const std::string& filename); |
| 46 void OpenFileCallback(int32_t pp_error); |
| 47 |
44 // Forwards the given string to the page. | 48 // Forwards the given string to the page. |
45 void ReportResponse(const char* name, int32_t pp_error); | 49 void ReportResponse(const char* name, int32_t pp_error); |
46 | 50 |
47 // Generates completion callbacks scoped to this class. | 51 // Generates completion callbacks scoped to this class. |
48 pp::CompletionCallbackFactory<MyInstance> factory_; | 52 pp::CompletionCallbackFactory<MyInstance> factory_; |
49 | 53 |
50 pp::InstanceHandle handle_; | 54 pp::InstanceHandle handle_; |
51 pp::IsolatedFileSystemPrivate crxfs_; | |
52 pp::FileRef file_ref_; | 55 pp::FileRef file_ref_; |
53 pp::FileIO file_io_; | 56 pp::FileIO file_io_; |
54 std::string filename_; | 57 std::string filename_; |
55 char read_buf_[kBufSize]; | 58 char read_buf_[kBufSize]; |
56 }; | 59 }; |
57 | 60 |
58 void MyInstance::HandleMessage(const pp::Var& message_data) { | 61 void MyInstance::HandleMessage(const pp::Var& message_data) { |
59 if (!message_data.is_string()) { | 62 if (!message_data.is_string()) { |
60 ReportResponse("HandleMessage: not a string", 0); | 63 ReportResponse("HandleMessage: not a string", 0); |
61 return; | 64 return; |
62 } | 65 } |
63 std::string filename = message_data.AsString(); | 66 std::string filename = message_data.AsString(); |
64 OpenCrxFsAndReadFile(filename); | 67 OpenPluginFsAndReadFile(filename); |
65 } | 68 } |
66 | 69 |
67 void MyInstance::OpenCrxFsAndReadFile(const std::string& filename) { | 70 void MyInstance::OpenPluginFsAndReadFile(const std::string& filename) { |
68 filename_ = filename; | 71 filename_ = filename; |
69 | 72 |
70 pp::CompletionCallbackWithOutput<pp::FileSystem> callback = | 73 pp::CompletionCallbackWithOutput<pp::FileSystem> callback = |
71 factory_.NewCallbackWithOutput(&MyInstance::CrxFileSystemCallback); | 74 factory_.NewCallbackWithOutput(&MyInstance::PluginFileSystemCallback); |
72 | 75 |
73 crxfs_ = pp::IsolatedFileSystemPrivate( | 76 pp::IsolatedFileSystemPrivate pluginfs = |
74 this, PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_CRX); | 77 pp::IsolatedFileSystemPrivate(this, PP_ISOLATEDFILESYSTEMTYPE_PRIVATE_PLUG
INPRIVATE); |
75 int32_t rv = crxfs_.Open(callback); | 78 int32_t rv = pluginfs.Open(callback); |
76 if (rv != PP_OK_COMPLETIONPENDING) | 79 if (rv != PP_OK_COMPLETIONPENDING) |
77 ReportResponse("ExtCrxFileSystemPrivate::Open", rv); | 80 ReportResponse("IsolatedFileSystemPrivate::Open", rv); |
78 } | 81 } |
79 | 82 |
80 void MyInstance::CrxFileSystemCallback(int32_t pp_error, | 83 void MyInstance::PluginFileSystemCallback(int32_t pp_error, |
81 pp::FileSystem file_system) { | 84 pp::FileSystem file_system) { |
82 if (pp_error != PP_OK) { | 85 if (pp_error != PP_OK) { |
83 ReportResponse("CrxFileSystemCallback", pp_error); | 86 ReportResponse("PluginFileSystemCallback", pp_error); |
84 return; | 87 return; |
85 } | 88 } |
86 | 89 |
87 file_io_ = pp::FileIO(handle_); | 90 file_io_ = pp::FileIO(handle_); |
88 file_ref_ = pp::FileRef(file_system, filename_.c_str()); | 91 file_ref_ = pp::FileRef(file_system, filename_.c_str()); |
89 int32_t rv = file_io_.Open( | 92 int32_t rv = file_io_.Open( |
90 file_ref_, PP_FILEOPENFLAG_READ, | 93 file_ref_, PP_FILEOPENFLAG_READ | PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_
CREATE | PP_FILEOPENFLAG_TRUNCATE, |
91 factory_.NewCallback(&MyInstance::FileIOOpenCallback)); | 94 factory_.NewCallback(&MyInstance::FileIOOpenCallback)); |
92 if (rv != PP_OK_COMPLETIONPENDING) | 95 if (rv != PP_OK_COMPLETIONPENDING) |
93 ReportResponse("FileIO::Open", rv); | 96 ReportResponse("FileIO::Open", rv); |
94 } | 97 } |
95 | 98 |
96 void MyInstance::FileIOOpenCallback(int32_t pp_error) { | 99 void MyInstance::FileIOOpenCallback(int32_t pp_error) { |
97 if (pp_error != PP_OK) { | 100 if (pp_error != PP_OK) { |
98 ReportResponse("FileIOOpenCallback", pp_error); | 101 ReportResponse("FileIOOpenCallback", pp_error); |
99 return; | 102 return; |
100 } | 103 } |
101 | 104 |
| 105 int32_t rv = file_io_.Write(0, "hogehoge", 8, |
| 106 factory_.NewCallback(&MyInstance::FileIOWriteCallb
ack)); |
| 107 if (rv != PP_OK) { |
| 108 ReportResponse("FileIO::Write", rv); |
| 109 return; |
| 110 } |
| 111 } |
| 112 |
| 113 void MyInstance::FileIOWriteCallback(int32_t written_bytes) { |
| 114 if (written_bytes <= 0) { |
| 115 ReportResponse("FileIOWriteCallback", written_bytes); |
| 116 return; |
| 117 } |
| 118 |
102 int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_), | 119 int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_), |
103 factory_.NewCallback(&MyInstance::FileIOReadCallback)); | 120 factory_.NewCallback(&MyInstance::FileIOReadCallback)); |
104 if (rv != PP_OK_COMPLETIONPENDING) { | 121 if (rv != PP_OK_COMPLETIONPENDING) { |
105 ReportResponse("FileIO::Read", rv); | 122 ReportResponse("FileIO::Read", rv); |
106 return; | 123 return; |
107 } | 124 } |
108 } | 125 } |
109 | 126 |
110 void MyInstance::FileIOReadCallback(int32_t pp_error) { | 127 void MyInstance::FileIOReadCallback(int32_t pp_error) { |
111 if (pp_error < 0) { | 128 if (pp_error < 0) { |
(...skipping 26 matching lines...) Expand all Loading... |
138 }; | 155 }; |
139 | 156 |
140 namespace pp { | 157 namespace pp { |
141 | 158 |
142 // Factory function for your specialization of the Module object. | 159 // Factory function for your specialization of the Module object. |
143 Module* CreateModule() { | 160 Module* CreateModule() { |
144 return new MyModule(); | 161 return new MyModule(); |
145 } | 162 } |
146 | 163 |
147 } // namespace pp | 164 } // namespace pp |
148 | |
OLD | NEW |