Chromium Code Reviews| Index: chrome/browser/chromeos/system_logs/debugd_log_source.cc |
| diff --git a/chrome/browser/chromeos/system_logs/debugd_log_source.cc b/chrome/browser/chromeos/system_logs/debugd_log_source.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3b104254a4fed017e7f73ef7fac209d8480392bd |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/system_logs/debugd_log_source.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/system_logs/debugd_log_source.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/string_util.h" |
| +#include "chrome/browser/chromeos/system_logs/system_logs_fetcher.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/debug_daemon_client.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace { |
| + |
| +const char kNotAvailable[] = "<not available>"; |
| + |
| +} // namespace |
| + |
| +namespace chromeos { |
| + |
| +// Fetches logs from the debug daemon over DBus. When all the logs have been |
| +// fetched, forwards the results to the supplied Request. Used like: |
| +// DebugDaemonLogSource* fetcher = new DebugDaemonLogSource(request); |
| +// fetcher->Fetch(); |
| +// Note that you do not need to delete the fetcher; it will delete itself after |
| +// Fetch() has forwarded the result to the request handler. |
| + |
| + |
| +DebugDaemonLogSource::DebugDaemonLogSource() |
| + : response_(new SystemLogsResponse()), |
| + num_pending_requests_(0), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| + |
| +DebugDaemonLogSource::~DebugDaemonLogSource() {} |
| + |
| +void DebugDaemonLogSource::Fetch(const SysLogsFetcherCallback& callback) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + DCHECK(!callback.is_null()); |
|
satorux1
2012/08/16 10:50:57
might also want to add
DCHECK(callback_.is_null()
tudalex(Chromium)
2012/08/16 22:33:03
Done.
|
| + |
| + callback_ = callback; |
| + DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); |
| + |
| + client->GetRoutes(true, // Numeric |
| + false, // No IPv6 |
| + base::Bind(&DebugDaemonLogSource::OnGetRoutes, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + ++num_pending_requests_; |
| + client->GetNetworkStatus(base::Bind(&DebugDaemonLogSource::OnGetNetworkStatus, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + ++num_pending_requests_; |
| + client->GetModemStatus(base::Bind(&DebugDaemonLogSource::OnGetModemStatus, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + ++num_pending_requests_; |
| + client->GetAllLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + ++num_pending_requests_; |
| +} |
| + |
| +void DebugDaemonLogSource::OnGetRoutes(bool succeeded, |
| + const std::vector<std::string>& routes) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + if (succeeded) |
| + (*response_)["routes"] = JoinString(routes, '\n'); |
| + else |
| + (*response_)["routes"] = kNotAvailable; |
| + RequestCompleted(); |
| +} |
| + |
| +void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded, |
| + const std::string& status) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + if (succeeded) |
| + (*response_)["network-status"] = status; |
| + else |
| + (*response_)["network-status"] = kNotAvailable; |
| + RequestCompleted(); |
| +} |
| + |
| +void DebugDaemonLogSource::OnGetModemStatus(bool succeeded, |
| + const std::string& status) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + if (succeeded) |
| + (*response_)["modem-status"] = status; |
| + else |
| + (*response_)["modem-status"] = kNotAvailable; |
| + RequestCompleted(); |
| +} |
| + |
| +void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */, |
| + const std::map<std::string, |
| + std::string>& logs) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + // We ignore 'succeeded' for this callback - we want to display as much of the |
| + // debug info as we can even if we failed partway through parsing, and if we |
| + // couldn't fetch any of it, none of the fields will even appear. |
| + for (std::map<std::string, std::string>::const_iterator it = logs.begin(); |
| + it != logs.end(); ++it) |
| + (*response_)[it->first] = it->second; |
| + RequestCompleted(); |
| +} |
| + |
| +void DebugDaemonLogSource::RequestCompleted() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + DCHECK(!callback_.is_null()); |
| + |
| + --num_pending_requests_; |
| + if (num_pending_requests_ > 0) |
| + return; |
| + callback_.Run(response_.get()); |
| +} |
| + |
| +} // namespace chromeos |