Home | History | Annotate | Download | only in java
      1 // Copyright 2014 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_ANDROID_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_
      6 #define CONTENT_BROWSER_ANDROID_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/android/jni_weak_ref.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 RenderFrameHost;
     23 
     24 // This class handles injecting Java objects into all of the RenderFrames
     25 // associated with a WebContents. It manages a set of JavaBridgeDispatcherHost
     26 // objects, one per RenderFrameHost.
     27 class JavaBridgeDispatcherHostManager
     28     : public WebContentsObserver,
     29       public base::SupportsWeakPtr<JavaBridgeDispatcherHostManager> {
     30  public:
     31   JavaBridgeDispatcherHostManager(WebContents* web_contents,
     32                                   jobject retained_object_set);
     33   virtual ~JavaBridgeDispatcherHostManager();
     34 
     35   // These methods add or remove the object to each JavaBridgeDispatcherHost.
     36   // Each one holds a reference to the NPObject while the object is bound to
     37   // the corresponding RenderFrame. See JavaBridgeDispatcherHost for details.
     38   void AddNamedObject(const base::string16& name, NPObject* object);
     39   void RemoveNamedObject(const base::string16& name);
     40 
     41   // WebContentsObserver overrides
     42   virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) OVERRIDE;
     43   virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE;
     44   virtual void DocumentAvailableInMainFrame() OVERRIDE;
     45   virtual bool OnMessageReceived(const IPC::Message& message,
     46                                  RenderFrameHost* render_frame_host) OVERRIDE;
     47 
     48   void JavaBoundObjectCreated(const base::android::JavaRef<jobject>& object);
     49   void JavaBoundObjectDestroyed(const base::android::JavaRef<jobject>& object);
     50 
     51   bool GetAllowObjectContentsInspection() const {
     52     return allow_object_contents_inspection_;
     53   }
     54   void SetAllowObjectContentsInspection(bool allow);
     55 
     56  private:
     57   typedef std::map<RenderFrameHost*, scoped_refptr<JavaBridgeDispatcherHost> >
     58       InstanceMap;
     59   InstanceMap instances_;
     60   typedef std::map<base::string16, NPObject*> ObjectMap;
     61   ObjectMap objects_;
     62   // Every time a JavaBoundObject backed by a real Java object is
     63   // created/destroyed, we insert/remove a strong ref to that Java object into
     64   // this set so that it doesn't get garbage collected while it's still
     65   // potentially in use. Although the set is managed native side, it's owned
     66   // and defined in Java so that pushing refs into it does not create new GC
     67   // roots that would prevent ContentViewCore from being garbage collected.
     68   JavaObjectWeakGlobalRef retained_object_set_;
     69   bool allow_object_contents_inspection_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHostManager);
     72 };
     73 
     74 }  // namespace content
     75 
     76 #endif  // CONTENT_BROWSER_ANDROID_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_MANAGER_H_
     77