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 #include "chrome/browser/guestview/guestview.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/guestview/adview/adview_guest.h"
9 #include "chrome/browser/guestview/guestview_constants.h"
10 #include "chrome/browser/guestview/webview/webview_guest.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/content_settings.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/url_constants.h"
16 #include "extensions/browser/event_router.h"
17 #include "net/base/escape.h"
19 using content::WebContents
;
23 // <embedder_process_id, guest_instance_id> => GuestView*
24 typedef std::map
<std::pair
<int, int>, GuestView
*> EmbedderGuestViewMap
;
25 static base::LazyInstance
<EmbedderGuestViewMap
> embedder_guestview_map
=
26 LAZY_INSTANCE_INITIALIZER
;
28 typedef std::map
<WebContents
*, GuestView
*> WebContentsGuestViewMap
;
29 static base::LazyInstance
<WebContentsGuestViewMap
> webcontents_guestview_map
=
30 LAZY_INSTANCE_INITIALIZER
;
34 GuestView::Event::Event(const std::string
& name
,
35 scoped_ptr
<base::DictionaryValue
> args
)
40 GuestView::Event::~Event() {
43 scoped_ptr
<base::DictionaryValue
> GuestView::Event::GetArguments() {
47 GuestView::GuestView(WebContents
* guest_web_contents
,
48 const std::string
& extension_id
)
49 : guest_web_contents_(guest_web_contents
),
50 embedder_web_contents_(NULL
),
51 extension_id_(extension_id
),
52 embedder_render_process_id_(0),
53 browser_context_(guest_web_contents
->GetBrowserContext()),
54 guest_instance_id_(guest_web_contents
->GetEmbeddedInstanceID()),
55 view_instance_id_(guestview::kInstanceIDNone
),
56 weak_ptr_factory_(this) {
57 webcontents_guestview_map
.Get().insert(
58 std::make_pair(guest_web_contents
, this));
62 GuestView::Type
GuestView::GetViewTypeFromString(const std::string
& api_type
) {
63 if (api_type
== "adview") {
64 return GuestView::ADVIEW
;
65 } else if (api_type
== "webview") {
66 return GuestView::WEBVIEW
;
68 return GuestView::UNKNOWN
;
72 GuestView
* GuestView::Create(WebContents
* guest_web_contents
,
73 const std::string
& extension_id
,
74 GuestView::Type view_type
) {
76 case GuestView::WEBVIEW
:
77 return new WebViewGuest(guest_web_contents
, extension_id
);
78 case GuestView::ADVIEW
:
79 return new AdViewGuest(guest_web_contents
, extension_id
);
87 GuestView
* GuestView::FromWebContents(WebContents
* web_contents
) {
88 WebContentsGuestViewMap
* guest_map
= webcontents_guestview_map
.Pointer();
89 WebContentsGuestViewMap::iterator it
= guest_map
->find(web_contents
);
90 return it
== guest_map
->end() ? NULL
: it
->second
;
94 GuestView
* GuestView::From(int embedder_process_id
, int guest_instance_id
) {
95 EmbedderGuestViewMap
* guest_map
= embedder_guestview_map
.Pointer();
96 EmbedderGuestViewMap::iterator it
= guest_map
->find(
97 std::make_pair(embedder_process_id
, guest_instance_id
));
98 return it
== guest_map
->end() ? NULL
: it
->second
;
102 bool GuestView::GetGuestPartitionConfigForSite(const GURL
& site
,
103 std::string
* partition_domain
,
104 std::string
* partition_name
,
106 if (!site
.SchemeIs(content::kGuestScheme
))
109 // Since guest URLs are only used for packaged apps, there must be an app
111 CHECK(site
.has_host());
112 *partition_domain
= site
.host();
113 // Since persistence is optional, the path must either be empty or the
115 *in_memory
= (site
.path() != "/persist");
116 // The partition name is user supplied value, which we have encoded when the
117 // URL was created, so it needs to be decoded.
118 *partition_name
= net::UnescapeURLComponent(site
.query(),
119 net::UnescapeRule::NORMAL
);
124 void GuestView::GetDefaultContentSettingRules(
125 RendererContentSettingRules
* rules
, bool incognito
) {
126 rules
->image_rules
.push_back(ContentSettingPatternSource(
127 ContentSettingsPattern::Wildcard(),
128 ContentSettingsPattern::Wildcard(),
129 CONTENT_SETTING_ALLOW
,
133 rules
->script_rules
.push_back(ContentSettingPatternSource(
134 ContentSettingsPattern::Wildcard(),
135 ContentSettingsPattern::Wildcard(),
136 CONTENT_SETTING_ALLOW
,
141 void GuestView::Attach(content::WebContents
* embedder_web_contents
,
142 const base::DictionaryValue
& args
) {
143 embedder_web_contents_
= embedder_web_contents
;
144 embedder_render_process_id_
=
145 embedder_web_contents
->GetRenderProcessHost()->GetID();
146 args
.GetInteger(guestview::kParameterInstanceId
, &view_instance_id_
);
148 std::pair
<int, int> key(embedder_render_process_id_
, guest_instance_id_
);
149 embedder_guestview_map
.Get().insert(std::make_pair(key
, this));
151 // GuestView::Attach is called prior to initialization (and initial
152 // navigation) of the guest in the content layer in order to permit mapping
153 // the necessary associations between the <*view> element and its guest. This
154 // is needed by the <webview> WebRequest API to allow intercepting resource
155 // requests during navigation. However, queued events should be fired after
156 // content layer initialization in order to ensure that load events (such as
157 // 'loadstop') fire in embedder after the contentWindow is available.
158 base::MessageLoop::current()->PostTask(
160 base::Bind(&GuestView::SendQueuedEvents
,
161 weak_ptr_factory_
.GetWeakPtr()));
164 GuestView::Type
GuestView::GetViewType() const {
165 return GuestView::UNKNOWN
;
168 WebViewGuest
* GuestView::AsWebView() {
172 AdViewGuest
* GuestView::AsAdView() {
176 GuestView::~GuestView() {
177 std::pair
<int, int> key(embedder_render_process_id_
, guest_instance_id_
);
178 embedder_guestview_map
.Get().erase(key
);
180 webcontents_guestview_map
.Get().erase(guest_web_contents());
182 while (!pending_events_
.empty()) {
183 delete pending_events_
.front();
184 pending_events_
.pop();
188 void GuestView::DispatchEvent(Event
* event
) {
190 pending_events_
.push(event
);
194 Profile
* profile
= Profile::FromBrowserContext(browser_context_
);
196 extensions::EventFilteringInfo info
;
198 info
.SetInstanceID(guest_instance_id_
);
199 scoped_ptr
<base::ListValue
> args(new base::ListValue());
200 args
->Append(event
->GetArguments().release());
202 extensions::EventRouter::DispatchEvent(
203 embedder_web_contents_
, profile
, extension_id_
,
204 event
->name(), args
.Pass(),
205 extensions::EventRouter::USER_GESTURE_UNKNOWN
, info
);
210 void GuestView::SendQueuedEvents() {
214 while (!pending_events_
.empty()) {
215 Event
* event
= pending_events_
.front();
216 pending_events_
.pop();
217 DispatchEvent(event
);