java_bridge_dispatcher_host_manager.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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#ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_ 6#define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_ 7 8#include <map> 9 10#include "base/android/jni_helper.h" 11#include "base/android/scoped_java_ref.h" 12#include "base/compiler_specific.h" 13#include "base/memory/ref_counted.h" 14#include "base/memory/weak_ptr.h" 15#include "base/string16.h" 16#include "content/public/browser/web_contents_observer.h" 17 18struct NPObject; 19 20namespace content { 21class JavaBridgeDispatcherHost; 22class RenderViewHost; 23 24// This class handles injecting Java objects into all of the RenderViews 25// associated with a WebContents. It manages a set of JavaBridgeDispatcherHost 26// objects, one per RenderViewHost. 27class JavaBridgeDispatcherHostManager 28 : public WebContentsObserver, 29 public base::SupportsWeakPtr<JavaBridgeDispatcherHostManager> { 30 public: 31 explicit JavaBridgeDispatcherHostManager(WebContents* web_contents); 32 virtual ~JavaBridgeDispatcherHostManager(); 33 34 // These methods add or remove the object to each JavaBridgeDispatcherHost. 35 // Each one holds a reference to the NPObject while the object is bound to 36 // the corresponding RenderView. See JavaBridgeDispatcherHost for details. 37 void AddNamedObject(const string16& name, NPObject* object); 38 void RemoveNamedObject(const string16& name); 39 40 // Every time a JavaBoundObject backed by a real Java object is 41 // created/destroyed, we insert/remove a strong ref to that Java object into 42 // this set so that it doesn't get garbage collected while it's still 43 // potentially in use. Although the set is managed native side, it's owned 44 // and defined in Java so that pushing refs into it does not create new GC 45 // roots that would prevent ContentViewCore from being garbage collected. 46 void SetRetainedObjectSet(const JavaObjectWeakGlobalRef& retained_object_set); 47 48 // WebContentsObserver overrides 49 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; 50 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE; 51 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE; 52 virtual void DocumentAvailableInMainFrame() OVERRIDE; 53 54 void JavaBoundObjectCreated(const base::android::JavaRef<jobject>& object); 55 void JavaBoundObjectDestroyed(const base::android::JavaRef<jobject>& object); 56 57 private: 58 typedef std::map<RenderViewHost*, scoped_refptr<JavaBridgeDispatcherHost> > 59 InstanceMap; 60 InstanceMap instances_; 61 typedef std::map<string16, NPObject*> ObjectMap; 62 ObjectMap objects_; 63 JavaObjectWeakGlobalRef retained_object_set_; 64 65 DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHostManager); 66}; 67 68} // namespace content 69 70#endif // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_ 71