| 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_manager/providers/web_contents/web_contents_task_p
rovider.h" | 5 #include "chrome/browser/task_manager/providers/web_contents/web_contents_task_p
rovider.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/memory/weak_ptr.h" |
| 10 #include "base/stl_util.h" |
| 9 #include "chrome/browser/task_manager/providers/web_contents/subframe_task.h" | 11 #include "chrome/browser/task_manager/providers/web_contents/subframe_task.h" |
| 10 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m
anager.h" | 12 #include "chrome/browser/task_manager/providers/web_contents/web_contents_tags_m
anager.h" |
| 11 #include "content/public/browser/navigation_handle.h" | 13 #include "content/public/browser/navigation_handle.h" |
| 12 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 13 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 14 #include "content/public/browser/render_widget_host.h" | 16 #include "content/public/browser/render_widget_host.h" |
| 15 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 17 | 19 |
| 18 using content::RenderFrameHost; | 20 using content::RenderFrameHost; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 // Returns the |RendererTask| that corresponds to the given | 47 // Returns the |RendererTask| that corresponds to the given |
| 46 // |render_frame_host| or |nullptr| if the given frame is not tracked by this | 48 // |render_frame_host| or |nullptr| if the given frame is not tracked by this |
| 47 // entry. | 49 // entry. |
| 48 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; | 50 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; |
| 49 | 51 |
| 50 // content::WebContentsObserver: | 52 // content::WebContentsObserver: |
| 51 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 53 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
| 52 void RenderFrameHostChanged(RenderFrameHost* old_host, | 54 void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 53 RenderFrameHost* new_host) override; | 55 RenderFrameHost* new_host) override; |
| 54 void RenderViewReady() override; | 56 void RenderFrameCreated(RenderFrameHost*) override; |
| 55 void WebContentsDestroyed() override; | 57 void WebContentsDestroyed() override; |
| 56 void RenderProcessGone(base::TerminationStatus status) override; | 58 void RenderProcessGone(base::TerminationStatus status) override; |
| 57 void OnRendererUnresponsive(RenderWidgetHost* render_widget_host) override; | 59 void OnRendererUnresponsive(RenderWidgetHost* render_widget_host) override; |
| 58 void DidFinishNavigation( | 60 void DidFinishNavigation( |
| 59 content::NavigationHandle* navigation_handle) override; | 61 content::NavigationHandle* navigation_handle) override; |
| 60 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; | 62 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; |
| 61 | 63 |
| 64 void RenderFrameReady(int process_routing_id, int frame_routing_id); |
| 65 |
| 62 private: | 66 private: |
| 63 // Defines a callback for WebContents::ForEachFrame() to create a | 67 // Defines a callback for WebContents::ForEachFrame() to create a |
| 64 // corresponding task for the given |render_frame_host| and notifying the | 68 // corresponding task for the given |render_frame_host| and notifying the |
| 65 // provider's observer of the new task. | 69 // provider's observer of the new task. |
| 66 void CreateTaskForFrame(RenderFrameHost* render_frame_host); | 70 void CreateTaskForFrame(RenderFrameHost* render_frame_host); |
| 67 | 71 |
| 68 // Clears the task that corresponds to the given |render_frame_host| and | 72 // Clears the task that corresponds to the given |render_frame_host| and |
| 69 // notifies the provider's observer of the tasks removal. | 73 // notifies the provider's observer of the tasks removal. |
| 70 void ClearTaskForFrame(RenderFrameHost* render_frame_host); | 74 void ClearTaskForFrame(RenderFrameHost* render_frame_host); |
| 71 | 75 |
| 72 // Calls |on_task| for each task managed by this WebContentsEntry. | 76 // Calls |on_task| for each task managed by this WebContentsEntry. |
| 73 void ForEachTask(const base::Callback<void(RendererTask*)>& on_task); | 77 void ForEachTask(const base::Callback<void(RendererTask*)>& on_task); |
| 74 | 78 |
| 75 // The provider that owns this entry. | 79 // The provider that owns this entry. |
| 76 WebContentsTaskProvider* provider_; | 80 WebContentsTaskProvider* provider_; |
| 77 | 81 |
| 78 // The RenderFrameHosts associated with this entry's WebContents that we're | 82 // The RenderFrameHosts associated with this entry's WebContents that we're |
| 79 // tracking mapped by their SiteInstances. | 83 // tracking mapped by their SiteInstances. |
| 80 using FramesList = std::vector<RenderFrameHost*>; | 84 using FramesList = std::vector<RenderFrameHost*>; |
| 81 std::map<SiteInstance*, FramesList> frames_by_site_instance_; | 85 std::map<SiteInstance*, FramesList> frames_by_site_instance_; |
| 82 | 86 |
| 83 // The RendererTasks that we create for the task manager, mapped by their | 87 // The RendererTasks that we create for the task manager, mapped by their |
| 84 // RenderFrameHosts. This owns the RenderTasks. | 88 // RenderFrameHosts. This owns the RenderTasks. |
| 85 std::map<RenderFrameHost*, RendererTask*> tasks_by_frames_; | 89 std::map<RenderFrameHost*, RendererTask*> tasks_by_frames_; |
| 86 | 90 |
| 87 // States whether we did record a main frame for this entry. | 91 // States whether we did record a main frame for this entry. |
| 88 SiteInstance* main_frame_site_instance_; | 92 SiteInstance* main_frame_site_instance_; |
| 89 | 93 |
| 94 base::WeakPtrFactory<WebContentsEntry> weak_factory_; |
| 95 |
| 90 DISALLOW_COPY_AND_ASSIGN(WebContentsEntry); | 96 DISALLOW_COPY_AND_ASSIGN(WebContentsEntry); |
| 91 }; | 97 }; |
| 92 | 98 |
| 93 //////////////////////////////////////////////////////////////////////////////// | 99 //////////////////////////////////////////////////////////////////////////////// |
| 94 | 100 |
| 95 WebContentsEntry::WebContentsEntry(content::WebContents* web_contents, | 101 WebContentsEntry::WebContentsEntry(content::WebContents* web_contents, |
| 96 WebContentsTaskProvider* provider) | 102 WebContentsTaskProvider* provider) |
| 97 : WebContentsObserver(web_contents), | 103 : WebContentsObserver(web_contents), |
| 98 provider_(provider), | 104 provider_(provider), |
| 99 main_frame_site_instance_(nullptr) { | 105 main_frame_site_instance_(nullptr), |
| 100 } | 106 weak_factory_(this) {} |
| 101 | 107 |
| 102 WebContentsEntry::~WebContentsEntry() { | 108 WebContentsEntry::~WebContentsEntry() { |
| 103 ClearAllTasks(false); | 109 ClearAllTasks(false); |
| 104 } | 110 } |
| 105 | 111 |
| 106 void WebContentsEntry::CreateAllTasks() { | 112 void WebContentsEntry::CreateAllTasks() { |
| 107 DCHECK(web_contents()->GetMainFrame()); | 113 DCHECK(web_contents()->GetMainFrame()); |
| 108 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, | 114 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, |
| 109 base::Unretained(this))); | 115 base::Unretained(this))); |
| 110 } | 116 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 139 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { | 145 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { |
| 140 ClearTaskForFrame(render_frame_host); | 146 ClearTaskForFrame(render_frame_host); |
| 141 } | 147 } |
| 142 | 148 |
| 143 void WebContentsEntry::RenderFrameHostChanged(RenderFrameHost* old_host, | 149 void WebContentsEntry::RenderFrameHostChanged(RenderFrameHost* old_host, |
| 144 RenderFrameHost* new_host) { | 150 RenderFrameHost* new_host) { |
| 145 ClearTaskForFrame(old_host); | 151 ClearTaskForFrame(old_host); |
| 146 CreateTaskForFrame(new_host); | 152 CreateTaskForFrame(new_host); |
| 147 } | 153 } |
| 148 | 154 |
| 149 void WebContentsEntry::RenderViewReady() { | 155 void WebContentsEntry::RenderFrameCreated(RenderFrameHost* render_frame_host) { |
| 150 ClearAllTasks(true); | 156 // Skip pending/speculative hosts. We'll create tasks for these if the |
| 151 CreateAllTasks(); | 157 // navigation commits, at which point RenderFrameHostChanged() will fire. |
| 158 if (!render_frame_host->IsCurrent()) |
| 159 return; |
| 160 |
| 161 // Task manager will have no separate entry for |render_frame_host| if it has |
| 162 // the same site instance as its parent - quit early in this case. |
| 163 if (render_frame_host->GetParent() && |
| 164 render_frame_host->GetParent()->GetSiteInstance() == |
| 165 render_frame_host->GetSiteInstance()) |
| 166 return; |
| 167 |
| 168 // Postpone processing |render_frame_host| until its process has a PID. |
| 169 render_frame_host->GetProcess()->PostTaskWhenProcessIsReady(base::Bind( |
| 170 &WebContentsEntry::RenderFrameReady, weak_factory_.GetWeakPtr(), |
| 171 render_frame_host->GetProcess()->GetID(), |
| 172 render_frame_host->GetRoutingID())); |
| 173 } |
| 174 |
| 175 void WebContentsEntry::RenderFrameReady(int render_process_id, |
| 176 int render_frame_id) { |
| 177 content::RenderFrameHost* render_frame_host = |
| 178 content::RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 179 if (render_frame_host) |
| 180 CreateTaskForFrame(render_frame_host); |
| 152 } | 181 } |
| 153 | 182 |
| 154 void WebContentsEntry::WebContentsDestroyed() { | 183 void WebContentsEntry::WebContentsDestroyed() { |
| 155 ClearAllTasks(true); | 184 ClearAllTasks(true); |
| 156 provider_->DeleteEntry(web_contents()); | 185 provider_->DeleteEntry(web_contents()); |
| 157 } | 186 } |
| 158 | 187 |
| 159 void WebContentsEntry::RenderProcessGone(base::TerminationStatus status) { | 188 void WebContentsEntry::RenderProcessGone(base::TerminationStatus status) { |
| 160 ClearAllTasks(true); | 189 ClearAllTasks(true); |
| 161 } | 190 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 235 |
| 207 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry, | 236 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry, |
| 208 bool explicit_set) { | 237 bool explicit_set) { |
| 209 ForEachTask(base::Bind([](RendererTask* task) { | 238 ForEachTask(base::Bind([](RendererTask* task) { |
| 210 task->UpdateTitle(); | 239 task->UpdateTitle(); |
| 211 task->UpdateFavicon(); | 240 task->UpdateFavicon(); |
| 212 })); | 241 })); |
| 213 } | 242 } |
| 214 | 243 |
| 215 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { | 244 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { |
| 245 DCHECK(render_frame_host); |
| 216 DCHECK(!tasks_by_frames_.count(render_frame_host)); | 246 DCHECK(!tasks_by_frames_.count(render_frame_host)); |
| 217 | 247 |
| 218 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); | 248 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); |
| 219 if (!site_instance->GetProcess()->HasConnection()) | 249 if (!site_instance->GetProcess()->HasConnection()) |
| 220 return; | 250 return; |
| 251 if (!render_frame_host->IsRenderFrameLive()) |
| 252 return; |
| 221 | 253 |
| 222 bool site_instance_exists = | 254 bool site_instance_exists = |
| 223 frames_by_site_instance_.count(site_instance) != 0; | 255 frames_by_site_instance_.count(site_instance) != 0; |
| 224 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); | 256 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); |
| 225 bool site_instance_is_main = (site_instance == main_frame_site_instance_); | 257 bool site_instance_is_main = (site_instance == main_frame_site_instance_); |
| 226 | 258 |
| 227 RendererTask* new_task = nullptr; | 259 RendererTask* new_task = nullptr; |
| 228 // We don't create a task if there's one for this site_instance AND | 260 // We don't create a task if there's one for this site_instance AND |
| 229 // if this is not the main frame or we did record a main frame for the entry. | 261 // if this is not the main frame or we did record a main frame for the entry. |
| 230 if (!site_instance_exists || (is_main_frame && !site_instance_is_main)) { | 262 if (!site_instance_exists || (is_main_frame && !site_instance_is_main)) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 } | 431 } |
| 400 | 432 |
| 401 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { | 433 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { |
| 402 // This erase() will delete the WebContentsEntry, which is actually our | 434 // This erase() will delete the WebContentsEntry, which is actually our |
| 403 // caller, but it's expecting us to delete it. | 435 // caller, but it's expecting us to delete it. |
| 404 bool success = entries_map_.erase(web_contents) != 0; | 436 bool success = entries_map_.erase(web_contents) != 0; |
| 405 DCHECK(success); | 437 DCHECK(success); |
| 406 } | 438 } |
| 407 | 439 |
| 408 } // namespace task_manager | 440 } // namespace task_manager |
| OLD | NEW |