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/debugd_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 SysLogsFetcherCallback& callback) { | |
| 43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 44 | |
| 45 callback_ = callback; | |
| 46 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); | |
| 47 | |
| 48 client->GetRoutes(true, // Numeric | |
| 49 false, // No IPv6 | |
| 50 base::Bind(&DebugDaemonLogSource::OnGetRoutes, | |
| 51 weak_ptr_factory_.GetWeakPtr())); | |
| 52 ++num_pending_requests_; | |
| 53 client->GetNetworkStatus(base::Bind(&DebugDaemonLogSource::OnGetNetworkStatus, | |
| 54 weak_ptr_factory_.GetWeakPtr())); | |
| 55 ++num_pending_requests_; | |
| 56 client->GetModemStatus(base::Bind(&DebugDaemonLogSource::OnGetModemStatus, | |
| 57 weak_ptr_factory_.GetWeakPtr())); | |
| 58 ++num_pending_requests_; | |
| 59 client->GetAllLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs, | |
| 60 weak_ptr_factory_.GetWeakPtr())); | |
| 61 ++num_pending_requests_; | |
| 62 } | |
| 63 | |
| 64 void DebugDaemonLogSource::OnGetRoutes(bool succeeded, | |
| 65 const std::vector<std::string>& routes) { | |
| 66 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 67 | |
| 68 if (succeeded) | |
| 69 (*response_)["routes"] = JoinString(routes, '\n'); | |
| 70 else | |
| 71 (*response_)["routes"] = kNotAvailable; | |
| 72 RequestCompleted(); | |
| 73 } | |
| 74 | |
| 75 void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded, | |
| 76 const std::string& status) { | |
| 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 78 | |
| 79 if (succeeded) | |
| 80 (*response_)["network-status"] = status; | |
| 81 else | |
| 82 (*response_)["network-status"] = kNotAvailable; | |
| 83 RequestCompleted(); | |
| 84 } | |
| 85 | |
| 86 void DebugDaemonLogSource::OnGetModemStatus(bool succeeded, | |
| 87 const std::string& status) { | |
| 88 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 89 | |
| 90 if (succeeded) | |
| 91 (*response_)["modem-status"] = status; | |
| 92 else | |
| 93 (*response_)["modem-status"] = kNotAvailable; | |
| 94 RequestCompleted(); | |
| 95 } | |
| 96 | |
| 97 void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */, | |
| 98 const std::map<std::string, | |
| 99 std::string>& logs) { | |
| 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 101 | |
| 102 // We ignore 'succeeded' for this callback - we want to display as much of the | |
| 103 // debug info as we can even if we failed partway through parsing, and if we | |
| 104 // couldn't fetch any of it, none of the fields will even appear. | |
| 105 for (std::map<std::string, std::string>::const_iterator it = logs.begin(); | |
| 106 it != logs.end(); ++it) | |
| 107 (*response_)[it->first] = it->second; | |
| 108 RequestCompleted(); | |
| 109 } | |
| 110 | |
| 111 void DebugDaemonLogSource::RequestCompleted() { | |
| 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
satorux1
2012/08/15 22:01:09
maybe add DCHECK(!callback_.is_null()) followed by
tudalex(Chromium)
2012/08/16 01:08:57
Done.
| |
| 113 --num_pending_requests_; | |
| 114 if (num_pending_requests_ > 0) | |
| 115 return; | |
| 116 callback_.Run(response_.get()); | |
| 117 } | |
| 118 | |
| 119 } // namespace chromeos | |
| OLD | NEW |