Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(407)

Side by Side Diff: chrome/browser/task_manager/providers/web_contents/web_contents_task_provider.cc

Issue 2857263003: Task Manager should listen to WebContentsObserver::RenderFrameCreated. (Closed)
Patch Set: RenderFrameHost::IsCurrent Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 hosts that will be processed anyway by RenderFrameHostChanged.
ncarter (slow) 2017/05/16 20:50:57 // Skip pending/speculative hosts. We'll create ta
Łukasz Anforowicz 2017/05/16 21:33:44 Done.
151 CreateAllTasks(); 157 if (!render_frame_host->IsCurrent())
158 return;
159
160 // Task manager will have no separate entry for |render_frame_host| if it has
161 // the same site instance as its parent - quit early in this case.
162 if (render_frame_host->GetParent() &&
163 render_frame_host->GetParent()->GetSiteInstance() ==
164 render_frame_host->GetSiteInstance())
165 return;
166
167 // Postpone processing |render_frame_host| until its process has a PID.
168 render_frame_host->GetProcess()->PostTaskWhenProcessIsReady(base::Bind(
169 &WebContentsEntry::RenderFrameReady, weak_factory_.GetWeakPtr(),
170 render_frame_host->GetProcess()->GetID(),
171 render_frame_host->GetRoutingID()));
172 }
173
174 void WebContentsEntry::RenderFrameReady(int render_process_id,
175 int render_frame_id) {
176 content::RenderFrameHost* render_frame_host =
177 content::RenderFrameHost::FromID(render_process_id, render_frame_id);
178 if (render_frame_host)
179 CreateTaskForFrame(render_frame_host);
152 } 180 }
153 181
154 void WebContentsEntry::WebContentsDestroyed() { 182 void WebContentsEntry::WebContentsDestroyed() {
155 ClearAllTasks(true); 183 ClearAllTasks(true);
156 provider_->DeleteEntry(web_contents()); 184 provider_->DeleteEntry(web_contents());
157 } 185 }
158 186
159 void WebContentsEntry::RenderProcessGone(base::TerminationStatus status) { 187 void WebContentsEntry::RenderProcessGone(base::TerminationStatus status) {
160 ClearAllTasks(true); 188 ClearAllTasks(true);
161 } 189 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 234
207 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry, 235 void WebContentsEntry::TitleWasSet(content::NavigationEntry* entry,
208 bool explicit_set) { 236 bool explicit_set) {
209 ForEachTask(base::Bind([](RendererTask* task) { 237 ForEachTask(base::Bind([](RendererTask* task) {
210 task->UpdateTitle(); 238 task->UpdateTitle();
211 task->UpdateFavicon(); 239 task->UpdateFavicon();
212 })); 240 }));
213 } 241 }
214 242
215 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) { 243 void WebContentsEntry::CreateTaskForFrame(RenderFrameHost* render_frame_host) {
244 DCHECK(render_frame_host);
216 DCHECK(!tasks_by_frames_.count(render_frame_host)); 245 DCHECK(!tasks_by_frames_.count(render_frame_host));
217 246
218 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance(); 247 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
219 if (!site_instance->GetProcess()->HasConnection()) 248 if (!site_instance->GetProcess()->HasConnection())
220 return; 249 return;
250 if (!render_frame_host->IsRenderFrameLive())
251 return;
221 252
222 bool site_instance_exists = 253 bool site_instance_exists =
223 frames_by_site_instance_.count(site_instance) != 0; 254 frames_by_site_instance_.count(site_instance) != 0;
224 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame()); 255 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame());
225 bool site_instance_is_main = (site_instance == main_frame_site_instance_); 256 bool site_instance_is_main = (site_instance == main_frame_site_instance_);
226 257
227 RendererTask* new_task = nullptr; 258 RendererTask* new_task = nullptr;
228 // We don't create a task if there's one for this site_instance AND 259 // 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. 260 // 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)) { 261 if (!site_instance_exists || (is_main_frame && !site_instance_is_main)) {
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 } 430 }
400 431
401 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) { 432 void WebContentsTaskProvider::DeleteEntry(content::WebContents* web_contents) {
402 // This erase() will delete the WebContentsEntry, which is actually our 433 // This erase() will delete the WebContentsEntry, which is actually our
403 // caller, but it's expecting us to delete it. 434 // caller, but it's expecting us to delete it.
404 bool success = entries_map_.erase(web_contents) != 0; 435 bool success = entries_map_.erase(web_contents) != 0;
405 DCHECK(success); 436 DCHECK(success);
406 } 437 }
407 438
408 } // namespace task_manager 439 } // namespace task_manager
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/task_manager/task_manager_browsertest.cc » ('j') | content/public/browser/render_frame_host.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698