diff options
-rw-r--r-- | content/common/content_message_generator.h | 1 | ||||
-rw-r--r-- | content/common/java_bridge_messages.h | 28 | ||||
-rw-r--r-- | content/content_common.gypi | 1 | ||||
-rw-r--r-- | content/content_renderer.gypi | 2 | ||||
-rw-r--r-- | content/renderer/java_bridge_dispatcher.cc | 90 | ||||
-rw-r--r-- | content/renderer/java_bridge_dispatcher.h | 43 | ||||
-rw-r--r-- | content/renderer/render_view_impl.cc | 9 | ||||
-rw-r--r-- | content/renderer/render_view_impl.h | 6 | ||||
-rw-r--r-- | ipc/ipc_message_utils.h | 1 |
9 files changed, 181 insertions, 0 deletions
diff --git a/content/common/content_message_generator.h b/content/common/content_message_generator.h index 842d42f..2f8fdfc 100644 --- a/content/common/content_message_generator.h +++ b/content/common/content_message_generator.h @@ -20,6 +20,7 @@ #include "content/common/gpu/gpu_messages.h" #include "content/common/indexed_db_messages.h" #include "content/common/intents_messages.h" +#include "content/common/java_bridge_messages.h" #include "content/common/media/audio_messages.h" #include "content/common/media/media_stream_messages.h" #include "content/common/media/video_capture_messages.h" diff --git a/content/common/java_bridge_messages.h b/content/common/java_bridge_messages.h new file mode 100644 index 0000000..8dd2353 --- /dev/null +++ b/content/common/java_bridge_messages.h @@ -0,0 +1,28 @@ +// 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. + +// IPC messages for injected Java objects. See JavaBridgeDispatcher for details. + +// Multiply-included message file, hence no include guard. + +#include "content/common/webkit_param_traits.h" // For NPVariant_Param +#include "ipc/ipc_channel_handle.h" +#include "ipc/ipc_message_macros.h" + +#define IPC_MESSAGE_START JavaBridgeMsgStart + +// Messages for handling Java objects injected into JavaScript ----------------- + +// Sent from browser to renderer to initialize the Java Bridge. +IPC_MESSAGE_ROUTED1(JavaBridgeMsg_Init, + IPC::ChannelHandle) /* channel handle */ + +// Sent from browser to renderer to add a Java object with the given name. +IPC_MESSAGE_ROUTED2(JavaBridgeMsg_AddNamedObject, + string16 /* name */, + NPVariant_Param) /* object */ + +// Sent from browser to renderer to remove a Java object with the given name. +IPC_MESSAGE_ROUTED1(JavaBridgeMsg_RemoveNamedObject, + string16 /* name */) diff --git a/content/content_common.gypi b/content/content_common.gypi index cf4eadd9..0803eb9 100644 --- a/content/content_common.gypi +++ b/content/content_common.gypi @@ -141,6 +141,7 @@ 'common/indexed_db_param_traits.cc', 'common/indexed_db_param_traits.h', 'common/intents_messages.h', + 'common/java_bridge_messages.h', 'common/mac/attributed_string_coder.h', 'common/mac/attributed_string_coder.mm', 'common/mac/font_descriptor.h', diff --git a/content/content_renderer.gypi b/content/content_renderer.gypi index 188f5fc..2a84101 100644 --- a/content/content_renderer.gypi +++ b/content/content_renderer.gypi @@ -72,6 +72,8 @@ 'renderer/intents_dispatcher.h', 'renderer/java_bridge_channel.cc', 'renderer/java_bridge_channel.h', + 'renderer/java_bridge_dispatcher.cc', + 'renderer/java_bridge_dispatcher.h', 'renderer/load_progress_tracker.cc', 'renderer/load_progress_tracker.h', 'renderer/media/audio_device.cc', diff --git a/content/renderer/java_bridge_dispatcher.cc b/content/renderer/java_bridge_dispatcher.cc new file mode 100644 index 0000000..2c9c2f1 --- /dev/null +++ b/content/renderer/java_bridge_dispatcher.cc @@ -0,0 +1,90 @@ +// 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/renderer/java_bridge_dispatcher.h" + +#include "content/common/child_process.h" +#include "content/common/java_bridge_messages.h" +#include "content/common/npobject_util.h" // For CreateNPVariant() +#include "content/public/renderer/render_thread.h" +#include "content/public/renderer/render_view.h" +#include "content/renderer/java_bridge_channel.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" + +JavaBridgeDispatcher::JavaBridgeDispatcher( + content::RenderView* render_view, + const IPC::ChannelHandle& channel_handle) + : RenderViewObserver(render_view) { + channel_.reset(JavaBridgeChannel::GetJavaBridgeChannel( + channel_handle, ChildProcess::current()->io_message_loop_proxy())); +} + +JavaBridgeDispatcher::~JavaBridgeDispatcher() { + for (ObjectMap::const_iterator iter = objects_.begin(); + iter != objects_.end(); ++iter) { + WebKit::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(WebKit::WebFrame* web_frame) { + // We only inject objects into the main frame. + if (web_frame != render_view()->GetWebView()->mainFrame()) + return; + + // Note that we have to (re)bind all objects, as they will have been unbound + // when the window object was cleared. + for (ObjectMap::const_iterator iter = objects_.begin(); + iter != objects_.end(); ++iter) { + // This refs the NPObject. This reference is dropped when either the window + // object is later cleared, or the object is GC'ed. So the object may be + // deleted at any time after OnRemoveNamedObject() is called. + web_frame->bindToWindowObject(iter->first, + NPVARIANT_TO_OBJECT(iter->second)); + } +} + +void JavaBridgeDispatcher::OnAddNamedObject( + const string16& name, + const NPVariant_Param& variant_param) { + DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID); + // This creates an NPObject, wrapped as an NPVariant. We don't need the + // containing window or the page URL, as we don't do re-entrant sync IPC. + NPVariant variant; + bool created = + CreateNPVariant(variant_param, channel_.get(), &variant, NULL, GURL()); + DCHECK(created); + DCHECK_EQ(variant.type, NPVariantType_Object); + + // The NPObject is created with a ref count of one, which we remove when + // OnRemoveNamedObject() is called for that object. + ObjectMap::iterator iter = objects_.find(name); + if (iter != objects_.end()) { + WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); + } + objects_[name] = variant; +} + +void JavaBridgeDispatcher::OnRemoveNamedObject(const string16& name) { + // Removing an object does not unbind it from JavaScript until the window + // object is next cleared. Note that the browser checks that the named object + // is present. + ObjectMap::iterator iter = objects_.find(name); + DCHECK(iter != objects_.end()); + WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); + objects_.erase(iter); +} diff --git a/content/renderer/java_bridge_dispatcher.h b/content/renderer/java_bridge_dispatcher.h new file mode 100644 index 0000000..bc73f0c --- /dev/null +++ b/content/renderer/java_bridge_dispatcher.h @@ -0,0 +1,43 @@ +// 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. + +#ifndef CONTENT_RENDERER_JAVA_BRIDGE_DISPATCHER_H_ +#define CONTENT_RENDERER_JAVA_BRIDGE_DISPATCHER_H_ + +#include "content/common/webkit_param_traits.h" // For NPVariant_Param +#include "content/public/renderer/render_view_observer.h" +#include "ipc/ipc_channel_handle.h" + +class JavaBridgeChannel; + +// This class handles injecting Java objects into the main frame of a +// RenderView. The 'add' and 'remove' messages received from the browser +// process modify the entries in a map of 'pending' objects. These objects are +// bound to the window object of the main frame when that window object is next +// cleared. These objects remain bound until the window object is cleared +// again. +class JavaBridgeDispatcher : public content::RenderViewObserver { + public: + JavaBridgeDispatcher(content::RenderView* render_view, + const IPC::ChannelHandle& channel_handle); + virtual ~JavaBridgeDispatcher(); + + private: + // RenderViewObserver override: + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + virtual void DidClearWindowObject(WebKit::WebFrame* frame) OVERRIDE; + + // Message handlers + void OnAddNamedObject(const string16& name, + const NPVariant_Param& variant_param); + void OnRemoveNamedObject(const string16& name); + + // Objects that will be bound to the window when the window object is next + // cleared. We hold a ref to these. + typedef std::map<string16, NPVariant> ObjectMap; + ObjectMap objects_; + scoped_ptr<JavaBridgeChannel> channel_; +}; + +#endif // CONTENT_RENDERER_JAVA_BRIDGE_DISPATCHER_H_ diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc index 1c332a4..ac6f1aa 100644 --- a/content/renderer/render_view_impl.cc +++ b/content/renderer/render_view_impl.cc @@ -32,6 +32,7 @@ #include "content/common/file_system/file_system_dispatcher.h" #include "content/common/file_system/webfilesystem_callback_dispatcher.h" #include "content/common/intents_messages.h" +#include "content/common/java_bridge_messages.h" #include "content/common/pepper_messages.h" #include "content/common/pepper_plugin_registry.h" #include "content/common/quota_dispatcher.h" @@ -51,6 +52,7 @@ #include "content/renderer/geolocation_dispatcher.h" #include "content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h" #include "content/renderer/intents_dispatcher.h" +#include "content/renderer/java_bridge_dispatcher.h" #include "content/renderer/load_progress_tracker.h" #include "content/renderer/media/audio_message_filter.h" #include "content/renderer/media/audio_renderer_impl.h" @@ -669,6 +671,7 @@ bool RenderViewImpl::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(ViewMsg_EnableViewSourceMode, OnEnableViewSourceMode) IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK, OnLockMouseACK) IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost, OnMouseLockLost) + IPC_MESSAGE_HANDLER(JavaBridgeMsg_Init, OnJavaBridgeInit) // Have the super handle all other messages. IPC_MESSAGE_UNHANDLED(handled = RenderWidget::OnMessageReceived(message)) @@ -4635,3 +4638,9 @@ void RenderViewImpl::OnMouseLockLost() { bool RenderViewImpl::WebWidgetHandlesCompositorScheduling() const { return webview()->settings()->useThreadedCompositor(); } + +void RenderViewImpl::OnJavaBridgeInit( + const IPC::ChannelHandle& channel_handle) { + DCHECK(!java_bridge_dispatcher_.get()); + java_bridge_dispatcher_.reset(new JavaBridgeDispatcher(this, channel_handle)); +} diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h index 09bdda0..d816f0b 100644 --- a/content/renderer/render_view_impl.h +++ b/content/renderer/render_view_impl.h @@ -61,6 +61,7 @@ class ExternalPopupMenu; class GeolocationDispatcher; class GURL; class IntentsDispatcher; +class JavaBridgeDispatcher; class LoadProgressTracker; class MediaStreamImpl; class NotificationProvider; @@ -862,6 +863,8 @@ class RenderViewImpl : public RenderWidget, void OnZoom(PageZoom::Function function); void OnEnableViewSourceMode(); + void OnJavaBridgeInit(const IPC::ChannelHandle& channel_handle); + // Adding a new message handler? Please add it in alphabetical order above // and put it in the same position in the .cc file. @@ -1154,6 +1157,9 @@ class RenderViewImpl : public RenderWidget, RendererAccessibility* renderer_accessibility_; + // Java Bridge dispatcher attached to this view; lazily initialized. + scoped_ptr<JavaBridgeDispatcher> java_bridge_dispatcher_; + // Misc ---------------------------------------------------------------------- // The current and pending file chooser completion objects. If the queue is diff --git a/ipc/ipc_message_utils.h b/ipc/ipc_message_utils.h index e151649..5ab768c 100644 --- a/ipc/ipc_message_utils.h +++ b/ipc/ipc_message_utils.h @@ -96,6 +96,7 @@ enum IPCMessageStart { ChromePluginMsgStart, ChromeBenchmarkingMsgStart, IntentsMsgStart, + JavaBridgeMsgStart, LastIPCMsgStart // Must come last. }; |