This source file includes following definitions.
- CleanUpStubs
- AddNamedObject
- RemoveNamedObject
- RenderFrameDeleted
- OnGetChannelHandle
- Send
- GetChannelHandle
- CreateNPVariantParam
- CreateObjectStub
#include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h"
#include "base/android/java_handler_thread.h"
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "content/browser/renderer_host/java/java_bridge_channel_host.h"
#include "content/child/child_process.h"
#include "content/child/npapi/npobject_stub.h"
#include "content/child/npapi/npobject_util.h"
#include "content/common/java_bridge_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "third_party/WebKit/public/web/WebBindings.h"
#if !defined(OS_ANDROID)
#error "JavaBridge only supports OS_ANDROID"
#endif
namespace content {
namespace {
class JavaBridgeThread : public base::android::JavaHandlerThread {
public:
JavaBridgeThread() : base::android::JavaHandlerThread("JavaBridge") {
Start();
}
virtual ~JavaBridgeThread() {
Stop();
}
};
void CleanUpStubs(const std::vector<base::WeakPtr<NPObjectStub> > & stubs) {
for (size_t i = 0; i < stubs.size(); ++i) {
if (stubs[i]) {
stubs[i]->DeleteSoon();
}
}
}
base::LazyInstance<JavaBridgeThread> g_background_thread =
LAZY_INSTANCE_INITIALIZER;
}
JavaBridgeDispatcherHost::JavaBridgeDispatcherHost(
RenderFrameHost* render_frame_host)
: render_frame_host_(render_frame_host) {
}
JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() {
g_background_thread.Get().message_loop()->PostTask(
FROM_HERE,
base::Bind(&CleanUpStubs, stubs_));
}
void JavaBridgeDispatcherHost::AddNamedObject(const base::string16& name,
NPObject* object) {
NPVariant_Param variant_param;
CreateNPVariantParam(object, &variant_param);
Send(new JavaBridgeMsg_AddNamedObject(
render_frame_host_->GetRoutingID(), name, variant_param));
}
void JavaBridgeDispatcherHost::RemoveNamedObject(const base::string16& name) {
Send(new JavaBridgeMsg_RemoveNamedObject(
render_frame_host_->GetRoutingID(), name));
}
void JavaBridgeDispatcherHost::RenderFrameDeleted() {
render_frame_host_ = NULL;
}
void JavaBridgeDispatcherHost::OnGetChannelHandle(IPC::Message* reply_msg) {
g_background_thread.Get().message_loop()->PostTask(
FROM_HERE,
base::Bind(&JavaBridgeDispatcherHost::GetChannelHandle, this, reply_msg));
}
void JavaBridgeDispatcherHost::Send(IPC::Message* msg) {
if (render_frame_host_) {
render_frame_host_->Send(msg);
return;
}
delete msg;
}
void JavaBridgeDispatcherHost::GetChannelHandle(IPC::Message* reply_msg) {
JavaBridgeHostMsg_GetChannelHandle::WriteReplyParams(
reply_msg,
channel_->channel_handle());
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&JavaBridgeDispatcherHost::Send, this, reply_msg));
}
void JavaBridgeDispatcherHost::CreateNPVariantParam(NPObject* object,
NPVariant_Param* param) {
param->type = NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID;
int route_id = JavaBridgeChannelHost::ThreadsafeGenerateRouteID();
param->npobject_routing_id = route_id;
blink::WebBindings::retainObject(object);
g_background_thread.Get().message_loop()->PostTask(
FROM_HERE,
base::Bind(&JavaBridgeDispatcherHost::CreateObjectStub, this, object,
render_frame_host_->GetProcess()->GetID(), route_id));
}
void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object,
int render_process_id,
int route_id) {
DCHECK_EQ(g_background_thread.Get().message_loop(),
base::MessageLoop::current());
if (!channel_.get()) {
channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost(
render_process_id,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
}
stubs_.push_back((new NPObjectStub(
object, channel_.get(), route_id, 0, GURL()))->AsWeakPtr());
blink::WebBindings::releaseObject(object);
}
}