[email protected] | 9f4f332 | 2012-01-18 22:29:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 5 | #include "content/browser/renderer_host/render_widget_helper.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 037edb5 | 2011-11-15 21:14:06 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 0a9d452d | 2011-12-10 02:39:19 | [diff] [blame] | 8 | #include "base/bind_helpers.h" |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 10 | #include "base/posix/eintr_wrapper.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 11 | #include "base/task/post_task.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 12 | #include "base/threading/thread.h" |
[email protected] | 3a7b66d | 2012-04-26 16:34:16 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 14 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | 59f4f2fa | 2011-03-23 01:00:55 | [diff] [blame] | 15 | #include "content/common/view_messages.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 16 | #include "content/public/browser/browser_task_traits.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 18 | namespace content { |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | typedef std::map<int, RenderWidgetHelper*> WidgetHelperMap; |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 22 | base::LazyInstance<WidgetHelperMap>::DestructorAtExit g_widget_helpers = |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 23 | LAZY_INSTANCE_INITIALIZER; |
| 24 | |
| 25 | void AddWidgetHelper(int render_process_id, |
| 26 | const scoped_refptr<RenderWidgetHelper>& widget_helper) { |
[email protected] | 8607aa9 | 2014-03-29 02:01:22 | [diff] [blame] | 27 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 28 | // We don't care if RenderWidgetHelpers overwrite an existing process_id. Just |
| 29 | // want this to be up to date. |
| 30 | g_widget_helpers.Get()[render_process_id] = widget_helper.get(); |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
John Abd-El-Malek | 0e82fe7f | 2019-07-27 00:06:29 | [diff] [blame] | 35 | RenderWidgetHelper::RenderWidgetHelper() : render_process_id_(-1) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
| 37 | RenderWidgetHelper::~RenderWidgetHelper() { |
[email protected] | 8607aa9 | 2014-03-29 02:01:22 | [diff] [blame] | 38 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 90c8b9ca6 | 2012-08-31 19:48:02 | [diff] [blame] | 39 | |
| 40 | // Delete this RWH from the map if it is found. |
| 41 | WidgetHelperMap& widget_map = g_widget_helpers.Get(); |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 42 | auto it = widget_map.find(render_process_id_); |
[email protected] | 90c8b9ca6 | 2012-08-31 19:48:02 | [diff] [blame] | 43 | if (it != widget_map.end() && it->second == this) |
| 44 | widget_map.erase(it); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | } |
| 46 | |
John Abd-El-Malek | 0e82fe7f | 2019-07-27 00:06:29 | [diff] [blame] | 47 | void RenderWidgetHelper::Init(int render_process_id) { |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 48 | render_process_id_ = render_process_id; |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 49 | |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame^] | 50 | base::PostTask(FROM_HERE, {BrowserThread::IO}, |
| 51 | base::BindOnce(&AddWidgetHelper, render_process_id_, |
| 52 | base::WrapRefCounted(this))); |
[email protected] | 4566f13 | 2009-03-12 01:55:13 | [diff] [blame] | 53 | } |
| 54 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | int RenderWidgetHelper::GetNextRoutingID() { |
lfg | 052af41 | 2015-03-19 16:49:53 | [diff] [blame] | 56 | return next_routing_id_.GetNext() + 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 59 | // static |
| 60 | RenderWidgetHelper* RenderWidgetHelper::FromProcessHostID( |
| 61 | int render_process_host_id) { |
[email protected] | 8607aa9 | 2014-03-29 02:01:22 | [diff] [blame] | 62 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | c63b4d4 | 2012-04-26 01:01:07 | [diff] [blame] | 63 | WidgetHelperMap::const_iterator ci = g_widget_helpers.Get().find( |
| 64 | render_process_host_id); |
| 65 | return (ci == g_widget_helpers.Get().end())? NULL : ci->second; |
| 66 | } |
| 67 | |
[email protected] | 0ebf387 | 2008-11-07 21:35:03 | [diff] [blame] | 68 | void RenderWidgetHelper::CreateNewWidget(int opener_id, |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 69 | mojom::WidgetPtr widget, |
piman | 5d36dae | 2015-09-24 22:47:05 | [diff] [blame] | 70 | int* route_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | *route_id = GetNextRoutingID(); |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 72 | |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame^] | 73 | base::PostTask(FROM_HERE, {BrowserThread::UI}, |
| 74 | base::BindOnce(&RenderWidgetHelper::OnCreateWidgetOnUI, this, |
| 75 | opener_id, *route_id, widget.PassInterface())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | ca5660e | 2011-02-11 00:51:09 | [diff] [blame] | 78 | void RenderWidgetHelper::CreateNewFullscreenWidget(int opener_id, |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 79 | mojom::WidgetPtr widget, |
piman | 5d36dae | 2015-09-24 22:47:05 | [diff] [blame] | 80 | int* route_id) { |
[email protected] | 48495594 | 2010-08-19 16:13:18 | [diff] [blame] | 81 | *route_id = GetNextRoutingID(); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame^] | 82 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 83 | FROM_HERE, {BrowserThread::UI}, |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 84 | base::BindOnce(&RenderWidgetHelper::OnCreateFullscreenWidgetOnUI, this, |
| 85 | opener_id, *route_id, widget.PassInterface())); |
[email protected] | 48495594 | 2010-08-19 16:13:18 | [diff] [blame] | 86 | } |
| 87 | |
avi | b533f5d | 2015-12-25 03:11:15 | [diff] [blame] | 88 | void RenderWidgetHelper::OnCreateWidgetOnUI(int32_t opener_id, |
| 89 | int32_t route_id, |
danakj | 829cdd14 | 2018-09-14 21:13:27 | [diff] [blame] | 90 | mojom::WidgetPtrInfo widget_info) { |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 91 | mojom::WidgetPtr widget; |
| 92 | widget.Bind(std::move(widget_info)); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 93 | RenderViewHostImpl* host = RenderViewHostImpl::FromID( |
| 94 | render_process_id_, opener_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | if (host) |
danakj | 829cdd14 | 2018-09-14 21:13:27 | [diff] [blame] | 96 | host->CreateNewWidget(route_id, std::move(widget)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 98 | |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 99 | void RenderWidgetHelper::OnCreateFullscreenWidgetOnUI( |
| 100 | int32_t opener_id, |
| 101 | int32_t route_id, |
| 102 | mojom::WidgetPtrInfo widget_info) { |
| 103 | mojom::WidgetPtr widget; |
| 104 | widget.Bind(std::move(widget_info)); |
[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 105 | RenderViewHostImpl* host = RenderViewHostImpl::FromID( |
| 106 | render_process_id_, opener_id); |
[email protected] | 48495594 | 2010-08-19 16:13:18 | [diff] [blame] | 107 | if (host) |
Dave Tapuska | 1bdf183 | 2017-07-07 18:07:19 | [diff] [blame] | 108 | host->CreateNewFullscreenWidget(route_id, std::move(widget)); |
[email protected] | 48495594 | 2010-08-19 16:13:18 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 111 | } // namespace content |