This source file includes following definitions.
- EnsureChannelIsSetUp
- OnMessageReceived
- DidClearWindowObject
- OnAddNamedObject
- OnRemoveNamedObject
#include "content/renderer/java/java_bridge_dispatcher.h"
#include "content/child/child_process.h"
#include "content/child/npapi/npobject_util.h"
#include "content/common/java_bridge_messages.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/renderer/java/java_bridge_channel.h"
#include "third_party/WebKit/public/web/WebBindings.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebView.h"
namespace content {
JavaBridgeDispatcher::JavaBridgeDispatcher(RenderFrame* render_frame)
: RenderFrameObserver(render_frame) {
}
void JavaBridgeDispatcher::EnsureChannelIsSetUp() {
if (channel_.get()) {
return;
}
IPC::ChannelHandle channel_handle;
Send(new JavaBridgeHostMsg_GetChannelHandle(routing_id(), &channel_handle));
channel_ = JavaBridgeChannel::GetJavaBridgeChannel(
channel_handle, ChildProcess::current()->io_message_loop_proxy());
}
JavaBridgeDispatcher::~JavaBridgeDispatcher() {
for (ObjectMap::const_iterator iter = objects_.begin();
iter != objects_.end(); ++iter) {
blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
}
}
bool JavaBridgeDispatcher::OnMessageReceived(const IPC::Message& msg) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcher, msg)
IPC_MESSAGE_HANDLER(JavaBridgeMsg_AddNamedObject, OnAddNamedObject)
IPC_MESSAGE_HANDLER(JavaBridgeMsg_RemoveNamedObject,
OnRemoveNamedObject)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void JavaBridgeDispatcher::DidClearWindowObject(int world_id) {
for (ObjectMap::const_iterator iter = objects_.begin();
iter != objects_.end();
++iter) {
render_frame()->GetWebFrame()->bindToWindowObject(
iter->first, NPVARIANT_TO_OBJECT(iter->second));
}
}
void JavaBridgeDispatcher::OnAddNamedObject(
const base::string16& name,
const NPVariant_Param& variant_param) {
DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID);
EnsureChannelIsSetUp();
if (!channel_.get()) {
return;
}
NPVariant variant;
bool created =
CreateNPVariant(variant_param, channel_.get(), &variant, 0, GURL());
DCHECK(created);
DCHECK_EQ(variant.type, NPVariantType_Object);
ObjectMap::iterator iter = objects_.find(name);
if (iter != objects_.end()) {
blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
}
objects_[name] = variant;
}
void JavaBridgeDispatcher::OnRemoveNamedObject(const base::string16& name) {
if (!channel_.get()) {
DCHECK(objects_.empty());
return;
}
ObjectMap::iterator iter = objects_.find(name);
DCHECK(iter != objects_.end());
blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
objects_.erase(iter);
}
}