This source file includes following definitions.
- weak_ptr_factory_
- Fetch
- OnGetRoutes
- OnGetNetworkStatus
- OnGetModemStatus
- OnGetWiMaxStatus
- OnGetLogs
- OnGetUserLogFiles
- ReadUserLogFiles
- MergeResponse
- RequestCompleted
#include "chrome/browser/chromeos/system_logs/debug_daemon_log_source.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/browser/profiles/profile_manager.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"
const char kNotAvailable[] = "<not available>";
const char kRoutesKeyName[] = "routes";
const char kNetworkStatusKeyName[] = "network-status";
const char kModemStatusKeyName[] = "modem-status";
const char kWiMaxStatusKeyName[] = "wimax-status";
const char kUserLogFileKeyName[] = "user_log_files";
namespace system_logs {
DebugDaemonLogSource::DebugDaemonLogSource(bool scrub)
: response_(new SystemLogsResponse()),
num_pending_requests_(0),
scrub_(scrub),
weak_ptr_factory_(this) {}
DebugDaemonLogSource::~DebugDaemonLogSource() {}
void DebugDaemonLogSource::Fetch(const SysLogsSourceCallback& callback) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(!callback.is_null());
DCHECK(callback_.is_null());
callback_ = callback;
chromeos::DebugDaemonClient* client =
chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
client->GetRoutes(true,
false,
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->GetWiMaxStatus(base::Bind(&DebugDaemonLogSource::OnGetWiMaxStatus,
weak_ptr_factory_.GetWeakPtr()));
++num_pending_requests_;
client->GetUserLogFiles(base::Bind(&DebugDaemonLogSource::OnGetUserLogFiles,
weak_ptr_factory_.GetWeakPtr()));
++num_pending_requests_;
if (scrub_) {
client->GetScrubbedLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs,
weak_ptr_factory_.GetWeakPtr()));
} else {
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_)[kRoutesKeyName] = JoinString(routes, '\n');
else
(*response_)[kRoutesKeyName] = kNotAvailable;
RequestCompleted();
}
void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded,
const std::string& status) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (succeeded)
(*response_)[kNetworkStatusKeyName] = status;
else
(*response_)[kNetworkStatusKeyName] = kNotAvailable;
RequestCompleted();
}
void DebugDaemonLogSource::OnGetModemStatus(bool succeeded,
const std::string& status) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (succeeded)
(*response_)[kModemStatusKeyName] = status;
else
(*response_)[kModemStatusKeyName] = kNotAvailable;
RequestCompleted();
}
void DebugDaemonLogSource::OnGetWiMaxStatus(bool succeeded,
const std::string& status) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (succeeded)
(*response_)[kWiMaxStatusKeyName] = status;
else
(*response_)[kWiMaxStatusKeyName] = kNotAvailable;
RequestCompleted();
}
void DebugDaemonLogSource::OnGetLogs(bool ,
const KeyValueMap& logs) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
response_->insert(logs.begin(), logs.end());
RequestCompleted();
}
void DebugDaemonLogSource::OnGetUserLogFiles(
bool succeeded,
const KeyValueMap& user_log_files) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (succeeded) {
SystemLogsResponse* response = new SystemLogsResponse;
std::vector<Profile*> last_used = ProfileManager::GetLastOpenedProfiles();
if (last_used.empty() &&
chromeos::UserManager::IsInitialized() &&
chromeos::UserManager::Get()->IsLoggedInAsKioskApp()) {
last_used.push_back(ProfileManager::GetActiveUserProfile());
}
content::BrowserThread::PostBlockingPoolTaskAndReply(
FROM_HERE,
base::Bind(&DebugDaemonLogSource::ReadUserLogFiles,
user_log_files, last_used, response),
base::Bind(&DebugDaemonLogSource::MergeResponse,
weak_ptr_factory_.GetWeakPtr(),
base::Owned(response)));
} else {
(*response_)[kUserLogFileKeyName] = kNotAvailable;
RequestCompleted();
}
}
void DebugDaemonLogSource::ReadUserLogFiles(
const KeyValueMap& user_log_files,
const std::vector<Profile*>& last_used_profiles,
SystemLogsResponse* response) {
for (size_t i = 0; i < last_used_profiles.size(); ++i) {
std::string profile_prefix = "Profile[" + base::UintToString(i) + "] ";
for (KeyValueMap::const_iterator it = user_log_files.begin();
it != user_log_files.end();
++it) {
std::string key = it->first;
std::string value;
std::string filename = it->second;
base::FilePath profile_dir = last_used_profiles[i]->GetPath();
bool read_success = base::ReadFileToString(
profile_dir.Append(filename), &value);
if (read_success && !value.empty())
(*response)[profile_prefix + key] = value;
else
(*response)[profile_prefix + filename] = kNotAvailable;
}
}
}
void DebugDaemonLogSource::MergeResponse(SystemLogsResponse* response) {
for (SystemLogsResponse::const_iterator it = response->begin();
it != response->end(); ++it)
response_->insert(*it);
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());
}
}