#include "method_dispatcher.h"
MethodDispatcher::MethodDispatcher(Application* app) : app_(app) {
}
void MethodDispatcher::Bind(JSObject& object,
const WebString& name,
ObjectMethodDelegate callback) {
// We can't bind methods to local JSObjects
if (object.type() == kJSObjectType_Local)
return;
object.SetCustomMethod(name, false);
ObjectMethodKey key(object.remote_id(), name);
bound_methods_[key] = callback;
}
void MethodDispatcher::BindWithRetval(JSObject& object,
const WebString& name,
ObjectMethodWithRetvalDelegate callback) {
// We can't bind methods to local JSObjects
if (object.type() == kJSObjectType_Local)
return;
object.SetCustomMethod(name, true);
ObjectMethodKey key(object.remote_id(), name);
bound_methods_with_retval_[key] = callback;
}
void MethodDispatcher::OnMethodCall(Awesomium::WebView* caller,
unsigned int remote_object_id,
const Awesomium::WebString& method_name,
const Awesomium::JSArray& args) {
// Find the method that matches the object id + method name
auto i = bound_methods_.find(ObjectMethodKey(remote_object_id, method_name));
// Call the method
if (i != bound_methods_.end())
i->second(app_, caller, args);
}
JSValue MethodDispatcher::OnMethodCallWithReturnValue(Awesomium::WebView* caller,
unsigned int remote_object_id,
const Awesomium::WebString& method_name,
const Awesomium::JSArray& args) {
// Find the method that matches the object id + method name
auto i = bound_methods_with_retval_.find(
ObjectMethodKey(remote_object_id, method_name));
// Call the method
if (i != bound_methods_with_retval_.end())
return i->second(app_, caller, args);
return JSValue::Undefined();
}