OLD | NEW |
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/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "base/time.h" | 18 #include "base/time.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/chromeos/cros/cros_library.h" | 21 #include "chrome/browser/chromeos/cros/cros_library.h" |
22 #include "chrome/browser/chromeos/system_logs/system_logs_fetcher.h" | 22 #include "chrome/browser/chromeos/system_logs/system_logs_fetcher.h" |
23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/web_ui_util.h" |
25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/jstemplate_builder.h" | 26 #include "chrome/common/jstemplate_builder.h" |
27 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
28 #include "content/public/browser/url_data_source_delegate.h" | 28 #include "content/public/browser/url_data_source.h" |
29 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
30 #include "content/public/browser/web_ui.h" | 30 #include "content/public/browser/web_ui.h" |
31 #include "content/public/browser/web_ui_message_handler.h" | 31 #include "content/public/browser/web_ui_message_handler.h" |
32 #include "grit/browser_resources.h" | 32 #include "grit/browser_resources.h" |
33 #include "grit/chromium_strings.h" | 33 #include "grit/chromium_strings.h" |
34 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
35 #include "grit/locale_settings.h" | 35 #include "grit/locale_settings.h" |
36 #include "net/base/directory_lister.h" | 36 #include "net/base/directory_lister.h" |
37 #include "net/base/escape.h" | 37 #include "net/base/escape.h" |
38 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
39 #include "ui/base/resource/resource_bundle.h" | 39 #include "ui/base/resource/resource_bundle.h" |
40 | 40 |
41 using content::WebContents; | 41 using content::WebContents; |
42 using content::WebUIMessageHandler; | 42 using content::WebUIMessageHandler; |
43 | 43 |
44 namespace chromeos { | 44 namespace chromeos { |
45 | 45 |
46 class SystemInfoUIHTMLSource : public content::URLDataSourceDelegate { | 46 class SystemInfoUIHTMLSource : public content::URLDataSource{ |
47 public: | 47 public: |
48 SystemInfoUIHTMLSource(); | 48 SystemInfoUIHTMLSource(); |
49 | 49 |
50 // content::URLDataSourceDelegate implementation. | 50 // content::URLDataSource implementation. |
51 virtual std::string GetSource() OVERRIDE; | 51 virtual std::string GetSource() OVERRIDE; |
52 virtual void StartDataRequest(const std::string& path, | 52 virtual void StartDataRequest( |
53 bool is_incognito, | 53 const std::string& path, |
54 int request_id) OVERRIDE; | 54 bool is_incognito, |
| 55 const content::URLDataSource::GotDataCallback& callback) OVERRIDE; |
55 virtual std::string GetMimeType(const std::string&) const OVERRIDE { | 56 virtual std::string GetMimeType(const std::string&) const OVERRIDE { |
56 return "text/html"; | 57 return "text/html"; |
57 } | 58 } |
58 | 59 |
59 private: | 60 private: |
60 ~SystemInfoUIHTMLSource() {} | 61 ~SystemInfoUIHTMLSource() {} |
61 | 62 |
62 void SysInfoComplete(scoped_ptr<SystemLogsResponse> response); | 63 void SysInfoComplete(scoped_ptr<SystemLogsResponse> response); |
63 void RequestComplete(); | 64 void RequestComplete(); |
64 void WaitForData(); | 65 void WaitForData(); |
65 | 66 |
66 // Stored data from StartDataRequest() | 67 // Stored data from StartDataRequest() |
67 std::string path_; | 68 std::string path_; |
68 int request_id_; | 69 content::URLDataSource::GotDataCallback callback_; |
69 | 70 |
70 scoped_ptr<SystemLogsResponse> response_; | 71 scoped_ptr<SystemLogsResponse> response_; |
71 base::WeakPtrFactory<SystemInfoUIHTMLSource> weak_ptr_factory_; | 72 base::WeakPtrFactory<SystemInfoUIHTMLSource> weak_ptr_factory_; |
72 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource); | 73 DISALLOW_COPY_AND_ASSIGN(SystemInfoUIHTMLSource); |
73 }; | 74 }; |
74 | 75 |
75 // The handler for Javascript messages related to the "system" view. | 76 // The handler for Javascript messages related to the "system" view. |
76 class SystemInfoHandler : public WebUIMessageHandler, | 77 class SystemInfoHandler : public WebUIMessageHandler, |
77 public base::SupportsWeakPtr<SystemInfoHandler> { | 78 public base::SupportsWeakPtr<SystemInfoHandler> { |
78 public: | 79 public: |
79 SystemInfoHandler(); | 80 SystemInfoHandler(); |
80 virtual ~SystemInfoHandler(); | 81 virtual ~SystemInfoHandler(); |
81 | 82 |
82 // WebUIMessageHandler implementation. | 83 // WebUIMessageHandler implementation. |
83 virtual void RegisterMessages() OVERRIDE; | 84 virtual void RegisterMessages() OVERRIDE; |
84 | 85 |
85 private: | 86 private: |
86 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler); | 87 DISALLOW_COPY_AND_ASSIGN(SystemInfoHandler); |
87 }; | 88 }; |
88 | 89 |
89 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
90 // | 91 // |
91 // SystemInfoUIHTMLSource | 92 // SystemInfoUIHTMLSource |
92 // | 93 // |
93 //////////////////////////////////////////////////////////////////////////////// | 94 //////////////////////////////////////////////////////////////////////////////// |
94 | 95 |
95 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() | 96 SystemInfoUIHTMLSource::SystemInfoUIHTMLSource() |
96 : request_id_(0), | 97 : response_(NULL), |
97 response_(NULL), | |
98 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 98 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
99 } | 99 } |
100 | 100 |
101 std::string SystemInfoUIHTMLSource::GetSource() { | 101 std::string SystemInfoUIHTMLSource::GetSource() { |
102 return chrome::kChromeUISystemInfoHost; | 102 return chrome::kChromeUISystemInfoHost; |
103 } | 103 } |
104 | 104 |
105 void SystemInfoUIHTMLSource::StartDataRequest(const std::string& path, | 105 void SystemInfoUIHTMLSource::StartDataRequest( |
106 bool is_incognito, | 106 const std::string& path, |
107 int request_id) { | 107 bool is_incognito, |
| 108 const content::URLDataSource::GotDataCallback& callback) { |
108 path_ = path; | 109 path_ = path; |
109 request_id_ = request_id; | 110 callback_ = callback; |
110 | 111 |
111 SystemLogsFetcher* fetcher = new SystemLogsFetcher(); | 112 SystemLogsFetcher* fetcher = new SystemLogsFetcher(); |
112 fetcher->Fetch(base::Bind(&SystemInfoUIHTMLSource::SysInfoComplete, | 113 fetcher->Fetch(base::Bind(&SystemInfoUIHTMLSource::SysInfoComplete, |
113 weak_ptr_factory_.GetWeakPtr())); | 114 weak_ptr_factory_.GetWeakPtr())); |
114 } | 115 } |
115 | 116 |
116 | 117 |
117 void SystemInfoUIHTMLSource::SysInfoComplete( | 118 void SystemInfoUIHTMLSource::SysInfoComplete( |
118 scoped_ptr<SystemLogsResponse> sys_info) { | 119 scoped_ptr<SystemLogsResponse> sys_info) { |
119 response_ = sys_info.Pass(); | 120 response_ = sys_info.Pass(); |
120 RequestComplete(); | 121 RequestComplete(); |
121 } | 122 } |
122 | 123 |
123 void SystemInfoUIHTMLSource::RequestComplete() { | 124 void SystemInfoUIHTMLSource::RequestComplete() { |
124 DictionaryValue strings; | 125 DictionaryValue strings; |
125 strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE)); | 126 strings.SetString("title", l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TITLE)); |
126 strings.SetString("description", | 127 strings.SetString("description", |
127 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC)); | 128 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_DESC)); |
128 strings.SetString("table_title", | 129 strings.SetString("table_title", |
129 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE)); | 130 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_TABLE_TITLE)); |
130 strings.SetString("expand_all_btn", | 131 strings.SetString("expand_all_btn", |
131 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL)); | 132 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND_ALL)); |
132 strings.SetString("collapse_all_btn", | 133 strings.SetString("collapse_all_btn", |
133 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL)); | 134 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE_ALL)); |
134 strings.SetString("expand_btn", | 135 strings.SetString("expand_btn", |
135 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND)); | 136 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_EXPAND)); |
136 strings.SetString("collapse_btn", | 137 strings.SetString("collapse_btn", |
137 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE)); | 138 l10n_util::GetStringUTF16(IDS_ABOUT_SYS_COLLAPSE)); |
138 URLDataSource::SetFontAndTextDirection(&strings); | 139 web_ui_util::SetFontAndTextDirection(&strings); |
139 if (response_.get()) { | 140 if (response_.get()) { |
140 ListValue* details = new ListValue(); | 141 ListValue* details = new ListValue(); |
141 strings.Set("details", details); | 142 strings.Set("details", details); |
142 for (SystemLogsResponse::const_iterator it = response_->begin(); | 143 for (SystemLogsResponse::const_iterator it = response_->begin(); |
143 it != response_->end(); | 144 it != response_->end(); |
144 ++it) { | 145 ++it) { |
145 DictionaryValue* val = new DictionaryValue; | 146 DictionaryValue* val = new DictionaryValue; |
146 val->SetString("stat_name", it->first); | 147 val->SetString("stat_name", it->first); |
147 val->SetString("stat_value", it->second); | 148 val->SetString("stat_value", it->second); |
148 details->Append(val); | 149 details->Append(val); |
149 } | 150 } |
150 strings.SetString("anchor", path_); | 151 strings.SetString("anchor", path_); |
151 } | 152 } |
152 static const base::StringPiece systeminfo_html( | 153 static const base::StringPiece systeminfo_html( |
153 ResourceBundle::GetSharedInstance().GetRawDataResource( | 154 ResourceBundle::GetSharedInstance().GetRawDataResource( |
154 IDR_ABOUT_SYS_HTML)); | 155 IDR_ABOUT_SYS_HTML)); |
155 std::string full_html = jstemplate_builder::GetTemplatesHtml( | 156 std::string full_html = jstemplate_builder::GetTemplatesHtml( |
156 systeminfo_html, &strings, "t" /* template root node id */); | 157 systeminfo_html, &strings, "t" /* template root node id */); |
157 url_data_source()->SendResponse( | 158 callback_.Run(base::RefCountedString::TakeString(&full_html)); |
158 request_id_, base::RefCountedString::TakeString(&full_html)); | |
159 } | 159 } |
160 | 160 |
161 //////////////////////////////////////////////////////////////////////////////// | 161 //////////////////////////////////////////////////////////////////////////////// |
162 // | 162 // |
163 // SystemInfoHandler | 163 // SystemInfoHandler |
164 // | 164 // |
165 //////////////////////////////////////////////////////////////////////////////// | 165 //////////////////////////////////////////////////////////////////////////////// |
166 SystemInfoHandler::SystemInfoHandler() { | 166 SystemInfoHandler::SystemInfoHandler() { |
167 } | 167 } |
168 | 168 |
(...skipping 14 matching lines...) Expand all Loading... |
183 SystemInfoHandler* handler = new SystemInfoHandler(); | 183 SystemInfoHandler* handler = new SystemInfoHandler(); |
184 web_ui->AddMessageHandler(handler); | 184 web_ui->AddMessageHandler(handler); |
185 SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource(); | 185 SystemInfoUIHTMLSource* html_source = new SystemInfoUIHTMLSource(); |
186 | 186 |
187 // Set up the chrome://system/ source. | 187 // Set up the chrome://system/ source. |
188 Profile* profile = Profile::FromWebUI(web_ui); | 188 Profile* profile = Profile::FromWebUI(web_ui); |
189 ChromeURLDataManager::AddDataSource(profile, html_source); | 189 ChromeURLDataManager::AddDataSource(profile, html_source); |
190 } | 190 } |
191 | 191 |
192 } // namespace chromeos | 192 } // namespace chromeos |
OLD | NEW |