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

Side by Side Diff: chrome/browser/ui/webui/net_internals_ui.cc

Issue 6995086: Update about:net-internals to use Javascript for loading logs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix variable names in response to comment Created 9 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/net_internals/main.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/net_internals_ui.h" 5 #include "chrome/browser/ui/webui/net_internals_ui.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <list> 8 #include <list>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/base64.h" 13 #include "base/base64.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/file_util.h"
16 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
17 #include "base/message_loop.h" 16 #include "base/message_loop.h"
18 #include "base/path_service.h" 17 #include "base/path_service.h"
19 #include "base/string_number_conversions.h" 18 #include "base/string_number_conversions.h"
20 #include "base/string_piece.h" 19 #include "base/string_piece.h"
21 #include "base/string_split.h" 20 #include "base/string_split.h"
22 #include "base/string_util.h" 21 #include "base/string_util.h"
23 #include "base/utf_string_conversions.h" 22 #include "base/utf_string_conversions.h"
24 #include "base/values.h" 23 #include "base/values.h"
25 #include "chrome/browser/browser_process.h" 24 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/io_thread.h" 25 #include "chrome/browser/io_thread.h"
27 #include "chrome/browser/net/chrome_net_log.h" 26 #include "chrome/browser/net/chrome_net_log.h"
28 #include "chrome/browser/net/connection_tester.h" 27 #include "chrome/browser/net/connection_tester.h"
29 #include "chrome/browser/net/passive_log_collector.h" 28 #include "chrome/browser/net/passive_log_collector.h"
30 #include "chrome/browser/net/url_fixer_upper.h" 29 #include "chrome/browser/net/url_fixer_upper.h"
31 #include "chrome/browser/platform_util.h" 30 #include "chrome/browser/platform_util.h"
32 #include "chrome/browser/prefs/pref_member.h" 31 #include "chrome/browser/prefs/pref_member.h"
33 #include "chrome/browser/profiles/profile.h" 32 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/ui/shell_dialogs.h"
35 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" 33 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
36 #include "chrome/common/chrome_paths.h" 34 #include "chrome/common/chrome_paths.h"
37 #include "chrome/common/chrome_version_info.h" 35 #include "chrome/common/chrome_version_info.h"
38 #include "chrome/common/jstemplate_builder.h" 36 #include "chrome/common/jstemplate_builder.h"
39 #include "chrome/common/pref_names.h" 37 #include "chrome/common/pref_names.h"
40 #include "chrome/common/url_constants.h" 38 #include "chrome/common/url_constants.h"
41 #include "content/browser/browser_thread.h" 39 #include "content/browser/browser_thread.h"
42 #include "content/browser/tab_contents/tab_contents.h"
43 #include "content/browser/tab_contents/tab_contents_view.h"
44 #include "content/common/notification_details.h" 40 #include "content/common/notification_details.h"
41 #include "content/common/notification_type.h"
45 #include "grit/generated_resources.h" 42 #include "grit/generated_resources.h"
46 #include "grit/net_internals_resources.h" 43 #include "grit/net_internals_resources.h"
47 #include "net/base/escape.h" 44 #include "net/base/escape.h"
48 #include "net/base/host_resolver_impl.h" 45 #include "net/base/host_resolver_impl.h"
49 #include "net/base/net_errors.h" 46 #include "net/base/net_errors.h"
50 #include "net/base/net_util.h" 47 #include "net/base/net_util.h"
51 #include "net/base/sys_addrinfo.h" 48 #include "net/base/sys_addrinfo.h"
52 #include "net/base/x509_cert_types.h" 49 #include "net/base/x509_cert_types.h"
53 #include "net/disk_cache/disk_cache.h" 50 #include "net/disk_cache/disk_cache.h"
54 #include "net/http/http_alternate_protocols.h" 51 #include "net/http/http_alternate_protocols.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // Note that the WebUI infrastructure runs on the UI thread, therefore all of 143 // Note that the WebUI infrastructure runs on the UI thread, therefore all of
147 // this class's methods are expected to run on the UI thread. 144 // this class's methods are expected to run on the UI thread.
148 // 145 //
149 // Since the network code we want to run lives on the IO thread, we proxy 146 // Since the network code we want to run lives on the IO thread, we proxy
150 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which 147 // almost everything over to NetInternalsMessageHandler::IOThreadImpl, which
151 // runs on the IO thread. 148 // runs on the IO thread.
152 // 149 //
153 // TODO(eroman): Can we start on the IO thread to begin with? 150 // TODO(eroman): Can we start on the IO thread to begin with?
154 class NetInternalsMessageHandler 151 class NetInternalsMessageHandler
155 : public WebUIMessageHandler, 152 : public WebUIMessageHandler,
156 public SelectFileDialog::Listener,
157 public base::SupportsWeakPtr<NetInternalsMessageHandler>, 153 public base::SupportsWeakPtr<NetInternalsMessageHandler>,
158 public NotificationObserver { 154 public NotificationObserver {
159 public: 155 public:
160 NetInternalsMessageHandler(); 156 NetInternalsMessageHandler();
161 virtual ~NetInternalsMessageHandler(); 157 virtual ~NetInternalsMessageHandler();
162 158
163 // WebUIMessageHandler implementation. 159 // WebUIMessageHandler implementation.
164 virtual WebUIMessageHandler* Attach(WebUI* web_ui); 160 virtual WebUIMessageHandler* Attach(WebUI* web_ui);
165 virtual void RegisterMessages(); 161 virtual void RegisterMessages();
166 162
167 // Executes the javascript function |function_name| in the renderer, passing 163 // Executes the javascript function |function_name| in the renderer, passing
168 // it the argument |value|. 164 // it the argument |value|.
169 void CallJavascriptFunction(const std::wstring& function_name, 165 void CallJavascriptFunction(const std::wstring& function_name,
170 const Value* value); 166 const Value* value);
171 167
172 // NotificationObserver implementation. 168 // NotificationObserver implementation.
173 virtual void Observe(NotificationType type, 169 virtual void Observe(NotificationType type,
174 const NotificationSource& source, 170 const NotificationSource& source,
175 const NotificationDetails& details); 171 const NotificationDetails& details);
176 172
177 // Javascript message handlers. 173 // Javascript message handlers.
178 void OnRendererReady(const ListValue* list); 174 void OnRendererReady(const ListValue* list);
179 void OnEnableHttpThrottling(const ListValue* list); 175 void OnEnableHttpThrottling(const ListValue* list);
180 #ifdef OS_CHROMEOS 176 #ifdef OS_CHROMEOS
181 void OnRefreshSystemLogs(const ListValue* list); 177 void OnRefreshSystemLogs(const ListValue* list);
182 void OnGetSystemLog(const ListValue* list); 178 void OnGetSystemLog(const ListValue* list);
183 #endif 179 #endif
184 180
185 // SelectFileDialog::Listener implementation
186 virtual void FileSelected(const FilePath& path, int index, void* params);
187 virtual void FileSelectionCanceled(void* params);
188
189 // The only callback handled on the UI thread. As it needs to access fields
190 // from |web_ui_|, it can't be called on the IO thread.
191 void OnLoadLogFile(const ListValue* list);
192
193 private: 181 private:
194 class IOThreadImpl; 182 class IOThreadImpl;
195 183
196 // Task run on the FILE thread to read the contents of a log file. The result
197 // is then passed to IOThreadImpl's CallJavascriptFunction, which sends it
198 // back to the web page. IOThreadImpl is used instead of the
199 // NetInternalsMessageHandler directly because it checks if the message
200 // handler has been destroyed in the meantime.
201 class ReadLogFileTask : public Task {
202 public:
203 ReadLogFileTask(IOThreadImpl* proxy, const FilePath& path);
204
205 virtual void Run();
206
207 private:
208 // IOThreadImpl implements existence checks already. Simpler to reused them
209 // then to reimplement them.
210 scoped_refptr<IOThreadImpl> proxy_;
211
212 // Path of the file to open.
213 const FilePath path_;
214 };
215
216 #ifdef OS_CHROMEOS 184 #ifdef OS_CHROMEOS
217 // Class that is used for getting network related ChromeOS logs. 185 // Class that is used for getting network related ChromeOS logs.
218 // Logs are fetched from ChromeOS libcros on user request, and only when we 186 // Logs are fetched from ChromeOS libcros on user request, and only when we
219 // don't yet have a copy of logs. If a copy is present, we send back data from 187 // don't yet have a copy of logs. If a copy is present, we send back data from
220 // it, else we save request and answer to it when we get logs from libcros. 188 // it, else we save request and answer to it when we get logs from libcros.
221 // If needed, we also send request for system logs to libcros. 189 // If needed, we also send request for system logs to libcros.
222 // Logs refresh has to be done explicitly, by deleting old logs and then 190 // Logs refresh has to be done explicitly, by deleting old logs and then
223 // loading them again. 191 // loading them again.
224 class SystemLogsGetter { 192 class SystemLogsGetter {
225 public: 193 public:
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 scoped_ptr<WebUI::MessageCallback> renderer_ready_io_callback_; 245 scoped_ptr<WebUI::MessageCallback> renderer_ready_io_callback_;
278 246
279 // This is the "real" message handler, which lives on the IO thread. 247 // This is the "real" message handler, which lives on the IO thread.
280 scoped_refptr<IOThreadImpl> proxy_; 248 scoped_refptr<IOThreadImpl> proxy_;
281 249
282 #ifdef OS_CHROMEOS 250 #ifdef OS_CHROMEOS
283 // Class that handles getting and filtering system logs. 251 // Class that handles getting and filtering system logs.
284 scoped_ptr<SystemLogsGetter> syslogs_getter_; 252 scoped_ptr<SystemLogsGetter> syslogs_getter_;
285 #endif 253 #endif
286 254
287 // Used for loading log files.
288 scoped_refptr<SelectFileDialog> select_log_file_dialog_;
289
290 DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler); 255 DISALLOW_COPY_AND_ASSIGN(NetInternalsMessageHandler);
291 }; 256 };
292 257
293 // This class is the "real" message handler. It is allocated and destroyed on 258 // This class is the "real" message handler. It is allocated and destroyed on
294 // the UI thread. With the exception of OnAddEntry, OnWebUIDeleted, and 259 // the UI thread. With the exception of OnAddEntry, OnWebUIDeleted, and
295 // CallJavascriptFunction, its methods are all expected to be called from the IO 260 // CallJavascriptFunction, its methods are all expected to be called from the IO
296 // thread. OnAddEntry and CallJavascriptFunction can be called from any thread, 261 // thread. OnAddEntry and CallJavascriptFunction can be called from any thread,
297 // and OnWebUIDeleted can only be called from the UI thread. 262 // and OnWebUIDeleted can only be called from the UI thread.
298 class NetInternalsMessageHandler::IOThreadImpl 263 class NetInternalsMessageHandler::IOThreadImpl
299 : public base::RefCountedThreadSafe< 264 : public base::RefCountedThreadSafe<
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 495
531 NetInternalsMessageHandler::NetInternalsMessageHandler() {} 496 NetInternalsMessageHandler::NetInternalsMessageHandler() {}
532 497
533 NetInternalsMessageHandler::~NetInternalsMessageHandler() { 498 NetInternalsMessageHandler::~NetInternalsMessageHandler() {
534 if (proxy_) { 499 if (proxy_) {
535 proxy_.get()->OnWebUIDeleted(); 500 proxy_.get()->OnWebUIDeleted();
536 // Notify the handler on the IO thread that the renderer is gone. 501 // Notify the handler on the IO thread that the renderer is gone.
537 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 502 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
538 NewRunnableMethod(proxy_.get(), &IOThreadImpl::Detach)); 503 NewRunnableMethod(proxy_.get(), &IOThreadImpl::Detach));
539 } 504 }
540 if (select_log_file_dialog_)
541 select_log_file_dialog_->ListenerDestroyed();
542 } 505 }
543 506
544 WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) { 507 WebUIMessageHandler* NetInternalsMessageHandler::Attach(WebUI* web_ui) {
545 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
546 509
547 PrefService* pref_service = web_ui->GetProfile()->GetPrefs(); 510 PrefService* pref_service = web_ui->GetProfile()->GetPrefs();
548 http_throttling_enabled_.Init(prefs::kHttpThrottlingEnabled, pref_service, 511 http_throttling_enabled_.Init(prefs::kHttpThrottlingEnabled, pref_service,
549 this); 512 this);
550 513
551 proxy_ = new IOThreadImpl(this->AsWeakPtr(), g_browser_process->io_thread(), 514 proxy_ = new IOThreadImpl(this->AsWeakPtr(), g_browser_process->io_thread(),
552 web_ui->GetProfile()->GetRequestContext()); 515 web_ui->GetProfile()->GetRequestContext());
553 #ifdef OS_CHROMEOS 516 #ifdef OS_CHROMEOS
554 syslogs_getter_.reset(new SystemLogsGetter(this, 517 syslogs_getter_.reset(new SystemLogsGetter(this,
555 chromeos::SystemAccess::GetInstance())); 518 chromeos::SystemAccess::GetInstance()));
556 #endif 519 #endif
557 renderer_ready_io_callback_.reset( 520 renderer_ready_io_callback_.reset(
558 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady)); 521 proxy_->CreateCallback(&IOThreadImpl::OnRendererReady));
559 522
560 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); 523 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui);
561 return result; 524 return result;
562 } 525 }
563 526
564 void NetInternalsMessageHandler::FileSelected(
565 const FilePath& path, int index, void* params) {
566 select_log_file_dialog_.release();
567 BrowserThread::PostTask(
568 BrowserThread::FILE, FROM_HERE,
569 new ReadLogFileTask(proxy_.get(), path));
570 }
571
572 void NetInternalsMessageHandler::FileSelectionCanceled(void* params) {
573 select_log_file_dialog_.release();
574 }
575
576 void NetInternalsMessageHandler::OnLoadLogFile(const ListValue* list) {
577 // Only allow a single dialog at a time.
578 if (select_log_file_dialog_.get())
579 return;
580 select_log_file_dialog_ = SelectFileDialog::Create(this);
581 select_log_file_dialog_->SelectFile(
582 SelectFileDialog::SELECT_OPEN_FILE, string16(), FilePath(), NULL, 0,
583 FILE_PATH_LITERAL(""), web_ui_->tab_contents(),
584 web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL);
585 }
586
587 void NetInternalsMessageHandler::RegisterMessages() { 527 void NetInternalsMessageHandler::RegisterMessages() {
588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
589 // Only callback handled on UI thread.
590 web_ui_->RegisterMessageCallback(
591 "loadLogFile",
592 NewCallback(this, &NetInternalsMessageHandler::OnLoadLogFile));
593
594 web_ui_->RegisterMessageCallback( 529 web_ui_->RegisterMessageCallback(
595 "notifyReady", 530 "notifyReady",
596 NewCallback(this, &NetInternalsMessageHandler::OnRendererReady)); 531 NewCallback(this, &NetInternalsMessageHandler::OnRendererReady));
597 web_ui_->RegisterMessageCallback( 532 web_ui_->RegisterMessageCallback(
598 "getProxySettings", 533 "getProxySettings",
599 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings)); 534 proxy_->CreateCallback(&IOThreadImpl::OnGetProxySettings));
600 web_ui_->RegisterMessageCallback( 535 web_ui_->RegisterMessageCallback(
601 "reloadProxySettings", 536 "reloadProxySettings",
602 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings)); 537 proxy_->CreateCallback(&IOThreadImpl::OnReloadProxySettings));
603 web_ui_->RegisterMessageCallback( 538 web_ui_->RegisterMessageCallback(
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 648
714 bool enable = false; 649 bool enable = false;
715 if (!list->GetBoolean(0, &enable)) { 650 if (!list->GetBoolean(0, &enable)) {
716 NOTREACHED(); 651 NOTREACHED();
717 return; 652 return;
718 } 653 }
719 654
720 http_throttling_enabled_.SetValue(enable); 655 http_throttling_enabled_.SetValue(enable);
721 } 656 }
722 657
723 ////////////////////////////////////////////////////////////////////////////////
724 //
725 // NetInternalsMessageHandler::ReadLogFileTask
726 //
727 ////////////////////////////////////////////////////////////////////////////////
728
729 NetInternalsMessageHandler::ReadLogFileTask::ReadLogFileTask(
730 IOThreadImpl* proxy, const FilePath& path)
731 : proxy_(proxy), path_(path) {
732 }
733
734 void NetInternalsMessageHandler::ReadLogFileTask::Run() {
735 std::string file_contents;
736 if (!file_util::ReadFileToString(path_, &file_contents))
737 return;
738 proxy_->CallJavascriptFunction(L"g_browser.loadedLogFile",
739 new StringValue(file_contents));
740 }
741
742 #ifdef OS_CHROMEOS 658 #ifdef OS_CHROMEOS
743 //////////////////////////////////////////////////////////////////////////////// 659 ////////////////////////////////////////////////////////////////////////////////
744 // 660 //
745 // NetInternalsMessageHandler::SystemLogsGetter 661 // NetInternalsMessageHandler::SystemLogsGetter
746 // 662 //
747 //////////////////////////////////////////////////////////////////////////////// 663 ////////////////////////////////////////////////////////////////////////////////
748 664
749 NetInternalsMessageHandler::SystemLogsGetter::SystemLogsGetter( 665 NetInternalsMessageHandler::SystemLogsGetter::SystemLogsGetter(
750 NetInternalsMessageHandler* handler, 666 NetInternalsMessageHandler* handler,
751 chromeos::SystemAccess* system_access) 667 chromeos::SystemAccess* system_access)
(...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after
1630 // NetInternalsUI 1546 // NetInternalsUI
1631 // 1547 //
1632 //////////////////////////////////////////////////////////////////////////////// 1548 ////////////////////////////////////////////////////////////////////////////////
1633 1549
1634 NetInternalsUI::NetInternalsUI(TabContents* contents) : ChromeWebUI(contents) { 1550 NetInternalsUI::NetInternalsUI(TabContents* contents) : ChromeWebUI(contents) {
1635 AddMessageHandler((new NetInternalsMessageHandler())->Attach(this)); 1551 AddMessageHandler((new NetInternalsMessageHandler())->Attach(this));
1636 1552
1637 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource(); 1553 NetInternalsHTMLSource* html_source = new NetInternalsHTMLSource();
1638 1554
1639 // Set up the chrome://net-internals/ source. 1555 // Set up the chrome://net-internals/ source.
1640 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); 1556 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source);
1641 } 1557 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/net_internals/main.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698