| 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_render_process_host_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 = web_contents->GetInstanceID(); |
| 105 |
| 106 BrowserThread::PostTask( |
| 107 BrowserThread::IO, FROM_HERE, |
| 108 base::Bind( |
| 109 &ExtensionRendererState::AddWebView, |
| 110 base::Unretained(ExtensionRendererState::GetInstance()), |
| 111 web_contents->GetRenderProcessHost()->GetID(), |
| 112 web_contents->GetRoutingID(), |
| 113 web_view_info)); |
| 114 break; |
| 115 } |
| 98 case chrome::NOTIFICATION_TAB_PARENTED: { | 116 case chrome::NOTIFICATION_TAB_PARENTED: { |
| 99 WebContents* web_contents = content::Source<WebContents>(source).ptr(); | 117 WebContents* web_contents = content::Source<WebContents>(source).ptr(); |
| 100 SessionTabHelper* session_tab_helper = | 118 SessionTabHelper* session_tab_helper = |
| 101 SessionTabHelper::FromWebContents(web_contents); | 119 SessionTabHelper::FromWebContents(web_contents); |
| 102 if (!session_tab_helper) | 120 if (!session_tab_helper) |
| 103 return; | 121 return; |
| 104 RenderViewHost* host = web_contents->GetRenderViewHost(); | 122 RenderViewHost* host = web_contents->GetRenderViewHost(); |
| 105 BrowserThread::PostTask( | 123 BrowserThread::PostTask( |
| 106 BrowserThread::IO, FROM_HERE, | 124 BrowserThread::IO, FROM_HERE, |
| 107 base::Bind( | 125 base::Bind( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 126 base::Bind( | 144 base::Bind( |
| 127 &ExtensionRendererState::SetTabAndWindowId, | 145 &ExtensionRendererState::SetTabAndWindowId, |
| 128 base::Unretained(ExtensionRendererState::GetInstance()), | 146 base::Unretained(ExtensionRendererState::GetInstance()), |
| 129 host->GetProcess()->GetID(), host->GetRoutingID(), | 147 host->GetProcess()->GetID(), host->GetRoutingID(), |
| 130 session_tab_helper->session_id().id(), | 148 session_tab_helper->session_id().id(), |
| 131 session_tab_helper->window_id().id())); | 149 session_tab_helper->window_id().id())); |
| 132 break; | 150 break; |
| 133 } | 151 } |
| 134 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { | 152 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { |
| 135 RenderViewHost* host = content::Source<RenderViewHost>(source).ptr(); | 153 RenderViewHost* host = content::Source<RenderViewHost>(source).ptr(); |
| 154 if (host->GetProcess()->IsGuest()) { |
| 155 BrowserThread::PostTask( |
| 156 BrowserThread::IO, FROM_HERE, |
| 157 base::Bind( |
| 158 &ExtensionRendererState::RemoveWebView, |
| 159 base::Unretained(ExtensionRendererState::GetInstance()), |
| 160 host->GetProcess()->GetID(), |
| 161 host->GetRoutingID())); |
| 162 return; |
| 163 } |
| 136 BrowserThread::PostTask( | 164 BrowserThread::PostTask( |
| 137 BrowserThread::IO, FROM_HERE, | 165 BrowserThread::IO, FROM_HERE, |
| 138 base::Bind( | 166 base::Bind( |
| 139 &ExtensionRendererState::ClearTabAndWindowId, | 167 &ExtensionRendererState::ClearTabAndWindowId, |
| 140 base::Unretained(ExtensionRendererState::GetInstance()), | 168 base::Unretained(ExtensionRendererState::GetInstance()), |
| 141 host->GetProcess()->GetID(), host->GetRoutingID())); | 169 host->GetProcess()->GetID(), host->GetRoutingID())); |
| 142 break; | 170 break; |
| 143 } | 171 } |
| 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: | 172 default: |
| 172 NOTREACHED(); | 173 NOTREACHED(); |
| 173 return; | 174 return; |
| 174 } | 175 } |
| 175 } | 176 } |
| 176 | 177 |
| 177 // | 178 // |
| 178 // ExtensionRendererState | 179 // ExtensionRendererState |
| 179 // | 180 // |
| 180 | 181 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 RenderId render_id(render_process_host_id, routing_id); | 218 RenderId render_id(render_process_host_id, routing_id); |
| 218 TabAndWindowIdMap::iterator iter = map_.find(render_id); | 219 TabAndWindowIdMap::iterator iter = map_.find(render_id); |
| 219 if (iter != map_.end()) { | 220 if (iter != map_.end()) { |
| 220 *tab_id = iter->second.first; | 221 *tab_id = iter->second.first; |
| 221 *window_id = iter->second.second; | 222 *window_id = iter->second.second; |
| 222 return true; | 223 return true; |
| 223 } | 224 } |
| 224 return false; | 225 return false; |
| 225 } | 226 } |
| 226 | 227 |
| 227 void ExtensionRendererState::AddGuestProcess(int render_process_host_id) { | 228 void ExtensionRendererState::AddWebView(int render_process_host_id, |
| 229 int routing_id, |
| 230 const WebViewInfo& web_view_info) { |
| 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 229 guest_set_.insert(render_process_host_id); | 232 RenderId render_id(render_process_host_id, routing_id); |
| 233 web_view_info_map_[render_id] = web_view_info; |
| 230 } | 234 } |
| 231 | 235 |
| 232 void ExtensionRendererState::RemoveGuestProcess(int render_process_host_id) { | 236 void ExtensionRendererState::RemoveWebView(int render_process_host_id, |
| 237 int routing_id) { |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 234 guest_set_.erase(render_process_host_id); | 239 RenderId render_id(render_process_host_id, routing_id); |
| 240 web_view_info_map_.erase(render_id); |
| 235 } | 241 } |
| 236 | 242 |
| 237 bool ExtensionRendererState::IsGuestProcess(int render_process_host_id) { | 243 bool ExtensionRendererState::GetWebViewInfo(int render_process_host_id, |
| 244 int routing_id, |
| 245 WebViewInfo* web_view_info) { |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 239 return guest_set_.count(render_process_host_id) > 0; | 247 RenderId render_id(render_process_host_id, routing_id); |
| 248 WebViewInfoMap::iterator iter = web_view_info_map_.find(render_id); |
| 249 if (iter != web_view_info_map_.end()) { |
| 250 *web_view_info = iter->second; |
| 251 return true; |
| 252 } |
| 253 return false; |
| 240 } | 254 } |
| OLD | NEW |