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 content::BrowserContext* browser_context, |
| 61 const GURL& url, |
| 62 int guest_instance_id, |
| 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 // TODO(fsamuel): An app is sending invalid responses. We should probably |
| 68 // kill it. |
| 69 return false; |
| 70 } |
| 71 |
| 72 linked_ptr<ResponseInfo> response_info = it->second; |
| 73 if (!response_info->app_view_guest || |
| 74 (response_info->guest_extension->id() != guest_extension_id)) { |
| 75 // TODO(fsamuel): An app is trying to respond to an <appview> that didn't |
| 76 // initiate communication with it. We should kill the app here. |
| 77 return false; |
| 78 } |
| 79 |
| 80 response_info->app_view_guest-> |
| 81 CompleteCreateWebContents(url, |
| 82 response_info->guest_extension, |
| 83 response_info->callback); |
| 84 |
| 85 response_map->erase(guest_instance_id); |
| 86 return true; |
| 87 } |
| 88 |
10 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, | 89 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, |
11 int guest_instance_id) | 90 int guest_instance_id) |
12 : GuestView<AppViewGuest>(browser_context, guest_instance_id) { | 91 : GuestView<AppViewGuest>(browser_context, guest_instance_id), |
| 92 weak_ptr_factory_(this) { |
13 } | 93 } |
14 | 94 |
15 AppViewGuest::~AppViewGuest() { | 95 AppViewGuest::~AppViewGuest() { |
16 } | 96 } |
17 | 97 |
| 98 extensions::WindowController* AppViewGuest::GetExtensionWindowController() |
| 99 const { |
| 100 return NULL; |
| 101 } |
| 102 |
| 103 content::WebContents* AppViewGuest::GetAssociatedWebContents() const { |
| 104 return guest_web_contents(); |
| 105 } |
| 106 |
| 107 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) { |
| 108 bool handled = true; |
| 109 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message) |
| 110 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) |
| 111 IPC_MESSAGE_UNHANDLED(handled = false) |
| 112 IPC_END_MESSAGE_MAP() |
| 113 return handled; |
| 114 } |
| 115 |
| 116 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) { |
| 117 ContextMenuDelegate* menu_delegate = |
| 118 ContextMenuDelegate::FromWebContents(guest_web_contents()); |
| 119 DCHECK(menu_delegate); |
| 120 |
| 121 scoped_ptr<RenderViewContextMenu> menu = |
| 122 menu_delegate->BuildMenu(guest_web_contents(), params); |
| 123 menu_delegate->ShowMenu(menu.Pass()); |
| 124 return true; |
| 125 } |
| 126 |
18 void AppViewGuest::CreateWebContents( | 127 void AppViewGuest::CreateWebContents( |
19 const std::string& embedder_extension_id, | 128 const std::string& embedder_extension_id, |
20 int embedder_render_process_id, | 129 int embedder_render_process_id, |
21 const base::DictionaryValue& create_params, | 130 const base::DictionaryValue& create_params, |
22 const WebContentsCreatedCallback& callback) { | 131 const WebContentsCreatedCallback& callback) { |
23 // TODO(fsamuel): Create a WebContents with the appropriate SiteInstance here. | 132 std::string app_id; |
24 // After the WebContents has been created, call the |callback|. | 133 if (!create_params.GetString(appview::kAppID, &app_id)) { |
25 // callback.Run(new_web_contents); | 134 callback.Run(NULL); |
| 135 return; |
| 136 } |
| 137 |
| 138 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 139 ExtensionService* service = |
| 140 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 141 const extensions::Extension* guest_extension = |
| 142 service->GetExtensionById(app_id, false); |
| 143 const extensions::Extension* embedder_extension = |
| 144 service->GetExtensionById(embedder_extension_id, false); |
| 145 |
| 146 if (!guest_extension || !guest_extension->is_platform_app() || |
| 147 !embedder_extension | !embedder_extension->is_platform_app()) { |
| 148 callback.Run(NULL); |
| 149 return; |
| 150 } |
| 151 |
| 152 pending_response_map.Get().insert( |
| 153 std::make_pair(GetGuestInstanceID(), |
| 154 make_linked_ptr(new ResponseInfo( |
| 155 guest_extension, |
| 156 weak_ptr_factory_.GetWeakPtr(), |
| 157 callback)))); |
| 158 |
| 159 extensions::LazyBackgroundTaskQueue* queue = |
| 160 extensions::ExtensionSystem::Get(profile)->lazy_background_task_queue(); |
| 161 if (queue->ShouldEnqueueTask(profile, guest_extension)) { |
| 162 queue->AddPendingTask(profile, |
| 163 guest_extension->id(), |
| 164 base::Bind(&AppViewGuest::LaunchAppAndFireEvent, |
| 165 weak_ptr_factory_.GetWeakPtr(), |
| 166 callback)); |
| 167 return; |
| 168 } |
| 169 |
| 170 extensions::ProcessManager* process_manager = |
| 171 extensions::ExtensionSystem::Get(profile)->process_manager(); |
| 172 ExtensionHost* host = |
| 173 process_manager->GetBackgroundHostForExtension(guest_extension->id()); |
| 174 DCHECK(host); |
| 175 LaunchAppAndFireEvent(callback, host); |
26 } | 176 } |
27 | 177 |
28 void AppViewGuest::DidAttachToEmbedder() { | 178 void AppViewGuest::DidAttachToEmbedder() { |
29 // This is called after the guest process has been attached to a host | 179 // 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 | 180 // 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 | 181 // events to the guest, and the guest knows how to get frames to the |
32 // embedder. | 182 // embedder. |
33 // TODO(fsamuel): Perform the initial navigation here. | 183 guest_web_contents()->GetController().LoadURL( |
| 184 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
34 } | 185 } |
| 186 |
| 187 void AppViewGuest::DidInitialize() { |
| 188 extension_function_dispatcher_.reset( |
| 189 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); |
| 190 } |
| 191 |
| 192 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) { |
| 193 extension_function_dispatcher_->Dispatch( |
| 194 params, guest_web_contents()->GetRenderViewHost()); |
| 195 } |
| 196 |
| 197 void AppViewGuest::CompleteCreateWebContents( |
| 198 const GURL& url, |
| 199 const extensions::Extension* guest_extension, |
| 200 const WebContentsCreatedCallback& callback) { |
| 201 if (!url.is_valid()) { |
| 202 callback.Run(NULL); |
| 203 return; |
| 204 } |
| 205 url_ = url; |
| 206 guest_extension_id_ = guest_extension->id(); |
| 207 |
| 208 WebContents::CreateParams params( |
| 209 browser_context(), |
| 210 content::SiteInstance::CreateForURL(browser_context(), |
| 211 guest_extension->url())); |
| 212 params.guest_delegate = this; |
| 213 callback.Run(WebContents::Create(params)); |
| 214 } |
| 215 |
| 216 void AppViewGuest::LaunchAppAndFireEvent( |
| 217 const WebContentsCreatedCallback& callback, |
| 218 ExtensionHost* extension_host) { |
| 219 Profile* profile = Profile::FromBrowserContext(browser_context()); |
| 220 extensions::ExtensionSystem* system = |
| 221 extensions::ExtensionSystem::Get(browser_context()); |
| 222 bool has_event_listener = system->event_router()->ExtensionHasEventListener( |
| 223 extension_host->extension()->id(), |
| 224 app_runtime::OnEmbedRequested::kEventName); |
| 225 if (!has_event_listener) { |
| 226 callback.Run(NULL); |
| 227 return; |
| 228 } |
| 229 |
| 230 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue()); |
| 231 embed_request->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID()); |
| 232 embed_request->SetString(appview::kEmbedderID, embedder_extension_id()); |
| 233 extensions::AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( |
| 234 profile, embed_request.Pass(), extension_host->extension()); |
| 235 } |
| 236 |
OLD | NEW |