| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/guest_view/app_view/app_view_guest.h" | |
| 6 | |
| 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" | |
| 26 | |
| 27 namespace app_runtime = extensions::core_api::app_runtime; | |
| 28 | |
| 29 using content::RenderFrameHost; | |
| 30 using content::WebContents; | |
| 31 using extensions::ExtensionHost; | |
| 32 | |
| 33 namespace extensions { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 struct ResponseInfo { | |
| 38 scoped_refptr<const Extension> guest_extension; | |
| 39 base::WeakPtr<AppViewGuest> app_view_guest; | |
| 40 GuestViewBase::WebContentsCreatedCallback callback; | |
| 41 | |
| 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) {} | |
| 48 | |
| 49 ~ResponseInfo() {} | |
| 50 }; | |
| 51 | |
| 52 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap; | |
| 53 static base::LazyInstance<PendingResponseMap> pending_response_map = | |
| 54 LAZY_INSTANCE_INITIALIZER; | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 // static. | |
| 59 const char AppViewGuest::Type[] = "appview"; | |
| 60 | |
| 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; | |
| 73 } | |
| 74 | |
| 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; | |
| 81 } | |
| 82 | |
| 83 response_info->app_view_guest-> | |
| 84 CompleteCreateWebContents(url, | |
| 85 response_info->guest_extension, | |
| 86 response_info->callback); | |
| 87 | |
| 88 response_map->erase(guest_instance_id); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 // static | |
| 93 GuestViewBase* AppViewGuest::Create(content::BrowserContext* browser_context, | |
| 94 int guest_instance_id) { | |
| 95 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 96 switches::kEnableAppView)) { | |
| 97 return NULL; | |
| 98 } | |
| 99 return new AppViewGuest(browser_context, guest_instance_id); | |
| 100 } | |
| 101 | |
| 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) { | |
| 106 } | |
| 107 | |
| 108 AppViewGuest::~AppViewGuest() { | |
| 109 } | |
| 110 | |
| 111 WindowController* AppViewGuest::GetExtensionWindowController() const { | |
| 112 return NULL; | |
| 113 } | |
| 114 | |
| 115 content::WebContents* AppViewGuest::GetAssociatedWebContents() const { | |
| 116 return guest_web_contents(); | |
| 117 } | |
| 118 | |
| 119 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) { | |
| 120 bool handled = true; | |
| 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() | |
| 125 return handled; | |
| 126 } | |
| 127 | |
| 128 bool AppViewGuest::HandleContextMenu(const content::ContextMenuParams& params) { | |
| 129 ContextMenuDelegate* menu_delegate = | |
| 130 ContextMenuDelegate::FromWebContents(guest_web_contents()); | |
| 131 DCHECK(menu_delegate); | |
| 132 | |
| 133 scoped_ptr<RenderViewContextMenu> menu = | |
| 134 menu_delegate->BuildMenu(guest_web_contents(), params); | |
| 135 menu_delegate->ShowMenu(menu.Pass()); | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 const char* AppViewGuest::GetAPINamespace() { | |
| 140 return appview::kEmbedderAPINamespace; | |
| 141 } | |
| 142 | |
| 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) { | |
| 148 std::string app_id; | |
| 149 if (!create_params.GetString(appview::kAppID, &app_id)) { | |
| 150 callback.Run(NULL); | |
| 151 return; | |
| 152 } | |
| 153 | |
| 154 const base::DictionaryValue* data = NULL; | |
| 155 if (!create_params.GetDictionary(appview::kData, &data)) { | |
| 156 callback.Run(NULL); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 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); | |
| 165 | |
| 166 if (!guest_extension || !guest_extension->is_platform_app() || | |
| 167 !embedder_extension | !embedder_extension->is_platform_app()) { | |
| 168 callback.Run(NULL); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 pending_response_map.Get().insert( | |
| 173 std::make_pair(GetGuestInstanceID(), | |
| 174 make_linked_ptr(new ResponseInfo( | |
| 175 guest_extension, | |
| 176 weak_ptr_factory_.GetWeakPtr(), | |
| 177 callback)))); | |
| 178 | |
| 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(), | |
| 184 base::Bind( | |
| 185 &AppViewGuest::LaunchAppAndFireEvent, | |
| 186 weak_ptr_factory_.GetWeakPtr(), | |
| 187 base::Passed(make_scoped_ptr(data->DeepCopy())), | |
| 188 callback)); | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 ProcessManager* process_manager = | |
| 193 ExtensionSystem::Get(browser_context())->process_manager(); | |
| 194 ExtensionHost* host = | |
| 195 process_manager->GetBackgroundHostForExtension(guest_extension->id()); | |
| 196 DCHECK(host); | |
| 197 LaunchAppAndFireEvent(make_scoped_ptr(data->DeepCopy()), callback, host); | |
| 198 } | |
| 199 | |
| 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 | |
| 204 // embedder. | |
| 205 guest_web_contents()->GetController().LoadURL( | |
| 206 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); | |
| 207 } | |
| 208 | |
| 209 void AppViewGuest::DidInitialize() { | |
| 210 extension_function_dispatcher_.reset( | |
| 211 new ExtensionFunctionDispatcher(browser_context(), this)); | |
| 212 } | |
| 213 | |
| 214 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) { | |
| 215 extension_function_dispatcher_->Dispatch( | |
| 216 params, guest_web_contents()->GetRenderViewHost()); | |
| 217 } | |
| 218 | |
| 219 void AppViewGuest::CompleteCreateWebContents( | |
| 220 const GURL& url, | |
| 221 const Extension* guest_extension, | |
| 222 const WebContentsCreatedCallback& callback) { | |
| 223 if (!url.is_valid()) { | |
| 224 callback.Run(NULL); | |
| 225 return; | |
| 226 } | |
| 227 url_ = url; | |
| 228 guest_extension_id_ = guest_extension->id(); | |
| 229 | |
| 230 WebContents::CreateParams params( | |
| 231 browser_context(), | |
| 232 content::SiteInstance::CreateForURL(browser_context(), | |
| 233 guest_extension->url())); | |
| 234 params.guest_delegate = this; | |
| 235 callback.Run(WebContents::Create(params)); | |
| 236 } | |
| 237 | |
| 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) { | |
| 247 callback.Run(NULL); | |
| 248 return; | |
| 249 } | |
| 250 | |
| 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()); | |
| 257 } | |
| 258 | |
| 259 } // namespace extensions | |
| OLD | NEW |