1 // Copyright (c) 2012 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 // This example demonstrates how to load content of the page into NaCl module.
9 #include "ppapi/cpp/instance.h"
10 #include "ppapi/cpp/module.h"
11 #include "ppapi/cpp/url_loader.h"
12 #include "ppapi/cpp/var.h"
14 #include "url_loader_handler.h"
16 // These are the method names as JavaScript sees them.
18 const char* const kLoadUrlMethodId
= "getUrl";
19 static const char kMessageArgumentSeparator
= ':';
22 class URLLoaderInstance
: public pp::Instance
{
24 explicit URLLoaderInstance(PP_Instance instance
) : pp::Instance(instance
) {}
25 virtual ~URLLoaderInstance() {}
27 // Called by the browser to handle the postMessage() call in Javascript.
28 // The message in this case is expected to contain the string 'getUrl'
29 // followed by a ':' separator, then the URL to fetch. If a valid message
30 // of the form 'getUrl:URL' is received, then start up an asynchronous
31 // download of URL. In the event that errors occur, this method posts an
32 // error string back to the browser.
33 virtual void HandleMessage(const pp::Var
& var_message
);
36 void URLLoaderInstance::HandleMessage(const pp::Var
& var_message
) {
37 if (!var_message
.is_string()) {
40 std::string message
= var_message
.AsString();
41 if (message
.find(kLoadUrlMethodId
) == 0) {
42 // The argument to getUrl is everything after the first ':'.
43 size_t sep_pos
= message
.find_first_of(kMessageArgumentSeparator
);
44 if (sep_pos
!= std::string::npos
) {
45 std::string url
= message
.substr(sep_pos
+ 1);
46 printf("URLLoaderInstance::HandleMessage('%s', '%s')\n",
50 URLLoaderHandler
* handler
= URLLoaderHandler::Create(this, url
);
51 if (handler
!= NULL
) {
52 // Starts asynchronous download. When download is finished or when an
53 // error occurs, |handler| posts the results back to the browser
54 // vis PostMessage and self-destroys.
61 class URLLoaderModule
: public pp::Module
{
63 URLLoaderModule() : pp::Module() {}
64 virtual ~URLLoaderModule() {}
66 // Create and return a URLLoaderInstance object.
67 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
68 return new URLLoaderInstance(instance
);
73 Module
* CreateModule() { return new URLLoaderModule(); }