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_process_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/bad_message.h"
15 #include "extensions/browser/event_router.h"
16 #include "extensions/browser/extension_host.h"
17 #include "extensions/browser/extension_registry.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::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 content::RenderProcessHost
* guest_render_process_host
) {
69 PendingResponseMap
* response_map
= pending_response_map
.Pointer();
70 PendingResponseMap::iterator it
= response_map
->find(guest_instance_id
);
71 // Kill the requesting process if it is not the real guest.
72 if (it
== response_map
->end()) {
73 // The requester used an invalid |guest_instance_id|.
74 bad_message::ReceivedBadMessage(guest_render_process_host
,
75 bad_message::AVG_BAD_INST_ID
);
79 linked_ptr
<ResponseInfo
> response_info
= it
->second
;
80 if (!response_info
->app_view_guest
||
81 (response_info
->guest_extension
->id() != guest_extension_id
)) {
82 // The app is trying to communicate with an <appview> not assigned to it, or
83 // the <appview> is already dead "nullptr".
84 bad_message::BadMessageReason reason
= !response_info
->app_view_guest
85 ? bad_message::AVG_NULL_AVG
86 : bad_message::AVG_BAD_EXT_ID
;
87 bad_message::ReceivedBadMessage(guest_render_process_host
, reason
);
91 response_info
->app_view_guest
->CompleteCreateWebContents(
92 url
, response_info
->guest_extension
.get(), response_info
->callback
);
94 response_map
->erase(guest_instance_id
);
99 GuestViewBase
* AppViewGuest::Create(content::WebContents
* owner_web_contents
) {
100 return new AppViewGuest(owner_web_contents
);
103 AppViewGuest::AppViewGuest(content::WebContents
* owner_web_contents
)
104 : GuestView
<AppViewGuest
>(owner_web_contents
),
105 app_view_guest_delegate_(
106 ExtensionsAPIClient::Get()->CreateAppViewGuestDelegate()),
107 weak_ptr_factory_(this) {
108 if (app_view_guest_delegate_
)
109 app_delegate_
.reset(app_view_guest_delegate_
->CreateAppDelegate());
112 AppViewGuest::~AppViewGuest() {
115 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams
& params
) {
116 if (app_view_guest_delegate_
) {
117 return app_view_guest_delegate_
->HandleContextMenu(web_contents(), params
);
122 void AppViewGuest::RequestMediaAccessPermission(
123 content::WebContents
* web_contents
,
124 const content::MediaStreamRequest
& request
,
125 const content::MediaResponseCallback
& callback
) {
126 if (!app_delegate_
) {
127 WebContentsDelegate::RequestMediaAccessPermission(web_contents
, request
,
131 const ExtensionSet
& enabled_extensions
=
132 ExtensionRegistry::Get(browser_context())->enabled_extensions();
133 const Extension
* guest_extension
=
134 enabled_extensions
.GetByID(guest_extension_id_
);
136 app_delegate_
->RequestMediaAccessPermission(web_contents
, request
, callback
,
140 bool AppViewGuest::CheckMediaAccessPermission(
141 content::WebContents
* web_contents
,
142 const GURL
& security_origin
,
143 content::MediaStreamType type
) {
144 if (!app_delegate_
) {
145 return WebContentsDelegate::CheckMediaAccessPermission(
146 web_contents
, security_origin
, type
);
148 const ExtensionSet
& enabled_extensions
=
149 ExtensionRegistry::Get(browser_context())->enabled_extensions();
150 const Extension
* guest_extension
=
151 enabled_extensions
.GetByID(guest_extension_id_
);
153 return app_delegate_
->CheckMediaAccessPermission(
154 web_contents
, security_origin
, type
, guest_extension
);
157 bool AppViewGuest::CanRunInDetachedState() const {
161 void AppViewGuest::CreateWebContents(
162 const base::DictionaryValue
& create_params
,
163 const WebContentsCreatedCallback
& callback
) {
165 if (!create_params
.GetString(appview::kAppID
, &app_id
)) {
166 callback
.Run(nullptr);
169 // Verifying that the appId is not the same as the host application.
170 if (owner_host() == app_id
) {
171 callback
.Run(nullptr);
174 const base::DictionaryValue
* data
= nullptr;
175 if (!create_params
.GetDictionary(appview::kData
, &data
)) {
176 callback
.Run(nullptr);
180 const ExtensionSet
& enabled_extensions
=
181 ExtensionRegistry::Get(browser_context())->enabled_extensions();
182 const Extension
* guest_extension
= enabled_extensions
.GetByID(app_id
);
183 const Extension
* embedder_extension
=
184 enabled_extensions
.GetByID(GetOwnerSiteURL().host());
186 if (!guest_extension
|| !guest_extension
->is_platform_app() ||
187 !embedder_extension
| !embedder_extension
->is_platform_app()) {
188 callback
.Run(nullptr);
192 pending_response_map
.Get().insert(
193 std::make_pair(guest_instance_id(),
194 make_linked_ptr(new ResponseInfo(
196 weak_ptr_factory_
.GetWeakPtr(),
199 LazyBackgroundTaskQueue
* queue
=
200 LazyBackgroundTaskQueue::Get(browser_context());
201 if (queue
->ShouldEnqueueTask(browser_context(), guest_extension
)) {
202 queue
->AddPendingTask(browser_context(),
203 guest_extension
->id(),
205 &AppViewGuest::LaunchAppAndFireEvent
,
206 weak_ptr_factory_
.GetWeakPtr(),
207 base::Passed(make_scoped_ptr(data
->DeepCopy())),
212 ProcessManager
* process_manager
= ProcessManager::Get(browser_context());
213 ExtensionHost
* host
=
214 process_manager
->GetBackgroundHostForExtension(guest_extension
->id());
216 LaunchAppAndFireEvent(make_scoped_ptr(data
->DeepCopy()), callback
, host
);
219 void AppViewGuest::DidInitialize(const base::DictionaryValue
& create_params
) {
220 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
222 if (!url_
.is_valid())
225 web_contents()->GetController().LoadURL(
226 url_
, content::Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
229 const char* AppViewGuest::GetAPINamespace() const {
230 return appview::kEmbedderAPINamespace
;
233 int AppViewGuest::GetTaskPrefix() const {
234 return IDS_EXTENSION_TASK_MANAGER_APPVIEW_TAG_PREFIX
;
237 void AppViewGuest::CompleteCreateWebContents(
239 const Extension
* guest_extension
,
240 const WebContentsCreatedCallback
& callback
) {
241 if (!url
.is_valid()) {
242 callback
.Run(nullptr);
246 guest_extension_id_
= guest_extension
->id();
248 WebContents::CreateParams
params(
250 content::SiteInstance::CreateForURL(browser_context(),
251 guest_extension
->url()));
252 params
.guest_delegate
= this;
253 callback
.Run(WebContents::Create(params
));
256 void AppViewGuest::LaunchAppAndFireEvent(
257 scoped_ptr
<base::DictionaryValue
> data
,
258 const WebContentsCreatedCallback
& callback
,
259 ExtensionHost
* extension_host
) {
260 bool has_event_listener
= EventRouter::Get(browser_context())
261 ->ExtensionHasEventListener(
262 extension_host
->extension()->id(),
263 app_runtime::OnEmbedRequested::kEventName
);
264 if (!has_event_listener
) {
265 callback
.Run(nullptr);
269 scoped_ptr
<base::DictionaryValue
> embed_request(new base::DictionaryValue());
270 embed_request
->SetInteger(appview::kGuestInstanceID
, guest_instance_id());
271 embed_request
->SetString(appview::kEmbedderID
, owner_host());
272 embed_request
->Set(appview::kData
, data
.release());
273 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
274 browser_context(), embed_request
.Pass(), extension_host
->extension());
277 void AppViewGuest::SetAppDelegateForTest(AppDelegate
* delegate
) {
278 app_delegate_
.reset(delegate
);
281 std::vector
<int> AppViewGuest::GetAllRegisteredInstanceIdsForTesting() {
282 std::vector
<int> instances
;
283 for (const auto& key_value
: pending_response_map
.Get()) {
284 instances
.push_back(key_value
.first
);
289 } // namespace extensions