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.
5 #ifndef CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_
6 #define CHROME_BROWSER_GUESTVIEW_GUESTVIEW_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 GuestView is the base class browser-side API implementation for a <*view>
20 // tag. GuestView maintains an association between a guest WebContents and an
21 // embedder WebContents. It receives events issued from the guest and relays
22 // them to the embedder.
23 class GuestView
: public content::BrowserPluginGuestDelegate
{
33 Event(const std::string
& name
, scoped_ptr
<base::DictionaryValue
> args
);
36 const std::string
& name() const { return name_
; }
38 scoped_ptr
<base::DictionaryValue
> GetArguments();
41 const std::string name_
;
42 scoped_ptr
<base::DictionaryValue
> args_
;
45 static Type
GetViewTypeFromString(const std::string
& api_type
);
47 static GuestView
* Create(content::WebContents
* guest_web_contents
,
48 const std::string
& extension_id
,
51 static GuestView
* FromWebContents(content::WebContents
* web_contents
);
53 static GuestView
* From(int embedder_process_id
, int instance_id
);
55 // For GuestViews, we create special guest processes, which host the
56 // tag content separately from the main application that embeds the tag.
57 // A GuestView can specify both the partition name and whether the storage
58 // for that partition should be persisted. Each tag gets a SiteInstance with
59 // a specially formatted URL, based on the application it is hosted by and
60 // the partition requested by it. The format for that URL is:
61 // chrome-guest://partition_domain/persist?partition_name
62 static bool GetGuestPartitionConfigForSite(const GURL
& site
,
63 std::string
* partition_domain
,
64 std::string
* partition_name
,
67 // By default, JavaScript and images are enabled in guest content.
68 static void GetDefaultContentSettingRules(
69 RendererContentSettingRules
* rules
, bool incognito
);
71 virtual void Attach(content::WebContents
* embedder_web_contents
,
72 const base::DictionaryValue
& args
);
74 content::WebContents
* embedder_web_contents() const {
75 return embedder_web_contents_
;
78 // Returns the guest WebContents.
79 content::WebContents
* guest_web_contents() const {
80 return guest_web_contents_
;
83 virtual Type
GetViewType() const;
85 // Returns a WebViewGuest if this GuestView belongs to a <webview>.
86 virtual WebViewGuest
* AsWebView() = 0;
88 // Returns an AdViewGuest if the GuestView belongs to an <adview>.
89 virtual AdViewGuest
* AsAdView() = 0;
91 // Returns whether this guest has an associated embedder.
92 bool attached() const { return !!embedder_web_contents_
; }
94 // Returns the instance ID of the <*view> element.
95 int view_instance_id() const { return view_instance_id_
; }
97 // Returns the instance ID of the guest WebContents.
98 int guest_instance_id() const { return guest_instance_id_
; }
100 // Returns the extension ID of the embedder.
101 const std::string
& extension_id() const { return extension_id_
; }
103 // Returns the user browser context of the embedder.
104 content::BrowserContext
* browser_context() const { return browser_context_
; }
106 // Returns the embedder's process ID.
107 int embedder_render_process_id() const { return embedder_render_process_id_
; }
110 GuestView(content::WebContents
* guest_web_contents
,
111 const std::string
& extension_id
);
112 virtual ~GuestView();
114 // Dispatches an event |event_name| to the embedder with the |event| fields.
115 void DispatchEvent(Event
* event
);
118 void SendQueuedEvents();
120 content::WebContents
* const guest_web_contents_
;
121 content::WebContents
* embedder_web_contents_
;
122 const std::string extension_id_
;
123 int embedder_render_process_id_
;
124 content::BrowserContext
* const browser_context_
;
125 // |guest_instance_id_| is a profile-wide unique identifier for a guest
127 const int guest_instance_id_
;
128 // |view_instance_id_| is an identifier that's unique within a particular
129 // embedder RenderViewHost for a particular <*view> instance.
130 int view_instance_id_
;
132 // This is a queue of Events that are destined to be sent to the embedder once
133 // the guest is attached to a particular embedder.
134 std::queue
<Event
*> pending_events_
;
136 // This is used to ensure pending tasks will not fire after this object is
138 base::WeakPtrFactory
<GuestView
> weak_ptr_factory_
;
140 DISALLOW_COPY_AND_ASSIGN(GuestView
);
143 #endif // CHROME_BROWSER_GUESTVIEW_GUESTVIEW_H_