OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/webui/memory_internals/memory_internals_proxy.h" | 5 #include "chrome/browser/ui/webui/memory_internals/memory_internals_proxy.h" |
6 | 6 |
| 7 #include <map> |
7 #include <set> | 8 #include <set> |
8 #include <string> | 9 #include <string> |
| 10 #include <utility> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
12 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
13 #include "base/sys_info.h" | 17 #include "base/sys_info.h" |
14 #include "base/values.h" | 18 #include "base/values.h" |
15 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/memory_details.h" | 21 #include "chrome/browser/memory_details.h" |
18 #include "chrome/browser/prerender/prerender_manager.h" | 22 #include "chrome/browser/prerender/prerender_manager.h" |
19 #include "chrome/browser/prerender/prerender_manager_factory.h" | 23 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 24 #include "chrome/browser/process_resource_usage.h" |
20 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/profiles/profile_manager.h" | 26 #include "chrome/browser/profiles/profile_manager.h" |
22 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 27 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
23 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 28 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
24 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 29 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
25 #include "chrome/browser/ui/browser.h" | 30 #include "chrome/browser/ui/browser.h" |
26 #include "chrome/browser/ui/browser_iterator.h" | 31 #include "chrome/browser/ui/browser_iterator.h" |
27 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 32 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
28 #include "chrome/browser/ui/webui/memory_internals/memory_internals_handler.h" | 33 #include "chrome/browser/ui/webui/memory_internals/memory_internals_handler.h" |
29 #include "chrome/common/render_messages.h" | 34 #include "chrome/common/render_messages.h" |
30 #include "content/public/browser/navigation_controller.h" | 35 #include "content/public/browser/navigation_controller.h" |
31 #include "content/public/browser/navigation_entry.h" | 36 #include "content/public/browser/navigation_entry.h" |
32 #include "content/public/browser/notification_observer.h" | |
33 #include "content/public/browser/notification_registrar.h" | |
34 #include "content/public/browser/notification_service.h" | |
35 #include "content/public/browser/render_process_host.h" | 37 #include "content/public/browser/render_process_host.h" |
36 #include "content/public/browser/render_view_host.h" | 38 #include "content/public/browser/render_view_host.h" |
37 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
38 #include "content/public/browser/web_ui.h" | 40 #include "content/public/browser/web_ui.h" |
| 41 #include "content/public/common/service_registry.h" |
39 | 42 |
40 #if defined(ENABLE_PRINT_PREVIEW) | 43 #if defined(ENABLE_PRINT_PREVIEW) |
41 #include "chrome/browser/printing/background_printing_manager.h" | 44 #include "chrome/browser/printing/background_printing_manager.h" |
42 #endif | 45 #endif |
43 | 46 |
44 using content::BrowserThread; | 47 using content::BrowserThread; |
45 | 48 |
46 class Profile; | 49 class Profile; |
47 | 50 |
48 namespace { | 51 namespace { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 printing::BackgroundPrintingManager* printing_manager = | 114 printing::BackgroundPrintingManager* printing_manager = |
112 g_browser_process->background_printing_manager(); | 115 g_browser_process->background_printing_manager(); |
113 std::set<content::WebContents*> printing_contents = | 116 std::set<content::WebContents*> printing_contents = |
114 printing_manager->CurrentContentSet(); | 117 printing_manager->CurrentContentSet(); |
115 web_contents->insert(printing_contents.begin(), printing_contents.end()); | 118 web_contents->insert(printing_contents.begin(), printing_contents.end()); |
116 #endif | 119 #endif |
117 } | 120 } |
118 | 121 |
119 } // namespace | 122 } // namespace |
120 | 123 |
121 class RendererDetails : public content::NotificationObserver { | 124 class RendererDetails { |
122 public: | 125 public: |
123 typedef base::Callback<void(const base::ProcessId pid, | 126 typedef base::Callback<void(const base::ProcessId pid, |
124 const size_t v8_allocated, | 127 const size_t v8_allocated, |
125 const size_t v8_used)> V8DataCallback; | 128 const size_t v8_used)> V8DataCallback; |
126 | 129 |
127 explicit RendererDetails(const V8DataCallback& callback) | 130 explicit RendererDetails(const V8DataCallback& callback) |
128 : callback_(callback) { | 131 : callback_(callback), weak_factory_(this) {} |
129 registrar_.Add(this, chrome::NOTIFICATION_RENDERER_V8_HEAP_STATS_COMPUTED, | 132 ~RendererDetails() {} |
130 content::NotificationService::AllSources()); | |
131 } | |
132 ~RendererDetails() override {} | |
133 | 133 |
134 void Request() { | 134 void Request() { |
135 for (std::set<content::WebContents*>::iterator iter = web_contents_.begin(); | 135 for (std::set<content::WebContents*>::iterator iter = web_contents_.begin(); |
136 iter != web_contents_.end(); ++iter) | 136 iter != web_contents_.end(); ++iter) { |
137 (*iter)->GetRenderViewHost()->Send(new ChromeViewMsg_GetV8HeapStats); | 137 auto rph = (*iter)->GetRenderViewHost()->GetProcess(); |
| 138 auto resource_usage = resource_usage_reporters_[(*iter)]; |
| 139 DCHECK(resource_usage.get()); |
| 140 resource_usage->Refresh(base::Bind(&RendererDetails::OnRefreshDone, |
| 141 weak_factory_.GetWeakPtr(), *iter, |
| 142 base::GetProcId(rph->GetHandle()))); |
| 143 } |
138 } | 144 } |
139 | 145 |
140 void AddWebContents(content::WebContents* content) { | 146 void AddWebContents(content::WebContents* content) { |
141 web_contents_.insert(content); | 147 web_contents_.insert(content); |
| 148 |
| 149 auto rph = content->GetRenderViewHost()->GetProcess(); |
| 150 content::ServiceRegistry* service_registry = rph->GetServiceRegistry(); |
| 151 ResourceUsageReporterPtr service; |
| 152 if (service_registry) |
| 153 service_registry->ConnectToRemoteService(&service); |
| 154 resource_usage_reporters_.insert(std::make_pair( |
| 155 content, make_linked_ptr(new ProcessResourceUsage(service.Pass())))); |
| 156 DCHECK_EQ(web_contents_.size(), resource_usage_reporters_.size()); |
142 } | 157 } |
143 | 158 |
144 void Clear() { | 159 void Clear() { |
145 web_contents_.clear(); | 160 web_contents_.clear(); |
| 161 resource_usage_reporters_.clear(); |
| 162 weak_factory_.InvalidateWeakPtrs(); |
146 } | 163 } |
147 | 164 |
148 void RemoveWebContents(base::ProcessId) { | 165 void RemoveWebContents(base::ProcessId) { |
| 166 // This function should only be called once for each WebContents, so this |
| 167 // should be non-empty every time it's called. |
| 168 DCHECK(!web_contents_.empty()); |
| 169 |
149 // We don't have to detect which content is the caller of this method. | 170 // We don't have to detect which content is the caller of this method. |
150 if (!web_contents_.empty()) | 171 if (!web_contents_.empty()) |
151 web_contents_.erase(web_contents_.begin()); | 172 web_contents_.erase(web_contents_.begin()); |
152 } | 173 } |
153 | 174 |
154 int IsClean() { | 175 int IsClean() { |
155 return web_contents_.empty(); | 176 return web_contents_.empty(); |
156 } | 177 } |
157 | 178 |
158 private: | 179 private: |
159 // NotificationObserver: | 180 void OnRefreshDone(content::WebContents* content, const base::ProcessId pid) { |
160 void Observe(int type, | 181 auto iter = resource_usage_reporters_.find(content); |
161 const content::NotificationSource& source, | 182 DCHECK(iter != resource_usage_reporters_.end()); |
162 const content::NotificationDetails& details) override { | 183 linked_ptr<ProcessResourceUsage> usage = iter->second; |
163 const base::ProcessId* pid = | 184 callback_.Run(pid, usage->GetV8MemoryAllocated(), usage->GetV8MemoryUsed()); |
164 content::Source<const base::ProcessId>(source).ptr(); | |
165 const ChromeRenderMessageFilter::V8HeapStatsDetails* v8_heap = | |
166 content::Details<const ChromeRenderMessageFilter::V8HeapStatsDetails>( | |
167 details).ptr(); | |
168 callback_.Run(*pid, | |
169 v8_heap->v8_memory_allocated(), | |
170 v8_heap->v8_memory_used()); | |
171 } | 185 } |
172 | 186 |
173 V8DataCallback callback_; | 187 V8DataCallback callback_; |
174 content::NotificationRegistrar registrar_; | |
175 std::set<content::WebContents*> web_contents_; // This class does not own | 188 std::set<content::WebContents*> web_contents_; // This class does not own |
| 189 std::map<content::WebContents*, linked_ptr<ProcessResourceUsage>> |
| 190 resource_usage_reporters_; |
| 191 |
| 192 base::WeakPtrFactory<RendererDetails> weak_factory_; |
176 | 193 |
177 DISALLOW_COPY_AND_ASSIGN(RendererDetails); | 194 DISALLOW_COPY_AND_ASSIGN(RendererDetails); |
178 }; | 195 }; |
179 | 196 |
180 MemoryInternalsProxy::MemoryInternalsProxy() | 197 MemoryInternalsProxy::MemoryInternalsProxy() |
181 : information_(new base::DictionaryValue()), | 198 : information_(new base::DictionaryValue()), |
182 renderer_details_(new RendererDetails( | 199 renderer_details_(new RendererDetails( |
183 base::Bind(&MemoryInternalsProxy::OnRendererAvailable, this))) {} | 200 base::Bind(&MemoryInternalsProxy::OnRendererAvailable, this))) {} |
184 | 201 |
185 void MemoryInternalsProxy::Attach(MemoryInternalsHandler* handler) { | 202 void MemoryInternalsProxy::Attach(MemoryInternalsHandler* handler) { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 const std::string& function, const base::Value& args) { | 361 const std::string& function, const base::Value& args) { |
345 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 362 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
346 | 363 |
347 std::vector<const base::Value*> args_vector(1, &args); | 364 std::vector<const base::Value*> args_vector(1, &args); |
348 base::string16 update = | 365 base::string16 update = |
349 content::WebUI::GetJavascriptCall(function, args_vector); | 366 content::WebUI::GetJavascriptCall(function, args_vector); |
350 // Don't forward updates to a destructed UI. | 367 // Don't forward updates to a destructed UI. |
351 if (handler_) | 368 if (handler_) |
352 handler_->OnUpdate(update); | 369 handler_->OnUpdate(update); |
353 } | 370 } |
OLD | NEW |