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 "chrome/browser/guest_view/app_view/app_view_guest.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/guest_view/app_view/app_view_constants.h"
11 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/renderer_context_menu/context_menu_delegate.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/common/renderer_preferences.h"
16 #include "extensions/browser/api/app_runtime/app_runtime_api.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/browser/extension_host.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/browser/guest_view/guest_view_manager.h"
21 #include "extensions/browser/lazy_background_task_queue.h"
22 #include "extensions/browser/view_type_utils.h"
23 #include "extensions/common/api/app_runtime.h"
24 #include "extensions/common/extension_messages.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 {
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->
84 CompleteCreateWebContents(url,
85 response_info->guest_extension,
86 response_info->callback);
88 response_map->erase(guest_instance_id);
93 GuestViewBase* AppViewGuest::Create(content::BrowserContext* browser_context,
94 int guest_instance_id) {
95 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
96 switches::kEnableAppView)) {
99 return new AppViewGuest(browser_context, guest_instance_id);
102 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context,
103 int guest_instance_id)
104 : GuestView<AppViewGuest>(browser_context, guest_instance_id),
105 weak_ptr_factory_(this) {
108 AppViewGuest::~AppViewGuest() {
111 WindowController* AppViewGuest::GetExtensionWindowController() const {
115 content::WebContents* AppViewGuest::GetAssociatedWebContents() const {
116 return guest_web_contents();
119 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) {
121 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message)
122 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
123 IPC_MESSAGE_UNHANDLED(handled = false)
124 IPC_END_MESSAGE_MAP()
128 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) {
129 ContextMenuDelegate* menu_delegate =
130 ContextMenuDelegate::FromWebContents(guest_web_contents());
131 DCHECK(menu_delegate);
133 scoped_ptr<RenderViewContextMenu> menu =
134 menu_delegate->BuildMenu(guest_web_contents(), params);
135 menu_delegate->ShowMenu(menu.Pass());
139 const char* AppViewGuest::GetAPINamespace() {
140 return appview::kEmbedderAPINamespace;
143 void AppViewGuest::CreateWebContents(
144 const std::string& embedder_extension_id,
145 int embedder_render_process_id,
146 const base::DictionaryValue& create_params,
147 const WebContentsCreatedCallback& callback) {
149 if (!create_params.GetString(appview::kAppID, &app_id)) {
154 const base::DictionaryValue* data = NULL;
155 if (!create_params.GetDictionary(appview::kData, &data)) {
160 ExtensionService* service =
161 ExtensionSystem::Get(browser_context())->extension_service();
162 const Extension* guest_extension = service->GetExtensionById(app_id, false);
163 const Extension* embedder_extension =
164 service->GetExtensionById(embedder_extension_id, false);
166 if (!guest_extension || !guest_extension->is_platform_app() ||
167 !embedder_extension | !embedder_extension->is_platform_app()) {
172 pending_response_map.Get().insert(
173 std::make_pair(GetGuestInstanceID(),
174 make_linked_ptr(new ResponseInfo(
176 weak_ptr_factory_.GetWeakPtr(),
179 LazyBackgroundTaskQueue* queue =
180 ExtensionSystem::Get(browser_context())->lazy_background_task_queue();
181 if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) {
182 queue->AddPendingTask(browser_context(),
183 guest_extension->id(),
185 &AppViewGuest::LaunchAppAndFireEvent,
186 weak_ptr_factory_.GetWeakPtr(),
187 base::Passed(make_scoped_ptr(data->DeepCopy())),
192 ProcessManager* process_manager =
193 ExtensionSystem::Get(browser_context())->process_manager();
194 ExtensionHost* host =
195 process_manager->GetBackgroundHostForExtension(guest_extension->id());
197 LaunchAppAndFireEvent(make_scoped_ptr(data->DeepCopy()), callback, host);
200 void AppViewGuest::DidAttachToEmbedder() {
201 // This is called after the guest process has been attached to a host
202 // element. This means that the host element knows how to route input
203 // events to the guest, and the guest knows how to get frames to the
205 guest_web_contents()->GetController().LoadURL(
206 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string());
209 void AppViewGuest::DidInitialize() {
210 extension_function_dispatcher_.reset(
211 new ExtensionFunctionDispatcher(browser_context(), this));
214 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) {
215 extension_function_dispatcher_->Dispatch(
216 params, guest_web_contents()->GetRenderViewHost());
219 void AppViewGuest::CompleteCreateWebContents(
221 const Extension* guest_extension,
222 const WebContentsCreatedCallback& callback) {
223 if (!url.is_valid()) {
228 guest_extension_id_ = guest_extension->id();
230 WebContents::CreateParams params(
232 content::SiteInstance::CreateForURL(browser_context(),
233 guest_extension->url()));
234 params.guest_delegate = this;
235 callback.Run(WebContents::Create(params));
238 void AppViewGuest::LaunchAppAndFireEvent(
239 scoped_ptr<base::DictionaryValue> data,
240 const WebContentsCreatedCallback& callback,
241 ExtensionHost* extension_host) {
242 ExtensionSystem* system = ExtensionSystem::Get(browser_context());
243 bool has_event_listener = system->event_router()->ExtensionHasEventListener(
244 extension_host->extension()->id(),
245 app_runtime::OnEmbedRequested::kEventName);
246 if (!has_event_listener) {
251 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue());
252 embed_request->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID());
253 embed_request->SetString(appview::kEmbedderID, embedder_extension_id());
254 embed_request->Set(appview::kData, data.release());
255 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
256 browser_context(), embed_request.Pass(), extension_host->extension());
259 } // namespace extensions