| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/task_management/providers/web_contents/web_contents_tas
k_provider.h" | 5 #include "chrome/browser/task_management/providers/web_contents/web_contents_tas
k_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/stl_util.h" | |
| 10 #include "chrome/browser/task_management/providers/web_contents/subframe_task.h" | 9 #include "chrome/browser/task_management/providers/web_contents/subframe_task.h" |
| 11 #include "chrome/browser/task_management/providers/web_contents/web_contents_tag
s_manager.h" | 10 #include "chrome/browser/task_management/providers/web_contents/web_contents_tag
s_manager.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 11 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_widget_host.h" | 13 #include "content/public/browser/render_widget_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 17 | 16 |
| 18 using content::RenderFrameHost; | 17 using content::RenderFrameHost; |
| 19 using content::RenderWidgetHost; | 18 using content::RenderWidgetHost; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 bool explicit_set) { | 194 bool explicit_set) { |
| 196 RendererTask* task = GetTaskForFrame(web_contents()->GetMainFrame()); | 195 RendererTask* task = GetTaskForFrame(web_contents()->GetMainFrame()); |
| 197 if (!task) | 196 if (!task) |
| 198 return; | 197 return; |
| 199 | 198 |
| 200 task->UpdateTitle(); | 199 task->UpdateTitle(); |
| 201 task->UpdateFavicon(); | 200 task->UpdateFavicon(); |
| 202 } | 201 } |
| 203 | 202 |
| 204 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { | 203 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { |
| 205 DCHECK(!ContainsKey(tasks_by_frames_, render_frame_host)); | 204 DCHECK(!tasks_by_frames_.count(render_frame_host)); |
| 206 | 205 |
| 207 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); | 206 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); |
| 208 if (!site_instance->GetProcess()->HasConnection()) | 207 if (!site_instance->GetProcess()->HasConnection()) |
| 209 return; | 208 return; |
| 210 | 209 |
| 211 bool site_instance_exists = | 210 bool site_instance_exists = |
| 212 ContainsKey(frames_by_site_instance_, site_instance); | 211 frames_by_site_instance_.count(site_instance) != 0; |
| 213 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); | 212 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); |
| 214 bool site_instance_is_main = (site_instance == main_frame_site_instance_); | 213 bool site_instance_is_main = (site_instance == main_frame_site_instance_); |
| 215 | 214 |
| 216 RendererTask* new_task = nullptr; | 215 RendererTask* new_task = nullptr; |
| 217 // We don't create a task if there's one for this site_instance AND | 216 // We don't create a task if there's one for this site_instance AND |
| 218 // if this is not the main frame or we did record a main frame for the entry. | 217 // if this is not the main frame or we did record a main frame for the entry. |
| 219 if (!site_instance_exists || (is_main_frame && !site_instance_is_main)) { | 218 if (!site_instance_exists || (is_main_frame && !site_instance_is_main)) { |
| 220 if (is_main_frame) { | 219 if (is_main_frame) { |
| 221 const WebContentsTag* tag = | 220 const WebContentsTag* tag = |
| 222 WebContentsTag::FromWebContents(web_contents()); | 221 WebContentsTag::FromWebContents(web_contents()); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 } | 291 } |
| 293 | 292 |
| 294 void WebContentsTaskProvider::OnWebContentsTagCreated( | 293 void WebContentsTaskProvider::OnWebContentsTagCreated( |
| 295 const WebContentsTag* tag) { | 294 const WebContentsTag* tag) { |
| 296 DCHECK(tag); | 295 DCHECK(tag); |
| 297 content::WebContents* web_contents = tag->web_contents(); | 296 content::WebContents* web_contents = tag->web_contents(); |
| 298 DCHECK(web_contents); | 297 DCHECK(web_contents); |
| 299 | 298 |
| 300 // TODO(afakhry): Check if we need this check. It seems that we no longer | 299 // TODO(afakhry): Check if we need this check. It seems that we no longer |
| 301 // need it in the new implementation. | 300 // need it in the new implementation. |
| 302 if (HasWebContents(web_contents)) { | 301 std::unique_ptr<WebContentsEntry>& entry = entries_map_[web_contents]; |
| 302 if (entry) { |
| 303 // This case may happen if we added a WebContents while collecting all the | 303 // This case may happen if we added a WebContents while collecting all the |
| 304 // pre-existing ones at the time |StartUpdating()| was called, but the | 304 // pre-existing ones at the time |StartUpdating()| was called, but the |
| 305 // notification of its connection hasn't been fired yet. In this case we | 305 // notification of its connection hasn't been fired yet. In this case we |
| 306 // ignore it since we're already tracking it. | 306 // ignore it since we're already tracking it. |
| 307 return; | 307 return; |
| 308 } | 308 } |
| 309 | 309 |
| 310 WebContentsEntry* entry = new WebContentsEntry(web_contents, this); | 310 entry.reset(new WebContentsEntry(web_contents, this)); |
| 311 entries_map_[web_contents] = entry; | |
| 312 entry->CreateAllTasks(); | 311 entry->CreateAllTasks(); |
| 313 } | 312 } |
| 314 | 313 |
| 315 void WebContentsTaskProvider::OnWebContentsTagRemoved( | 314 void WebContentsTaskProvider::OnWebContentsTagRemoved( |
| 316 const WebContentsTag* tag) { | 315 const WebContentsTag* tag) { |
| 317 DCHECK(tag); | 316 DCHECK(tag); |
| 318 content::WebContents* web_contents = tag->web_contents(); | 317 content::WebContents* web_contents = tag->web_contents(); |
| 319 DCHECK(web_contents); | 318 DCHECK(web_contents); |
| 320 | 319 |
| 321 auto itr = entries_map_.find(web_contents); | 320 auto itr = entries_map_.find(web_contents); |
| 322 DCHECK(itr != entries_map_.end()); | 321 DCHECK(itr != entries_map_.end()); |
| 323 WebContentsEntry* entry = itr->second; | |
| 324 | 322 |
| 325 // Must manually clear the tasks and notify the observer. | 323 // Must manually clear the tasks and notify the observer. |
| 326 entry->ClearAllTasks(true); | 324 itr->second->ClearAllTasks(true); |
| 327 entries_map_.erase(itr); | 325 entries_map_.erase(itr); // Deletes the WebContentsEntry. |
| 328 delete entry; | |
| 329 } | 326 } |
| 330 | 327 |
| 331 Task* WebContentsTaskProvider::GetTaskOfUrlRequest(int origin_pid, | 328 Task* WebContentsTaskProvider::GetTaskOfUrlRequest(int origin_pid, |
| 332 int child_id, | 329 int child_id, |
| 333 int route_id) { | 330 int route_id) { |
| 334 // If an origin PID was specified then the URL request originated in a plugin | 331 // If an origin PID was specified then the URL request originated in a plugin |
| 335 // working on the WebContents' behalf, so ignore it. | 332 // working on the WebContents' behalf, so ignore it. |
| 336 if (origin_pid) | 333 if (origin_pid) |
| 337 return nullptr; | 334 return nullptr; |
| 338 | 335 |
| 339 content::RenderFrameHost* rfh = | 336 content::RenderFrameHost* rfh = |
| 340 content::RenderFrameHost::FromID(child_id, route_id); | 337 content::RenderFrameHost::FromID(child_id, route_id); |
| 341 content::WebContents* web_contents = | 338 content::WebContents* web_contents = |
| 342 content::WebContents::FromRenderFrameHost(rfh); | 339 content::WebContents::FromRenderFrameHost(rfh); |
| 343 | 340 |
| 344 auto itr = entries_map_.find(web_contents); | 341 auto itr = entries_map_.find(web_contents); |
| 345 if (itr == entries_map_.end()) { | 342 if (itr == entries_map_.end()) { |
| 346 // Can happen if the tab was closed while a network request was being | 343 // Can happen if the tab was closed while a network request was being |
| 347 // performed. | 344 // performed. |
| 348 return nullptr; | 345 return nullptr; |
| 349 } | 346 } |
| 350 | 347 |
| 351 return itr->second->GetTaskForFrame(rfh); | 348 return itr->second->GetTaskForFrame(rfh); |
| 352 } | 349 } |
| 353 | 350 |
| 354 bool WebContentsTaskProvider::HasWebContents( | 351 bool WebContentsTaskProvider::HasWebContents( |
| 355 content::WebContents* web_contents) const { | 352 content::WebContents* web_contents) const { |
| 356 return ContainsKey(entries_map_, web_contents); | 353 return entries_map_.count(web_contents) != 0; |
| 357 } | 354 } |
| 358 | 355 |
| 359 void WebContentsTaskProvider::StartUpdating() { | 356 void WebContentsTaskProvider::StartUpdating() { |
| 360 is_updating_ = true; | 357 is_updating_ = true; |
| 361 | 358 |
| 362 // 1- Collect all pre-existing WebContents from the WebContentsTagsManager. | 359 // 1- Collect all pre-existing WebContents from the WebContentsTagsManager. |
| 363 WebContentsTagsManager* tags_manager = WebContentsTagsManager::GetInstance(); | 360 WebContentsTagsManager* tags_manager = WebContentsTagsManager::GetInstance(); |
| 364 for (const auto* tag : tags_manager->tracked_tags()) | 361 for (const auto* tag : tags_manager->tracked_tags()) |
| 365 OnWebContentsTagCreated(tag); | 362 OnWebContentsTagCreated(tag); |
| 366 | 363 |
| 367 // 2- Start observing newly connected ones. | 364 // 2- Start observing newly connected ones. |
| 368 tags_manager->SetProvider(this); | 365 tags_manager->SetProvider(this); |
| 369 } | 366 } |
| 370 | 367 |
| 371 void WebContentsTaskProvider::StopUpdating() { | 368 void WebContentsTaskProvider::StopUpdating() { |
| 372 is_updating_ = false; | 369 is_updating_ = false; |
| 373 | 370 |
| 374 // 1- Stop observing. | 371 // 1- Stop observing. |
| 375 WebContentsTagsManager::GetInstance()->ClearProvider(); | 372 WebContentsTagsManager::GetInstance()->ClearProvider(); |
| 376 | 373 |
| 377 // 2- Clear storage. | 374 // 2- Clear storage. |
| 378 STLDeleteValues(&entries_map_); | 375 entries_map_.clear(); |
| 379 } | 376 } |
| 380 | 377 |
| 381 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { | 378 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { |
| 382 auto itr = entries_map_.find(web_contents); | 379 // This erase() will delete the WebContentsEntry, which is actually our |
| 383 if (itr == entries_map_.end()) { | 380 // caller, but it's expecting us to delete it. |
| 384 NOTREACHED(); | 381 bool success = entries_map_.erase(web_contents) != 0; |
| 385 return; | 382 DCHECK(success); |
| 386 } | |
| 387 | |
| 388 WebContentsEntry* entry = itr->second; | |
| 389 entries_map_.erase(itr); | |
| 390 | |
| 391 // The entry we're about to delete is our caller, however its' still fine to | |
| 392 // delete it. | |
| 393 delete entry; | |
| 394 } | 383 } |
| 395 | 384 |
| 396 } // namespace task_management | 385 } // namespace task_management |
| OLD | NEW |