| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/extension_renderer_state.h" | 5 #include "chrome/browser/extensions/extension_renderer_state.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "chrome/browser/sessions/session_tab_helper.h" | 9 #include "chrome/browser/sessions/session_tab_helper.h" |
| 10 #include "chrome/browser/tab_contents/retargeting_details.h" | 10 #include "chrome/browser/tab_contents/retargeting_details.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 const content::NotificationDetails& details); | 44 const content::NotificationDetails& details); |
| 45 | 45 |
| 46 content::NotificationRegistrar registrar_; | 46 content::NotificationRegistrar registrar_; |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 ExtensionRendererState::TabObserver::TabObserver() { | 49 ExtensionRendererState::TabObserver::TabObserver() { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 51 registrar_.Add(this, | 51 registrar_.Add(this, |
| 52 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, | 52 content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED, |
| 53 content::NotificationService::AllBrowserContextsAndSources()); | 53 content::NotificationService::AllBrowserContextsAndSources()); |
| 54 registrar_.Add(this, |
| 55 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 56 content::NotificationService::AllBrowserContextsAndSources()); |
| 54 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_DELETED, | 57 registrar_.Add(this, content::NOTIFICATION_RENDER_VIEW_HOST_DELETED, |
| 55 content::NotificationService::AllBrowserContextsAndSources()); | 58 content::NotificationService::AllBrowserContextsAndSources()); |
| 56 registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED, | 59 registrar_.Add(this, chrome::NOTIFICATION_TAB_PARENTED, |
| 57 content::NotificationService::AllBrowserContextsAndSources()); | 60 content::NotificationService::AllBrowserContextsAndSources()); |
| 58 registrar_.Add(this, chrome::NOTIFICATION_RETARGETING, | 61 registrar_.Add(this, chrome::NOTIFICATION_RETARGETING, |
| 59 content::NotificationService::AllBrowserContextsAndSources()); | 62 content::NotificationService::AllBrowserContextsAndSources()); |
| 60 registrar_.Add(this, | |
| 61 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 62 content::NotificationService::AllBrowserContextsAndSources()); | |
| 63 registrar_.Add(this, | |
| 64 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 65 content::NotificationService::AllBrowserContextsAndSources()); | |
| 66 registrar_.Add(this, | |
| 67 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 68 content::NotificationService::AllBrowserContextsAndSources()); | |
| 69 } | 63 } |
| 70 | 64 |
| 71 ExtensionRendererState::TabObserver::~TabObserver() { | 65 ExtensionRendererState::TabObserver::~TabObserver() { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 73 } | 67 } |
| 74 | 68 |
| 75 void ExtensionRendererState::TabObserver::Observe( | 69 void ExtensionRendererState::TabObserver::Observe( |
| 76 int type, const content::NotificationSource& source, | 70 int type, const content::NotificationSource& source, |
| 77 const content::NotificationDetails& details) { | 71 const content::NotificationDetails& details) { |
| 78 switch (type) { | 72 switch (type) { |
| 79 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: { | 73 case content::NOTIFICATION_WEB_CONTENTS_RENDER_VIEW_HOST_CREATED: { |
| 80 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 74 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 81 SessionTabHelper* session_tab_helper = | 75 SessionTabHelper* session_tab_helper = |
| 82 SessionTabHelper::FromWebContents(web_contents); | 76 SessionTabHelper::FromWebContents(web_contents); |
| 83 if (!session_tab_helper) | 77 if (!session_tab_helper) |
| 84 return; | 78 return; |
| 85 RenderViewHost* host = content::Details<RenderViewHost>(details).ptr(); | 79 RenderViewHost* host = content::Details<RenderViewHost>(details).ptr(); |
| 86 // TODO(mpcomplete): How can we tell if window_id is bogus? It may not | 80 // TODO(mpcomplete): How can we tell if window_id is bogus? It may not |
| 87 // have been set yet. | 81 // have been set yet. |
| 88 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
| 89 BrowserThread::IO, FROM_HERE, | 83 BrowserThread::IO, FROM_HERE, |
| 90 base::Bind( | 84 base::Bind( |
| 91 &ExtensionRendererState::SetTabAndWindowId, | 85 &ExtensionRendererState::SetTabAndWindowId, |
| 92 base::Unretained(ExtensionRendererState::GetInstance()), | 86 base::Unretained(ExtensionRendererState::GetInstance()), |
| 93 host->GetProcess()->GetID(), host->GetRoutingID(), | 87 host->GetProcess()->GetID(), host->GetRoutingID(), |
| 94 session_tab_helper->session_id().id(), | 88 session_tab_helper->session_id().id(), |
| 95 session_tab_helper->window_id().id())); | 89 session_tab_helper->window_id().id())); |
| 96 break; | 90 break; |
| 97 } | 91 } |
| 92 case content::NOTIFICATION_WEB_CONTENTS_CONNECTED: { |
| 93 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 94 if (!web_contents->GetRenderProcessHost()->IsGuest()) |
| 95 return; |
| 96 |
| 97 WebContents* embedder_web_contents = |
| 98 web_contents->GetEmbedderWebContents(); |
| 99 WebViewInfo web_view_info; |
| 100 web_view_info.embedder_process_id = |
| 101 embedder_web_contents->GetRenderProcessHost()->GetID(); |
| 102 web_view_info.embedder_routing_id = |
| 103 embedder_web_contents->GetRoutingID(); |
| 104 web_view_info.web_view_instance_id = |
| 105 web_contents->GetEmbeddedInstanceID(); |
| 106 |
| 107 BrowserThread::PostTask( |
| 108 BrowserThread::IO, FROM_HERE, |
| 109 base::Bind( |
| 110 &ExtensionRendererState::AddWebView, |
| 111 base::Unretained(ExtensionRendererState::GetInstance()), |
| 112 web_contents->GetRenderProcessHost()->GetID(), |
| 113 web_contents->GetRoutingID(), |
| 114 web_view_info)); |
| 115 break; |
| 116 } |
| 98 case chrome::NOTIFICATION_TAB_PARENTED: { | 117 case chrome::NOTIFICATION_TAB_PARENTED: { |
| 99 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 118 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 100 SessionTabHelper* session_tab_helper = | 119 SessionTabHelper* session_tab_helper = |
| 101 SessionTabHelper::FromWebContents(web_contents); | 120 SessionTabHelper::FromWebContents(web_contents); |
| 102 if (!session_tab_helper) | 121 if (!session_tab_helper) |
| 103 return; | 122 return; |
| 104 RenderViewHost* host = web_contents->GetRenderViewHost(); | 123 RenderViewHost* host = web_contents->GetRenderViewHost(); |
| 105 BrowserThread::PostTask( | 124 BrowserThread::PostTask( |
| 106 BrowserThread::IO, FROM_HERE, | 125 BrowserThread::IO, FROM_HERE, |
| 107 base::Bind( | 126 base::Bind( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 126 base::Bind( | 145 base::Bind( |
| 127 &ExtensionRendererState::SetTabAndWindowId, | 146 &ExtensionRendererState::SetTabAndWindowId, |
| 128 base::Unretained(ExtensionRendererState::GetInstance()), | 147 base::Unretained(ExtensionRendererState::GetInstance()), |
| 129 host->GetProcess()->GetID(), host->GetRoutingID(), | 148 host->GetProcess()->GetID(), host->GetRoutingID(), |
| 130 session_tab_helper->session_id().id(), | 149 session_tab_helper->session_id().id(), |
| 131 session_tab_helper->window_id().id())); | 150 session_tab_helper->window_id().id())); |
| 132 break; | 151 break; |
| 133 } | 152 } |
| 134 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { | 153 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { |
| 135 RenderViewHost* host = content::Source<RenderViewHost>(source).ptr(); | 154 RenderViewHost* host = content::Source<RenderViewHost>(source).ptr(); |
| 155 if (host->GetProcess()->IsGuest()) { |
| 156 BrowserThread::PostTask( |
| 157 BrowserThread::IO, FROM_HERE, |
| 158 base::Bind( |
| 159 &ExtensionRendererState::RemoveWebView, |
| 160 base::Unretained(ExtensionRendererState::GetInstance()), |
| 161 host->GetProcess()->GetID(), |
| 162 host->GetRoutingID())); |
| 163 return; |
| 164 } |
| 136 BrowserThread::PostTask( | 165 BrowserThread::PostTask( |
| 137 BrowserThread::IO, FROM_HERE, | 166 BrowserThread::IO, FROM_HERE, |
| 138 base::Bind( | 167 base::Bind( |
| 139 &ExtensionRendererState::ClearTabAndWindowId, | 168 &ExtensionRendererState::ClearTabAndWindowId, |
| 140 base::Unretained(ExtensionRendererState::GetInstance()), | 169 base::Unretained(ExtensionRendererState::GetInstance()), |
| 141 host->GetProcess()->GetID(), host->GetRoutingID())); | 170 host->GetProcess()->GetID(), host->GetRoutingID())); |
| 142 break; | 171 break; |
| 143 } | 172 } |
| 144 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | |
| 145 RenderProcessHost* render_process_host = | |
| 146 content::Source<RenderProcessHost>(source).ptr(); | |
| 147 if (!render_process_host->IsGuest()) | |
| 148 return; | |
| 149 BrowserThread::PostTask( | |
| 150 BrowserThread::IO, FROM_HERE, | |
| 151 base::Bind( | |
| 152 &ExtensionRendererState::AddGuestProcess, | |
| 153 base::Unretained(ExtensionRendererState::GetInstance()), | |
| 154 render_process_host->GetID())); | |
| 155 break; | |
| 156 } | |
| 157 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: | |
| 158 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
| 159 RenderProcessHost* render_process_host = | |
| 160 content::Source<RenderProcessHost>(source).ptr(); | |
| 161 if (!render_process_host->IsGuest()) | |
| 162 return; | |
| 163 BrowserThread::PostTask( | |
| 164 BrowserThread::IO, FROM_HERE, | |
| 165 base::Bind( | |
| 166 &ExtensionRendererState::RemoveGuestProcess, | |
| 167 base::Unretained(ExtensionRendererState::GetInstance()), | |
| 168 render_process_host->GetID())); | |
| 169 break; | |
| 170 } | |
| 171 default: | 173 default: |
| 172 NOTREACHED(); | 174 NOTREACHED(); |
| 173 return; | 175 return; |
| 174 } | 176 } |
| 175 } | 177 } |
| 176 | 178 |
| 177 // | 179 // |
| 178 // ExtensionRendererState | 180 // ExtensionRendererState |
| 179 // | 181 // |
| 180 | 182 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 RenderId render_id(render_process_host_id, routing_id); | 219 RenderId render_id(render_process_host_id, routing_id); |
| 218 TabAndWindowIdMap::iterator iter = map_.find(render_id); | 220 TabAndWindowIdMap::iterator iter = map_.find(render_id); |
| 219 if (iter != map_.end()) { | 221 if (iter != map_.end()) { |
| 220 *tab_id = iter->second.first; | 222 *tab_id = iter->second.first; |
| 221 *window_id = iter->second.second; | 223 *window_id = iter->second.second; |
| 222 return true; | 224 return true; |
| 223 } | 225 } |
| 224 return false; | 226 return false; |
| 225 } | 227 } |
| 226 | 228 |
| 227 void ExtensionRendererState::AddGuestProcess(int render_process_host_id) { | 229 void ExtensionRendererState::AddWebView(int guest_process_id, |
| 230 int guest_routing_id, |
| 231 const WebViewInfo& web_view_info) { |
| 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 229 guest_set_.insert(render_process_host_id); | 233 RenderId render_id(guest_process_id, guest_routing_id); |
| 234 web_view_info_map_[render_id] = web_view_info; |
| 230 } | 235 } |
| 231 | 236 |
| 232 void ExtensionRendererState::RemoveGuestProcess(int render_process_host_id) { | 237 void ExtensionRendererState::RemoveWebView(int guest_process_id, |
| 238 int guest_routing_id) { |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 234 guest_set_.erase(render_process_host_id); | 240 RenderId render_id(guest_process_id, guest_routing_id); |
| 241 web_view_info_map_.erase(render_id); |
| 235 } | 242 } |
| 236 | 243 |
| 237 bool ExtensionRendererState::IsGuestProcess(int render_process_host_id) { | 244 bool ExtensionRendererState::GetWebViewInfo(int guest_process_id, |
| 245 int guest_routing_id, |
| 246 WebViewInfo* web_view_info) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 239 return guest_set_.count(render_process_host_id) > 0; | 248 RenderId render_id(guest_process_id, guest_routing_id); |
| 249 WebViewInfoMap::iterator iter = web_view_info_map_.find(render_id); |
| 250 if (iter != web_view_info_map_.end()) { |
| 251 *web_view_info = iter->second; |
| 252 return true; |
| 253 } |
| 254 return false; |
| 240 } | 255 } |
| OLD | NEW |