Home | History | Annotate | Download | only in java
      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/strings/string16.h"
     16 #include "content/public/browser/web_contents_observer.h"
     17 
     18 struct NPObject;
     19 
     20 namespace content {
     21 class JavaBridgeDispatcherHost;
     22 class 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.
     27 class 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 base::string16& name, NPObject* object);
     38   void RemoveNamedObject(const base::string16& name);
     39 
     40   void OnGetChannelHandle(RenderViewHost* render_view_host,
     41                           IPC::Message* reply_msg);
     42 
     43   // Every time a JavaBoundObject backed by a real Java object is
     44   // created/destroyed, we insert/remove a strong ref to that Java object into
     45   // this set so that it doesn't get garbage collected while it's still
     46   // potentially in use. Although the set is managed native side, it's owned
     47   // and defined in Java so that pushing refs into it does not create new GC
     48   // roots that would prevent ContentViewCore from being garbage collected.
     49   void SetRetainedObjectSet(const JavaObjectWeakGlobalRef& retained_object_set);
     50 
     51   // WebContentsObserver overrides
     52   virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
     53   virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE;
     54   virtual void DocumentAvailableInMainFrame() OVERRIDE;
     55 
     56   void JavaBoundObjectCreated(const base::android::JavaRef<jobject>& object);
     57   void JavaBoundObjectDestroyed(const base::android::JavaRef<jobject>& object);
     58 
     59  private:
     60   typedef std::map<RenderViewHost*, scoped_refptr<JavaBridgeDispatcherHost> >
     61       InstanceMap;
     62   InstanceMap instances_;
     63   typedef std::map<base::string16, NPObject*> ObjectMap;
     64   ObjectMap objects_;
     65   JavaObjectWeakGlobalRef retained_object_set_;
     66 
     67   DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHostManager);
     68 };
     69 
     70 }  // namespace content
     71 
     72 #endif  // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_
     73