Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "chrome/browser/chromeos/system_logs/debug_daemon_log_source.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "chrome/browser/chromeos/system_logs/system_logs_fetcher.h" | |
| 14 #include "chrome/common/chrome_switches.h" | |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 16 #include "chromeos/dbus/debug_daemon_client.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const char kNotAvailable[] = "<not available>"; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace chromeos { | |
| 26 | |
| 27 // Fetches logs from the debug daemon over DBus. When all the logs have been | |
| 28 // fetched, forwards the results to the supplied Request. Used like: | |
| 29 // DebugDaemonLogSource* fetcher = new DebugDaemonLogSource(request); | |
| 30 // fetcher->Fetch(); | |
| 31 // Note that you do not need to delete the fetcher; it will delete itself after | |
| 32 // Fetch() has forwarded the result to the request handler. | |
| 33 | |
| 34 | |
| 35 DebugDaemonLogSource::DebugDaemonLogSource() | |
| 36 : response_(new SystemLogsResponse()), | |
| 37 num_pending_requests_(0), | |
| 38 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} | |
| 39 | |
| 40 DebugDaemonLogSource::~DebugDaemonLogSource() {} | |
| 41 | |
| 42 void DebugDaemonLogSource::Fetch(const SysLogsSourceCallback& callback) { | |
| 43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 44 DCHECK(!callback.is_null()); | |
| 45 DCHECK(callback_.is_null()); | |
| 46 | |
| 47 callback_ = callback; | |
| 48 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); | |
| 49 | |
| 50 client->GetRoutes(true, // Numeric | |
| 51 false, // No IPv6 | |
| 52 base::Bind(&DebugDaemonLogSource::OnGetRoutes, | |
| 53 weak_ptr_factory_.GetWeakPtr())); | |
| 54 ++num_pending_requests_; | |
| 55 client->GetNetworkStatus(base::Bind(&DebugDaemonLogSource::OnGetNetworkStatus, | |
| 56 weak_ptr_factory_.GetWeakPtr())); | |
| 57 ++num_pending_requests_; | |
| 58 client->GetModemStatus(base::Bind(&DebugDaemonLogSource::OnGetModemStatus, | |
| 59 weak_ptr_factory_.GetWeakPtr())); | |
| 60 ++num_pending_requests_; | |
| 61 client->GetAllLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs, | |
| 62 weak_ptr_factory_.GetWeakPtr())); | |
| 63 ++num_pending_requests_; | |
| 64 } | |
| 65 | |
| 66 void DebugDaemonLogSource::OnGetRoutes(bool succeeded, | |
| 67 const std::vector<std::string>& routes) { | |
| 68 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 69 | |
| 70 if (succeeded) | |
| 71 (*response_)["routes"] = JoinString(routes, '\n'); | |
|
Lei Zhang
2012/08/21 19:56:55
nit: make these constants? same with {network,mode
tudalex(Chromium)
2012/08/21 21:21:12
Done.
| |
| 72 else | |
| 73 (*response_)["routes"] = kNotAvailable; | |
| 74 RequestCompleted(); | |
| 75 } | |
| 76 | |
| 77 void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded, | |
| 78 const std::string& status) { | |
| 79 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 80 | |
| 81 if (succeeded) | |
| 82 (*response_)["network-status"] = status; | |
| 83 else | |
| 84 (*response_)["network-status"] = kNotAvailable; | |
| 85 RequestCompleted(); | |
| 86 } | |
| 87 | |
| 88 void DebugDaemonLogSource::OnGetModemStatus(bool succeeded, | |
| 89 const std::string& status) { | |
| 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 91 | |
| 92 if (succeeded) | |
| 93 (*response_)["modem-status"] = status; | |
| 94 else | |
| 95 (*response_)["modem-status"] = kNotAvailable; | |
| 96 RequestCompleted(); | |
| 97 } | |
| 98 | |
| 99 void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */, | |
| 100 const std::map<std::string, | |
| 101 std::string>& logs) { | |
| 102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 103 | |
| 104 // We ignore 'succeeded' for this callback - we want to display as much of the | |
| 105 // debug info as we can even if we failed partway through parsing, and if we | |
| 106 // couldn't fetch any of it, none of the fields will even appear. | |
| 107 response_->insert(logs.begin(), logs.end()); | |
| 108 RequestCompleted(); | |
| 109 } | |
| 110 | |
| 111 void DebugDaemonLogSource::RequestCompleted() { | |
| 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 113 DCHECK(!callback_.is_null()); | |
| 114 | |
| 115 --num_pending_requests_; | |
| 116 if (num_pending_requests_ > 0) | |
| 117 return; | |
| 118 callback_.Run(response_.get()); | |
| 119 } | |
| 120 | |
| 121 } // namespace chromeos | |
| OLD | NEW |