- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / task_manager / browser_process_resource_provider.cc
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/task_manager/browser_process_resource_provider.h"
6
7 #include "base/command_line.h"
8 #include "base/strings/string16.h"
9 #include "chrome/browser/task_manager/resource_provider.h"
10 #include "chrome/browser/task_manager/task_manager.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "net/proxy/proxy_resolver_v8.h"
15 #include "third_party/sqlite/sqlite3.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/image/image_skia.h"
19
20 #if defined(OS_MACOSX)
21 #include "ui/gfx/image/image_skia_util_mac.h"
22 #endif  // defined(OS_MACOSX)
23
24 #if defined(OS_WIN)
25 #include "chrome/browser/app_icon_win.h"
26 #include "ui/gfx/icon_util.h"
27 #endif  // defined(OS_WIN)
28
29 namespace task_manager {
30
31 gfx::ImageSkia* BrowserProcessResource::default_icon_ = NULL;
32
33 BrowserProcessResource::BrowserProcessResource()
34     : title_() {
35   int pid = base::GetCurrentProcId();
36   bool success = base::OpenPrivilegedProcessHandle(pid, &process_);
37   DCHECK(success);
38 #if defined(OS_WIN)
39   if (!default_icon_) {
40     HICON icon = GetAppIcon();
41     if (icon) {
42       scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
43       default_icon_ = new gfx::ImageSkia(gfx::ImageSkiaRep(*bitmap, 1.0f));
44     }
45   }
46 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
47   if (!default_icon_) {
48     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
49     default_icon_ = rb.GetImageSkiaNamed(IDR_PRODUCT_LOGO_16);
50   }
51 #elif defined(OS_MACOSX)
52   if (!default_icon_) {
53     // IDR_PRODUCT_LOGO_16 doesn't quite look like chrome/mac's icns icon. Load
54     // the real app icon (requires a nsimage->image_skia->nsimage
55     // conversion :-().
56     default_icon_ = new gfx::ImageSkia(gfx::ApplicationIconAtSize(16));
57   }
58 #else
59   // TODO(port): Port icon code.
60   NOTIMPLEMENTED();
61 #endif  // defined(OS_WIN)
62   default_icon_->MakeThreadSafe();
63 }
64
65 BrowserProcessResource::~BrowserProcessResource() {
66   base::CloseProcessHandle(process_);
67 }
68
69 // Resource methods:
70 string16 BrowserProcessResource::GetTitle() const {
71   if (title_.empty()) {
72     title_ = l10n_util::GetStringUTF16(IDS_TASK_MANAGER_WEB_BROWSER_CELL_TEXT);
73   }
74   return title_;
75 }
76
77 string16 BrowserProcessResource::GetProfileName() const {
78   return string16();
79 }
80
81 gfx::ImageSkia BrowserProcessResource::GetIcon() const {
82   return *default_icon_;
83 }
84
85 size_t BrowserProcessResource::SqliteMemoryUsedBytes() const {
86   return static_cast<size_t>(sqlite3_memory_used());
87 }
88
89 base::ProcessHandle BrowserProcessResource::GetProcess() const {
90   return base::GetCurrentProcessHandle();  // process_;
91 }
92
93 int BrowserProcessResource::GetUniqueChildProcessId() const {
94   return 0;
95 }
96
97 Resource::Type BrowserProcessResource::GetType() const {
98   return BROWSER;
99 }
100
101 bool BrowserProcessResource::SupportNetworkUsage() const {
102   return true;
103 }
104
105 void BrowserProcessResource::SetSupportNetworkUsage() {
106   NOTREACHED();
107 }
108
109 bool BrowserProcessResource::ReportsSqliteMemoryUsed() const {
110   return true;
111 }
112
113 // BrowserProcess uses v8 for proxy resolver in certain cases.
114 bool BrowserProcessResource::ReportsV8MemoryStats() const {
115   const CommandLine* command_line = CommandLine::ForCurrentProcess();
116   bool using_v8 = !command_line->HasSwitch(switches::kWinHttpProxyResolver);
117   if (using_v8 && command_line->HasSwitch(switches::kSingleProcess)) {
118     using_v8 = false;
119   }
120   return using_v8;
121 }
122
123 size_t BrowserProcessResource::GetV8MemoryAllocated() const {
124   return net::ProxyResolverV8::GetTotalHeapSize();
125 }
126
127 size_t BrowserProcessResource::GetV8MemoryUsed() const {
128   return net::ProxyResolverV8::GetUsedHeapSize();
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // BrowserProcessResourceProvider class
133 ////////////////////////////////////////////////////////////////////////////////
134
135 BrowserProcessResourceProvider::
136     BrowserProcessResourceProvider(TaskManager* task_manager)
137     : updating_(false),
138       task_manager_(task_manager) {
139 }
140
141 BrowserProcessResourceProvider::~BrowserProcessResourceProvider() {
142 }
143
144 Resource* BrowserProcessResourceProvider::GetResource(
145     int origin_pid,
146     int render_process_host_id,
147     int routing_id) {
148   if (origin_pid || render_process_host_id != -1) {
149     return NULL;
150   }
151
152   return &resource_;
153 }
154
155 void BrowserProcessResourceProvider::StartUpdating() {
156   task_manager_->AddResource(&resource_);
157 }
158
159 void BrowserProcessResourceProvider::StopUpdating() {
160 }
161
162 }  // namespace task_manager