Chromium Code Reviews| Index: examples/hello_world/hello_world.cc |
| =================================================================== |
| --- examples/hello_world/hello_world.cc (revision 858) |
| +++ examples/hello_world/hello_world.cc (working copy) |
| @@ -24,34 +24,22 @@ |
| #include <ppapi/cpp/dev/scriptable_object_deprecated.h> |
|
garianov
2011/05/18 15:45:19
Do we need this header?
David Springer
2011/05/18 18:01:42
Uh no. Gone.
|
| #include <ppapi/cpp/var.h> |
|
dmichael(do not use this one)
2011/05/18 18:46:10
Whoops, just noticed... if we're using "" for ppa
|
| #include <cstdio> |
| +#include <cstring> |
| #include <string> |
| #include <algorithm> // for reverse |
| #include "examples/hello_world/helper_functions.h" |
| -namespace { |
| -// Helper function to set the scripting exception. Both |exception| and |
| -// |except_string| can be NULL. If |exception| is NULL, this function does |
| -// nothing. |
| -void SetExceptionString(pp::Var* exception, const std::string& except_string) { |
| - if (exception) { |
| - *exception = except_string; |
| - } |
| -} |
| - |
| -// Exception strings. These are passed back to the browser when errors |
| -// happen during property accesses or method calls. |
| -const char* const kExceptionMethodNotAString = "Method name is not a string"; |
| -const char* const kExceptionNoMethodName = "No method named "; |
| -} // namespace |
| - |
| namespace hello_world { |
| -/// method name for ReverseText, as seen by JavaScript code. |
| +/// Method name for ReverseText, as seen by JavaScript code. |
| const char* const kReverseTextMethodId = "reverseText"; |
| -/// method name for FortyTwo, as seen by Javascript code. @see FortyTwo() |
| +/// Method name for FortyTwo, as seen by Javascript code. @see FortyTwo() |
| const char* const kFortyTwoMethodId = "fortyTwo"; |
| +/// Separator charater for the reverseText method. |
|
dmichael (off chromium)
2011/05/18 15:43:56
charater->character
David Springer
2011/05/18 18:01:42
Done.
|
| +static const char kMessageArgumentSeparator = ':'; |
| + |
| /// This is the module's function that invokes FortyTwo and converts the return |
| /// value from an int32_t to a pp::Var for return. |
| pp::Var MarshallFortyTwo() { |
| @@ -65,78 +53,10 @@ |
| /// On good input, it calls ReverseText and returns the result. The |
| /// ScriptableObject that called this function returns this string back to the |
| /// browser as a JavaScript value. |
| -pp::Var MarshallReverseText(const std::vector<pp::Var>& args) { |
| - // There should be exactly one arg, which should be an object |
| - if (args.size() != 1) { |
| - printf("Unexpected number of args\n"); |
| - return "Unexpected number of args"; |
| - } |
| - if (!args[0].is_string()) { |
| - printf("Arg %s is NOT a string\n", args[0].DebugString().c_str()); |
| - return "Arg from Javascript is not a string!"; |
| - } |
| - return pp::Var(ReverseText(args[0].AsString())); |
| +pp::Var MarshallReverseText(const std::string& text) { |
|
dmichael (off chromium)
2011/05/18 15:43:56
Did you update the unit test? For what it's worth
David Springer
2011/05/18 18:01:42
Oh - whoops. I spaced that. Done.
dmichael(do not use this one)
2011/05/18 18:46:10
Could you add it to the CL please? And... did th
|
| + return pp::Var(ReverseText(text)); |
| } |
| -/// This class exposes the scripting interface for this NaCl module. The |
| -/// HasMethod() method is called by the browser when executing a method call on |
| -/// the @a helloWorldModule object (see the reverseText() function in |
| -/// hello_world.html). The name of the JavaScript function (e.g. "fortyTwo") is |
| -/// passed in the @a method parameter as a string pp::Var. If HasMethod() |
| -/// returns @a true, then the browser will call the Call() method to actually |
| -/// invoke the method. |
| -class HelloWorldScriptableObject : public pp::deprecated::ScriptableObject { |
| - public: |
| - /// Determines whether a given method is implemented in this object. |
| - /// @param[in] method A JavaScript string containing the method name to check |
| - /// @param exception Unused |
| - /// @return @a true if @a method is one of the exposed method names. |
| - virtual bool HasMethod(const pp::Var& method, pp::Var* exception); |
| - |
| - /// Invoke the function associated with @a method. The argument list passed |
| - /// via JavaScript is marshaled into a vector of pp::Vars. None of the |
| - /// functions in this example take arguments, so this vector is always empty. |
| - /// @param[in] method A JavaScript string with the name of the method to call |
| - /// @param[in] args A list of the JavaScript parameters passed to this method |
| - /// @param exception unused |
| - /// @return the return value of the invoked method |
| - virtual pp::Var Call(const pp::Var& method, |
| - const std::vector<pp::Var>& args, |
| - pp::Var* exception); |
| -}; |
| - |
| -bool HelloWorldScriptableObject::HasMethod(const pp::Var& method, |
| - pp::Var* exception) { |
| - if (!method.is_string()) { |
| - SetExceptionString(exception, kExceptionMethodNotAString); |
| - return false; |
| - } |
| - std::string method_name = method.AsString(); |
| - return method_name == kReverseTextMethodId || |
| - method_name == kFortyTwoMethodId; |
| -} |
| - |
| -pp::Var HelloWorldScriptableObject::Call(const pp::Var& method, |
| - const std::vector<pp::Var>& args, |
| - pp::Var* exception) { |
| - if (!method.is_string()) { |
| - SetExceptionString(exception, kExceptionMethodNotAString); |
| - return pp::Var(); |
| - } |
| - std::string method_name = method.AsString(); |
| - if (method_name == kReverseTextMethodId) { |
| - // note that the vector of pp::Var |args| is passed to ReverseText |
| - return MarshallReverseText(args); |
| - } else if (method_name == kFortyTwoMethodId) { |
| - // note that no arguments are passed in to FortyTwo. |
| - return MarshallFortyTwo(); |
| - } else { |
| - SetExceptionString(exception, |
| - std::string(kExceptionNoMethodName) + method_name); |
| - } |
| - return pp::Var(); |
| -} |
| - |
| /// 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 |
| @@ -154,15 +74,40 @@ |
| explicit HelloWorldInstance(PP_Instance instance) : pp::Instance(instance) {} |
| virtual ~HelloWorldInstance() {} |
| - /// @return a new pp::deprecated::ScriptableObject as a JavaScript @a Var |
| - /// @note The pp::Var takes over ownership of the HelloWorldScriptableObject |
| - /// and is responsible for deallocating memory. |
| - virtual pp::Var GetInstanceObject() { |
| - HelloWorldScriptableObject* hw_object = new HelloWorldScriptableObject(); |
| - return pp::Var(this, hw_object); |
| - } |
| + /// Called by the browser to handle the postMessage() call in Javascript. |
| + /// Detects which method is being called from the message contents, and |
| + /// calls the appropriate function. Posts the resutl back to the browser |
|
dmichael (off chromium)
2011/05/18 15:43:56
resutl->result
David Springer
2011/05/18 18:01:42
Done.
|
| + /// asynchronously. |
| + /// @param[in] var_message The message posted by the browser. |
|
garianov
2011/05/18 15:45:19
It would be nice to provide example of the possibl
David Springer
2011/05/18 18:01:42
Done.
|
| + virtual void HandleMessage(const pp::Var& var_message); |
| }; |
| +void HelloWorldInstance::HandleMessage(const pp::Var& var_message) { |
| + if (!var_message.is_string()) { |
| + return; |
| + } |
| + std::string message = var_message.AsString(); |
| + pp::Var return_var; |
| + if (message == kFortyTwoMethodId) { |
| + // Note that no arguments are passed in to FortyTwo. |
| + return_var = MarshallFortyTwo(); |
| + } else if (message.find(kReverseTextMethodId, |
| + 0, |
| + strlen(kReverseTextMethodId)) != std::string::npos) { |
|
dmichael (off chromium)
2011/05/18 15:43:56
You don't really need the latter 2 arguments, sinc
David Springer
2011/05/18 18:01:42
Done.
|
| + // The argument to reverseText is everything after the first ':'. |
| + size_t sep_pos = message.find_first_of(kMessageArgumentSeparator); |
| + if (sep_pos != std::string::npos) { |
| + std::string string_arg = message.substr(sep_pos + 1); |
| + return_var = MarshallReverseText(string_arg); |
| + } |
| + } |
| + // Post the return result back to the browser. Note that HandleMessage() is |
| + // always called on the main thread, so it's OK to post the return message |
| + // directly from here. The return post is asynhronous: PostMessage returns |
| + // immediately. |
| + PostMessage(return_var); |
| +} |
| + |
| /// 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 |