cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / ppapi / examples / crxfs / crxfs.cc
blob5e4b422cd30fed617985973b93df0b9df2c2174f
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.
5 #include <sstream>
7 #include "ppapi/c/ppb_file_io.h"
8 #include "ppapi/cpp/file_io.h"
9 #include "ppapi/cpp/file_ref.h"
10 #include "ppapi/cpp/instance.h"
11 #include "ppapi/cpp/module.h"
12 #include "ppapi/cpp/private/ext_crx_file_system_private.h"
13 #include "ppapi/utility/completion_callback_factory.h"
15 // When compiling natively on Windows, PostMessage can be #define-d to
16 // something else.
17 #ifdef PostMessage
18 #undef PostMessage
19 #endif
21 // Buffer size for reading file.
22 const size_t kBufSize = 1024;
24 class MyInstance : public pp::Instance {
25 public:
26 explicit MyInstance(PP_Instance instance)
27 : pp::Instance(instance),
28 handle_(instance) {
29 factory_.Initialize(this);
31 virtual ~MyInstance() {
34 // Handler for the page sending us messages.
35 virtual void HandleMessage(const pp::Var& message_data);
37 private:
38 void OpenCrxFsAndReadFile(const std::string& filename);
40 void CrxFileSystemCallback(int32_t pp_error, pp::FileSystem file_system);
41 void FileIOOpenCallback(int32_t pp_error);
42 void FileIOReadCallback(int32_t pp_error);
44 // Forwards the given string to the page.
45 void ReportResponse(const char* name, int32_t pp_error);
47 // Generates completion callbacks scoped to this class.
48 pp::CompletionCallbackFactory<MyInstance> factory_;
50 pp::InstanceHandle handle_;
51 pp::ExtCrxFileSystemPrivate crxfs_;
52 pp::FileRef file_ref_;
53 pp::FileIO file_io_;
54 std::string filename_;
55 char read_buf_[kBufSize];
58 void MyInstance::HandleMessage(const pp::Var& message_data) {
59 if (!message_data.is_string()) {
60 ReportResponse("HandleMessage: not a string", 0);
61 return;
63 std::string filename = message_data.AsString();
64 OpenCrxFsAndReadFile(filename);
67 void MyInstance::OpenCrxFsAndReadFile(const std::string& filename) {
68 filename_ = filename;
70 pp::CompletionCallbackWithOutput<pp::FileSystem> callback =
71 factory_.NewCallbackWithOutput(&MyInstance::CrxFileSystemCallback);
73 crxfs_ = pp::ExtCrxFileSystemPrivate(this);
74 int32_t rv = crxfs_.Open(callback);
75 if (rv != PP_OK_COMPLETIONPENDING)
76 ReportResponse("ExtCrxFileSystemPrivate::Open", rv);
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;
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);
95 void MyInstance::FileIOOpenCallback(int32_t pp_error) {
96 if (pp_error != PP_OK) {
97 ReportResponse("FileIOOpenCallback", pp_error);
98 return;
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;
109 void MyInstance::FileIOReadCallback(int32_t pp_error) {
110 if (pp_error < 0) {
111 ReportResponse("FileIOReadCallback", pp_error);
112 return;
115 std::string content;
116 content.append(read_buf_, pp_error);
117 PostMessage(pp::Var(content));
120 void MyInstance::ReportResponse(const char* name, int32_t rv) {
121 std::ostringstream out;
122 out << name << " failed, pp_error: " << rv;
123 PostMessage(pp::Var(out.str()));
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() {}
133 // Override CreateInstance to create your customized Instance object.
134 virtual pp::Instance* CreateInstance(PP_Instance instance) {
135 return new MyInstance(instance);
139 namespace pp {
141 // Factory function for your specialization of the Module object.
142 Module* CreateModule() {
143 return new MyModule();
146 } // namespace pp