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

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

Powered by Google App Engine
This is Rietveld 408576698