1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/renderer/java/java_bridge_channel.h"
6
7#include "content/child/child_process.h"
8#include "content/child/plugin_messages.h"
9#include "content/common/java_bridge_messages.h"
10#include "third_party/WebKit/public/web/WebBindings.h"
11
12namespace content {
13
14JavaBridgeChannel* JavaBridgeChannel::GetJavaBridgeChannel(
15    const IPC::ChannelHandle& channel_handle,
16    base::MessageLoopProxy* ipc_message_loop) {
17  return static_cast<JavaBridgeChannel*>(NPChannelBase::GetChannel(
18      channel_handle,
19      IPC::Channel::MODE_CLIENT,
20      ClassFactory,
21      ipc_message_loop,
22      true,
23      ChildProcess::current()->GetShutDownEvent()));
24}
25
26JavaBridgeChannel::JavaBridgeChannel()
27    : peer_owner_id_(new struct _NPP) {
28  // Register the dummy owner Id for our peer (the Browser process) as an object
29  // owner, and have all objects received from the peer owned by it.
30  blink::WebBindings::registerObjectOwner(peer_owner_id_.get());
31  SetDefaultNPObjectOwner(peer_owner_id_.get());
32}
33
34JavaBridgeChannel::~JavaBridgeChannel() {
35  blink::WebBindings::unregisterObjectOwner(peer_owner_id_.get());
36}
37
38int JavaBridgeChannel::GenerateRouteID() {
39  // Use a control message as this going to the JavaBridgeChannelHost, not an
40  // injected object.
41  int route_id = MSG_ROUTING_NONE;
42  Send(new JavaBridgeMsg_GenerateRouteID(&route_id));
43  // This should never fail, as the JavaBridgeChannelHost should always outlive
44  // us.
45  DCHECK_NE(MSG_ROUTING_NONE, route_id);
46  return route_id;
47}
48
49bool JavaBridgeChannel::OnControlMessageReceived(const IPC::Message& msg) {
50  // We need to intercept these two message types because the default
51  // implementation of NPChannelBase::OnControlMessageReceived() is to
52  // DCHECK(false). However, we don't need to do anything, as we don't need to
53  // worry about the window system hanging when a modal dialog is displayed.
54  // This is because, unlike in the case of plugins, the host does not need to
55  // pump the message queue to avoid hangs.
56  if (msg.type() == PluginMsg_SignalModalDialogEvent::ID ||
57      msg.type() == PluginMsg_ResetModalDialogEvent::ID) {
58    return true;
59  }
60  return NPChannelBase::OnControlMessageReceived(msg);
61}
62
63}  // namespace content
64