Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / content / renderer / java / java_bridge_dispatcher.cc
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_dispatcher.h"
6
7 #include "content/child/child_process.h"
8 #include "content/child/npapi/npobject_util.h"  // For CreateNPVariant()
9 #include "content/common/java_bridge_messages.h"
10 #include "content/public/renderer/render_frame.h"
11 #include "content/public/renderer/render_thread.h"
12 #include "content/renderer/java/java_bridge_channel.h"
13 #include "third_party/WebKit/public/web/WebBindings.h"
14 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebView.h"
17
18 namespace content {
19
20 JavaBridgeDispatcher::JavaBridgeDispatcher(RenderFrame* render_frame)
21     : RenderFrameObserver(render_frame) {
22 }
23
24 void JavaBridgeDispatcher::EnsureChannelIsSetUp() {
25   if (channel_.get()) {
26     return;
27   }
28
29   IPC::ChannelHandle channel_handle;
30   Send(new JavaBridgeHostMsg_GetChannelHandle(routing_id(), &channel_handle));
31
32   channel_ = JavaBridgeChannel::GetJavaBridgeChannel(
33       channel_handle, ChildProcess::current()->io_message_loop_proxy());
34 }
35
36 JavaBridgeDispatcher::~JavaBridgeDispatcher() {
37   for (ObjectMap::const_iterator iter = objects_.begin();
38       iter != objects_.end(); ++iter) {
39     blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
40   }
41 }
42
43 bool JavaBridgeDispatcher::OnMessageReceived(const IPC::Message& msg) {
44   bool handled = true;
45   IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcher, msg)
46     IPC_MESSAGE_HANDLER(JavaBridgeMsg_AddNamedObject, OnAddNamedObject)
47     IPC_MESSAGE_HANDLER(JavaBridgeMsg_RemoveNamedObject,
48                         OnRemoveNamedObject)
49     IPC_MESSAGE_UNHANDLED(handled = false)
50   IPC_END_MESSAGE_MAP()
51   return handled;
52 }
53
54 void JavaBridgeDispatcher::DidClearWindowObject() {
55   // Note that we have to (re)bind all objects, as they will have been unbound
56   // when the window object was cleared.
57   for (ObjectMap::const_iterator iter = objects_.begin();
58        iter != objects_.end();
59        ++iter) {
60     NPObject* object = NPVARIANT_TO_OBJECT(iter->second);
61     // De-associate from the existing V8 wrapper, so we don't pull any
62     // of the wrapper's custom properties into the context of the page we
63     // have navigated to.
64     blink::WebBindings::dropV8WrapperForObject(object);
65     // This refs the NPObject. This reference is dropped when either the window
66     // object is later cleared, or the object is GC'ed. So the object may be
67     // deleted at any time after OnRemoveNamedObject() is called.
68     render_frame()->GetWebFrame()->bindToWindowObject(iter->first, object);
69   }
70 }
71
72 void JavaBridgeDispatcher::OnAddNamedObject(
73     const base::string16& name,
74     const NPVariant_Param& variant_param) {
75   DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID);
76
77   EnsureChannelIsSetUp();
78   if (!channel_.get()) {
79     // It's possible for |channel_| to be NULL if the RenderView is going away.
80     return;
81   }
82
83   // This creates an NPObject, wrapped as an NPVariant. Pass 0 for the for
84   // containing window, as this is only used by plugins to pump the window
85   // message queue when a method on a renderer-side object causes a dialog to
86   // be displayed, and the Java Bridge does not need this functionality. The
87   // page URL is also not required.
88   NPVariant variant;
89   bool created =
90       CreateNPVariant(variant_param, channel_.get(), &variant, 0, GURL());
91   DCHECK(created);
92   DCHECK_EQ(variant.type, NPVariantType_Object);
93
94   // The NPObject is created with a ref count of one, which we remove when
95   // OnRemoveNamedObject() is called for that object.
96   ObjectMap::iterator iter = objects_.find(name);
97   if (iter != objects_.end()) {
98     blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
99   }
100   objects_[name] = variant;
101 }
102
103 void JavaBridgeDispatcher::OnRemoveNamedObject(const base::string16& name) {
104   if (!channel_.get()) {
105     DCHECK(objects_.empty());
106     return;
107   }
108
109   // Removing an object does not unbind it from JavaScript until the window
110   // object is next cleared. Note that the browser checks that the named object
111   // is present.
112   ObjectMap::iterator iter = objects_.find(name);
113   DCHECK(iter != objects_.end());
114   blink::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second));
115   objects_.erase(iter);
116 }
117
118 }  // namespace content