memory_internals_proxy.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 3// found in the LICENSE file. 4 5#include "chrome/browser/ui/webui/memory_internals/memory_internals_proxy.h" 6 7#include <string> 8#include <vector> 9 10#include "base/bind.h" 11#include "base/string16.h" 12#include "base/stringprintf.h" 13#include "base/strings/string_number_conversions.h" 14#include "base/utf_string_conversions.h" 15#include "base/values.h" 16#include "chrome/browser/defaults.h" 17#include "chrome/browser/memory_details.h" 18#include "chrome/browser/ui/webui/memory_internals/memory_internals_handler.h" 19#include "content/public/browser/url_data_source.h" 20#include "content/public/browser/web_ui.h" 21#include "grit/chromium_strings.h" 22#include "ui/base/l10n/l10n_util.h" 23#include "ui/webui/jstemplate_builder.h" 24#include "ui/webui/web_ui_util.h" 25 26using content::BrowserThread; 27 28namespace { 29 30class BrowserProcessDetails : public MemoryDetails { 31 public: 32 typedef base::Callback<void(const ProcessData&)> DataCallback; 33 explicit BrowserProcessDetails(const DataCallback& callback) 34 : callback_(callback) {} 35 virtual void OnDetailsAvailable() OVERRIDE { 36 const std::vector<ProcessData>& browser_processes = processes(); 37 callback_.Run(browser_processes[0]); 38 } 39 40 private: 41 virtual ~BrowserProcessDetails() {} 42 43 DataCallback callback_; 44 45 DISALLOW_COPY_AND_ASSIGN(BrowserProcessDetails); 46}; 47 48} // namespace 49 50MemoryInternalsProxy::MemoryInternalsProxy() {} 51 52void MemoryInternalsProxy::Attach(MemoryInternalsHandler* handler) { 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 54 handler_ = handler; 55} 56 57void MemoryInternalsProxy::Detach() { 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 59 handler_ = NULL; 60} 61 62void MemoryInternalsProxy::GetInfo(const base::ListValue* list) { 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 64 65 scoped_refptr<BrowserProcessDetails> browsers(new BrowserProcessDetails( 66 base::Bind(&MemoryInternalsProxy::OnDetailsAvailable, this))); 67 browsers->StartFetch(MemoryDetails::SKIP_USER_METRICS); 68} 69 70MemoryInternalsProxy::~MemoryInternalsProxy() {} 71 72void MemoryInternalsProxy::UpdateUIOnUIThread(const string16& update) { 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 74 75 // Don't forward updates to a destructed UI. 76 if (handler_) 77 handler_->OnUpdate(update); 78} 79 80void MemoryInternalsProxy::OnDetailsAvailable(const ProcessData& browser) { 81 base::DictionaryValue details; 82 83 base::ListValue* processes = new ListValue(); 84 details.Set("processes", processes); 85 for (ProcessMemoryInformationList::const_iterator 86 iter = browser.processes.begin(); 87 iter != browser.processes.end(); ++iter) { 88 base::DictionaryValue* info = new DictionaryValue(); 89 processes->Append(info); 90 91 // Information from MemoryDetails. 92 info->SetInteger("pid", iter->pid); 93 info->SetString("type", 94 ProcessMemoryInformation::GetFullTypeNameInEnglish( 95 iter->process_type, iter->renderer_type)); 96 info->SetInteger("memory_private", 97 iter->working_set.priv + iter->committed.priv); 98 base::ListValue* titles = new ListValue(); 99 info->Set("titles", titles); 100 for (size_t i = 0; i < iter->titles.size(); ++i) 101 titles->AppendString(iter->titles[i]); 102 } 103 104 CallJavaScriptFunctionOnUIThread("g_main_view.onSetSnapshot", &details); 105} 106 107void MemoryInternalsProxy::CallJavaScriptFunctionOnUIThread( 108 const std::string& function, base::Value* args) { 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 110 111 std::vector<const base::Value*> args_vector; 112 args_vector.push_back(args); 113 string16 update = content::WebUI::GetJavascriptCall(function, args_vector); 114 UpdateUIOnUIThread(update); 115} 116