Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / guest_view / app_view / app_view_guest.cc
blob64bcd906eee20357c1cd13cbabb722d4130cbe27
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 {
36 namespace {
38 struct ResponseInfo {
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),
48 callback(callback) {}
50 ~ResponseInfo() {}
53 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap;
54 static base::LazyInstance<PendingResponseMap> pending_response_map =
55 LAZY_INSTANCE_INITIALIZER;
57 } // namespace
59 // static.
60 const char AppViewGuest::Type[] = "appview";
62 // static.
63 bool AppViewGuest::CompletePendingRequest(
64 content::BrowserContext* browser_context,
65 const GURL& url,
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);
76 return false;
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);
88 return false;
91 response_info->app_view_guest->CompleteCreateWebContents(
92 url, response_info->guest_extension.get(), response_info->callback);
94 response_map->erase(guest_instance_id);
95 return true;
98 // static
99 GuestViewBase* AppViewGuest::Create(WebContents* owner_web_contents) {
100 return new AppViewGuest(owner_web_contents);
103 AppViewGuest::AppViewGuest(WebContents* owner_web_contents)
104 : GuestView<AppViewGuest>(owner_web_contents),
105 app_view_guest_delegate_(ExtensionsAPIClient::Get()
106 ->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);
119 return false;
122 void AppViewGuest::RequestMediaAccessPermission(
123 WebContents* web_contents,
124 const content::MediaStreamRequest& request,
125 const content::MediaResponseCallback& callback) {
126 if (!app_delegate_) {
127 WebContentsDelegate::RequestMediaAccessPermission(web_contents, request,
128 callback);
129 return;
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,
137 guest_extension);
140 bool AppViewGuest::CheckMediaAccessPermission(WebContents* web_contents,
141 const GURL& security_origin,
142 content::MediaStreamType type) {
143 if (!app_delegate_) {
144 return WebContentsDelegate::CheckMediaAccessPermission(
145 web_contents, security_origin, type);
147 const ExtensionSet& enabled_extensions =
148 ExtensionRegistry::Get(browser_context())->enabled_extensions();
149 const Extension* guest_extension =
150 enabled_extensions.GetByID(guest_extension_id_);
152 return app_delegate_->CheckMediaAccessPermission(
153 web_contents, security_origin, type, guest_extension);
156 bool AppViewGuest::CanRunInDetachedState() const {
157 return true;
160 void AppViewGuest::CreateWebContents(
161 const base::DictionaryValue& create_params,
162 const WebContentsCreatedCallback& callback) {
163 std::string app_id;
164 if (!create_params.GetString(appview::kAppID, &app_id)) {
165 callback.Run(nullptr);
166 return;
168 // Verifying that the appId is not the same as the host application.
169 if (owner_host() == app_id) {
170 callback.Run(nullptr);
171 return;
173 const base::DictionaryValue* data = nullptr;
174 if (!create_params.GetDictionary(appview::kData, &data)) {
175 callback.Run(nullptr);
176 return;
179 const ExtensionSet& enabled_extensions =
180 ExtensionRegistry::Get(browser_context())->enabled_extensions();
181 const Extension* guest_extension = enabled_extensions.GetByID(app_id);
182 const Extension* embedder_extension =
183 enabled_extensions.GetByID(GetOwnerSiteURL().host());
185 if (!guest_extension || !guest_extension->is_platform_app() ||
186 !embedder_extension | !embedder_extension->is_platform_app()) {
187 callback.Run(nullptr);
188 return;
191 pending_response_map.Get().insert(
192 std::make_pair(guest_instance_id(),
193 make_linked_ptr(new ResponseInfo(
194 guest_extension,
195 weak_ptr_factory_.GetWeakPtr(),
196 callback))));
198 LazyBackgroundTaskQueue* queue =
199 LazyBackgroundTaskQueue::Get(browser_context());
200 if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) {
201 queue->AddPendingTask(browser_context(),
202 guest_extension->id(),
203 base::Bind(
204 &AppViewGuest::LaunchAppAndFireEvent,
205 weak_ptr_factory_.GetWeakPtr(),
206 base::Passed(make_scoped_ptr(data->DeepCopy())),
207 callback));
208 return;
211 ProcessManager* process_manager = ProcessManager::Get(browser_context());
212 ExtensionHost* host =
213 process_manager->GetBackgroundHostForExtension(guest_extension->id());
214 DCHECK(host);
215 LaunchAppAndFireEvent(make_scoped_ptr(data->DeepCopy()), callback, host);
218 void AppViewGuest::DidInitialize(const base::DictionaryValue& create_params) {
219 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
221 if (!url_.is_valid())
222 return;
224 web_contents()->GetController().LoadURL(
225 url_, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
228 const char* AppViewGuest::GetAPINamespace() const {
229 return appview::kEmbedderAPINamespace;
232 int AppViewGuest::GetTaskPrefix() const {
233 return IDS_EXTENSION_TASK_MANAGER_APPVIEW_TAG_PREFIX;
236 void AppViewGuest::CompleteCreateWebContents(
237 const GURL& url,
238 const Extension* guest_extension,
239 const WebContentsCreatedCallback& callback) {
240 if (!url.is_valid()) {
241 callback.Run(nullptr);
242 return;
244 url_ = url;
245 guest_extension_id_ = guest_extension->id();
247 WebContents::CreateParams params(
248 browser_context(),
249 content::SiteInstance::CreateForURL(browser_context(),
250 guest_extension->url()));
251 params.guest_delegate = this;
252 callback.Run(WebContents::Create(params));
255 void AppViewGuest::LaunchAppAndFireEvent(
256 scoped_ptr<base::DictionaryValue> data,
257 const WebContentsCreatedCallback& callback,
258 ExtensionHost* extension_host) {
259 bool has_event_listener = EventRouter::Get(browser_context())
260 ->ExtensionHasEventListener(
261 extension_host->extension()->id(),
262 app_runtime::OnEmbedRequested::kEventName);
263 if (!has_event_listener) {
264 callback.Run(nullptr);
265 return;
268 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue());
269 embed_request->SetInteger(appview::kGuestInstanceID, guest_instance_id());
270 embed_request->SetString(appview::kEmbedderID, owner_host());
271 embed_request->Set(appview::kData, data.release());
272 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
273 browser_context(), embed_request.Pass(), extension_host->extension());
276 void AppViewGuest::SetAppDelegateForTest(AppDelegate* delegate) {
277 app_delegate_.reset(delegate);
280 std::vector<int> AppViewGuest::GetAllRegisteredInstanceIdsForTesting() {
281 std::vector<int> instances;
282 for (const auto& key_value : pending_response_map.Get()) {
283 instances.push_back(key_value.first);
285 return instances;
288 } // namespace extensions