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"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "content/public/browser/web_contents_observer.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
,
24 public content::WebContentsDelegate
,
25 public content::WebContentsObserver
{
29 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
32 const std::string
& name() const { return name_
; }
34 scoped_ptr
<base::DictionaryValue
> GetArguments();
37 const std::string name_
;
38 scoped_ptr
<base::DictionaryValue
> args_
;
41 // Returns a *ViewGuest if this GuestView is of the given view type.
44 if (IsViewType(T::Type
))
45 return static_cast<T
*>(this);
50 static GuestViewBase
* Create(int guest_instance_id
,
51 content::WebContents
* guest_web_contents
,
52 const std::string
& embedder_extension_id
,
53 const std::string
& view_type
);
55 static GuestViewBase
* FromWebContents(content::WebContents
* web_contents
);
57 static GuestViewBase
* From(int embedder_process_id
, int instance_id
);
59 static bool IsGuest(content::WebContents
* web_contents
);
61 // For GuestViewBases, we create special guest processes, which host the
62 // tag content separately from the main application that embeds the tag.
63 // A GuestViewBase can specify both the partition name and whether the storage
64 // for that partition should be persisted. Each tag gets a SiteInstance with
65 // a specially formatted URL, based on the application it is hosted by and
66 // the partition requested by it. The format for that URL is:
67 // chrome-guest://partition_domain/persist?partition_name
68 static bool GetGuestPartitionConfigForSite(const GURL
& site
,
69 std::string
* partition_domain
,
70 std::string
* partition_name
,
73 // By default, JavaScript and images are enabled in guest content.
74 static void GetDefaultContentSettingRules(RendererContentSettingRules
* rules
,
77 virtual const char* GetViewType() const = 0;
79 // This method can be overriden by subclasses. This method is called when
80 // the initial set of frames within the page have completed loading.
81 virtual void DidStopLoading() {}
83 // This method can be overridden by subclasses. It indicates that this guest's
84 // embedder has been destroyed and the guest will be destroyed shortly. This
85 // method gives derived classes the opportunity to perform some cleanup.
86 virtual void EmbedderDestroyed() {}
88 // This method queries whether drag-and-drop is enabled for this particular
89 // view. By default, drag-and-drop is disabled. Derived classes can override
90 // this behavior to enable drag-and-drop.
91 virtual bool IsDragAndDropEnabled() const;
93 bool IsViewType(const char* const view_type
) const {
94 return !strcmp(GetViewType(), view_type
);
97 base::WeakPtr
<GuestViewBase
> AsWeakPtr();
99 virtual void Attach(content::WebContents
* embedder_web_contents
,
100 const base::DictionaryValue
& args
);
102 content::WebContents
* embedder_web_contents() const {
103 return embedder_web_contents_
;
106 // Returns the guest WebContents.
107 content::WebContents
* guest_web_contents() const {
108 return web_contents();
111 // Returns the extra parameters associated with this GuestView passed
112 // in from JavaScript.
113 base::DictionaryValue
* extra_params() const {
114 return extra_params_
.get();
117 // Returns whether this guest has an associated embedder.
118 bool attached() const { return !!embedder_web_contents_
; }
120 // Returns the instance ID of the <*view> element.
121 int view_instance_id() const { return view_instance_id_
; }
123 // Returns the instance ID of the guest WebContents.
124 int guest_instance_id() const { return guest_instance_id_
; }
126 // Returns the extension ID of the embedder.
127 const std::string
& embedder_extension_id() const {
128 return embedder_extension_id_
;
131 // Returns whether this GuestView is embedded in an extension/app.
132 bool in_extension() const { return !embedder_extension_id_
.empty(); }
134 // Returns the user browser context of the embedder.
135 content::BrowserContext
* browser_context() const { return browser_context_
; }
137 // Returns the embedder's process ID.
138 int embedder_render_process_id() const { return embedder_render_process_id_
; }
140 GuestViewBase
* GetOpener() const {
141 return opener_
.get();
144 void SetOpener(GuestViewBase
* opener
);
146 // WebContentsObserver implementation.
147 virtual void DidStopLoading(
148 content::RenderViewHost
* render_view_host
) OVERRIDE FINAL
;
149 virtual void WebContentsDestroyed() OVERRIDE
;
151 // WebContentsDelegate implementation.
152 virtual bool ShouldFocusPageAfterCrash() OVERRIDE
;
153 virtual bool PreHandleGestureEvent(
154 content::WebContents
* source
,
155 const blink::WebGestureEvent
& event
) OVERRIDE
;
157 // BrowserPluginGuestDelegate implementation.
158 virtual void Destroy() OVERRIDE
;
159 virtual void RegisterDestructionCallback(
160 const DestructionCallback
& callback
) OVERRIDE
;
162 GuestViewBase(int guest_instance_id
,
163 content::WebContents
* guest_web_contents
,
164 const std::string
& embedder_extension_id
);
165 virtual ~GuestViewBase();
167 // Dispatches an event |event_name| to the embedder with the |event| fields.
168 void DispatchEvent(Event
* event
);
171 class EmbedderWebContentsObserver
;
173 void SendQueuedEvents();
175 content::WebContents
* embedder_web_contents_
;
176 const std::string embedder_extension_id_
;
177 int embedder_render_process_id_
;
178 content::BrowserContext
* const browser_context_
;
179 // |guest_instance_id_| is a profile-wide unique identifier for a guest
181 const int guest_instance_id_
;
182 // |view_instance_id_| is an identifier that's unique within a particular
183 // embedder RenderViewHost for a particular <*view> instance.
184 int view_instance_id_
;
186 // This is a queue of Events that are destined to be sent to the embedder once
187 // the guest is attached to a particular embedder.
188 std::deque
<linked_ptr
<Event
> > pending_events_
;
190 // The opener guest view.
191 base::WeakPtr
<GuestViewBase
> opener_
;
193 DestructionCallback destruction_callback_
;
195 // The extra parameters associated with this GuestView passed
196 // in from JavaScript. This will typically be the view instance ID,
197 // the API to use, and view-specific parameters. These parameters
198 // are passed along to new guests that are created from this guest.
199 scoped_ptr
<base::DictionaryValue
> extra_params_
;
201 scoped_ptr
<EmbedderWebContentsObserver
> embedder_web_contents_observer_
;
203 // This is used to ensure pending tasks will not fire after this object is
205 base::WeakPtrFactory
<GuestViewBase
> weak_ptr_factory_
;
207 DISALLOW_COPY_AND_ASSIGN(GuestViewBase
);
210 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_