Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(811)

Side by Side Diff: chrome/browser/extensions/api/messaging/extension_message_port.cc

Issue 1966283002: Remove port lifetime management from renderer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Devlin's nits up to #10 Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/extensions/api/messaging/extension_message_port.h" 5 #include "chrome/browser/extensions/api/messaging/extension_message_port.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/scoped_observer.h" 8 #include "base/scoped_observer.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/interstitial_page.h" 10 #include "content/public/browser/interstitial_page.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 ExtensionMessagePort::ExtensionMessagePort( 92 ExtensionMessagePort::ExtensionMessagePort(
93 base::WeakPtr<MessageService> message_service, 93 base::WeakPtr<MessageService> message_service,
94 int port_id, 94 int port_id,
95 const std::string& extension_id, 95 const std::string& extension_id,
96 content::RenderProcessHost* extension_process) 96 content::RenderProcessHost* extension_process)
97 : weak_message_service_(message_service), 97 : weak_message_service_(message_service),
98 port_id_(port_id), 98 port_id_(port_id),
99 extension_id_(extension_id), 99 extension_id_(extension_id),
100 browser_context_(extension_process->GetBrowserContext()), 100 browser_context_(extension_process->GetBrowserContext()),
101 extension_process_(extension_process), 101 extension_process_(extension_process),
102 opener_tab_id_(-1),
102 did_create_port_(false), 103 did_create_port_(false),
103 background_host_ptr_(nullptr), 104 background_host_ptr_(nullptr),
104 frame_tracker_(new FrameTracker(this)) { 105 frame_tracker_(new FrameTracker(this)) {
105 auto all_hosts = ProcessManager::Get(browser_context_) 106 auto all_hosts = ProcessManager::Get(browser_context_)
106 ->GetRenderFrameHostsForExtension(extension_id); 107 ->GetRenderFrameHostsForExtension(extension_id);
107 for (content::RenderFrameHost* rfh : all_hosts) 108 for (content::RenderFrameHost* rfh : all_hosts)
108 RegisterFrame(rfh); 109 RegisterFrame(rfh);
109 110
110 frame_tracker_->TrackExtensionProcessFrames(); 111 frame_tracker_->TrackExtensionProcessFrames();
111 } 112 }
112 113
113 ExtensionMessagePort::ExtensionMessagePort( 114 ExtensionMessagePort::ExtensionMessagePort(
114 base::WeakPtr<MessageService> message_service, 115 base::WeakPtr<MessageService> message_service,
115 int port_id, 116 int port_id,
116 const std::string& extension_id, 117 const std::string& extension_id,
117 content::RenderFrameHost* rfh, 118 content::RenderFrameHost* rfh,
118 bool include_child_frames) 119 bool include_child_frames)
119 : weak_message_service_(message_service), 120 : weak_message_service_(message_service),
120 port_id_(port_id), 121 port_id_(port_id),
121 extension_id_(extension_id), 122 extension_id_(extension_id),
122 browser_context_(rfh->GetProcess()->GetBrowserContext()), 123 browser_context_(rfh->GetProcess()->GetBrowserContext()),
123 extension_process_(nullptr), 124 extension_process_(nullptr),
125 opener_tab_id_(-1),
124 did_create_port_(false), 126 did_create_port_(false),
125 background_host_ptr_(nullptr), 127 background_host_ptr_(nullptr),
126 frame_tracker_(new FrameTracker(this)) { 128 frame_tracker_(new FrameTracker(this)) {
127 content::WebContents* tab = content::WebContents::FromRenderFrameHost(rfh); 129 content::WebContents* tab = content::WebContents::FromRenderFrameHost(rfh);
128 if (!tab) { 130 if (!tab) {
129 content::InterstitialPage* interstitial = 131 content::InterstitialPage* interstitial =
130 content::InterstitialPage::FromRenderFrameHost(rfh); 132 content::InterstitialPage::FromRenderFrameHost(rfh);
131 // A RenderFrameHost must be hosted in a WebContents or InterstitialPage. 133 // A RenderFrameHost must be hosted in a WebContents or InterstitialPage.
132 CHECK(interstitial); 134 CHECK(interstitial);
133 135
(...skipping 17 matching lines...) Expand all
151 if (include_child_frames) { 153 if (include_child_frames) {
152 tab->ForEachFrame(base::Bind(&ExtensionMessagePort::RegisterFrame, 154 tab->ForEachFrame(base::Bind(&ExtensionMessagePort::RegisterFrame,
153 base::Unretained(this))); 155 base::Unretained(this)));
154 } else { 156 } else {
155 RegisterFrame(rfh); 157 RegisterFrame(rfh);
156 } 158 }
157 } 159 }
158 160
159 ExtensionMessagePort::~ExtensionMessagePort() {} 161 ExtensionMessagePort::~ExtensionMessagePort() {}
160 162
163 void ExtensionMessagePort::RevalidatePort() {
164 // Only opener ports need to be revalidated, because these are created in the
165 // renderer before the browser knows about them.
166 DCHECK(!extension_process_);
167 DCHECK_LE(frames_.size(), 1U);
168
169 SendToPort(base::WrapUnique(
170 new ExtensionMsg_ValidateMessagePort(MSG_ROUTING_NONE, port_id_)));
Devlin 2016/05/18 17:55:09 It's probably worth duplicating the comment that v
171 }
172
161 void ExtensionMessagePort::RemoveCommonFrames(const MessagePort& port) { 173 void ExtensionMessagePort::RemoveCommonFrames(const MessagePort& port) {
162 // Avoid overlap in the set of frames to make sure that it does not matter 174 // Avoid overlap in the set of frames to make sure that it does not matter
163 // when UnregisterFrame is called. 175 // when UnregisterFrame is called.
164 for (std::set<content::RenderFrameHost*>::iterator it = frames_.begin(); 176 for (std::set<content::RenderFrameHost*>::iterator it = frames_.begin();
165 it != frames_.end(); ) { 177 it != frames_.end(); ) {
166 if (port.HasFrame(*it)) { 178 if (port.HasFrame(*it)) {
167 frames_.erase(it++); 179 frames_.erase(it++);
168 } else { 180 } else {
169 ++it; 181 ++it;
170 } 182 }
(...skipping 12 matching lines...) Expand all
183 const std::string& channel_name, 195 const std::string& channel_name,
184 std::unique_ptr<base::DictionaryValue> source_tab, 196 std::unique_ptr<base::DictionaryValue> source_tab,
185 int source_frame_id, 197 int source_frame_id,
186 int guest_process_id, 198 int guest_process_id,
187 int guest_render_frame_routing_id, 199 int guest_render_frame_routing_id,
188 const std::string& source_extension_id, 200 const std::string& source_extension_id,
189 const std::string& target_extension_id, 201 const std::string& target_extension_id,
190 const GURL& source_url, 202 const GURL& source_url,
191 const std::string& tls_channel_id) { 203 const std::string& tls_channel_id) {
192 ExtensionMsg_TabConnectionInfo source; 204 ExtensionMsg_TabConnectionInfo source;
193 if (source_tab) 205 if (source_tab) {
194 source.tab.Swap(source_tab.get()); 206 source.tab.Swap(source_tab.get());
207 source.tab.GetInteger("id", &opener_tab_id_);
208 }
195 source.frame_id = source_frame_id; 209 source.frame_id = source_frame_id;
196 210
197 ExtensionMsg_ExternalConnectionInfo info; 211 ExtensionMsg_ExternalConnectionInfo info;
198 info.target_id = target_extension_id; 212 info.target_id = target_extension_id;
199 info.source_id = source_extension_id; 213 info.source_id = source_extension_id;
200 info.source_url = source_url; 214 info.source_url = source_url;
201 info.guest_process_id = guest_process_id; 215 info.guest_process_id = guest_process_id;
202 info.guest_render_frame_routing_id = guest_render_frame_routing_id; 216 info.guest_render_frame_routing_id = guest_render_frame_routing_id;
203 217
204 SendToPort(base::WrapUnique(new ExtensionMsg_DispatchOnConnect( 218 SendToPort(base::WrapUnique(new ExtensionMsg_DispatchOnConnect(
205 MSG_ROUTING_NONE, port_id_, channel_name, source, info, tls_channel_id))); 219 MSG_ROUTING_NONE, port_id_, channel_name, source, info, tls_channel_id)));
206 } 220 }
207 221
208 void ExtensionMessagePort::DispatchOnDisconnect( 222 void ExtensionMessagePort::DispatchOnDisconnect(
209 const std::string& error_message) { 223 const std::string& error_message) {
210 SendToPort(base::WrapUnique(new ExtensionMsg_DispatchOnDisconnect( 224 SendToPort(base::WrapUnique(new ExtensionMsg_DispatchOnDisconnect(
211 MSG_ROUTING_NONE, port_id_, error_message))); 225 MSG_ROUTING_NONE, port_id_, error_message)));
212 } 226 }
213 227
214 void ExtensionMessagePort::DispatchOnMessage(const Message& message) { 228 void ExtensionMessagePort::DispatchOnMessage(const Message& message) {
215 SendToPort(base::WrapUnique( 229 SendToPort(base::WrapUnique(new ExtensionMsg_DeliverMessage(
216 new ExtensionMsg_DeliverMessage(MSG_ROUTING_NONE, port_id_, message))); 230 MSG_ROUTING_NONE, port_id_, opener_tab_id_, message)));
217 } 231 }
218 232
219 void ExtensionMessagePort::IncrementLazyKeepaliveCount() { 233 void ExtensionMessagePort::IncrementLazyKeepaliveCount() {
220 ProcessManager* pm = ProcessManager::Get(browser_context_); 234 ProcessManager* pm = ProcessManager::Get(browser_context_);
221 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_); 235 ExtensionHost* host = pm->GetBackgroundHostForExtension(extension_id_);
222 if (host && BackgroundInfo::HasLazyBackgroundPage(host->extension())) 236 if (host && BackgroundInfo::HasLazyBackgroundPage(host->extension()))
223 pm->IncrementLazyKeepaliveCount(host->extension()); 237 pm->IncrementLazyKeepaliveCount(host->extension());
224 238
225 // Keep track of the background host, so when we decrement, we only do so if 239 // Keep track of the background host, so when we decrement, we only do so if
226 // the host hasn't reloaded. 240 // the host hasn't reloaded.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 return; 303 return;
290 } 304 }
291 for (content::RenderFrameHost* rfh : frames_) { 305 for (content::RenderFrameHost* rfh : frames_) {
292 IPC::Message* msg_copy = new IPC::Message(*msg.get()); 306 IPC::Message* msg_copy = new IPC::Message(*msg.get());
293 msg_copy->set_routing_id(rfh->GetRoutingID()); 307 msg_copy->set_routing_id(rfh->GetRoutingID());
294 rfh->Send(msg_copy); 308 rfh->Send(msg_copy);
295 } 309 }
296 } 310 }
297 311
298 } // namespace extensions 312 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698