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 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" | 8 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/guest_view/app_view/app_view_constants.h" | 10 #include "chrome/browser/guest_view/app_view/app_view_constants.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 using content::RenderFrameHost; | 29 using content::RenderFrameHost; |
30 using content::WebContents; | 30 using content::WebContents; |
31 using extensions::ExtensionHost; | 31 using extensions::ExtensionHost; |
32 | 32 |
33 namespace extensions { | 33 namespace extensions { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 struct ResponseInfo { | 37 struct ResponseInfo { |
38 scoped_refptr<const extensions::Extension> guest_extension; | 38 scoped_refptr<const Extension> guest_extension; |
39 base::WeakPtr<AppViewGuest> app_view_guest; | 39 base::WeakPtr<AppViewGuest> app_view_guest; |
40 GuestViewBase::WebContentsCreatedCallback callback; | 40 GuestViewBase::WebContentsCreatedCallback callback; |
41 | 41 |
42 ResponseInfo(const extensions::Extension* guest_extension, | 42 ResponseInfo(const Extension* guest_extension, |
43 const base::WeakPtr<AppViewGuest>& app_view_guest, | 43 const base::WeakPtr<AppViewGuest>& app_view_guest, |
44 const GuestViewBase::WebContentsCreatedCallback& callback) | 44 const GuestViewBase::WebContentsCreatedCallback& callback) |
45 : guest_extension(guest_extension), | 45 : guest_extension(guest_extension), |
46 app_view_guest(app_view_guest), | 46 app_view_guest(app_view_guest), |
47 callback(callback) {} | 47 callback(callback) {} |
48 | 48 |
49 ~ResponseInfo() {} | 49 ~ResponseInfo() {} |
50 }; | 50 }; |
51 | 51 |
52 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap; | 52 typedef std::map<int, linked_ptr<ResponseInfo> > PendingResponseMap; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, | 102 AppViewGuest::AppViewGuest(content::BrowserContext* browser_context, |
103 int guest_instance_id) | 103 int guest_instance_id) |
104 : GuestView<AppViewGuest>(browser_context, guest_instance_id), | 104 : GuestView<AppViewGuest>(browser_context, guest_instance_id), |
105 weak_ptr_factory_(this) { | 105 weak_ptr_factory_(this) { |
106 } | 106 } |
107 | 107 |
108 AppViewGuest::~AppViewGuest() { | 108 AppViewGuest::~AppViewGuest() { |
109 } | 109 } |
110 | 110 |
111 extensions::WindowController* AppViewGuest::GetExtensionWindowController() | 111 WindowController* AppViewGuest::GetExtensionWindowController() const { |
112 const { | |
113 return NULL; | 112 return NULL; |
114 } | 113 } |
115 | 114 |
116 content::WebContents* AppViewGuest::GetAssociatedWebContents() const { | 115 content::WebContents* AppViewGuest::GetAssociatedWebContents() const { |
117 return guest_web_contents(); | 116 return guest_web_contents(); |
118 } | 117 } |
119 | 118 |
120 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) { | 119 bool AppViewGuest::OnMessageReceived(const IPC::Message& message) { |
121 bool handled = true; | 120 bool handled = true; |
122 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message) | 121 IPC_BEGIN_MESSAGE_MAP(AppViewGuest, message) |
(...skipping 23 matching lines...) Expand all Loading... |
146 int embedder_render_process_id, | 145 int embedder_render_process_id, |
147 const base::DictionaryValue& create_params, | 146 const base::DictionaryValue& create_params, |
148 const WebContentsCreatedCallback& callback) { | 147 const WebContentsCreatedCallback& callback) { |
149 std::string app_id; | 148 std::string app_id; |
150 if (!create_params.GetString(appview::kAppID, &app_id)) { | 149 if (!create_params.GetString(appview::kAppID, &app_id)) { |
151 callback.Run(NULL); | 150 callback.Run(NULL); |
152 return; | 151 return; |
153 } | 152 } |
154 | 153 |
155 ExtensionService* service = | 154 ExtensionService* service = |
156 extensions::ExtensionSystem::Get(browser_context())->extension_service(); | 155 ExtensionSystem::Get(browser_context())->extension_service(); |
157 const extensions::Extension* guest_extension = | 156 const Extension* guest_extension = service->GetExtensionById(app_id, false); |
158 service->GetExtensionById(app_id, false); | 157 const Extension* embedder_extension = |
159 const extensions::Extension* embedder_extension = | |
160 service->GetExtensionById(embedder_extension_id, false); | 158 service->GetExtensionById(embedder_extension_id, false); |
161 | 159 |
162 if (!guest_extension || !guest_extension->is_platform_app() || | 160 if (!guest_extension || !guest_extension->is_platform_app() || |
163 !embedder_extension | !embedder_extension->is_platform_app()) { | 161 !embedder_extension | !embedder_extension->is_platform_app()) { |
164 callback.Run(NULL); | 162 callback.Run(NULL); |
165 return; | 163 return; |
166 } | 164 } |
167 | 165 |
168 pending_response_map.Get().insert( | 166 pending_response_map.Get().insert( |
169 std::make_pair(GetGuestInstanceID(), | 167 std::make_pair(GetGuestInstanceID(), |
170 make_linked_ptr(new ResponseInfo( | 168 make_linked_ptr(new ResponseInfo( |
171 guest_extension, | 169 guest_extension, |
172 weak_ptr_factory_.GetWeakPtr(), | 170 weak_ptr_factory_.GetWeakPtr(), |
173 callback)))); | 171 callback)))); |
174 | 172 |
175 extensions::LazyBackgroundTaskQueue* queue = | 173 LazyBackgroundTaskQueue* queue = |
176 extensions::ExtensionSystem::Get(browser_context())-> | 174 ExtensionSystem::Get(browser_context())->lazy_background_task_queue(); |
177 lazy_background_task_queue(); | |
178 if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) { | 175 if (queue->ShouldEnqueueTask(browser_context(), guest_extension)) { |
179 queue->AddPendingTask(browser_context(), | 176 queue->AddPendingTask(browser_context(), |
180 guest_extension->id(), | 177 guest_extension->id(), |
181 base::Bind(&AppViewGuest::LaunchAppAndFireEvent, | 178 base::Bind(&AppViewGuest::LaunchAppAndFireEvent, |
182 weak_ptr_factory_.GetWeakPtr(), | 179 weak_ptr_factory_.GetWeakPtr(), |
183 callback)); | 180 callback)); |
184 return; | 181 return; |
185 } | 182 } |
186 | 183 |
187 extensions::ProcessManager* process_manager = | 184 ProcessManager* process_manager = |
188 extensions::ExtensionSystem::Get(browser_context())->process_manager(); | 185 ExtensionSystem::Get(browser_context())->process_manager(); |
189 ExtensionHost* host = | 186 ExtensionHost* host = |
190 process_manager->GetBackgroundHostForExtension(guest_extension->id()); | 187 process_manager->GetBackgroundHostForExtension(guest_extension->id()); |
191 DCHECK(host); | 188 DCHECK(host); |
192 LaunchAppAndFireEvent(callback, host); | 189 LaunchAppAndFireEvent(callback, host); |
193 } | 190 } |
194 | 191 |
195 void AppViewGuest::DidAttachToEmbedder() { | 192 void AppViewGuest::DidAttachToEmbedder() { |
196 // This is called after the guest process has been attached to a host | 193 // This is called after the guest process has been attached to a host |
197 // element. This means that the host element knows how to route input | 194 // element. This means that the host element knows how to route input |
198 // events to the guest, and the guest knows how to get frames to the | 195 // events to the guest, and the guest knows how to get frames to the |
199 // embedder. | 196 // embedder. |
200 guest_web_contents()->GetController().LoadURL( | 197 guest_web_contents()->GetController().LoadURL( |
201 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); | 198 url_, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
202 } | 199 } |
203 | 200 |
204 void AppViewGuest::DidInitialize() { | 201 void AppViewGuest::DidInitialize() { |
205 extension_function_dispatcher_.reset( | 202 extension_function_dispatcher_.reset( |
206 new extensions::ExtensionFunctionDispatcher(browser_context(), this)); | 203 new ExtensionFunctionDispatcher(browser_context(), this)); |
207 } | 204 } |
208 | 205 |
209 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) { | 206 void AppViewGuest::OnRequest(const ExtensionHostMsg_Request_Params& params) { |
210 extension_function_dispatcher_->Dispatch( | 207 extension_function_dispatcher_->Dispatch( |
211 params, guest_web_contents()->GetRenderViewHost()); | 208 params, guest_web_contents()->GetRenderViewHost()); |
212 } | 209 } |
213 | 210 |
214 void AppViewGuest::CompleteCreateWebContents( | 211 void AppViewGuest::CompleteCreateWebContents( |
215 const GURL& url, | 212 const GURL& url, |
216 const extensions::Extension* guest_extension, | 213 const Extension* guest_extension, |
217 const WebContentsCreatedCallback& callback) { | 214 const WebContentsCreatedCallback& callback) { |
218 if (!url.is_valid()) { | 215 if (!url.is_valid()) { |
219 callback.Run(NULL); | 216 callback.Run(NULL); |
220 return; | 217 return; |
221 } | 218 } |
222 url_ = url; | 219 url_ = url; |
223 guest_extension_id_ = guest_extension->id(); | 220 guest_extension_id_ = guest_extension->id(); |
224 | 221 |
225 WebContents::CreateParams params( | 222 WebContents::CreateParams params( |
226 browser_context(), | 223 browser_context(), |
227 content::SiteInstance::CreateForURL(browser_context(), | 224 content::SiteInstance::CreateForURL(browser_context(), |
228 guest_extension->url())); | 225 guest_extension->url())); |
229 params.guest_delegate = this; | 226 params.guest_delegate = this; |
230 callback.Run(WebContents::Create(params)); | 227 callback.Run(WebContents::Create(params)); |
231 } | 228 } |
232 | 229 |
233 void AppViewGuest::LaunchAppAndFireEvent( | 230 void AppViewGuest::LaunchAppAndFireEvent( |
234 const WebContentsCreatedCallback& callback, | 231 const WebContentsCreatedCallback& callback, |
235 ExtensionHost* extension_host) { | 232 ExtensionHost* extension_host) { |
236 extensions::ExtensionSystem* system = | 233 ExtensionSystem* system = ExtensionSystem::Get(browser_context()); |
237 extensions::ExtensionSystem::Get(browser_context()); | |
238 bool has_event_listener = system->event_router()->ExtensionHasEventListener( | 234 bool has_event_listener = system->event_router()->ExtensionHasEventListener( |
239 extension_host->extension()->id(), | 235 extension_host->extension()->id(), |
240 app_runtime::OnEmbedRequested::kEventName); | 236 app_runtime::OnEmbedRequested::kEventName); |
241 if (!has_event_listener) { | 237 if (!has_event_listener) { |
242 callback.Run(NULL); | 238 callback.Run(NULL); |
243 return; | 239 return; |
244 } | 240 } |
245 | 241 |
246 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue()); | 242 scoped_ptr<base::DictionaryValue> embed_request(new base::DictionaryValue()); |
247 embed_request->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID()); | 243 embed_request->SetInteger(appview::kGuestInstanceID, GetGuestInstanceID()); |
248 embed_request->SetString(appview::kEmbedderID, embedder_extension_id()); | 244 embed_request->SetString(appview::kEmbedderID, embedder_extension_id()); |
249 extensions::AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( | 245 AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent( |
250 browser_context(), embed_request.Pass(), extension_host->extension()); | 246 browser_context(), embed_request.Pass(), extension_host->extension()); |
251 } | 247 } |
252 | 248 |
253 } // namespace extensions | 249 } // namespace extensions |
OLD | NEW |