| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 // This example demonstrates how to load content of the page into NaCl module. | |
| 6 | |
| 7 #include <cstdio> | |
| 8 #include <string> | |
| 9 #include "ppapi/cpp/instance.h" | |
| 10 #include "ppapi/cpp/url_loader.h" | |
| 11 #include "ppapi/cpp/module.h" | |
| 12 #include "ppapi/cpp/var.h" | |
| 13 | |
| 14 #include "geturl_handler.h" | |
| 15 | |
| 16 // These are the method names as JavaScript sees them. | |
| 17 namespace { | |
| 18 const char* const kLoadUrlMethodId = "getUrl"; | |
| 19 static const char kMessageArgumentSeparator = ':'; | |
| 20 | |
| 21 // Exception strings. These are passed back to the browser when errors | |
| 22 // happen during property accesses or method calls. | |
| 23 const char* const kExceptionStartFailed = "GetURLHandler::Start() failed"; | |
| 24 const char* const kExceptionURLNotAString = "URL is not a string"; | |
| 25 } // namespace | |
| 26 | |
| 27 // The Instance class. One of these exists for each instance of your NaCl | |
| 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: | |
| 35 explicit GetURLInstance(PP_Instance instance) : pp::Instance(instance) {} | |
| 36 virtual ~GetURLInstance() {} | |
| 37 | |
| 38 // Called by the browser to handle the postMessage() call in Javascript. | |
| 39 // 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 | |
| 41 // 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 | |
| 43 // error string back to the browser. | |
| 44 virtual void HandleMessage(const pp::Var& var_message); | |
| 45 }; | |
| 46 | |
| 47 void GetURLInstance::HandleMessage(const pp::Var& var_message) { | |
| 48 if (!var_message.is_string()) { | |
| 49 return; | |
| 50 } | |
| 51 std::string message = var_message.AsString(); | |
| 52 if (message.find(kLoadUrlMethodId) == 0) { | |
| 53 // The argument to getUrl is everything after the first ':'. | |
| 54 size_t sep_pos = message.find_first_of(kMessageArgumentSeparator); | |
| 55 if (sep_pos != std::string::npos) { | |
| 56 std::string url = message.substr(sep_pos + 1); | |
| 57 printf("GetURLInstance::HandleMessage('%s', '%s')\n", | |
| 58 message.c_str(), | |
| 59 url.c_str()); | |
| 60 fflush(stdout); | |
| 61 GetURLHandler* handler = GetURLHandler::Create(this, url); | |
| 62 if (handler != NULL) { | |
| 63 // Starts asynchronous download. When download is finished or when an | |
| 64 // error occurs, |handler| posts the results back to the browser | |
| 65 // vis PostMessage and self-destroys. | |
| 66 handler->Start(); | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 // The Module class. The browser calls the CreateInstance() method to create | |
| 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: | |
| 77 GetURLModule() : pp::Module() {} | |
| 78 virtual ~GetURLModule() {} | |
| 79 | |
| 80 // Create and return a GetURLInstance object. | |
| 81 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 82 return new GetURLInstance(instance); | |
| 83 } | |
| 84 }; | |
| 85 | |
| 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 { | |
| 92 Module* CreateModule() { return new GetURLModule(); } | |
| 93 } // namespace pp | |
| OLD | NEW |