Home | History | Annotate | Download | only in guestview
      1 // Copyright 2013 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 CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
      6 #define CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
      7 
      8 #include <queue>
      9 
     10 #include "base/values.h"
     11 #include "content/public/browser/browser_plugin_guest_delegate.h"
     12 #include "content/public/browser/web_contents.h"
     13 
     14 class AdViewGuest;
     15 class WebViewGuest;
     16 
     17 // A GuestView is the base class browser-side API implementation for a <*view>
     18 // tag. GuestView maintains an association between a guest WebContents and an
     19 // embedder WebContents. It receives events issued from the guest and relays
     20 // them to the embedder.
     21 class GuestView : public content::BrowserPluginGuestDelegate {
     22  public:
     23   enum Type {
     24     WEBVIEW,
     25     ADVIEW,
     26     UNKNOWN
     27   };
     28 
     29   class Event {
     30    public:
     31      Event(const std::string& event_name, scoped_ptr<DictionaryValue> args);
     32      ~Event();
     33 
     34     const std::string& event_name() const { return event_name_; }
     35 
     36     scoped_ptr<DictionaryValue> GetArguments();
     37 
     38    private:
     39     const std::string event_name_;
     40     scoped_ptr<DictionaryValue> args_;
     41   };
     42 
     43   explicit GuestView(content::WebContents* guest_web_contents);
     44 
     45   static GuestView* FromWebContents(content::WebContents* web_contents);
     46 
     47   static GuestView* From(int embedder_process_id, int instance_id);
     48 
     49   virtual void Attach(content::WebContents* embedder_web_contents,
     50                       const std::string& extension_id,
     51                       const base::DictionaryValue& args);
     52 
     53   content::WebContents* embedder_web_contents() const {
     54     return embedder_web_contents_;
     55   }
     56 
     57   // Returns the guest WebContents.
     58   content::WebContents* guest_web_contents() const {
     59     return guest_web_contents_;
     60   }
     61 
     62   virtual Type GetViewType() const;
     63 
     64   // Returns a WebViewGuest if this GuestView belongs to a <webview>.
     65   virtual WebViewGuest* AsWebView() = 0;
     66 
     67   // Returns an AdViewGuest if the GuestView belongs to an <adview>.
     68   virtual AdViewGuest* AsAdView() = 0;
     69 
     70   // Returns whether this guest has an associated embedder.
     71   bool attached() const { return !!embedder_web_contents_; }
     72 
     73   // Returns the instance ID of the <*view> element.
     74   int view_instance_id() const { return view_instance_id_; }
     75 
     76   // Returns the instance ID of the guest WebContents.
     77   int guest_instance_id() const { return guest_instance_id_; }
     78 
     79   // Returns the extension ID of the embedder.
     80   const std::string& extension_id() const { return extension_id_; }
     81 
     82   // Returns the user browser context of the embedder.
     83   content::BrowserContext* browser_context() const { return browser_context_; }
     84 
     85   // Returns the embedder's process ID.
     86   int embedder_render_process_id() const { return embedder_render_process_id_; }
     87 
     88  protected:
     89   virtual ~GuestView();
     90 
     91   // Dispatches an event |event_name| to the embedder with the |event| fields.
     92   void DispatchEvent(Event* event);
     93 
     94  private:
     95   void SendQueuedEvents();
     96 
     97   content::WebContents* guest_web_contents_;
     98   content::WebContents* embedder_web_contents_;
     99   std::string extension_id_;
    100   int embedder_render_process_id_;
    101   content::BrowserContext* browser_context_;
    102   // |guest_instance_id_| is a profile-wide unique identifier for a guest
    103   // WebContents.
    104   const int guest_instance_id_;
    105   // |view_instance_id_| is an identifier that's unique within a particular
    106   // embedder RenderViewHost for a particular <*view> instance.
    107   int view_instance_id_;
    108 
    109   // This is a queue of Events that are destined to be sent to the embedder once
    110   // the guest is attached to a particular embedder.
    111   std::queue<Event*> pending_events_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(GuestView);
    114 };
    115 
    116 #endif  // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
    117