Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 13th, 2012  |  syntax: C++  |  size: 1.87 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include "method_dispatcher.h"
  2.  
  3. MethodDispatcher::MethodDispatcher(Application* app) : app_(app) {
  4. }
  5.  
  6. void MethodDispatcher::Bind(JSObject& object,
  7.   const WebString& name,
  8.   ObjectMethodDelegate callback) {
  9.   // We can't bind methods to local JSObjects
  10.   if (object.type() == kJSObjectType_Local)
  11.     return;
  12.  
  13.   object.SetCustomMethod(name, false);
  14.  
  15.   ObjectMethodKey key(object.remote_id(), name);
  16.   bound_methods_[key] = callback;
  17. }
  18.  
  19. void MethodDispatcher::BindWithRetval(JSObject& object,
  20.   const WebString& name,
  21.   ObjectMethodWithRetvalDelegate callback) {
  22.   // We can't bind methods to local JSObjects
  23.   if (object.type() == kJSObjectType_Local)
  24.     return;
  25.  
  26.   object.SetCustomMethod(name, true);
  27.  
  28.   ObjectMethodKey key(object.remote_id(), name);
  29.   bound_methods_with_retval_[key] = callback;
  30. }
  31.  
  32. void MethodDispatcher::OnMethodCall(Awesomium::WebView* caller,
  33.                   unsigned int remote_object_id,
  34.                   const Awesomium::WebString& method_name,
  35.                   const Awesomium::JSArray& args) {
  36.   // Find the method that matches the object id + method name
  37.   auto i = bound_methods_.find(ObjectMethodKey(remote_object_id, method_name));
  38.  
  39.   // Call the method
  40.   if (i != bound_methods_.end())
  41.     i->second(app_, caller, args);
  42. }
  43.  
  44. JSValue MethodDispatcher::OnMethodCallWithReturnValue(Awesomium::WebView* caller,
  45.                                     unsigned int remote_object_id,
  46.                                     const Awesomium::WebString& method_name,
  47.                                     const Awesomium::JSArray& args) {
  48.   // Find the method that matches the object id + method name
  49.   auto i = bound_methods_with_retval_.find(
  50.     ObjectMethodKey(remote_object_id, method_name));
  51.  
  52.   // Call the method
  53.   if (i != bound_methods_with_retval_.end())
  54.     return i->second(app_, caller, args);
  55.  
  56.   return JSValue::Undefined();
  57. }