This source file includes following definitions.
- HandleMessage
- StartRequest
- OnOpenComplete
- OnStreamComplete
- OnOpenFileComplete
- OnReadComplete
- ReportResponse
- CreateInstance
- CreateModule
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/url_loader.h"
#include "ppapi/cpp/url_request_info.h"
#include "ppapi/cpp/url_response_info.h"
#include "ppapi/utility/completion_callback_factory.h"
#ifdef PostMessage
#undef PostMessage
#endif
const int kBufSize = 1024;
class MyInstance : public pp::Instance {
public:
explicit MyInstance(PP_Instance instance)
: pp::Instance(instance) {
factory_.Initialize(this);
}
virtual ~MyInstance() {
loader_.Close();
}
virtual void HandleMessage(const pp::Var& message_data);
private:
void StartRequest(const std::string& url);
void OnOpenComplete(int32_t result);
void OnStreamComplete(int32_t result);
void OnOpenFileComplete(int32_t result);
void OnReadComplete(int32_t result);
void ReportResponse(const std::string& data);
pp::CompletionCallbackFactory<MyInstance> factory_;
pp::URLLoader loader_;
pp::URLResponseInfo response_;
pp::FileRef dest_file_;
pp::FileIO file_io_;
char buf_[kBufSize];
std::string content_;
};
void MyInstance::HandleMessage(const pp::Var& message_data) {
if (message_data.is_string() && message_data.AsString() == "go")
StartRequest("./fetched_content.html");
}
void MyInstance::StartRequest(const std::string& url) {
content_.clear();
pp::URLRequestInfo request(this);
request.SetURL(url);
request.SetMethod("GET");
request.SetStreamToFile(true);
loader_ = pp::URLLoader(this);
loader_.Open(request,
factory_.NewCallback(&MyInstance::OnOpenComplete));
}
void MyInstance::OnOpenComplete(int32_t result) {
if (result != PP_OK) {
ReportResponse("URL could not be requested");
return;
}
loader_.FinishStreamingToFile(
factory_.NewCallback(&MyInstance::OnStreamComplete));
response_ = loader_.GetResponseInfo();
dest_file_ = response_.GetBodyAsFileRef();
}
void MyInstance::OnStreamComplete(int32_t result) {
if (result == PP_OK) {
file_io_ = pp::FileIO(this);
file_io_.Open(dest_file_, PP_FILEOPENFLAG_READ,
factory_.NewCallback(&MyInstance::OnOpenFileComplete));
} else {
ReportResponse("Could not stream to file");
}
}
void MyInstance::OnOpenFileComplete(int32_t result) {
if (result == PP_OK) {
file_io_.Read(0, buf_, kBufSize,
factory_.NewCallback(&MyInstance::OnReadComplete));
} else {
ReportResponse("Could not open file");
}
}
void MyInstance::OnReadComplete(int32_t result) {
if (result >= 0) {
content_.append(buf_, result);
ReportResponse(buf_);
} else {
ReportResponse("Could not read file");
}
loader_ = pp::URLLoader();
response_ = pp::URLResponseInfo();
dest_file_ = pp::FileRef();
file_io_ = pp::FileIO();
}
void MyInstance::ReportResponse(const std::string& data) {
PostMessage(pp::Var(data));
}
class MyModule : public pp::Module {
public:
MyModule() : pp::Module() {}
virtual ~MyModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new MyInstance(instance);
}
};
namespace pp {
Module* CreateModule() {
return new MyModule();
}
}