OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/guest_view/app_view/app_view_guest.h" | 5 #include "chrome/browser/guest_view/app_view/app_view_guest.h" |
6 | 6 |
7 // static | 7 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/guest_view/app_view/app_view_constants.h" |
| 10 #include "chrome/browser/guest_view/guest_view_manager.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_context_menu/context_menu_delegate.h" |
| 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.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/lazy_background_task_queue.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 "ipc/ipc_message_macros.h" |
| 25 |
| 26 namespace app_runtime = extensions::core_api::app_runtime; |
| 27 |
| 28 using content::RenderFrameHost; |
| 29 using content::WebContents; |
| 30 using extensions::ExtensionHost; |
| 31 |
| 32 namespace { |
| 33 |
| 34 struct ResponseInfo { |
| 35 scoped_refptr<const extensions::Extension> guest_extension; |
| 36 base::WeakPtr<AppViewGuest> app_view_guest; |
| 37 GuestViewBase::WebContentsCreatedCallback callback; |
| 38 |
| 39 ResponseInfo(const extensions::Extension* guest_extension, |
| 40 const base::WeakPtr<AppViewGuest>& app_view_guest, |
| 41 const GuestViewBase::WebContentsCreatedCallback& callback) |
| 42 : guest_extension(guest_extension), |
| 43 app_view_guest(app_view_guest), |
| 44 callback(callback) {} |
| 45 |
| 46 ~ResponseInfo() {} |
| 47 }; |
| 48 |
| 49 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap; |
| 50 static base::LazyInstance<PendingResponseMap> pending_response_map = |
| 51 LAZY_INSTANCE_INITIALIZER; |
| 52 |
| 53 } // namespace |
| 54 |
| 55 // static. |
8 const char AppViewGuest::Type[] = "appview"; | 56 const char AppViewGuest::Type[] = "appview"; |
9 | 57 |
| 58 // static. |
| 59 bool AppViewGuest::CompletePendingRequest( |
| 60 const GURL& url, |
| 61 int guest_instance_id, |
| 62 content::BrowserContext* browser_context, |
| 63 const std::string& guest_extension_id) { |
| 64 PendingResponseMap* response_map = pending_response_map.Pointer(); |
| 65 PendingResponseMap::iterator it = response_map->find(guest_instance_id); |
| 66 if (it == response_map->end()) { |
| 67 // An app is sending invalid responses. We should probably kill it. |
| 68 return false; |
| 69 } |
| 70 |
| 71 linked_ptr<ResponseInfo> response_info = it->second; |
| 72 if (!response_info->app_view_guest || |
| 73 (response_info->guest_extension->id() != guest_extension_id)) { |
| 74 // An app is trying to respond to an <appview> that didn't initiate |
| 75 // communication with it. We should kill the app here. |
| 76 return false; |
| 77 } |
| 78 |
| 79 response_info->app_view_guest-> |
| 80 CompleteCreateWebContents(url, |
| 81 response_info->guest_extension, |
| 82 response_info->callback); |
| 83 |
| 84 response_map->erase(guest_instance_id); |
| 85 return true; |
| 86 } |
| 87 |
10 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, | 88 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, |
11 int guest_instance_id) | 89 int guest_instance_id) |
12 : GuestView<AppViewGuest>(browser_context, guest_instance_id) { | 90 : GuestView<AppViewGuest>(browser_context, guest_instance_id), |
| 91 weak_ptr_factory_(this) { |
13 } | 92 } |
14 | 93 |
15 AppViewGuest::~AppViewGuest() { | 94 AppViewGuest::~AppViewGuest() { |
16 } | 95 } |
17 | 96 |
| 97 extensions::WindowController* AppViewGuest::GetExtensionWindowController() |
| 98 const { |
| 99 return NULL; |
| 100 } |
| 101 |
| 102 content::WebContents* AppViewGuest::GetAssociatedWebContents() const { |
| 103 return guest_web_contents(); |
| 104 } |
| 105 |
| 106 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) { |
| 107 bool handled = true; |
| 108 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message) |
| 109 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 110 IPC_MESSAGE_UNHANDLED(handled = false) |
| 111 IPC_END_MESSAGE_MAP() |
| 112 return handled; |
| 113 } |
| 114 |
| 115 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) { |
| 116 ContextMenuDelegate* menu_delegate = |
| 117 ContextMenuDelegate::FromWebContents(guest_web_contents()); |
| 118 DCHECK(menu_delegate); |
| 119 |
| 120 scoped_ptr<RenderViewContextMenu> menu = |
| 121 menu_delegate->BuildMenu(guest_web_contents(), params); |
| 122 menu_delegate->ShowMenu(menu.Pass()); |
| 123 return true; |
| 124 } |
| 125 |
18 void AppViewGuest::CreateWebContents( | 126 void AppViewGuest::CreateWebContents( |
19 const std::string& embedder_extension_id, | 127 const std::string& embedder_extension_id, |
20 int embedder_render_process_id, | 128 int embedder_render_process_id, |
21 const base::DictionaryValue& create_params, | 129 const base::DictionaryValue& create_params, |
22 const WebContentsCreatedCallback& callback) { | 130 const WebContentsCreatedCallback& callback) { |
23 // TODO(fsamuel): Create a WebContents with the appropriate SiteInstance here. | 131 std::string app_id; |
24 // After the WebContents has been created, call the |callback|. | 132 if (!create_params.GetString(appview::kAppID, &app_id)) { |
25 // callback.Run(new_web_contents); | 133 callback.Run(NULL); |
| 134 return; |
| 135 } |
| 136 |
| 137 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 138 ExtensionService* service = |
| 139 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 140 const extensions::Extension* guest_extension = |
| 141 service->GetExtensionById(app_id, false); |
| 142 const extensions::Extension* embedder_extension = |
| 143 service->GetExtensionById(embedder_extension_id, false); |
| 144 |
| 145 if (!guest_extension || !guest_extension->is_platform_app() || |
| 146 !embedder_extension | !embedder_extension->is_platform_app()) { |
| 147 callback.Run(NULL); |
| 148 return; |
| 149 } |
| 150 |
| 151 pending_response_map.Get().insert( |
| 152 std::make_pair(GetGuestInstanceID(), |
| 153 new ResponseInfo(guest_extension, |
| 154 weak_ptr_factory_.GetWeakPtr(), |
| 155 callback))); |
| 156 |
| 157 extensions::LazyBackgroundTaskQueue* queue = |
| 158 extensions::ExtensionSystem::Get(profile)->lazy_background_task_queue(); |
| 159 if (queue->ShouldEnqueueTask(profile, guest_extension)) { |
| 160 queue->AddPendingTask(profile, |
| 161 guest_extension->id(), |
| 162 base::Bind(&AppViewGuest::LaunchAppAndFireEvent, |
| 163 weak_ptr_factory_.GetWeakPtr(), |
| 164 callback)); |
| 165 return; |
| 166 } |
| 167 |
| 168 extensions::ProcessManager* process_manager = |
| 169 extensions::ExtensionSystem::Get(profile)->process_manager(); |
| 170 ExtensionHost* host = |
| 171 process_manager->GetBackgroundHostForExtension(guest_extension->id()); |
| 172 DCHECK(host); |
| 173 LaunchAppAndFireEvent(callback, host); |
26 } | 174 } |
27 | 175 |
28 void AppViewGuest::DidAttachToEmbedder() { | 176 void AppViewGuest::DidAttachToEmbedder() { |
29 // This is called after the guest process has been attached to a host | 177 // This is called after the guest process has been attached to a host |
30 // element. This means that the host element knows how to route input | 178 // element. This means that the host element knows how to route input |
31 // events to the guest, and the guest knows how to get frames to the | 179 // events to the guest, and the guest knows how to get frames to the |
32 // embedder. | 180 // embedder. |
33 // TODO(fsamuel): Perform the initial navigation here. | 181 guest_web_contents()->GetController().LoadURL( |
| 182 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
34 } | 183 } |
| 184 |
| 185 void AppViewGuest::DidInitialize() { |
| 186 extension_function_dispatcher_.reset( |
| 187 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| 188 } |
| 189 |
| 190 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) { |
| 191 extension_function_dispatcher_->Dispatch( |
| 192 params, guest_web_contents()->GetRenderViewHost()); |
| 193 } |
| 194 |
| 195 void AppViewGuest::CompleteCreateWebContents( |
| 196 const GURL& url, |
| 197 const extensions::Extension* guest_extension, |
| 198 const WebContentsCreatedCallback& callback) { |
| 199 if (!url.is_valid()) { |
| 200 callback.Run(NULL); |
| 201 return; |
| 202 } |
| 203 url_ = url; |
| 204 guest_extension_id_ = guest_extension->id(); |
| 205 |
| 206 WebContents::CreateParams params( |
| 207 browser_context(), |
| 208 content::SiteInstance::CreateForURL(browser_context(), |
| 209 guest_extension->url())); |
| 210 params.guest_delegate = this; |
| 211 callback.Run(WebContents::Create(params)); |
| 212 } |
| 213 |
| 214 void AppViewGuest::LaunchAppAndFireEvent( |
| 215 const WebContentsCreatedCallback& callback, |
| 216 ExtensionHost* extension_host) { |
| 217 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 218 extensions::ExtensionSystem* system = |
| 219 extensions::ExtensionSystem::Get(browser_context()); |
| 220 bool has_event_listener = system->event_router()->ExtensionHasEventListener( |
| 221 extension_host->extension()->id(), |
| 222 app_runtime::OnAppEmbeddingRequest::kEventName); |
| 223 if (!has_event_listener) { |
| 224 callback.Run(NULL); |
| 225 return; |
| 226 } |
| 227 |
| 228 scoped_ptr<base::DictionaryValue> embed_app_data(new base::DictionaryValue()); |
| 229 embed_app_data->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID()); |
| 230 embed_app_data->SetString(appview::kEmbedderExtensionID, |
| 231 embedder_extension_id()); |
| 232 extensions::AppRuntimeEventRouter::DispatchOnAppEmbeddingRequestEvent( |
| 233 profile, embed_app_data.Pass(), extension_host->extension()); |
| 234 } |
| 235 |
OLD | NEW |