Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: ppapi/examples/crxfs/crxfs.cc

Issue 14188019: CRX FileSystem Pepper private API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_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.
20 const int kBufSize = 1024;
palmer 2013/05/02 21:30:09 size_t
victorhsieh 2013/05/03 17:26:58 Done.
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::FileSystem fs_;
51 pp::FileRef file_ref_;
52 pp::FileIO file_io_;
53 std::string filename_;
54 char read_buf_[8192];
palmer 2013/05/02 21:30:09 Not kBufSize?
victorhsieh 2013/05/03 17:26:58 Done.
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 pp::CompletionCallbackWithOutput<pp::FileSystem>
palmer 2013/05/02 21:30:09 Nit: pp::CompletionCallbackWithOutput<pp::FileS
victorhsieh 2013/05/03 17:26:58 Done.
70 callback = factory_.NewCallbackWithOutput(
71 &MyInstance::CrxFileSystemCallback);
72
73 crxfs_ = pp::ExtCrxFileSystemPrivate(this);
74 int32_t rv = crxfs_.Open(callback);
75 if (rv != PP_OK_COMPLETIONPENDING)
76 ReportResponse("ExtCrxFileSystemPrivate::Open", rv);
77 }
78
79 void MyInstance::CrxFileSystemCallback(int32_t pp_error,
80 pp::FileSystem file_system) {
81 if (pp_error != PP_OK) {
82 ReportResponse("CrxFileSystemCallback", pp_error);
83 return;
84 }
85
86 file_io_ = pp::FileIO(handle_);
87 file_ref_ = pp::FileRef(file_system, filename_.c_str());
88 int32_t rv = file_io_.Open(
89 file_ref_, PP_FILEOPENFLAG_READ,
90 factory_.NewCallback(&MyInstance::FileIOOpenCallback));
91 if (rv != PP_OK_COMPLETIONPENDING)
92 ReportResponse("FileIO::Open", rv);
93 }
94
95 void MyInstance::FileIOOpenCallback(int32_t pp_error) {
96 if (pp_error != PP_OK) {
97 ReportResponse("FileIOOpenCallback", pp_error);
98 return;
99 }
100
101 int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_),
102 factory_.NewCallback(&MyInstance::FileIOReadCallback));
103 if (rv != PP_OK_COMPLETIONPENDING) {
104 ReportResponse("FileIO::Read", rv);
105 return;
106 }
107 }
108
109 void MyInstance::FileIOReadCallback(int32_t pp_error) {
110 if (pp_error < 0) {
111 ReportResponse("FileIOReadCallback", pp_error);
112 return;
113 }
114
115 std::string content;
116 content.append(read_buf_, pp_error);
palmer 2013/05/02 21:30:09 Check that pp_error is not > sizeof(read_buf_), an
victorhsieh 2013/05/03 17:26:58 I think this is a serious API bug if it's true. F
117 PostMessage(pp::Var(content));
118 }
119
120 void MyInstance::ReportResponse(const char* name, int32_t rv) {
121 char buf[1024];
122 snprintf(buf, sizeof(buf), "%s failed, pp_error: %d", name, rv);
palmer 2013/05/02 21:30:09 Why not StringPrintf? Alternately, check the retu
victorhsieh 2013/05/03 17:26:58 I think most developer don't have StringPrintf, so
123 PostMessage(pp::Var(buf));
124 }
125
126 // This object is the global object representing this plugin library as long
127 // as it is loaded.
128 class MyModule : public pp::Module {
129 public:
130 MyModule() : pp::Module() {}
131 virtual ~MyModule() {}
132
133 // Override CreateInstance to create your customized Instance object.
134 virtual pp::Instance* CreateInstance(PP_Instance instance) {
135 return new MyInstance(instance);
136 }
137 };
138
139 namespace pp {
140
141 // Factory function for your specialization of the Module object.
142 Module* CreateModule() {
143 return new MyModule();
144 }
145
146 } // namespace pp
147
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698