Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / guest_view / guest_view_base.h
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 CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
7
8 #include <queue>
9
10 #include "base/memory/weak_ptr.h"
11 #include "base/values.h"
12 #include "content/public/browser/browser_plugin_guest_delegate.h"
13 #include "content/public/browser/web_contents.h"
14
15 class AdViewGuest;
16 class WebViewGuest;
17 struct RendererContentSettingRules;
18
19 // A GuestViewBase is the base class browser-side API implementation for a
20 // <*view> tag. GuestViewBase maintains an association between a guest
21 // WebContents and an embedder WebContents. It receives events issued from
22 // the guest and relays them to the embedder.
23 class GuestViewBase : public content::BrowserPluginGuestDelegate {
24  public:
25   class Event {
26    public:
27     Event(const std::string& name, scoped_ptr<base::DictionaryValue> args);
28     ~Event();
29
30     const std::string& name() const { return name_; }
31
32     scoped_ptr<base::DictionaryValue> GetArguments();
33
34    private:
35     const std::string name_;
36     scoped_ptr<base::DictionaryValue> args_;
37   };
38
39   // Returns a *ViewGuest if this GuestView is of the given view type.
40   template <typename T>
41   T* As() {
42     if (IsViewType(T::Type))
43       return static_cast<T*>(this);
44
45     return NULL;
46   }
47
48   static GuestViewBase* Create(content::WebContents* guest_web_contents,
49                                const std::string& embedder_extension_id,
50                                const std::string& view_type,
51                                const base::WeakPtr<GuestViewBase>& opener);
52
53   static GuestViewBase* FromWebContents(content::WebContents* web_contents);
54
55   static GuestViewBase* From(int embedder_process_id, int instance_id);
56
57   // For GuestViewBases, we create special guest processes, which host the
58   // tag content separately from the main application that embeds the tag.
59   // A GuestViewBase can specify both the partition name and whether the storage
60   // for that partition should be persisted. Each tag gets a SiteInstance with
61   // a specially formatted URL, based on the application it is hosted by and
62   // the partition requested by it. The format for that URL is:
63   // chrome-guest://partition_domain/persist?partition_name
64   static bool GetGuestPartitionConfigForSite(const GURL& site,
65                                              std::string* partition_domain,
66                                              std::string* partition_name,
67                                              bool* in_memory);
68
69   // By default, JavaScript and images are enabled in guest content.
70   static void GetDefaultContentSettingRules(RendererContentSettingRules* rules,
71                                             bool incognito);
72
73   virtual const char* GetViewType() const = 0;
74
75   bool IsViewType(const char* const view_type) const {
76     return !strcmp(GetViewType(), view_type);
77   }
78
79   base::WeakPtr<GuestViewBase> AsWeakPtr();
80
81   virtual void Attach(content::WebContents* embedder_web_contents,
82                       const base::DictionaryValue& args);
83
84   content::WebContents* embedder_web_contents() const {
85     return embedder_web_contents_;
86   }
87
88   // Returns the guest WebContents.
89   content::WebContents* guest_web_contents() const {
90     return guest_web_contents_;
91   }
92
93   // Returns whether this guest has an associated embedder.
94   bool attached() const { return !!embedder_web_contents_; }
95
96   // Returns the instance ID of the <*view> element.
97   int view_instance_id() const { return view_instance_id_; }
98
99   // Returns the instance ID of the guest WebContents.
100   int guest_instance_id() const { return guest_instance_id_; }
101
102   // Returns the extension ID of the embedder.
103   const std::string& embedder_extension_id() const {
104     return embedder_extension_id_;
105   }
106
107   // Returns whether this GuestView is embedded in an extension/app.
108   bool in_extension() const { return !embedder_extension_id_.empty(); }
109
110   // Returns the user browser context of the embedder.
111   content::BrowserContext* browser_context() const { return browser_context_; }
112
113   // Returns the embedder's process ID.
114   int embedder_render_process_id() const { return embedder_render_process_id_; }
115
116   // BrowserPluginGuestDelegate implementation.
117   virtual content::WebContents* GetOpener() const OVERRIDE;
118   virtual void SetOpener(content::WebContents* opener) OVERRIDE;
119
120  protected:
121   GuestViewBase(content::WebContents* guest_web_contents,
122                 const std::string& embedder_extension_id,
123                 const base::WeakPtr<GuestViewBase>& opener);
124   virtual ~GuestViewBase();
125
126   // Dispatches an event |event_name| to the embedder with the |event| fields.
127   void DispatchEvent(Event* event);
128
129  private:
130   void SendQueuedEvents();
131
132   content::WebContents* const guest_web_contents_;
133   content::WebContents* embedder_web_contents_;
134   const std::string embedder_extension_id_;
135   int embedder_render_process_id_;
136   content::BrowserContext* const browser_context_;
137   // |guest_instance_id_| is a profile-wide unique identifier for a guest
138   // WebContents.
139   const int guest_instance_id_;
140   // |view_instance_id_| is an identifier that's unique within a particular
141   // embedder RenderViewHost for a particular <*view> instance.
142   int view_instance_id_;
143
144   // This is a queue of Events that are destined to be sent to the embedder once
145   // the guest is attached to a particular embedder.
146   std::deque<linked_ptr<Event> > pending_events_;
147
148   // The opener guest view.
149   base::WeakPtr<GuestViewBase> opener_;
150
151   // This is used to ensure pending tasks will not fire after this object is
152   // destroyed.
153   base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_;
154
155   DISALLOW_COPY_AND_ASSIGN(GuestViewBase);
156 };
157
158 #endif  // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_