This source file includes following definitions.
- HandleMessage
- CreateInstance
- CreateModule
#include <cstdio>
#include <string>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/url_loader.h"
#include "ppapi/cpp/var.h"
#include "url_loader_handler.h"
namespace {
const char* const kLoadUrlMethodId = "getUrl";
static const char kMessageArgumentSeparator = ':';
}
class URLLoaderInstance : public pp::Instance {
public:
explicit URLLoaderInstance(PP_Instance instance) : pp::Instance(instance) {}
virtual ~URLLoaderInstance() {}
virtual void HandleMessage(const pp::Var& var_message);
};
void URLLoaderInstance::HandleMessage(const pp::Var& var_message) {
if (!var_message.is_string()) {
return;
}
std::string message = var_message.AsString();
if (message.find(kLoadUrlMethodId) == 0) {
size_t sep_pos = message.find_first_of(kMessageArgumentSeparator);
if (sep_pos != std::string::npos) {
std::string url = message.substr(sep_pos + 1);
printf("URLLoaderInstance::HandleMessage('%s', '%s')\n",
message.c_str(),
url.c_str());
fflush(stdout);
URLLoaderHandler* handler = URLLoaderHandler::Create(this, url);
if (handler != NULL) {
handler->Start();
}
}
}
}
class URLLoaderModule : public pp::Module {
public:
URLLoaderModule() : pp::Module() {}
virtual ~URLLoaderModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new URLLoaderInstance(instance);
}
};
namespace pp {
Module* CreateModule() { return new URLLoaderModule(); }
}