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 struct RendererContentSettingRules;
     17 
     18 // A GuestView is the base class browser-side API implementation for a <*view>
     19 // tag. GuestView maintains an association between a guest WebContents and an
     20 // embedder WebContents. It receives events issued from the guest and relays
     21 // them to the embedder.
     22 class GuestView : public content::BrowserPluginGuestDelegate {
     23  public:
     24   enum Type {
     25     WEBVIEW,
     26     ADVIEW,
     27     UNKNOWN
     28   };
     29 
     30   class Event {
     31    public:
     32      Event(const std::string& name, scoped_ptr<DictionaryValue> args);
     33      ~Event();
     34 
     35     const std::string& name() const { return name_; }
     36 
     37     scoped_ptr<DictionaryValue> GetArguments();
     38 
     39    private:
     40     const std::string name_;
     41     scoped_ptr<DictionaryValue> args_;
     42   };
     43 
     44   static Type GetViewTypeFromString(const std::string& api_type);
     45 
     46   static GuestView* Create(content::WebContents* guest_web_contents,
     47                            const std::string& extension_id,
     48                            Type view_type);
     49 
     50   static GuestView* FromWebContents(content::WebContents* web_contents);
     51 
     52   static GuestView* From(int embedder_process_id, int instance_id);
     53 
     54   // For GuestViews, we create special guest processes, which host the
     55   // tag content separately from the main application that embeds the tag.
     56   // A GuestView can specify both the partition name and whether the storage
     57   // for that partition should be persisted. Each tag gets a SiteInstance with
     58   // a specially formatted URL, based on the application it is hosted by and
     59   // the partition requested by it. The format for that URL is:
     60   // chrome-guest://partition_domain/persist?partition_name
     61   static bool GetGuestPartitionConfigForSite(const GURL& site,
     62                                              std::string* partition_domain,
     63                                              std::string* partition_name,
     64                                              bool* in_memory);
     65 
     66   // By default, JavaScript and images are enabled in guest content.
     67   static void GetDefaultContentSettingRules(
     68       RendererContentSettingRules* rules, bool incognito);
     69 
     70   virtual void Attach(content::WebContents* embedder_web_contents,
     71                       const base::DictionaryValue& args);
     72 
     73   content::WebContents* embedder_web_contents() const {
     74     return embedder_web_contents_;
     75   }
     76 
     77   // Returns the guest WebContents.
     78   content::WebContents* guest_web_contents() const {
     79     return guest_web_contents_;
     80   }
     81 
     82   virtual Type GetViewType() const;
     83 
     84   // Returns a WebViewGuest if this GuestView belongs to a <webview>.
     85   virtual WebViewGuest* AsWebView() = 0;
     86 
     87   // Returns an AdViewGuest if the GuestView belongs to an <adview>.
     88   virtual AdViewGuest* AsAdView() = 0;
     89 
     90   // Returns whether this guest has an associated embedder.
     91   bool attached() const { return !!embedder_web_contents_; }
     92 
     93   // Returns the instance ID of the <*view> element.
     94   int view_instance_id() const { return view_instance_id_; }
     95 
     96   // Returns the instance ID of the guest WebContents.
     97   int guest_instance_id() const { return guest_instance_id_; }
     98 
     99   // Returns the extension ID of the embedder.
    100   const std::string& extension_id() const { return extension_id_; }
    101 
    102   // Returns the user browser context of the embedder.
    103   content::BrowserContext* browser_context() const { return browser_context_; }
    104 
    105   // Returns the embedder's process ID.
    106   int embedder_render_process_id() const { return embedder_render_process_id_; }
    107 
    108  protected:
    109   GuestView(content::WebContents* guest_web_contents,
    110             const std::string& extension_id);
    111   virtual ~GuestView();
    112 
    113   // Dispatches an event |event_name| to the embedder with the |event| fields.
    114   void DispatchEvent(Event* event);
    115 
    116  private:
    117   void SendQueuedEvents();
    118 
    119   content::WebContents* const guest_web_contents_;
    120   content::WebContents* embedder_web_contents_;
    121   const std::string extension_id_;
    122   int embedder_render_process_id_;
    123   content::BrowserContext* const browser_context_;
    124   // |guest_instance_id_| is a profile-wide unique identifier for a guest
    125   // WebContents.
    126   const int guest_instance_id_;
    127   // |view_instance_id_| is an identifier that's unique within a particular
    128   // embedder RenderViewHost for a particular <*view> instance.
    129   int view_instance_id_;
    130 
    131   // This is a queue of Events that are destined to be sent to the embedder once
    132   // the guest is attached to a particular embedder.
    133   std::queue<Event*> pending_events_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(GuestView);
    136 };
    137 
    138 #endif  // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
    139