Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / task_manager / web_contents_resource_provider.cc
blobdc720db0a0d529682eec71886aa805b44bf42b96
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/task_manager/web_contents_resource_provider.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/prerender/prerender_manager.h"
12 #include "chrome/browser/prerender/prerender_manager_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/task_manager/renderer_resource.h"
16 #include "chrome/browser/task_manager/task_manager.h"
17 #include "chrome/browser/task_manager/task_manager_util.h"
18 #include "chrome/browser/task_manager/web_contents_information.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/render_widget_host_iterator.h"
24 #include "content/public/browser/site_instance.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_observer.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/gfx/image/image_skia.h"
30 using content::RenderFrameHost;
31 using content::RenderProcessHost;
32 using content::RenderViewHost;
33 using content::SiteInstance;
34 using content::WebContents;
36 namespace task_manager {
38 // A resource for a process hosting out-of-process iframes.
39 class SubframeResource : public RendererResource {
40 public:
41 explicit SubframeResource(WebContents* web_contents,
42 SiteInstance* site_instance,
43 RenderFrameHost* example_rfh);
44 ~SubframeResource() override {}
46 // Resource methods:
47 Type GetType() const override;
48 base::string16 GetTitle() const override;
49 gfx::ImageSkia GetIcon() const override;
50 WebContents* GetWebContents() const override;
52 private:
53 WebContents* web_contents_;
54 base::string16 title_;
55 DISALLOW_COPY_AND_ASSIGN(SubframeResource);
58 SubframeResource::SubframeResource(WebContents* web_contents,
59 SiteInstance* subframe_site_instance,
60 RenderFrameHost* example_rfh)
61 : RendererResource(subframe_site_instance->GetProcess()->GetHandle(),
62 example_rfh->GetRenderViewHost()),
63 web_contents_(web_contents) {
64 int message_id = subframe_site_instance->GetBrowserContext()->IsOffTheRecord()
65 ? IDS_TASK_MANAGER_SUBFRAME_INCOGNITO_PREFIX
66 : IDS_TASK_MANAGER_SUBFRAME_PREFIX;
67 title_ = l10n_util::GetStringFUTF16(
68 message_id,
69 base::UTF8ToUTF16(subframe_site_instance->GetSiteURL().spec()));
72 Resource::Type SubframeResource::GetType() const {
73 return RENDERER;
76 base::string16 SubframeResource::GetTitle() const {
77 return title_;
80 gfx::ImageSkia SubframeResource::GetIcon() const {
81 return gfx::ImageSkia();
84 WebContents* SubframeResource::GetWebContents() const {
85 return web_contents_;
88 // Tracks changes to one WebContents, and manages task manager resources for
89 // that WebContents, on behalf of a WebContentsResourceProvider.
90 class TaskManagerWebContentsEntry : public content::WebContentsObserver,
91 public content::RenderProcessHostObserver {
92 public:
93 typedef std::multimap<SiteInstance*, RendererResource*> ResourceMap;
94 typedef std::pair<ResourceMap::iterator, ResourceMap::iterator> ResourceRange;
96 TaskManagerWebContentsEntry(WebContents* web_contents,
97 WebContentsResourceProvider* provider)
98 : content::WebContentsObserver(web_contents),
99 provider_(provider),
100 main_frame_site_instance_(NULL) {}
102 ~TaskManagerWebContentsEntry() override { ClearAllResources(false); }
104 // content::WebContentsObserver implementation.
105 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override {
106 ClearResourceForFrame(render_frame_host);
109 void RenderFrameHostChanged(RenderFrameHost* old_host,
110 RenderFrameHost* new_host) override {
111 if (old_host)
112 ClearResourceForFrame(old_host);
113 CreateResourceForFrame(new_host);
116 void RenderViewReady() override {
117 ClearAllResources(true);
118 CreateAllResources();
121 void WebContentsDestroyed() override {
122 ClearAllResources(true);
123 provider_->DeleteEntry(web_contents(), this); // Deletes |this|.
126 // content::RenderProcessHostObserver implementation.
127 void RenderProcessExited(RenderProcessHost* process_host,
128 base::TerminationStatus status,
129 int exit_code) override {
130 ClearResourcesForProcess(process_host);
133 void RenderProcessHostDestroyed(RenderProcessHost* process_host) override {
134 tracked_process_hosts_.erase(process_host);
137 // Called by WebContentsResourceProvider.
138 RendererResource* GetResourceForSiteInstance(SiteInstance* site_instance) {
139 ResourceMap::iterator i = resources_by_site_instance_.find(site_instance);
140 if (i == resources_by_site_instance_.end())
141 return NULL;
142 return i->second;
145 void CreateAllResources() {
146 // We'll show one row per SiteInstance in the task manager.
147 DCHECK(web_contents()->GetMainFrame() != NULL);
148 web_contents()->ForEachFrame(
149 base::Bind(&TaskManagerWebContentsEntry::CreateResourceForFrame,
150 base::Unretained(this)));
153 void ClearAllResources(bool update_task_manager) {
154 RendererResource* last_resource = NULL;
155 for (auto& x : resources_by_site_instance_) {
156 RendererResource* resource = x.second;
157 if (resource == last_resource)
158 continue; // Skip multiset duplicates.
159 if (update_task_manager)
160 task_manager()->RemoveResource(resource);
161 delete resource;
162 last_resource = resource;
164 resources_by_site_instance_.clear();
166 RenderProcessHost* last_process_host = NULL;
167 for (RenderProcessHost* process_host : tracked_process_hosts_) {
168 if (last_process_host == process_host)
169 continue; // Skip multiset duplicates.
170 process_host->RemoveObserver(this);
171 last_process_host = process_host;
173 tracked_process_hosts_.clear();
174 tracked_frame_hosts_.clear();
177 void ClearResourceForFrame(RenderFrameHost* render_frame_host) {
178 SiteInstance* site_instance = render_frame_host->GetSiteInstance();
179 std::set<RenderFrameHost*>::iterator frame_set_iterator =
180 tracked_frame_hosts_.find(render_frame_host);
181 if (frame_set_iterator == tracked_frame_hosts_.end()) {
182 // We weren't tracking this RenderFrameHost.
183 return;
185 tracked_frame_hosts_.erase(frame_set_iterator);
186 ResourceRange resource_range =
187 resources_by_site_instance_.equal_range(site_instance);
188 if (resource_range.first == resource_range.second) {
189 NOTREACHED();
190 return;
192 RendererResource* resource = resource_range.first->second;
193 resources_by_site_instance_.erase(resource_range.first++);
194 if (resource_range.first == resource_range.second) {
195 // The removed entry was the sole remaining reference to that resource, so
196 // actually destroy it.
197 task_manager()->RemoveResource(resource);
198 delete resource;
199 DecrementProcessWatch(site_instance->GetProcess());
200 if (site_instance == main_frame_site_instance_) {
201 main_frame_site_instance_ = NULL;
206 void ClearResourcesForProcess(RenderProcessHost* crashed_process) {
207 std::vector<RenderFrameHost*> frame_hosts_to_delete;
208 for (RenderFrameHost* frame_host : tracked_frame_hosts_) {
209 if (frame_host->GetProcess() == crashed_process) {
210 frame_hosts_to_delete.push_back(frame_host);
213 for (RenderFrameHost* frame_host : frame_hosts_to_delete) {
214 ClearResourceForFrame(frame_host);
218 void CreateResourceForFrame(RenderFrameHost* render_frame_host) {
219 SiteInstance* site_instance = render_frame_host->GetSiteInstance();
221 DCHECK_EQ(0u, tracked_frame_hosts_.count(render_frame_host));
223 if (!site_instance->GetProcess()->HasConnection())
224 return;
226 tracked_frame_hosts_.insert(render_frame_host);
228 ResourceRange existing_resource_range =
229 resources_by_site_instance_.equal_range(site_instance);
230 bool existing_resource =
231 (existing_resource_range.first != existing_resource_range.second);
232 bool is_main_frame = (render_frame_host == web_contents()->GetMainFrame());
233 bool site_instance_is_main = (site_instance == main_frame_site_instance_);
234 scoped_ptr<RendererResource> new_resource;
235 if (!existing_resource || (is_main_frame && !site_instance_is_main)) {
236 if (is_main_frame) {
237 new_resource = info()->MakeResource(web_contents());
238 main_frame_site_instance_ = site_instance;
239 } else {
240 new_resource.reset(new SubframeResource(
241 web_contents(), site_instance, render_frame_host));
245 if (existing_resource) {
246 RendererResource* old_resource = existing_resource_range.first->second;
247 if (!new_resource) {
248 resources_by_site_instance_.insert(
249 std::make_pair(site_instance, old_resource));
250 } else {
251 for (ResourceMap::iterator it = existing_resource_range.first;
252 it != existing_resource_range.second;
253 ++it) {
254 it->second = new_resource.get();
256 task_manager()->RemoveResource(old_resource);
257 delete old_resource;
258 DecrementProcessWatch(site_instance->GetProcess());
262 if (new_resource) {
263 task_manager()->AddResource(new_resource.get());
264 resources_by_site_instance_.insert(
265 std::make_pair(site_instance, new_resource.release()));
266 IncrementProcessWatch(site_instance->GetProcess());
270 // Add ourself as an observer of |process|, if we aren't already. Must be
271 // balanced by a call to DecrementProcessWatch().
272 // TODO(nick): Move away from RenderProcessHostObserver once
273 // WebContentsObserver supports per-frame process death notices.
274 void IncrementProcessWatch(RenderProcessHost* process) {
275 auto range = tracked_process_hosts_.equal_range(process);
276 if (range.first == range.second) {
277 process->AddObserver(this);
279 tracked_process_hosts_.insert(range.first, process);
282 void DecrementProcessWatch(RenderProcessHost* process) {
283 auto range = tracked_process_hosts_.equal_range(process);
284 if (range.first == range.second) {
285 NOTREACHED();
286 return;
289 auto element = range.first++;
290 if (range.first == range.second) {
291 process->RemoveObserver(this);
293 tracked_process_hosts_.erase(element, range.first);
296 private:
297 TaskManager* task_manager() { return provider_->task_manager(); }
299 WebContentsInformation* info() { return provider_->info(); }
301 WebContentsResourceProvider* const provider_;
303 // Every RenderFrameHost that we're watching.
304 std::set<RenderFrameHost*> tracked_frame_hosts_;
306 // The set of processes we're currently observing. There is one entry here per
307 // RendererResource we create. A multimap because we may request observation
308 // more than once, say if two resources happen to share a process.
309 std::multiset<RenderProcessHost*> tracked_process_hosts_;
311 // Maps SiteInstances to the RendererResources. A multimap, this contains one
312 // entry per tracked RenderFrameHost, so we can tell when we're done reusing.
313 ResourceMap resources_by_site_instance_;
315 // The site instance of the main frame.
316 SiteInstance* main_frame_site_instance_;
319 ////////////////////////////////////////////////////////////////////////////////
320 // WebContentsResourceProvider class
321 ////////////////////////////////////////////////////////////////////////////////
323 WebContentsResourceProvider::WebContentsResourceProvider(
324 TaskManager* task_manager,
325 scoped_ptr<WebContentsInformation> info)
326 : task_manager_(task_manager), info_(info.Pass()) {
329 WebContentsResourceProvider::~WebContentsResourceProvider() {}
331 RendererResource* WebContentsResourceProvider::GetResource(int origin_pid,
332 int child_id,
333 int route_id) {
334 RenderFrameHost* rfh = RenderFrameHost::FromID(child_id, route_id);
335 WebContents* web_contents = WebContents::FromRenderFrameHost(rfh);
337 // If an origin PID was specified then the request originated in a plugin
338 // working on the WebContents's behalf, so ignore it.
339 if (origin_pid)
340 return NULL;
342 EntryMap::const_iterator web_contents_it = entries_.find(web_contents);
344 if (web_contents_it == entries_.end()) {
345 // Can happen if the tab was closed while a network request was being
346 // performed.
347 return NULL;
350 return web_contents_it->second->GetResourceForSiteInstance(
351 rfh->GetSiteInstance());
354 void WebContentsResourceProvider::StartUpdating() {
355 WebContentsInformation::NewWebContentsCallback new_web_contents_callback =
356 base::Bind(&WebContentsResourceProvider::OnWebContentsCreated, this);
357 info_->GetAll(new_web_contents_callback);
358 info_->StartObservingCreation(new_web_contents_callback);
361 void WebContentsResourceProvider::StopUpdating() {
362 info_->StopObservingCreation();
364 // Delete all entries; this dissassociates them from the WebContents too.
365 STLDeleteValues(&entries_);
368 void WebContentsResourceProvider::OnWebContentsCreated(
369 WebContents* web_contents) {
370 // Don't add dead tabs or tabs that haven't yet connected.
371 if (!web_contents->GetRenderProcessHost()->GetHandle() ||
372 !web_contents->WillNotifyDisconnection()) {
373 return;
376 DCHECK(info_->CheckOwnership(web_contents));
377 if (entries_.count(web_contents)) {
378 // The case may happen that we have added a WebContents as part of the
379 // iteration performed during StartUpdating() call but the notification that
380 // it has connected was not fired yet. So when the notification happens, we
381 // are already observing this WebContents and just ignore it.
382 return;
384 scoped_ptr<TaskManagerWebContentsEntry> entry(
385 new TaskManagerWebContentsEntry(web_contents, this));
386 entry->CreateAllResources();
387 entries_[web_contents] = entry.release();
390 void WebContentsResourceProvider::DeleteEntry(
391 WebContents* web_contents,
392 TaskManagerWebContentsEntry* entry) {
393 if (!entries_.erase(web_contents)) {
394 NOTREACHED();
395 return;
397 delete entry; // Typically, this is our caller. Deletion is okay.
400 } // namespace task_manager