blob: fa3838da8af17855f047a75a19d9fdb9a96fc455 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This example demonstrates how to load content of the page into NaCl module.
#include <cstdio>
#include <string>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/url_loader.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
#include "geturl_handler.h"
// These are the method names as JavaScript sees them.
namespace {
const char* const kLoadUrlMethodId = "getUrl";
static const char kMessageArgumentSeparator = ':';
// Exception strings. These are passed back to the browser when errors
// happen during property accesses or method calls.
const char* const kExceptionStartFailed = "GetURLHandler::Start() failed";
const char* const kExceptionURLNotAString = "URL is not a string";
} // namespace
// The Instance class. One of these exists for each instance of your NaCl
// module on the web page. The browser will ask the Module object to create
// a new Instance for each occurrence of the <embed> tag that has these
// attributes:
// type="application/x-nacl"
// src="geturl.nmf"
class GetURLInstance : public pp::Instance {
public:
explicit GetURLInstance(PP_Instance instance) : pp::Instance(instance) {}
virtual ~GetURLInstance() {}
// Called by the browser to handle the postMessage() call in Javascript.
// The message in this case is expected to contain the string 'getUrl'
// followed by a ':' separator, then the URL to fetch. If a valid message
// of the form 'getUrl:URL' is received, then start up an asynchronous
// download of URL. In the event that errors occur, this method posts an
// error string back to the browser.
virtual void HandleMessage(const pp::Var& var_message);
};
void GetURLInstance::HandleMessage(const pp::Var& var_message) {
if (!var_message.is_string()) {
return;
}
std::string message = var_message.AsString();
if (message.find(kLoadUrlMethodId) == 0) {
// The argument to getUrl is everything after the first ':'.
size_t sep_pos = message.find_first_of(kMessageArgumentSeparator);
if (sep_pos != std::string::npos) {
std::string url = message.substr(sep_pos + 1);
printf("GetURLInstance::HandleMessage('%s', '%s')\n",
message.c_str(),
url.c_str());
fflush(stdout);
GetURLHandler* handler = GetURLHandler::Create(this, url);
if (handler != NULL) {
// Starts asynchronous download. When download is finished or when an
// error occurs, |handler| posts the results back to the browser
// vis PostMessage and self-destroys.
handler->Start();
}
}
}
}
// The Module class. The browser calls the CreateInstance() method to create
// an instance of you NaCl module on the web page. The browser creates a new
// instance for each <embed> tag with type="application/x-nacl".
class GetURLModule : public pp::Module {
public:
GetURLModule() : pp::Module() {}
virtual ~GetURLModule() {}
// Create and return a GetURLInstance object.
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new GetURLInstance(instance);
}
};
// Factory function called by the browser when the module is first loaded.
// The browser keeps a singleton of this module. It calls the
// CreateInstance() method on the object you return to make instances. There
// is one instance per <embed> tag on the page. This is the main binding
// point for your NaCl module with the browser.
namespace pp {
Module* CreateModule() {
return new GetURLModule();
}
} // namespace pp
|