Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / system / monitor / tray_monitor.cc
1 // Copyright (c) 2012 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 "ash/system/monitor/tray_monitor.h"
6
7 #include "ash/gpu_support.h"
8 #include "ash/shell.h"
9 #include "ash/system/tray/tray_item_view.h"
10 #include "base/process/memory.h"
11 #include "base/process/process_metrics.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/base/text/bytes_formatting.h"
15 #include "ui/views/border.h"
16 #include "ui/views/controls/label.h"
17
18 namespace {
19 const int kRefreshTimeoutMs = 1000;
20 }
21
22 namespace ash {
23 namespace internal {
24
25 TrayMonitor::TrayMonitor(SystemTray* system_tray)
26     : SystemTrayItem(system_tray),
27       label_(NULL) {
28   refresh_timer_.Start(FROM_HERE,
29       base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
30       this, &TrayMonitor::OnTimer);
31 }
32
33 TrayMonitor::~TrayMonitor() {
34   label_ = NULL;
35 }
36
37 views::View* TrayMonitor::CreateTrayView(user::LoginStatus status) {
38   TrayItemView* view = new TrayItemView(this);
39   view->CreateLabel();
40   label_ = view->label();
41   label_->SetAutoColorReadabilityEnabled(false);
42   label_->SetEnabledColor(SK_ColorWHITE);
43   label_->SetBackgroundColor(SkColorSetARGB(0, 255, 255, 255));
44   label_->SetShadowColors(SkColorSetARGB(64, 0, 0, 0),
45                           SkColorSetARGB(64, 0, 0, 0));
46   label_->SetShadowOffset(0, 1);
47   label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
48   label_->SetFontList(label_->font_list().DeriveWithSizeDelta(-2));
49   return view;
50 }
51
52 void TrayMonitor::DestroyTrayView() {
53   label_ = NULL;
54 }
55
56 void TrayMonitor::OnTimer() {
57   GPUSupport::GetGpuProcessHandlesCallback callback =
58       base::Bind(&TrayMonitor::OnGotHandles, base::Unretained(this));
59   refresh_timer_.Stop();
60   Shell::GetInstance()->gpu_support()->GetGpuProcessHandles(callback);
61 }
62
63 void TrayMonitor::OnGotHandles(const std::list<base::ProcessHandle>& handles) {
64   base::SystemMemoryInfoKB mem_info;
65   base::GetSystemMemoryInfo(&mem_info);
66   std::string output;
67   base::string16 free_bytes =
68       ui::FormatBytes(static_cast<int64>(mem_info.free) * 1024);
69   output = base::StringPrintf("free: %s",
70                               base::UTF16ToUTF8(free_bytes).c_str());
71 #if defined(OS_CHROMEOS)
72   if (mem_info.gem_size != -1) {
73     base::string16 gem_size = ui::FormatBytes(mem_info.gem_size);
74     output += base::StringPrintf("  gmem: %s",
75                                  base::UTF16ToUTF8(gem_size).c_str());
76     if (mem_info.gem_objects != -1)
77       output += base::StringPrintf("  gobjects: %d", mem_info.gem_objects);
78   }
79 #endif
80   size_t total_private_bytes = 0, total_shared_bytes = 0;
81   for (std::list<base::ProcessHandle>::const_iterator i = handles.begin();
82        i != handles.end(); ++i) {
83     base::ProcessMetrics* pm = base::ProcessMetrics::CreateProcessMetrics(*i);
84     size_t private_bytes, shared_bytes;
85     pm->GetMemoryBytes(&private_bytes, &shared_bytes);
86     total_private_bytes += private_bytes;
87     total_shared_bytes += shared_bytes;
88     delete pm;
89   }
90   base::string16 private_size = ui::FormatBytes(total_private_bytes);
91   base::string16 shared_size = ui::FormatBytes(total_shared_bytes);
92
93   output += base::StringPrintf("\nGPU private: %s  shared: %s",
94                                base::UTF16ToUTF8(private_size).c_str(),
95                                base::UTF16ToUTF8(shared_size).c_str());
96   label_->SetText(base::UTF8ToUTF16(output));
97   refresh_timer_.Start(FROM_HERE,
98       base::TimeDelta::FromMilliseconds(kRefreshTimeoutMs),
99       this, &TrayMonitor::OnTimer);
100 }
101
102 }  // namespace internal
103 }  // namespace ash