Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: chrome/browser/ui/webui/chromeos/system_info_ui.cc

Issue 10827130: Refactoring the SysInfoProvider. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/chromeos/system_info_ui.h" 5 #include "chrome/browser/ui/webui/chromeos/system_info_ui.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/memory/ref_counted_memory.h" 9 #include "base/memory/ref_counted_memory.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/path_service.h" 12 #include "base/path_service.h"
13 #include "base/string_piece.h" 13 #include "base/string_piece.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
17 #include "base/time.h" 17 #include "base/time.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "chrome/browser/chromeos/cros/cros_library.h" 20 #include "chrome/browser/chromeos/cros/cros_library.h"
21 #include "chrome/browser/chromeos/system/sysinfo_provider.h" 21 #include "chrome/browser/chromeos/system/syslogs_fetcher_factory.h"
22 #include "chrome/browser/chromeos/system/syslogs_provider.h"
23 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" 23 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
25 #include "chrome/common/chrome_paths.h" 24 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/jstemplate_builder.h" 25 #include "chrome/common/jstemplate_builder.h"
27 #include "chrome/common/url_constants.h" 26 #include "chrome/common/url_constants.h"
28 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
30 #include "content/public/browser/web_ui.h" 29 #include "content/public/browser/web_ui.h"
31 #include "content/public/browser/web_ui_message_handler.h" 30 #include "content/public/browser/web_ui_message_handler.h"
32 #include "grit/browser_resources.h" 31 #include "grit/browser_resources.h"
(...skipping 18 matching lines...) Expand all
51 virtual void StartDataRequest(const std::string& path, 50 virtual void StartDataRequest(const std::string& path,
52 bool is_incognito, 51 bool is_incognito,
53 int request_id); 52 int request_id);
54 virtual std::string GetMimeType(const std::string&) const { 53 virtual std::string GetMimeType(const std::string&) const {
55 return "text/html"; 54 return "text/html";
56 } 55 }
57 56
58 private: 57 private:
59 ~SystemInfoUIHTMLSource() {} 58 ~SystemInfoUIHTMLSource() {}
60 59
61 void SyslogsComplete(chromeos::system::LogDictionaryType* sys_info,
62 std::string* ignored_content);
63 void SysInfoComplete(chromeos::system::SysInfoResponse* response); 60 void SysInfoComplete(chromeos::system::SysInfoResponse* response);
64 void RequestComplete(); 61 void RequestComplete();
65 void WaitForData(); 62 void WaitForData();
66 63
67 CancelableRequestConsumer logs_consumer_;
68 CancelableRequestConsumer sys_info_consumer_;
69
70 // Stored data from StartDataRequest() 64 // Stored data from StartDataRequest()
71 std::string path_; 65 std::string path_;
72 int request_id_; 66 int request_id_;
73 67
74 int pending_requests_;
75 chromeos::system::LogDictionaryType* logs_;
76 chromeos::system::SysInfoResponse* sys_info_; 68 chromeos::system::SysInfoResponse* sys_info_;
77 scoped_ptr<chromeos::system::SysInfoProvider> sys_info_provider_; 69 base::WeakPtrFactory<SystemInfoUIHTMLSource> weak_ptr_factory_;
78
79 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource); 70 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource);
80 }; 71 };
81 72
82 // The handler for Javascript messages related to the "system" view. 73 // The handler for Javascript messages related to the "system" view.
83 class SystemInfoHandler : public WebUIMessageHandler, 74 class SystemInfoHandler : public WebUIMessageHandler,
84 public base::SupportsWeakPtr<SystemInfoHandler> { 75 public base::SupportsWeakPtr<SystemInfoHandler> {
85 public: 76 public:
86 SystemInfoHandler(); 77 SystemInfoHandler();
87 virtual ~SystemInfoHandler(); 78 virtual ~SystemInfoHandler();
88 79
89 // WebUIMessageHandler implementation. 80 // WebUIMessageHandler implementation.
90 virtual void RegisterMessages() OVERRIDE; 81 virtual void RegisterMessages() OVERRIDE;
91 82
92 private: 83 private:
93 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler); 84 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler);
94 }; 85 };
95 86
96 //////////////////////////////////////////////////////////////////////////////// 87 ////////////////////////////////////////////////////////////////////////////////
97 // 88 //
98 // SystemInfoUIHTMLSource 89 // SystemInfoUIHTMLSource
99 // 90 //
100 //////////////////////////////////////////////////////////////////////////////// 91 ////////////////////////////////////////////////////////////////////////////////
101 92
102 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() 93 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource()
103 : DataSource(chrome::kChromeUISystemInfoHost, MessageLoop::current()), 94 : DataSource(chrome::kChromeUISystemInfoHost, MessageLoop::current()),
104 request_id_(0), logs_(NULL), sys_info_(NULL), 95 request_id_(0), sys_info_(NULL),
105 sys_info_provider_(chromeos::system::SysInfoProvider::Create()) { 96 weak_ptr_factory_(this) {
rkc1 2012/08/04 01:15:05 use ALLOW_THIS_IN_INITIALIZER_LIST everywhere you
tudalex(Chromium) 2012/08/05 01:15:12 Done.
106 } 97 }
107 98
108 void SystemInfoUIHTMLSource::StartDataRequest(const std::string& path, 99 void SystemInfoUIHTMLSource::StartDataRequest(const std::string& path,
109 bool is_incognito, 100 bool is_incognito,
110 int request_id) { 101 int request_id) {
111 path_ = path; 102 path_ = path;
112 request_id_ = request_id; 103 request_id_ = request_id;
113 pending_requests_ = 0;
114 104
115 chromeos::system::SyslogsProvider* provider = 105 chromeos::system::SysLogsFetcher * fetcher =
116 chromeos::system::SyslogsProvider::GetInstance(); 106 chromeos::system::SysLogsFetcherFactory::GetInstance()->GetFetcher();
117 if (provider) { 107 fetcher->Fetch(base::Bind(
118 provider->RequestSyslogs( 108 &SystemInfoUIHTMLSource::SysInfoComplete,
119 false, // don't compress. 109 weak_ptr_factory_.GetWeakPtr()));
120 chromeos::system::SyslogsProvider::SYSLOGS_SYSINFO,
121 &logs_consumer_,
122 base::Bind(&SystemInfoUIHTMLSource::SyslogsComplete,
123 base::Unretained(this)));
124 pending_requests_++;
125 }
126 110
127 sys_info_provider_->Fetch(&sys_info_consumer_,
128 base::Bind(
129 &SystemInfoUIHTMLSource::SysInfoComplete,
130 base::Unretained(this)));
131 pending_requests_++;
132 } 111 }
133 112
134 void SystemInfoUIHTMLSource::SyslogsComplete(
135 chromeos::system::LogDictionaryType* sys_info,
136 std::string* ignored_content) {
137 logs_ = sys_info;
138 RequestComplete();
139 }
140 113
141 void SystemInfoUIHTMLSource::SysInfoComplete( 114 void SystemInfoUIHTMLSource::SysInfoComplete(
142 chromeos::system::SysInfoResponse* sys_info) { 115 chromeos::system::SysInfoResponse* sys_info) {
143 sys_info_ = sys_info; 116 sys_info_ = sys_info;
144 RequestComplete(); 117 RequestComplete();
145 } 118 }
146 119
147 void SystemInfoUIHTMLSource::RequestComplete() { 120 void SystemInfoUIHTMLSource::RequestComplete() {
148 if (--pending_requests_) 121
149 return;
150 122
151 DictionaryValue strings; 123 DictionaryValue strings;
152 strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE)); 124 strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE));
153 strings.SetString("description", 125 strings.SetString("description",
154 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC)); 126 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC));
155 strings.SetString("table_title", 127 strings.SetString("table_title",
156 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE)); 128 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE));
157 strings.SetString("expand_all_btn", 129 strings.SetString("expand_all_btn",
158 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL)); 130 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL));
159 strings.SetString("collapse_all_btn", 131 strings.SetString("collapse_all_btn",
160 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL)); 132 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL));
161 strings.SetString("expand_btn", 133 strings.SetString("expand_btn",
162 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND)); 134 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND));
163 strings.SetString("collapse_btn", 135 strings.SetString("collapse_btn",
164 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE)); 136 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE));
165 SetFontAndTextDirection(&strings); 137 SetFontAndTextDirection(&strings);
166 138
167 if (logs_ || sys_info_) { 139 ListValue* details = new ListValue();
168 ListValue* details = new ListValue(); 140 strings.Set("details", details);
169 strings.Set("details", details); 141
170 if (logs_) { 142 chromeos::system::SysInfoResponse::iterator it;
171 chromeos::system::LogDictionaryType::iterator it; 143 for (it = sys_info_->begin(); it != sys_info_->end(); ++it) {
rkc1 2012/08/04 01:15:05 Add a check for sys_info_ being NULL.
tudalex(Chromium) 2012/08/05 01:15:12 Done.
172 for (it = logs_->begin(); it != logs_->end(); ++it) { 144 DictionaryValue* val = new DictionaryValue;
173 DictionaryValue* val = new DictionaryValue; 145 val->SetString("stat_name", it->first);
174 val->SetString("stat_name", it->first); 146 val->SetString("stat_value", it->second);
175 val->SetString("stat_value", it->second); 147 details->Append(val);
176 details->Append(val);
177 }
178 delete logs_;
179 }
180 if (sys_info_) {
181 chromeos::system::SysInfoResponse::iterator it;
182 for (it = sys_info_->begin(); it != sys_info_->end(); ++it) {
183 DictionaryValue* val = new DictionaryValue;
184 // Prefix stats coming from debugd with 'debugd-' for now. The code that
185 // displays stats can only handle one stat with a given name, so this
186 // prevents names from overlapping. Once the duplicates have been
187 // removed from userfeedback's list by
188 // <https://gerrit.chromium.org/gerrit/25106>, the prefix can go away.
189 val->SetString("stat_name", "debugd-" + it->first);
190 val->SetString("stat_value", it->second);
191 details->Append(val);
192 }
193 delete sys_info_;
194 }
195 strings.SetString("anchor", path_);
196 } 148 }
149 delete sys_info_;
150
151 strings.SetString("anchor", path_);
152
197 static const base::StringPiece systeminfo_html( 153 static const base::StringPiece systeminfo_html(
198 ResourceBundle::GetSharedInstance().GetRawDataResource( 154 ResourceBundle::GetSharedInstance().GetRawDataResource(
199 IDR_ABOUT_SYS_HTML, ui::SCALE_FACTOR_NONE)); 155 IDR_ABOUT_SYS_HTML, ui::SCALE_FACTOR_NONE));
200 std::string full_html = jstemplate_builder::GetTemplatesHtml( 156 std::string full_html = jstemplate_builder::GetTemplatesHtml(
201 systeminfo_html, &strings, "t" /* template root node id */); 157 systeminfo_html, &strings, "t" /* template root node id */);
202 158
203 SendResponse(request_id_, base::RefCountedString::TakeString(&full_html)); 159 SendResponse(request_id_, base::RefCountedString::TakeString(&full_html));
204 } 160 }
205 161
206 //////////////////////////////////////////////////////////////////////////////// 162 ////////////////////////////////////////////////////////////////////////////////
(...skipping 19 matching lines...) Expand all
226 182
227 SystemInfoUI::SystemInfoUI(content::WebUI* web_ui) : WebUIController(web_ui) { 183 SystemInfoUI::SystemInfoUI(content::WebUI* web_ui) : WebUIController(web_ui) {
228 SystemInfoHandler* handler = new SystemInfoHandler(); 184 SystemInfoHandler* handler = new SystemInfoHandler();
229 web_ui->AddMessageHandler(handler); 185 web_ui->AddMessageHandler(handler);
230 SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource(); 186 SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource();
231 187
232 // Set up the chrome://system/ source. 188 // Set up the chrome://system/ source.
233 Profile* profile = Profile::FromWebUI(web_ui); 189 Profile* profile = Profile::FromWebUI(web_ui);
234 ChromeURLDataManager::AddDataSource(profile, html_source); 190 ChromeURLDataManager::AddDataSource(profile, html_source);
235 } 191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698