| 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/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "chrome/browser/task_management/providers/web_contents/subframe_task.h" | 9 #include "chrome/browser/task_management/providers/web_contents/subframe_task.h" |
| 10 #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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 class WebContentsEntry : public content::WebContentsObserver { | 28 class WebContentsEntry : public content::WebContentsObserver { |
| 29 public: | 29 public: |
| 30 WebContentsEntry(content::WebContents* web_contents, | 30 WebContentsEntry(content::WebContents* web_contents, |
| 31 WebContentsTaskProvider* provider); | 31 WebContentsTaskProvider* provider); |
| 32 ~WebContentsEntry() override; | 32 ~WebContentsEntry() override; |
| 33 | 33 |
| 34 // Creates all the tasks associated with each |RenderFrameHost| in this | 34 // Creates all the tasks associated with each |RenderFrameHost| in this |
| 35 // entry's WebContents. | 35 // entry's WebContents. |
| 36 void CreateAllTasks(); | 36 void CreateAllTasks(); |
| 37 | 37 |
| 38 // Clears all the tasks in this entry. The provider's observer will be |
| 39 // notified if |notify_observer| is true. |
| 40 void ClearAllTasks(bool notify_observer); |
| 41 |
| 38 // Returns the |RendererTask| that corresponds to the given | 42 // Returns the |RendererTask| that corresponds to the given |
| 39 // |render_frame_host| or |nullptr| if the given frame is not tracked by this | 43 // |render_frame_host| or |nullptr| if the given frame is not tracked by this |
| 40 // entry. | 44 // entry. |
| 41 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; | 45 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; |
| 42 | 46 |
| 43 // content::WebContentsObserver: | 47 // content::WebContentsObserver: |
| 44 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 48 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
| 45 void RenderFrameHostChanged(RenderFrameHost* old_host, | 49 void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 46 RenderFrameHost* new_host) override; | 50 RenderFrameHost* new_host) override; |
| 47 void RenderViewReady() override; | 51 void RenderViewReady() override; |
| 48 void WebContentsDestroyed() override; | 52 void WebContentsDestroyed() override; |
| 49 void RenderProcessGone(base::TerminationStatus status) override; | 53 void RenderProcessGone(base::TerminationStatus status) override; |
| 50 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; | 54 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; |
| 51 void DidUpdateFaviconURL( | 55 void DidUpdateFaviconURL( |
| 52 const std::vector<content::FaviconURL>& candidates) override; | 56 const std::vector<content::FaviconURL>& candidates) override; |
| 53 | 57 |
| 54 private: | 58 private: |
| 55 // Defines a callback for WebContents::ForEachFrame() to create a | 59 // Defines a callback for WebContents::ForEachFrame() to create a |
| 56 // corresponding task for the given |render_frame_host| and notifying the | 60 // corresponding task for the given |render_frame_host| and notifying the |
| 57 // provider's observer of the new task. | 61 // provider's observer of the new task. |
| 58 void CreateTaskForFrame(RenderFrameHost* render_frame_host); | 62 void CreateTaskForFrame(RenderFrameHost* render_frame_host); |
| 59 | 63 |
| 60 // Clears the task that corresponds to the given |render_frame_host| and | 64 // Clears the task that corresponds to the given |render_frame_host| and |
| 61 // notifies the provider's observer of the tasks removal. | 65 // notifies the provider's observer of the tasks removal. |
| 62 void ClearTaskForFrame(RenderFrameHost* render_frame_host); | 66 void ClearTaskForFrame(RenderFrameHost* render_frame_host); |
| 63 | 67 |
| 64 // Clears all the tasks in this entry. The provider's observer will be | |
| 65 // notified if |notify_observer| is true. | |
| 66 void ClearAllTasks(bool notify_observer); | |
| 67 | |
| 68 // The provider that owns this entry. | 68 // The provider that owns this entry. |
| 69 WebContentsTaskProvider* provider_; | 69 WebContentsTaskProvider* provider_; |
| 70 | 70 |
| 71 // The RenderFrameHosts associated with this entry's WebContents that we're | 71 // The RenderFrameHosts associated with this entry's WebContents that we're |
| 72 // tracking mapped by their SiteInstances. | 72 // tracking mapped by their SiteInstances. |
| 73 typedef std::vector<RenderFrameHost*> FramesList; | 73 typedef std::vector<RenderFrameHost*> FramesList; |
| 74 typedef std::map<SiteInstance*, FramesList> SiteInstanceToFramesMap; | 74 typedef std::map<SiteInstance*, FramesList> SiteInstanceToFramesMap; |
| 75 SiteInstanceToFramesMap frames_by_site_instance_; | 75 SiteInstanceToFramesMap frames_by_site_instance_; |
| 76 | 76 |
| 77 // The RendererTasks that we create for the task manager, mapped by their | 77 // The RendererTasks that we create for the task manager, mapped by their |
| (...skipping 21 matching lines...) Expand all Loading... |
| 99 WebContentsEntry::~WebContentsEntry() { | 99 WebContentsEntry::~WebContentsEntry() { |
| 100 ClearAllTasks(false); | 100 ClearAllTasks(false); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void WebContentsEntry::CreateAllTasks() { | 103 void WebContentsEntry::CreateAllTasks() { |
| 104 DCHECK(web_contents()->GetMainFrame()); | 104 DCHECK(web_contents()->GetMainFrame()); |
| 105 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, | 105 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, |
| 106 base::Unretained(this))); | 106 base::Unretained(this))); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void WebContentsEntry::ClearAllTasks(bool notify_observer) { |
| 110 for (auto& pair : frames_by_site_instance_) { |
| 111 FramesList& frames_list = pair.second; |
| 112 DCHECK(!frames_list.empty()); |
| 113 RendererTask* task = tasks_by_frames_[frames_list[0]]; |
| 114 if (notify_observer) |
| 115 provider_->NotifyObserverTaskRemoved(task); |
| 116 delete task; |
| 117 } |
| 118 |
| 119 frames_by_site_instance_.clear(); |
| 120 tasks_by_frames_.clear(); |
| 121 main_frame_site_instance_ = nullptr; |
| 122 } |
| 123 |
| 109 RendererTask* WebContentsEntry::GetTaskForFrame( | 124 RendererTask* WebContentsEntry::GetTaskForFrame( |
| 110 RenderFrameHost* render_frame_host) const { | 125 RenderFrameHost* render_frame_host) const { |
| 111 auto itr = tasks_by_frames_.find(render_frame_host); | 126 auto itr = tasks_by_frames_.find(render_frame_host); |
| 112 if (itr == tasks_by_frames_.end()) | 127 if (itr == tasks_by_frames_.end()) |
| 113 return nullptr; | 128 return nullptr; |
| 114 | 129 |
| 115 return itr->second; | 130 return itr->second; |
| 116 } | 131 } |
| 117 | 132 |
| 118 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { | 133 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if (frames.empty()) { | 246 if (frames.empty()) { |
| 232 frames_by_site_instance_.erase(site_instance); | 247 frames_by_site_instance_.erase(site_instance); |
| 233 provider_->NotifyObserverTaskRemoved(task); | 248 provider_->NotifyObserverTaskRemoved(task); |
| 234 delete task; | 249 delete task; |
| 235 | 250 |
| 236 if (site_instance == main_frame_site_instance_) | 251 if (site_instance == main_frame_site_instance_) |
| 237 main_frame_site_instance_ = nullptr; | 252 main_frame_site_instance_ = nullptr; |
| 238 } | 253 } |
| 239 } | 254 } |
| 240 | 255 |
| 241 void WebContentsEntry::ClearAllTasks(bool notify_observer) { | |
| 242 for (auto& pair : frames_by_site_instance_) { | |
| 243 FramesList& frames_list = pair.second; | |
| 244 DCHECK(!frames_list.empty()); | |
| 245 RendererTask* task = tasks_by_frames_[frames_list[0]]; | |
| 246 if (notify_observer) | |
| 247 provider_->NotifyObserverTaskRemoved(task); | |
| 248 delete task; | |
| 249 } | |
| 250 | |
| 251 frames_by_site_instance_.clear(); | |
| 252 tasks_by_frames_.clear(); | |
| 253 main_frame_site_instance_ = nullptr; | |
| 254 } | |
| 255 | |
| 256 //////////////////////////////////////////////////////////////////////////////// | 256 //////////////////////////////////////////////////////////////////////////////// |
| 257 | 257 |
| 258 WebContentsTaskProvider::WebContentsTaskProvider() : entries_map_() { | 258 WebContentsTaskProvider::WebContentsTaskProvider() : entries_map_() { |
| 259 } | 259 } |
| 260 | 260 |
| 261 WebContentsTaskProvider::~WebContentsTaskProvider() { | 261 WebContentsTaskProvider::~WebContentsTaskProvider() { |
| 262 STLDeleteValues(&entries_map_); | 262 STLDeleteValues(&entries_map_); |
| 263 } | 263 } |
| 264 | 264 |
| 265 void WebContentsTaskProvider::OnWebContentsTagCreated(WebContentsTag* tag) { | 265 void WebContentsTaskProvider::OnWebContentsTagCreated( |
| 266 const WebContentsTag* tag) { |
| 266 DCHECK(tag); | 267 DCHECK(tag); |
| 267 content::WebContents* web_contents = tag->web_contents(); | 268 content::WebContents* web_contents = tag->web_contents(); |
| 268 DCHECK(web_contents); | 269 DCHECK(web_contents); |
| 269 | 270 |
| 270 // TODO(afakhry): Check if we need this check. It seems that we no longer | 271 // TODO(afakhry): Check if we need this check. It seems that we no longer |
| 271 // need it in the new implementation. | 272 // need it in the new implementation. |
| 272 if (entries_map_.count(web_contents)) { | 273 if (entries_map_.count(web_contents)) { |
| 273 // This case may happen if we added a WebContents while collecting all the | 274 // This case may happen if we added a WebContents while collecting all the |
| 274 // pre-existing ones at the time |StartUpdating()| was called, but the | 275 // pre-existing ones at the time |StartUpdating()| was called, but the |
| 275 // notification of its connection hasn't been fired yet. In this case we | 276 // notification of its connection hasn't been fired yet. In this case we |
| 276 // ignore it since we're already tracking it. | 277 // ignore it since we're already tracking it. |
| 277 return; | 278 return; |
| 278 } | 279 } |
| 279 | 280 |
| 280 WebContentsEntry* entry = new WebContentsEntry(web_contents, this); | 281 WebContentsEntry* entry = new WebContentsEntry(web_contents, this); |
| 281 entries_map_[web_contents] = entry; | 282 entries_map_[web_contents] = entry; |
| 282 entry->CreateAllTasks(); | 283 entry->CreateAllTasks(); |
| 283 } | 284 } |
| 284 | 285 |
| 286 void WebContentsTaskProvider::OnWebContentsTagRemoved( |
| 287 const WebContentsTag* tag) { |
| 288 DCHECK(tag); |
| 289 content::WebContents* web_contents = tag->web_contents(); |
| 290 DCHECK(web_contents); |
| 291 |
| 292 auto itr = entries_map_.find(web_contents); |
| 293 DCHECK(itr != entries_map_.end()); |
| 294 WebContentsEntry* entry = itr->second; |
| 295 |
| 296 // Must manually clear the tasks and notify the observer. |
| 297 entry->ClearAllTasks(true); |
| 298 entries_map_.erase(itr); |
| 299 delete entry; |
| 300 } |
| 301 |
| 285 Task* WebContentsTaskProvider::GetTaskOfUrlRequest(int origin_pid, | 302 Task* WebContentsTaskProvider::GetTaskOfUrlRequest(int origin_pid, |
| 286 int child_id, | 303 int child_id, |
| 287 int route_id) { | 304 int route_id) { |
| 288 // If an origin PID was specified then the URL request originated in a plugin | 305 // If an origin PID was specified then the URL request originated in a plugin |
| 289 // working on the WebContents' behalf, so ignore it. | 306 // working on the WebContents' behalf, so ignore it. |
| 290 if (origin_pid) | 307 if (origin_pid) |
| 291 return nullptr; | 308 return nullptr; |
| 292 | 309 |
| 293 content::RenderFrameHost* rfh = | 310 content::RenderFrameHost* rfh = |
| 294 content::RenderFrameHost::FromID(child_id, route_id); | 311 content::RenderFrameHost::FromID(child_id, route_id); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 | 354 |
| 338 WebContentsEntry* entry = itr->second; | 355 WebContentsEntry* entry = itr->second; |
| 339 entries_map_.erase(itr); | 356 entries_map_.erase(itr); |
| 340 | 357 |
| 341 // The entry we're about to delete is our caller, however its' still fine to | 358 // The entry we're about to delete is our caller, however its' still fine to |
| 342 // delete it. | 359 // delete it. |
| 343 delete entry; | 360 delete entry; |
| 344 } | 361 } |
| 345 | 362 |
| 346 } // namespace task_management | 363 } // namespace task_management |
| OLD | NEW |