Chromium Code Reviews| 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/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/browser/render_process_host_utils.h" | |
| 13 #include "content/public/browser/render_widget_host.h" | 16 #include "content/public/browser/render_widget_host.h" |
| 14 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 16 | 19 |
| 17 using content::RenderFrameHost; | 20 using content::RenderFrameHost; |
| 18 using content::RenderWidgetHost; | 21 using content::RenderWidgetHost; |
| 19 using content::SiteInstance; | 22 using content::SiteInstance; |
| 20 using content::WebContents; | 23 using content::WebContents; |
| 21 | 24 |
| 22 namespace task_manager { | 25 namespace task_manager { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 42 void ClearAllTasks(bool notify_observer); | 45 void ClearAllTasks(bool notify_observer); |
| 43 | 46 |
| 44 // Returns the |RendererTask| that corresponds to the given | 47 // Returns the |RendererTask| that corresponds to the given |
| 45 // |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 |
| 46 // entry. | 49 // entry. |
| 47 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; | 50 RendererTask* GetTaskForFrame(RenderFrameHost* render_frame_host) const; |
| 48 | 51 |
| 49 // content::WebContentsObserver: | 52 // content::WebContentsObserver: |
| 50 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 53 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
| 51 void RenderFrameHostChanged(RenderFrameHost* old_host, | 54 void RenderFrameHostChanged(RenderFrameHost* old_host, |
| 52 RenderFrameHost* new_host) override; | 55 RenderFrameHost* new_host) override; |
|
Łukasz Anforowicz
2017/05/05 22:01:05
Initially I've thought that I can remove RenderFra
| |
| 56 void RenderFrameCreated(RenderFrameHost*) override; | |
| 53 void RenderViewReady() override; | 57 void RenderViewReady() override; |
| 54 void WebContentsDestroyed() override; | 58 void WebContentsDestroyed() override; |
| 55 void RenderProcessGone(base::TerminationStatus status) override; | 59 void RenderProcessGone(base::TerminationStatus status) override; |
| 56 void OnRendererUnresponsive(RenderWidgetHost* render_widget_host) override; | 60 void OnRendererUnresponsive(RenderWidgetHost* render_widget_host) override; |
| 57 void DidFinishNavigation( | 61 void DidFinishNavigation( |
| 58 content::NavigationHandle* navigation_handle) override; | 62 content::NavigationHandle* navigation_handle) override; |
| 59 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; | 63 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override; |
| 60 | 64 |
| 65 void RenderFrameReady(int process_routing_id, int frame_routing_id); | |
| 66 | |
| 61 private: | 67 private: |
| 62 // Defines a callback for WebContents::ForEachFrame() to create a | 68 // Defines a callback for WebContents::ForEachFrame() to create a |
| 63 // corresponding task for the given |render_frame_host| and notifying the | 69 // corresponding task for the given |render_frame_host| and notifying the |
| 64 // provider's observer of the new task. | 70 // provider's observer of the new task. |
| 65 void CreateTaskForFrame(RenderFrameHost* render_frame_host); | 71 void CreateTaskForFrame(RenderFrameHost* render_frame_host); |
| 66 | 72 |
| 67 // Clears the task that corresponds to the given |render_frame_host| and | 73 // Clears the task that corresponds to the given |render_frame_host| and |
| 68 // notifies the provider's observer of the tasks removal. | 74 // notifies the provider's observer of the tasks removal. |
| 69 void ClearTaskForFrame(RenderFrameHost* render_frame_host); | 75 void ClearTaskForFrame(RenderFrameHost* render_frame_host); |
| 70 | 76 |
| 71 // Calls |on_task| for each task managed by this WebContentsEntry. | 77 // Calls |on_task| for each task managed by this WebContentsEntry. |
| 72 void ForEachTask(const base::Callback<void(RendererTask*)>& on_task); | 78 void ForEachTask(const base::Callback<void(RendererTask*)>& on_task); |
| 73 | 79 |
| 74 // The provider that owns this entry. | 80 // The provider that owns this entry. |
| 75 WebContentsTaskProvider* provider_; | 81 WebContentsTaskProvider* provider_; |
| 76 | 82 |
| 77 // The RenderFrameHosts associated with this entry's WebContents that we're | 83 // The RenderFrameHosts associated with this entry's WebContents that we're |
| 78 // tracking mapped by their SiteInstances. | 84 // tracking mapped by their SiteInstances. |
| 79 using FramesList = std::vector<RenderFrameHost*>; | 85 using FramesList = std::vector<RenderFrameHost*>; |
| 80 std::map<SiteInstance*, FramesList> frames_by_site_instance_; | 86 std::map<SiteInstance*, FramesList> frames_by_site_instance_; |
| 81 | 87 |
| 82 // The RendererTasks that we create for the task manager, mapped by their | 88 // The RendererTasks that we create for the task manager, mapped by their |
| 83 // RenderFrameHosts. This owns the RenderTasks. | 89 // RenderFrameHosts. This owns the RenderTasks. |
| 84 std::map<RenderFrameHost*, RendererTask*> tasks_by_frames_; | 90 std::map<RenderFrameHost*, RendererTask*> tasks_by_frames_; |
| 85 | 91 |
| 86 // States whether we did record a main frame for this entry. | 92 // States whether we did record a main frame for this entry. |
| 87 SiteInstance* main_frame_site_instance_; | 93 SiteInstance* main_frame_site_instance_; |
| 88 | 94 |
| 95 base::WeakPtrFactory<WebContentsEntry> weak_factory_; | |
| 96 | |
| 89 DISALLOW_COPY_AND_ASSIGN(WebContentsEntry); | 97 DISALLOW_COPY_AND_ASSIGN(WebContentsEntry); |
| 90 }; | 98 }; |
| 91 | 99 |
| 92 //////////////////////////////////////////////////////////////////////////////// | 100 //////////////////////////////////////////////////////////////////////////////// |
| 93 | 101 |
| 94 WebContentsEntry::WebContentsEntry(content::WebContents* web_contents, | 102 WebContentsEntry::WebContentsEntry(content::WebContents* web_contents, |
| 95 WebContentsTaskProvider* provider) | 103 WebContentsTaskProvider* provider) |
| 96 : WebContentsObserver(web_contents), | 104 : WebContentsObserver(web_contents), |
| 97 provider_(provider), | 105 provider_(provider), |
| 98 main_frame_site_instance_(nullptr) { | 106 main_frame_site_instance_(nullptr), |
| 99 } | 107 weak_factory_(this) {} |
| 100 | 108 |
| 101 WebContentsEntry::~WebContentsEntry() { | 109 WebContentsEntry::~WebContentsEntry() { |
| 102 ClearAllTasks(false); | 110 ClearAllTasks(false); |
| 103 } | 111 } |
| 104 | 112 |
| 105 void WebContentsEntry::CreateAllTasks() { | 113 void WebContentsEntry::CreateAllTasks() { |
| 106 DCHECK(web_contents()->GetMainFrame()); | 114 DCHECK(web_contents()->GetMainFrame()); |
| 107 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, | 115 web_contents()->ForEachFrame(base::Bind(&WebContentsEntry::CreateTaskForFrame, |
| 108 base::Unretained(this))); | 116 base::Unretained(this))); |
| 109 } | 117 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 138 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { | 146 void WebContentsEntry::RenderFrameDeleted(RenderFrameHost* render_frame_host) { |
| 139 ClearTaskForFrame(render_frame_host); | 147 ClearTaskForFrame(render_frame_host); |
| 140 } | 148 } |
| 141 | 149 |
| 142 void WebContentsEntry::RenderFrameHostChanged(RenderFrameHost* old_host, | 150 void WebContentsEntry::RenderFrameHostChanged(RenderFrameHost* old_host, |
| 143 RenderFrameHost* new_host) { | 151 RenderFrameHost* new_host) { |
| 144 ClearTaskForFrame(old_host); | 152 ClearTaskForFrame(old_host); |
| 145 CreateTaskForFrame(new_host); | 153 CreateTaskForFrame(new_host); |
| 146 } | 154 } |
| 147 | 155 |
| 156 void WebContentsEntry::RenderFrameCreated(RenderFrameHost* render_frame_host) { | |
| 157 // Postpone processing |render_frame_host| until its process has a PID. | |
| 158 PostTaskWhenRenderProcessHostIsReady( | |
| 159 render_frame_host->GetProcess(), | |
| 160 base::Bind(&WebContentsEntry::RenderFrameReady, | |
| 161 weak_factory_.GetWeakPtr(), | |
| 162 render_frame_host->GetProcess()->GetID(), | |
| 163 render_frame_host->GetRoutingID())); | |
| 164 } | |
| 165 | |
| 166 void WebContentsEntry::RenderFrameReady(int render_process_id, | |
| 167 int render_frame_id) { | |
| 168 content::RenderFrameHost* render_frame_host = | |
| 169 content::RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| 170 if (render_frame_host) | |
| 171 CreateTaskForFrame(render_frame_host); | |
| 172 } | |
| 173 | |
| 148 void WebContentsEntry::RenderViewReady() { | 174 void WebContentsEntry::RenderViewReady() { |
| 149 ClearAllTasks(true); | 175 ClearAllTasks(true); |
| 150 CreateAllTasks(); | 176 CreateAllTasks(); |
| 151 } | 177 } |
| 152 | 178 |
| 153 void WebContentsEntry::WebContentsDestroyed() { | 179 void WebContentsEntry::WebContentsDestroyed() { |
| 154 ClearAllTasks(true); | 180 ClearAllTasks(true); |
| 155 provider_->DeleteEntry(web_contents()); | 181 provider_->DeleteEntry(web_contents()); |
| 156 } | 182 } |
| 157 | 183 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 | 222 |
| 197 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry, | 223 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry, |
| 198 bool explicit_set) { | 224 bool explicit_set) { |
| 199 ForEachTask(base::Bind([](RendererTask* task) { | 225 ForEachTask(base::Bind([](RendererTask* task) { |
| 200 task->UpdateTitle(); | 226 task->UpdateTitle(); |
| 201 task->UpdateFavicon(); | 227 task->UpdateFavicon(); |
| 202 })); | 228 })); |
| 203 } | 229 } |
| 204 | 230 |
| 205 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { | 231 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { |
| 206 DCHECK(!tasks_by_frames_.count(render_frame_host)); | 232 if (base::ContainsKey(tasks_by_frames_, render_frame_host)) |
|
Łukasz Anforowicz
2017/05/05 22:01:05
This DCHECK will be violated, because (as I descri
| |
| 233 ClearTaskForFrame(render_frame_host); | |
| 207 | 234 |
| 208 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); | 235 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); |
| 209 if (!site_instance->GetProcess()->HasConnection()) | 236 if (!site_instance->GetProcess()->HasConnection()) |
| 210 return; | 237 return; |
| 211 | 238 |
| 212 bool site_instance_exists = | 239 bool site_instance_exists = |
| 213 frames_by_site_instance_.count(site_instance) != 0; | 240 frames_by_site_instance_.count(site_instance) != 0; |
| 214 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); | 241 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); |
| 215 bool site_instance_is_main = (site_instance == main_frame_site_instance_); | 242 bool site_instance_is_main = (site_instance == main_frame_site_instance_); |
| 216 | 243 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 } | 416 } |
| 390 | 417 |
| 391 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { | 418 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { |
| 392 // This erase() will delete the WebContentsEntry, which is actually our | 419 // This erase() will delete the WebContentsEntry, which is actually our |
| 393 // caller, but it's expecting us to delete it. | 420 // caller, but it's expecting us to delete it. |
| 394 bool success = entries_map_.erase(web_contents) != 0; | 421 bool success = entries_map_.erase(web_contents) != 0; |
| 395 DCHECK(success); | 422 DCHECK(success); |
| 396 } | 423 } |
| 397 | 424 |
| 398 } // namespace task_manager | 425 } // namespace task_manager |
| OLD | NEW |