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/extension_system.h"
18 #include "extensions/browser/guest_view/app_view/app_view_constants.h"
19 #include "extensions/browser/lazy_background_task_queue.h"
20 #include "extensions/browser/process_manager.h"
21 #include "extensions/browser/view_type_utils.h"
22 #include "extensions/common/api/app_runtime.h"
23 #include "extensions/common/extension_messages.h"
24 #include "extensions/strings/grit/extensions_strings.h"
25 #include "ipc/ipc_message_macros.h"
27 namespace app_runtime
= extensions::core_api::app_runtime
;
29 using content::RenderFrameHost
;
30 using content::WebContents
;
31 using extensions::ExtensionHost
;
32 using guest_view::GuestViewBase
;
34 namespace extensions
{
39 scoped_refptr
<const Extension
> guest_extension
;
40 base::WeakPtr
<AppViewGuest
> app_view_guest
;
41 GuestViewBase::WebContentsCreatedCallback callback
;
43 ResponseInfo(const Extension
* guest_extension
,
44 const base::WeakPtr
<AppViewGuest
>& app_view_guest
,
45 const GuestViewBase::WebContentsCreatedCallback
& callback
)
46 : guest_extension(guest_extension
),
47 app_view_guest(app_view_guest
),
53 typedef std::map
<int, linked_ptr
<ResponseInfo
> > PendingResponseMap
;
54 static base::LazyInstance
<PendingResponseMap
> pending_response_map
=
55 LAZY_INSTANCE_INITIALIZER
;
60 const char AppViewGuest::Type
[] = "appview";
63 bool AppViewGuest::CompletePendingRequest(
64 content::BrowserContext
* browser_context
,
66 int guest_instance_id
,
67 const std::string
& guest_extension_id
) {
68 PendingResponseMap
* response_map
= pending_response_map
.Pointer();
69 PendingResponseMap::iterator it
= response_map
->find(guest_instance_id
);
70 if (it
== response_map
->end()) {
71 // TODO(fsamuel): An app is sending invalid responses. We should probably
76 linked_ptr
<ResponseInfo
> response_info
= it
->second
;
77 if (!response_info
->app_view_guest
||
78 (response_info
->guest_extension
->id() != guest_extension_id
)) {
79 // TODO(fsamuel): An app is trying to respond to an <appview> that didn't
80 // initiate communication with it. We should kill the app here.
84 response_info
->app_view_guest
->CompleteCreateWebContents(
85 url
, response_info
->guest_extension
.get(), response_info
->callback
);
87 response_map
->erase(guest_instance_id
);
92 GuestViewBase
* AppViewGuest::Create(content::WebContents
* owner_web_contents
) {
93 return new AppViewGuest(owner_web_contents
);
96 AppViewGuest::AppViewGuest(content::WebContents
* owner_web_contents
)
97 : GuestView
<AppViewGuest
>(owner_web_contents
),
98 app_view_guest_delegate_(
99 ExtensionsAPIClient::Get()->CreateAppViewGuestDelegate()),
100 weak_ptr_factory_(this) {
101 if (app_view_guest_delegate_
)
102 app_delegate_
.reset(app_view_guest_delegate_
->CreateAppDelegate());
105 AppViewGuest::~AppViewGuest() {
108 WindowController
* AppViewGuest::GetExtensionWindowController() const {
112 content::WebContents
* AppViewGuest::GetAssociatedWebContents() const {
113 return web_contents();
116 bool AppViewGuest::OnMessageReceived(const IPC::Message
& message
) {
118 IPC_BEGIN_MESSAGE_MAP(AppViewGuest
, message
)
119 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
120 IPC_MESSAGE_UNHANDLED(handled
= false)
121 IPC_END_MESSAGE_MAP()
125 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams
& params
) {
126 if (app_view_guest_delegate_
) {
127 return app_view_guest_delegate_
->HandleContextMenu(web_contents(), params
);
132 void AppViewGuest::RequestMediaAccessPermission(
133 content::WebContents
* web_contents
,
134 const content::MediaStreamRequest
& request
,
135 const content::MediaResponseCallback
& callback
) {
136 if (!app_delegate_
) {
137 WebContentsDelegate::RequestMediaAccessPermission(web_contents
, request
,
141 const ExtensionSet
& enabled_extensions
=
142 ExtensionRegistry::Get(browser_context())->enabled_extensions();
143 const Extension
* guest_extension
=
144 enabled_extensions
.GetByID(guest_extension_id_
);
146 app_delegate_
->RequestMediaAccessPermission(web_contents
, request
, callback
,
150 bool AppViewGuest::CheckMediaAccessPermission(
151 content::WebContents
* web_contents
,
152 const GURL
& security_origin
,
153 content::MediaStreamType type
) {
154 if (!app_delegate_
) {
155 return WebContentsDelegate::CheckMediaAccessPermission(
156 web_contents
, security_origin
, type
);
158 const ExtensionSet
& enabled_extensions
=
159 ExtensionRegistry::Get(browser_context())->enabled_extensions();
160 const Extension
* guest_extension
=
161 enabled_extensions
.GetByID(guest_extension_id_
);
163 return app_delegate_
->CheckMediaAccessPermission(
164 web_contents
, security_origin
, type
, guest_extension
);
167 bool AppViewGuest::CanRunInDetachedState() const {
171 void AppViewGuest::CreateWebContents(
172 const base::DictionaryValue
& create_params
,
173 const WebContentsCreatedCallback
& callback
) {
175 if (!create_params
.GetString(appview::kAppID
, &app_id
)) {
176 callback
.Run(nullptr);
179 // Verifying that the appId is not the same as the host application.
180 if (owner_host() == app_id
) {
181 callback
.Run(nullptr);
184 const base::DictionaryValue
* data
= nullptr;
185 if (!create_params
.GetDictionary(appview::kData
, &data
)) {
186 callback
.Run(nullptr);
190 const ExtensionSet
& enabled_extensions
=
191 ExtensionRegistry::Get(browser_context())->enabled_extensions();
192 const Extension
* guest_extension
= enabled_extensions
.GetByID(app_id
);
193 const Extension
* embedder_extension
=
194 enabled_extensions
.GetByID(GetOwnerSiteURL().host());
196 if (!guest_extension
|| !guest_extension
->is_platform_app() ||
197 !embedder_extension
| !embedder_extension
->is_platform_app()) {
198 callback
.Run(nullptr);
202 pending_response_map
.Get().insert(
203 std::make_pair(guest_instance_id(),
204 make_linked_ptr(new ResponseInfo(
206 weak_ptr_factory_
.GetWeakPtr(),
209 LazyBackgroundTaskQueue
* queue
=
210 LazyBackgroundTaskQueue::Get(browser_context());
211 if (queue
->ShouldEnqueueTask(browser_context(), guest_extension
)) {
212 queue
->AddPendingTask(browser_context(),
213 guest_extension
->id(),
215 &AppViewGuest::LaunchAppAndFireEvent
,
216 weak_ptr_factory_
.GetWeakPtr(),
217 base::Passed(make_scoped_ptr(data
->DeepCopy())),
222 ProcessManager
* process_manager
= ProcessManager::Get(browser_context());
223 ExtensionHost
* host
=
224 process_manager
->GetBackgroundHostForExtension(guest_extension
->id());
226 LaunchAppAndFireEvent(make_scoped_ptr(data
->DeepCopy()), callback
, host
);
229 void AppViewGuest::DidInitialize(const base::DictionaryValue
& create_params
) {
230 extension_function_dispatcher_
.reset(
231 new ExtensionFunctionDispatcher(browser_context(), this));
233 if (!url_
.is_valid())
236 web_contents()->GetController().LoadURL(
237 url_
, content::Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
240 const char* AppViewGuest::GetAPINamespace() const {
241 return appview::kEmbedderAPINamespace
;
244 int AppViewGuest::GetTaskPrefix() const {
245 return IDS_EXTENSION_TASK_MANAGER_APPVIEW_TAG_PREFIX
;
248 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params
& params
) {
249 extension_function_dispatcher_
->Dispatch(params
,
250 web_contents()->GetRenderViewHost());
253 void AppViewGuest::CompleteCreateWebContents(
255 const Extension
* guest_extension
,
256 const WebContentsCreatedCallback
& callback
) {
257 if (!url
.is_valid()) {
258 callback
.Run(nullptr);
262 guest_extension_id_
= guest_extension
->id();
264 WebContents::CreateParams
params(
266 content::SiteInstance::CreateForURL(browser_context(),
267 guest_extension
->url()));
268 params
.guest_delegate
= this;
269 callback
.Run(WebContents::Create(params
));
272 void AppViewGuest::LaunchAppAndFireEvent(
273 scoped_ptr
<base::DictionaryValue
> data
,
274 const WebContentsCreatedCallback
& callback
,
275 ExtensionHost
* extension_host
) {
276 ExtensionSystem
* system
= ExtensionSystem::Get(browser_context());
277 bool has_event_listener
= system
->event_router()->ExtensionHasEventListener(
278 extension_host
->extension()->id(),
279 app_runtime::OnEmbedRequested::kEventName
);
280 if (!has_event_listener
) {
281 callback
.Run(nullptr);
285 scoped_ptr
<base::DictionaryValue
> embed_request(new base::DictionaryValue());
286 embed_request
->SetInteger(appview::kGuestInstanceID
, guest_instance_id());
287 embed_request
->SetString(appview::kEmbedderID
, owner_host());
288 embed_request
->Set(appview::kData
, data
.release());
289 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
290 browser_context(), embed_request
.Pass(), extension_host
->extension());
293 void AppViewGuest::SetAppDelegateForTest(AppDelegate
* delegate
) {
294 app_delegate_
.reset(delegate
);
297 } // namespace extensions