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_H_
      6 #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_
      7 
      8 #include <vector>
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/strings/string16.h"
     12 #include "content/child/npapi/npobject_stub.h"
     13 #include "content/public/browser/render_view_host_observer.h"
     14 
     15 class RouteIDGenerator;
     16 struct NPObject;
     17 
     18 namespace content {
     19 class NPChannelBase;
     20 class RenderViewHost;
     21 struct NPVariant_Param;
     22 
     23 // This class handles injecting Java objects into a single RenderView. The Java
     24 // object itself lives in the browser process on a background thread, while a
     25 // proxy object is created in the renderer. An instance of this class exists
     26 // for each RenderViewHost.
     27 class JavaBridgeDispatcherHost
     28     : public base::RefCountedThreadSafe<JavaBridgeDispatcherHost>,
     29       public RenderViewHostObserver {
     30  public:
     31   // We hold a weak pointer to the RenderViewhost. It must outlive this object.
     32   JavaBridgeDispatcherHost(RenderViewHost* render_view_host);
     33 
     34   // Injects |object| into the main frame of the corresponding RenderView. A
     35   // proxy object is created in the renderer and when the main frame's window
     36   // object is next cleared, this proxy object is bound to the window object
     37   // using |name|. The proxy object remains bound until the next time the
     38   // window object is cleared after a call to RemoveNamedObject() or
     39   // AddNamedObject() with the same name. The proxy object proxies calls back
     40   // to |object|, which is manipulated on the background thread. This class
     41   // holds a reference to |object| for the time that the proxy object is bound
     42   // to the window object.
     43   void AddNamedObject(const string16& name, NPObject* object);
     44   void RemoveNamedObject(const string16& name);
     45 
     46   // RenderViewHostObserver overrides:
     47   // The IPC macros require this to be public.
     48   virtual bool Send(IPC::Message* msg) OVERRIDE;
     49   virtual void RenderViewHostDestroyed(
     50       RenderViewHost* render_view_host) OVERRIDE;
     51 
     52  private:
     53   friend class base::RefCountedThreadSafe<JavaBridgeDispatcherHost>;
     54   virtual ~JavaBridgeDispatcherHost();
     55 
     56   // RenderViewHostObserver override:
     57   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     58 
     59   // Message handlers
     60   void OnGetChannelHandle(IPC::Message* reply_msg);
     61 
     62   void GetChannelHandle(IPC::Message* reply_msg);
     63   void CreateNPVariantParam(NPObject* object, NPVariant_Param* param);
     64   void CreateObjectStub(NPObject* object, int route_id);
     65 
     66   scoped_refptr<NPChannelBase> channel_;
     67   bool is_renderer_initialized_;
     68   std::vector<base::WeakPtr<NPObjectStub> > stubs_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHost);
     71 };
     72 
     73 }  // namespace content
     74 
     75 #endif  // CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_
     76