summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/java_bridge_dispatcher_host.cc
blob: 2cc64865a77dae38e081ea18121d33dd5c7a1a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/java_bridge_dispatcher_host.h"

#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/java_bridge_channel_host.h"
#include "content/browser/renderer_host/browser_render_process_host.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/common/child_process.h"
#include "content/common/java_bridge_messages.h"
#include "content/common/npobject_stub.h"
#include "content/common/npobject_util.h"  // For CreateNPVariantParam()
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"

JavaBridgeDispatcherHost::JavaBridgeDispatcherHost(
    RenderViewHost* render_view_host)
    : render_view_host_(render_view_host) {
  DCHECK(render_view_host_);
}

JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() {
}

void JavaBridgeDispatcherHost::AddNamedObject(const string16& name,
                                              NPObject* object) {
  // Post to the WEBKIT thread, as that's where we want injected objects to
  // live.
  WebKit::WebBindings::retainObject(object);
  BrowserThread::PostTask(
      BrowserThread::WEBKIT,
      FROM_HERE,
      NewRunnableMethod(this,
                        &JavaBridgeDispatcherHost::DoAddNamedObject,
                        name,
                        object));
}

void JavaBridgeDispatcherHost::RemoveNamedObject(const string16& name) {
  // On receipt of this message, the JavaBridgeDispatcher will drop its
  // reference to the corresponding proxy object. When the last reference is
  // removed, the proxy object will delete its NPObjectProxy, which will cause
  // the NPObjectStub to be deleted, which will drop its reference to the
  // original NPObject.
  render_view_host_->Send(new JavaBridgeMsg_RemoveNamedObject(
      render_view_host_->routing_id(), name));
}

void JavaBridgeDispatcherHost::DoAddNamedObject(const string16& name,
                                                NPObject* object) {
  EnsureChannelIsSetUp();

  NPVariant variant;
  OBJECT_TO_NPVARIANT(object, variant);
  DCHECK_EQ(variant.type, NPVariantType_Object);

  // Create an NPVariantParam suitable for serialization over IPC from our
  // NPVariant. Internally, this will create an NPObjectStub, which takes a
  // reference to the underlying NPObject. We pass release = true to release
  // the ref added in AddNamedObject(). We don't need the containing window or
  // the page URL, as we don't do re-entrant sync IPC.
  NPVariant_Param variant_param;
  CreateNPVariantParam(variant, channel_, &variant_param, true, 0, GURL());
  DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID);

  render_view_host_->Send(new JavaBridgeMsg_AddNamedObject(
      render_view_host_->routing_id(), name, variant_param));
}

void JavaBridgeDispatcherHost::EnsureChannelIsSetUp() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT));
  if (channel_) {
    return;
  }

  channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost(
      render_view_host_->process()->id(),
      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));

  // The channel creates the channel handle based on the renderer ID we passed
  // to GetJavaBridgeChannelHost() and, on POSIX, the file descriptor used by
  // the underlying channel.
  IPC::ChannelHandle channel_handle = channel_->channel_handle();

  bool sent = render_view_host_->Send(new JavaBridgeMsg_Init(
      render_view_host_->routing_id(), channel_handle));
  DCHECK(sent);
}