Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: native_client_sdk/src/examples/api/url_loader/url_loader.cc

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

Powered by Google App Engine
This is Rietveld 408576698