Remove unused parameter.
[chromium-blink-merge.git] / extensions / browser / guest_view / app_view / app_view_guest.cc
blob328ef0b209fdcccc91390e7511e105e322ba6170
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 "content/public/browser/render_view_host.h"
9 #include "content/public/common/renderer_preferences.h"
10 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
11 #include "extensions/browser/api/extensions_api_client.h"
12 #include "extensions/browser/app_window/app_delegate.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extension_system.h"
17 #include "extensions/browser/guest_view/app_view/app_view_constants.h"
18 #include "extensions/browser/guest_view/guest_view_manager.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;
33 namespace extensions {
35 namespace {
37 struct ResponseInfo {
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),
47 callback(callback) {}
49 ~ResponseInfo() {}
52 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap;
53 static base::LazyInstance<PendingResponseMap> pending_response_map =
54 LAZY_INSTANCE_INITIALIZER;
56 } // namespace
58 // static.
59 const char AppViewGuest::Type[] = "appview";
61 // static.
62 bool AppViewGuest::CompletePendingRequest(
63 content::BrowserContext* browser_context,
64 const GURL& url,
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
71 // kill it.
72 return false;
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.
80 return false;
83 response_info->app_view_guest->CompleteCreateWebContents(
84 url, response_info->guest_extension.get(), response_info->callback);
86 response_map->erase(guest_instance_id);
87 return true;
90 // static
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 WindowController* AppViewGuest::GetExtensionWindowController() const {
108 return nullptr;
111 content::WebContents* AppViewGuest::GetAssociatedWebContents() const {
112 return web_contents();
115 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) {
116 bool handled = true;
117 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message)
118 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
119 IPC_MESSAGE_UNHANDLED(handled = false)
120 IPC_END_MESSAGE_MAP()
121 return handled;
124 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) {
125 if (app_view_guest_delegate_) {
126 return app_view_guest_delegate_->HandleContextMenu(web_contents(), params);
128 return false;
131 void AppViewGuest::RequestMediaAccessPermission(
132 content::WebContents* web_contents,
133 const content::MediaStreamRequest& request,
134 const content::MediaResponseCallback& callback) {
135 if (!app_delegate_) {
136 WebContentsDelegate::RequestMediaAccessPermission(web_contents, request,
137 callback);
138 return;
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 app_delegate_->RequestMediaAccessPermission(web_contents, request, callback,
146 guest_extension);
149 bool AppViewGuest::CheckMediaAccessPermission(
150 content::WebContents* web_contents,
151 const GURL& security_origin,
152 content::MediaStreamType type) {
153 if (!app_delegate_) {
154 return WebContentsDelegate::CheckMediaAccessPermission(
155 web_contents, security_origin, type);
157 const ExtensionSet& enabled_extensions =
158 ExtensionRegistry::Get(browser_context())->enabled_extensions();
159 const Extension* guest_extension =
160 enabled_extensions.GetByID(guest_extension_id_);
162 return app_delegate_->CheckMediaAccessPermission(
163 web_contents, security_origin, type, guest_extension);
166 bool AppViewGuest::CanRunInDetachedState() const {
167 return true;
170 void AppViewGuest::CreateWebContents(
171 const base::DictionaryValue& create_params,
172 const WebContentsCreatedCallback& callback) {
173 std::string app_id;
174 if (!create_params.GetString(appview::kAppID, &app_id)) {
175 callback.Run(nullptr);
176 return;
179 const base::DictionaryValue* data = nullptr;
180 if (!create_params.GetDictionary(appview::kData, &data)) {
181 callback.Run(nullptr);
182 return;
185 const ExtensionSet& enabled_extensions =
186 ExtensionRegistry::Get(browser_context())->enabled_extensions();
187 const Extension* guest_extension = enabled_extensions.GetByID(app_id);
188 const Extension* embedder_extension =
189 enabled_extensions.GetByID(GetOwnerSiteURL().host());
191 if (!guest_extension || !guest_extension->is_platform_app() ||
192 !embedder_extension | !embedder_extension->is_platform_app()) {
193 callback.Run(nullptr);
194 return;
197 pending_response_map.Get().insert(
198 std::make_pair(guest_instance_id(),
199 make_linked_ptr(new ResponseInfo(
200 guest_extension,
201 weak_ptr_factory_.GetWeakPtr(),
202 callback))));
204 LazyBackgroundTaskQueue* queue =
205 ExtensionSystem::Get(browser_context())->lazy_background_task_queue();
206 if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) {
207 queue->AddPendingTask(browser_context(),
208 guest_extension->id(),
209 base::Bind(
210 &AppViewGuest::LaunchAppAndFireEvent,
211 weak_ptr_factory_.GetWeakPtr(),
212 base::Passed(make_scoped_ptr(data->DeepCopy())),
213 callback));
214 return;
217 ProcessManager* process_manager = ProcessManager::Get(browser_context());
218 ExtensionHost* host =
219 process_manager->GetBackgroundHostForExtension(guest_extension->id());
220 DCHECK(host);
221 LaunchAppAndFireEvent(make_scoped_ptr(data->DeepCopy()), callback, host);
224 void AppViewGuest::DidInitialize(const base::DictionaryValue& create_params) {
225 extension_function_dispatcher_.reset(
226 new ExtensionFunctionDispatcher(browser_context(), this));
228 if (!url_.is_valid())
229 return;
231 web_contents()->GetController().LoadURL(
232 url_, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string());
235 const char* AppViewGuest::GetAPINamespace() const {
236 return appview::kEmbedderAPINamespace;
239 int AppViewGuest::GetTaskPrefix() const {
240 return IDS_EXTENSION_TASK_MANAGER_APPVIEW_TAG_PREFIX;
243 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) {
244 extension_function_dispatcher_->Dispatch(params,
245 web_contents()->GetRenderViewHost());
248 void AppViewGuest::CompleteCreateWebContents(
249 const GURL& url,
250 const Extension* guest_extension,
251 const WebContentsCreatedCallback& callback) {
252 if (!url.is_valid()) {
253 callback.Run(nullptr);
254 return;
256 url_ = url;
257 guest_extension_id_ = guest_extension->id();
259 WebContents::CreateParams params(
260 browser_context(),
261 content::SiteInstance::CreateForURL(browser_context(),
262 guest_extension->url()));
263 params.guest_delegate = this;
264 callback.Run(WebContents::Create(params));
267 void AppViewGuest::LaunchAppAndFireEvent(
268 scoped_ptr<base::DictionaryValue> data,
269 const WebContentsCreatedCallback& callback,
270 ExtensionHost* extension_host) {
271 ExtensionSystem* system = ExtensionSystem::Get(browser_context());
272 bool has_event_listener = system->event_router()->ExtensionHasEventListener(
273 extension_host->extension()->id(),
274 app_runtime::OnEmbedRequested::kEventName);
275 if (!has_event_listener) {
276 callback.Run(nullptr);
277 return;
280 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue());
281 embed_request->SetInteger(appview::kGuestInstanceID, guest_instance_id());
282 embed_request->SetString(appview::kEmbedderID, owner_extension_id());
283 embed_request->Set(appview::kData, data.release());
284 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
285 browser_context(), embed_request.Pass(), extension_host->extension());
288 void AppViewGuest::SetAppDelegateForTest(AppDelegate* delegate) {
289 app_delegate_.reset(delegate);
292 } // namespace extensions