OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 i// 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 "components/guest_view/browser/guest_view_manager.h" | 5 #include "components/guest_view/browser/guest_view_manager.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "components/guest_view/browser/guest_view_base.h" | 9 #include "components/guest_view/browser/guest_view_base.h" |
10 #include "components/guest_view/browser/guest_view_manager_delegate.h" | 10 #include "components/guest_view/browser/guest_view_manager_delegate.h" |
11 #include "components/guest_view/browser/guest_view_manager_factory.h" | 11 #include "components/guest_view/browser/guest_view_manager_factory.h" |
12 #include "components/guest_view/common/guest_view_constants.h" | 12 #include "components/guest_view/common/guest_view_constants.h" |
13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
14 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/user_metrics.h" | 17 #include "content/public/browser/user_metrics.h" |
18 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
19 #include "content/public/common/child_process_host.h" | 19 #include "content/public/common/child_process_host.h" |
20 #include "content/public/common/result_codes.h" | 20 #include "content/public/common/result_codes.h" |
21 #include "content/public/common/url_constants.h" | 21 #include "content/public/common/url_constants.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 using content::BrowserContext; | 24 using content::BrowserContext; |
25 using content::RenderProcessHost; | |
25 using content::SiteInstance; | 26 using content::SiteInstance; |
26 using content::WebContents; | 27 using content::WebContents; |
27 | 28 |
28 namespace guest_view { | 29 namespace guest_view { |
29 | 30 |
31 // This observer observes the RenderProcessHosts of GuestView embedders, and | |
32 // notifies the GuestViewManager when they are destroyed. | |
33 class GuestViewManager::EmbedderRenderProcessHostObserver | |
34 : public content::RenderProcessHostObserver { | |
35 public: | |
36 EmbedderRenderProcessHostObserver(GuestViewManager* guest_view_manager, | |
37 int embedder_process_id) | |
38 : guest_view_manager_(guest_view_manager), id_(embedder_process_id) { | |
39 RenderProcessHost* rph = RenderProcessHost::FromID(id_); | |
40 rph->AddObserver(this); | |
41 } | |
42 | |
43 ~EmbedderRenderProcessHostObserver() override { | |
44 RenderProcessHost* rph = RenderProcessHost::FromID(id_); | |
45 if (rph) | |
46 rph->RemoveObserver(this); | |
47 } | |
48 | |
49 void RenderProcessHostDestroyed(RenderProcessHost* host) override { | |
50 guest_view_manager_->EmbedderProcessDestroyed(id_); | |
51 } | |
52 | |
53 private: | |
54 GuestViewManager* guest_view_manager_; | |
55 int id_; | |
56 }; | |
57 | |
30 // static | 58 // static |
31 GuestViewManagerFactory* GuestViewManager::factory_ = nullptr; | 59 GuestViewManagerFactory* GuestViewManager::factory_ = nullptr; |
32 | 60 |
33 GuestViewManager::GuestViewManager( | 61 GuestViewManager::GuestViewManager( |
34 content::BrowserContext* context, | 62 content::BrowserContext* context, |
35 scoped_ptr<GuestViewManagerDelegate> delegate) | 63 scoped_ptr<GuestViewManagerDelegate> delegate) |
36 : current_instance_id_(0), | 64 : current_instance_id_(0), |
37 last_instance_id_removed_(0), | 65 last_instance_id_removed_(0), |
38 context_(context), | 66 context_(context), |
39 delegate_(delegate.Pass()) { | 67 delegate_(delegate.Pass()) { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 if (instance_id != last_instance_id_removed_ + 1) | 266 if (instance_id != last_instance_id_removed_ + 1) |
239 break; | 267 break; |
240 ++last_instance_id_removed_; | 268 ++last_instance_id_removed_; |
241 removed_instance_ids_.erase(iter++); | 269 removed_instance_ids_.erase(iter++); |
242 } | 270 } |
243 } else { | 271 } else { |
244 removed_instance_ids_.insert(guest_instance_id); | 272 removed_instance_ids_.insert(guest_instance_id); |
245 } | 273 } |
246 } | 274 } |
247 | 275 |
248 void GuestViewManager::EmbedderWillBeDestroyed(int embedder_process_id) { | 276 void GuestViewManager::EmbedderProcessDestroyed(int embedder_process_id) { |
249 // Find and call any callbacks associated with the embedder that is being | 277 CallViewDestructionCallbacks(embedder_process_id); |
250 // destroyed. | |
251 auto embedder_it = view_destruction_callback_map_.find(embedder_process_id); | |
252 if (embedder_it == view_destruction_callback_map_.end()) | |
253 return; | |
254 CallbacksForEachViewID& callbacks_for_embedder = embedder_it->second; | |
255 for (auto& view_pair : callbacks_for_embedder) { | |
256 Callbacks& callbacks_for_view = view_pair.second; | |
257 for (auto& callback : callbacks_for_view) { | |
258 callback.Run(); | |
259 } | |
260 } | |
261 view_destruction_callback_map_.erase(embedder_it); | |
262 } | 278 } |
263 | 279 |
264 void GuestViewManager::ViewCreated(int embedder_process_id, | 280 void GuestViewManager::ViewCreated(int embedder_process_id, |
265 int view_instance_id, | 281 int view_instance_id, |
266 const std::string& view_type) { | 282 const std::string& view_type) { |
267 if (guest_view_registry_.empty()) | 283 if (guest_view_registry_.empty()) |
268 RegisterGuestViewTypes(); | 284 RegisterGuestViewTypes(); |
269 auto view_it = guest_view_registry_.find(view_type); | 285 auto view_it = guest_view_registry_.find(view_type); |
270 CHECK(view_it != guest_view_registry_.end()) | 286 CHECK(view_it != guest_view_registry_.end()) |
271 << "Invalid GuestView created of type \"" << view_type << "\""; | 287 << "Invalid GuestView created of type \"" << view_type << "\""; |
272 | 288 |
273 // Register the cleanup callback for when this view is destroyed. | 289 // Register the cleanup callback for when this view is destroyed. |
274 RegisterViewDestructionCallback(embedder_process_id, | 290 RegisterViewDestructionCallback(embedder_process_id, |
275 view_instance_id, | 291 view_instance_id, |
276 base::Bind(view_it->second.cleanup_function, | 292 base::Bind(view_it->second.cleanup_function, |
293 context_, | |
277 embedder_process_id, | 294 embedder_process_id, |
278 view_instance_id)); | 295 view_instance_id)); |
279 } | 296 } |
280 | 297 |
281 void GuestViewManager::ViewGarbageCollected(int embedder_process_id, | 298 void GuestViewManager::ViewGarbageCollected(int embedder_process_id, |
282 int view_instance_id) { | 299 int view_instance_id) { |
283 // Find and call any callbacks associated with the view that has been garbage | 300 CallViewDestructionCallbacks(embedder_process_id, view_instance_id); |
284 // collected. | 301 } |
302 | |
303 void GuestViewManager::CallViewDestructionCallbacks(int embedder_process_id, | |
304 int view_instance_id) { | |
305 // Find the callbacks for the embedder with ID |embedder_process_id|. | |
285 auto embedder_it = view_destruction_callback_map_.find(embedder_process_id); | 306 auto embedder_it = view_destruction_callback_map_.find(embedder_process_id); |
286 if (embedder_it == view_destruction_callback_map_.end()) | 307 if (embedder_it == view_destruction_callback_map_.end()) |
287 return; | 308 return; |
288 CallbacksForEachViewID& callbacks_for_embedder = embedder_it->second; | 309 CallbacksForEachViewID& callbacks_for_embedder = embedder_it->second; |
310 | |
311 // If |view_instance_id| is guest_view::kInstanceIDNone, then all callbacks | |
312 // for this embedder should be called. | |
313 if (view_instance_id == guest_view::kInstanceIDNone) { | |
314 // Call all callbacks for the embedder with ID |embedder_process_id|. | |
315 for (auto& view_pair : callbacks_for_embedder) { | |
316 Callbacks& callbacks_for_view = view_pair.second; | |
317 for (auto& callback : callbacks_for_view) | |
318 callback.Run(); | |
319 } | |
320 view_destruction_callback_map_.erase(embedder_it); | |
321 return; | |
322 } | |
323 | |
324 // Otherwise, call the callbacks only for the specific view with ID | |
325 // |view_instance_id|. | |
289 auto view_it = callbacks_for_embedder.find(view_instance_id); | 326 auto view_it = callbacks_for_embedder.find(view_instance_id); |
290 if (view_it == callbacks_for_embedder.end()) | 327 if (view_it == callbacks_for_embedder.end()) |
291 return; | 328 return; |
292 Callbacks& callbacks_for_view = view_it->second; | 329 Callbacks& callbacks_for_view = view_it->second; |
293 for (auto& callback : callbacks_for_view) | 330 for (auto& callback : callbacks_for_view) |
294 callback.Run(); | 331 callback.Run(); |
295 callbacks_for_embedder.erase(view_it); | 332 callbacks_for_embedder.erase(view_it); |
296 } | 333 } |
297 | 334 |
335 void GuestViewManager::CallViewDestructionCallbacks(int embedder_process_id) { | |
336 CallViewDestructionCallbacks(embedder_process_id, | |
337 guest_view::kInstanceIDNone); | |
338 } | |
339 | |
298 GuestViewBase* GuestViewManager::CreateGuestInternal( | 340 GuestViewBase* GuestViewManager::CreateGuestInternal( |
299 content::WebContents* owner_web_contents, | 341 content::WebContents* owner_web_contents, |
300 const std::string& view_type) { | 342 const std::string& view_type) { |
301 if (guest_view_registry_.empty()) | 343 if (guest_view_registry_.empty()) |
302 RegisterGuestViewTypes(); | 344 RegisterGuestViewTypes(); |
303 | 345 |
304 auto it = guest_view_registry_.find(view_type); | 346 auto it = guest_view_registry_.find(view_type); |
305 if (it == guest_view_registry_.end()) { | 347 if (it == guest_view_registry_.end()) { |
306 NOTREACHED(); | 348 NOTREACHED(); |
307 return nullptr; | 349 return nullptr; |
308 } | 350 } |
309 | 351 |
310 return it->second.create_function.Run(owner_web_contents); | 352 return it->second.create_function.Run(owner_web_contents); |
311 } | 353 } |
312 | 354 |
313 void GuestViewManager::RegisterGuestViewTypes() { | 355 void GuestViewManager::RegisterGuestViewTypes() { |
314 delegate_->RegisterAdditionalGuestViewTypes(); | 356 delegate_->RegisterAdditionalGuestViewTypes(); |
315 } | 357 } |
316 | 358 |
317 void GuestViewManager::RegisterViewDestructionCallback( | 359 void GuestViewManager::RegisterViewDestructionCallback( |
318 int embedder_process_id, | 360 int embedder_process_id, |
319 int view_instance_id, | 361 int view_instance_id, |
320 const base::Closure& callback) { | 362 const base::Closure& callback) { |
363 if (!view_destruction_callback_map_.count(embedder_process_id)) { | |
364 embedder_render_process_host_observers_.push_back( | |
Fady Samuel
2015/07/10 15:24:01
When do you remove an observer? I would just have
paulmeyer
2015/07/10 15:34:47
Done.
| |
365 new EmbedderRenderProcessHostObserver(this, embedder_process_id)); | |
366 } | |
367 | |
321 view_destruction_callback_map_[embedder_process_id][view_instance_id] | 368 view_destruction_callback_map_[embedder_process_id][view_instance_id] |
322 .push_back(callback); | 369 .push_back(callback); |
323 } | 370 } |
324 | 371 |
325 bool GuestViewManager::IsGuestAvailableToContext(GuestViewBase* guest) { | 372 bool GuestViewManager::IsGuestAvailableToContext(GuestViewBase* guest) { |
326 return delegate_->IsGuestAvailableToContext(guest); | 373 return delegate_->IsGuestAvailableToContext(guest); |
327 } | 374 } |
328 | 375 |
329 void GuestViewManager::DispatchEvent(const std::string& event_name, | 376 void GuestViewManager::DispatchEvent(const std::string& event_name, |
330 scoped_ptr<base::DictionaryValue> args, | 377 scoped_ptr<base::DictionaryValue> args, |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
432 } | 479 } |
433 | 480 |
434 GuestViewManager::GuestViewData::GuestViewData( | 481 GuestViewManager::GuestViewData::GuestViewData( |
435 const GuestViewCreateFunction& create_function, | 482 const GuestViewCreateFunction& create_function, |
436 const GuestViewCleanUpFunction& cleanup_function) | 483 const GuestViewCleanUpFunction& cleanup_function) |
437 : create_function(create_function), cleanup_function(cleanup_function) {} | 484 : create_function(create_function), cleanup_function(cleanup_function) {} |
438 | 485 |
439 GuestViewManager::GuestViewData::~GuestViewData() {} | 486 GuestViewManager::GuestViewData::~GuestViewData() {} |
440 | 487 |
441 } // namespace guest_view | 488 } // namespace guest_view |
OLD | NEW |