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.
5 #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_
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"
17 struct RendererContentSettingRules
;
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
{
27 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
30 const std::string
& name() const { return name_
; }
32 scoped_ptr
<base::DictionaryValue
> GetArguments();
35 const std::string name_
;
36 scoped_ptr
<base::DictionaryValue
> args_
;
39 // Returns a *ViewGuest if this GuestView is of the given view type.
42 if (IsViewType(T::Type
))
43 return static_cast<T
*>(this);
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
);
53 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
55 static GuestViewBase
* From(int embedder_process_id
, int instance_id
);
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
,
69 // By default, JavaScript and images are enabled in guest content.
70 static void GetDefaultContentSettingRules(RendererContentSettingRules
* rules
,
73 virtual const char* GetViewType() const = 0;
75 bool IsViewType(const char* const view_type
) const {
76 return !strcmp(GetViewType(), view_type
);
79 base::WeakPtr
<GuestViewBase
> AsWeakPtr();
81 virtual void Attach(content::WebContents
* embedder_web_contents
,
82 const base::DictionaryValue
& args
);
84 content::WebContents
* embedder_web_contents() const {
85 return embedder_web_contents_
;
88 // Returns the guest WebContents.
89 content::WebContents
* guest_web_contents() const {
90 return guest_web_contents_
;
93 // Returns whether this guest has an associated embedder.
94 bool attached() const { return !!embedder_web_contents_
; }
96 // Returns the instance ID of the <*view> element.
97 int view_instance_id() const { return view_instance_id_
; }
99 // Returns the instance ID of the guest WebContents.
100 int guest_instance_id() const { return guest_instance_id_
; }
102 // Returns the extension ID of the embedder.
103 const std::string
& embedder_extension_id() const {
104 return embedder_extension_id_
;
107 // Returns whether this GuestView is embedded in an extension/app.
108 bool in_extension() const { return !embedder_extension_id_
.empty(); }
110 // Returns the user browser context of the embedder.
111 content::BrowserContext
* browser_context() const { return browser_context_
; }
113 // Returns the embedder's process ID.
114 int embedder_render_process_id() const { return embedder_render_process_id_
; }
116 // BrowserPluginGuestDelegate implementation.
117 virtual content::WebContents
* GetOpener() const OVERRIDE
;
118 virtual void SetOpener(content::WebContents
* opener
) OVERRIDE
;
121 GuestViewBase(content::WebContents
* guest_web_contents
,
122 const std::string
& embedder_extension_id
,
123 const base::WeakPtr
<GuestViewBase
>& opener
);
124 virtual ~GuestViewBase();
126 // Dispatches an event |event_name| to the embedder with the |event| fields.
127 void DispatchEvent(Event
* event
);
130 void SendQueuedEvents();
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
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_
;
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_
;
148 // The opener guest view.
149 base::WeakPtr
<GuestViewBase
> opener_
;
151 // This is used to ensure pending tasks will not fire after this object is
153 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
155 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
158 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_