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

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;
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_[8192];
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>
69 callback = factory_.NewCallbackWithOutput(
70 &MyInstance::CrxFileSystemCallback);
71
72 crxfs_ = pp::ExtCrxFileSystemPrivate(this);
73 int32_t rv = crxfs_.Open(callback);
74 if (rv != PP_OK_COMPLETIONPENDING)
75 ReportResponse("ExtCrxFileSystemPrivate::Open", rv);
76 }
77
78 void MyInstance::CrxFileSystemCallback(int32_t pp_error,
79 pp::FileSystem file_system) {
80 if (pp_error != PP_OK) {
81 ReportResponse("CrxFileSystemCallback", pp_error);
82 return;
83 }
84
85 file_io_ = pp::FileIO(handle_);
86 file_ref_ = pp::FileRef(file_system, filename_.c_str());
87 int32_t rv = file_io_.Open(
88 file_ref_, PP_FILEOPENFLAG_READ,
89 factory_.NewCallback(&MyInstance::FileIOOpenCallback));
90 if (rv != PP_OK_COMPLETIONPENDING)
91 ReportResponse("FileIO::Open", rv);
92 }
93
94 void MyInstance::FileIOOpenCallback(int32_t pp_error) {
95 if (pp_error != PP_OK) {
96 ReportResponse("FileIOOpenCallback", pp_error);
97 return;
98 }
99
100 int32_t rv = file_io_.Read(0, read_buf_, sizeof(read_buf_),
101 factory_.NewCallback(&MyInstance::FileIOReadCallback));
102 if (rv != PP_OK_COMPLETIONPENDING) {
103 ReportResponse("FileIO::Read", rv);
104 return;
105 }
106 }
107
108 void MyInstance::FileIOReadCallback(int32_t pp_error) {
109 if (pp_error < 0) {
110 ReportResponse("FileIOReadCallback", pp_error);
111 return;
112 }
113
114 std::string content;
115 content.append(read_buf_, pp_error);
116 PostMessage(pp::Var(content));
117 }
118
119 void MyInstance::ReportResponse(const char* name, int32_t rv) {
120 char buf[1024];
121 snprintf(buf, sizeof(buf), "%s failed, pp_error: %d", name, rv);
122 PostMessage(pp::Var(buf));
123 }
124
125 // This object is the global object representing this plugin library as long
126 // as it is loaded.
127 class MyModule : public pp::Module {
128 public:
129 MyModule() : pp::Module() {}
130 virtual ~MyModule() {}
131
132 // Override CreateInstance to create your customized Instance object.
133 virtual pp::Instance* CreateInstance(PP_Instance instance) {
134 return new MyInstance(instance);
135 }
136 };
137
138 namespace pp {
139
140 // Factory function for your specialization of the Module object.
141 Module* CreateModule() {
142 return new MyModule();
143 }
144
145 } // namespace pp
146
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698