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 #include "extensions/browser/guest_view/app_view/app_view_guest.h"
7 #include "base/command_line.h"
8 #include "components/guest_view/browser/guest_view_manager.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/common/renderer_preferences.h"
11 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
12 #include "extensions/browser/api/extensions_api_client.h"
13 #include "extensions/browser/app_window/app_delegate.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/browser/extension_host.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/guest_view/app_view/app_view_constants.h"
18 #include "extensions/browser/lazy_background_task_queue.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/browser/view_type_utils.h"
21 #include "extensions/common/api/app_runtime.h"
22 #include "extensions/common/extension_messages.h"
23 #include "extensions/strings/grit/extensions_strings.h"
24 #include "ipc/ipc_message_macros.h"
26 namespace app_runtime
= extensions::core_api::app_runtime
;
28 using content::RenderFrameHost
;
29 using content::WebContents
;
30 using extensions::ExtensionHost
;
31 using guest_view::GuestViewBase
;
33 namespace extensions
{
38 scoped_refptr
<const Extension
> guest_extension
;
39 base::WeakPtr
<AppViewGuest
> app_view_guest
;
40 GuestViewBase::WebContentsCreatedCallback callback
;
42 ResponseInfo(const Extension
* guest_extension
,
43 const base::WeakPtr
<AppViewGuest
>& app_view_guest
,
44 const GuestViewBase::WebContentsCreatedCallback
& callback
)
45 : guest_extension(guest_extension
),
46 app_view_guest(app_view_guest
),
52 typedef std::map
<int, linked_ptr
<ResponseInfo
> > PendingResponseMap
;
53 static base::LazyInstance
<PendingResponseMap
> pending_response_map
=
54 LAZY_INSTANCE_INITIALIZER
;
59 const char AppViewGuest::Type
[] = "appview";
62 bool AppViewGuest::CompletePendingRequest(
63 content::BrowserContext
* browser_context
,
65 int guest_instance_id
,
66 const std::string
& guest_extension_id
) {
67 PendingResponseMap
* response_map
= pending_response_map
.Pointer();
68 PendingResponseMap::iterator it
= response_map
->find(guest_instance_id
);
69 if (it
== response_map
->end()) {
70 // TODO(fsamuel): An app is sending invalid responses. We should probably
75 linked_ptr
<ResponseInfo
> response_info
= it
->second
;
76 if (!response_info
->app_view_guest
||
77 (response_info
->guest_extension
->id() != guest_extension_id
)) {
78 // TODO(fsamuel): An app is trying to respond to an <appview> that didn't
79 // initiate communication with it. We should kill the app here.
83 response_info
->app_view_guest
->CompleteCreateWebContents(
84 url
, response_info
->guest_extension
.get(), response_info
->callback
);
86 response_map
->erase(guest_instance_id
);
91 GuestViewBase
* AppViewGuest::Create(content::WebContents
* owner_web_contents
) {
92 return new AppViewGuest(owner_web_contents
);
95 AppViewGuest::AppViewGuest(content::WebContents
* owner_web_contents
)
96 : GuestView
<AppViewGuest
>(owner_web_contents
),
97 app_view_guest_delegate_(
98 ExtensionsAPIClient::Get()->CreateAppViewGuestDelegate()),
99 weak_ptr_factory_(this) {
100 if (app_view_guest_delegate_
)
101 app_delegate_
.reset(app_view_guest_delegate_
->CreateAppDelegate());
104 AppViewGuest::~AppViewGuest() {
107 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams
& params
) {
108 if (app_view_guest_delegate_
) {
109 return app_view_guest_delegate_
->HandleContextMenu(web_contents(), params
);
114 void AppViewGuest::RequestMediaAccessPermission(
115 content::WebContents
* web_contents
,
116 const content::MediaStreamRequest
& request
,
117 const content::MediaResponseCallback
& callback
) {
118 if (!app_delegate_
) {
119 WebContentsDelegate::RequestMediaAccessPermission(web_contents
, request
,
123 const ExtensionSet
& enabled_extensions
=
124 ExtensionRegistry::Get(browser_context())->enabled_extensions();
125 const Extension
* guest_extension
=
126 enabled_extensions
.GetByID(guest_extension_id_
);
128 app_delegate_
->RequestMediaAccessPermission(web_contents
, request
, callback
,
132 bool AppViewGuest::CheckMediaAccessPermission(
133 content::WebContents
* web_contents
,
134 const GURL
& security_origin
,
135 content::MediaStreamType type
) {
136 if (!app_delegate_
) {
137 return WebContentsDelegate::CheckMediaAccessPermission(
138 web_contents
, security_origin
, type
);
140 const ExtensionSet
& enabled_extensions
=
141 ExtensionRegistry::Get(browser_context())->enabled_extensions();
142 const Extension
* guest_extension
=
143 enabled_extensions
.GetByID(guest_extension_id_
);
145 return app_delegate_
->CheckMediaAccessPermission(
146 web_contents
, security_origin
, type
, guest_extension
);
149 bool AppViewGuest::CanRunInDetachedState() const {
153 void AppViewGuest::CreateWebContents(
154 const base::DictionaryValue
& create_params
,
155 const WebContentsCreatedCallback
& callback
) {
157 if (!create_params
.GetString(appview::kAppID
, &app_id
)) {
158 callback
.Run(nullptr);
161 // Verifying that the appId is not the same as the host application.
162 if (owner_host() == app_id
) {
163 callback
.Run(nullptr);
166 const base::DictionaryValue
* data
= nullptr;
167 if (!create_params
.GetDictionary(appview::kData
, &data
)) {
168 callback
.Run(nullptr);
172 const ExtensionSet
& enabled_extensions
=
173 ExtensionRegistry::Get(browser_context())->enabled_extensions();
174 const Extension
* guest_extension
= enabled_extensions
.GetByID(app_id
);
175 const Extension
* embedder_extension
=
176 enabled_extensions
.GetByID(GetOwnerSiteURL().host());
178 if (!guest_extension
|| !guest_extension
->is_platform_app() ||
179 !embedder_extension
| !embedder_extension
->is_platform_app()) {
180 callback
.Run(nullptr);
184 pending_response_map
.Get().insert(
185 std::make_pair(guest_instance_id(),
186 make_linked_ptr(new ResponseInfo(
188 weak_ptr_factory_
.GetWeakPtr(),
191 LazyBackgroundTaskQueue
* queue
=
192 LazyBackgroundTaskQueue::Get(browser_context());
193 if (queue
->ShouldEnqueueTask(browser_context(), guest_extension
)) {
194 queue
->AddPendingTask(browser_context(),
195 guest_extension
->id(),
197 &AppViewGuest::LaunchAppAndFireEvent
,
198 weak_ptr_factory_
.GetWeakPtr(),
199 base::Passed(make_scoped_ptr(data
->DeepCopy())),
204 ProcessManager
* process_manager
= ProcessManager::Get(browser_context());
205 ExtensionHost
* host
=
206 process_manager
->GetBackgroundHostForExtension(guest_extension
->id());
208 LaunchAppAndFireEvent(make_scoped_ptr(data
->DeepCopy()), callback
, host
);
211 void AppViewGuest::DidInitialize(const base::DictionaryValue
& create_params
) {
212 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
214 if (!url_
.is_valid())
217 web_contents()->GetController().LoadURL(
218 url_
, content::Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
221 const char* AppViewGuest::GetAPINamespace() const {
222 return appview::kEmbedderAPINamespace
;
225 int AppViewGuest::GetTaskPrefix() const {
226 return IDS_EXTENSION_TASK_MANAGER_APPVIEW_TAG_PREFIX
;
229 void AppViewGuest::CompleteCreateWebContents(
231 const Extension
* guest_extension
,
232 const WebContentsCreatedCallback
& callback
) {
233 if (!url
.is_valid()) {
234 callback
.Run(nullptr);
238 guest_extension_id_
= guest_extension
->id();
240 WebContents::CreateParams
params(
242 content::SiteInstance::CreateForURL(browser_context(),
243 guest_extension
->url()));
244 params
.guest_delegate
= this;
245 callback
.Run(WebContents::Create(params
));
248 void AppViewGuest::LaunchAppAndFireEvent(
249 scoped_ptr
<base::DictionaryValue
> data
,
250 const WebContentsCreatedCallback
& callback
,
251 ExtensionHost
* extension_host
) {
252 bool has_event_listener
= EventRouter::Get(browser_context())
253 ->ExtensionHasEventListener(
254 extension_host
->extension()->id(),
255 app_runtime::OnEmbedRequested::kEventName
);
256 if (!has_event_listener
) {
257 callback
.Run(nullptr);
261 scoped_ptr
<base::DictionaryValue
> embed_request(new base::DictionaryValue());
262 embed_request
->SetInteger(appview::kGuestInstanceID
, guest_instance_id());
263 embed_request
->SetString(appview::kEmbedderID
, owner_host());
264 embed_request
->Set(appview::kData
, data
.release());
265 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
266 browser_context(), embed_request
.Pass(), extension_host
->extension());
269 void AppViewGuest::SetAppDelegateForTest(AppDelegate
* delegate
) {
270 app_delegate_
.reset(delegate
);
273 } // namespace extensions