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.
5 #include "chrome/browser/chromeos/system_logs/debug_daemon_log_source.h"
8 #include "base/bind_helpers.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "chromeos/dbus/debug_daemon_client.h"
19 #include "content/public/browser/browser_thread.h"
21 const char kNotAvailable[] = "<not available>";
22 const char kRoutesKeyName[] = "routes";
23 const char kNetworkStatusKeyName[] = "network-status";
24 const char kModemStatusKeyName[] = "modem-status";
25 const char kWiMaxStatusKeyName[] = "wimax-status";
26 const char kUserLogFileKeyName[] = "user_log_files";
30 DebugDaemonLogSource::DebugDaemonLogSource(bool scrub)
31 : response_(new SystemLogsResponse()),
32 num_pending_requests_(0),
34 weak_ptr_factory_(this) {}
36 DebugDaemonLogSource::~DebugDaemonLogSource() {}
38 void DebugDaemonLogSource::Fetch(const SysLogsSourceCallback& callback) {
39 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
40 DCHECK(!callback.is_null());
41 DCHECK(callback_.is_null());
44 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient();
46 client->GetRoutes(true, // Numeric
48 base::Bind(&DebugDaemonLogSource::OnGetRoutes,
49 weak_ptr_factory_.GetWeakPtr()));
50 ++num_pending_requests_;
51 client->GetNetworkStatus(base::Bind(&DebugDaemonLogSource::OnGetNetworkStatus,
52 weak_ptr_factory_.GetWeakPtr()));
53 ++num_pending_requests_;
54 client->GetModemStatus(base::Bind(&DebugDaemonLogSource::OnGetModemStatus,
55 weak_ptr_factory_.GetWeakPtr()));
56 ++num_pending_requests_;
57 client->GetWiMaxStatus(base::Bind(&DebugDaemonLogSource::OnGetWiMaxStatus,
58 weak_ptr_factory_.GetWeakPtr()));
59 ++num_pending_requests_;
60 client->GetUserLogFiles(base::Bind(&DebugDaemonLogSource::OnGetUserLogFiles,
61 weak_ptr_factory_.GetWeakPtr()));
62 ++num_pending_requests_;
65 client->GetScrubbedLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs,
66 weak_ptr_factory_.GetWeakPtr()));
68 client->GetAllLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs,
69 weak_ptr_factory_.GetWeakPtr()));
71 ++num_pending_requests_;
74 void DebugDaemonLogSource::OnGetRoutes(bool succeeded,
75 const std::vector<std::string>& routes) {
76 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
79 (*response_)[kRoutesKeyName] = JoinString(routes, '\n');
81 (*response_)[kRoutesKeyName] = kNotAvailable;
85 void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded,
86 const std::string& status) {
87 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
90 (*response_)[kNetworkStatusKeyName] = status;
92 (*response_)[kNetworkStatusKeyName] = kNotAvailable;
96 void DebugDaemonLogSource::OnGetModemStatus(bool succeeded,
97 const std::string& status) {
98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
101 (*response_)[kModemStatusKeyName] = status;
103 (*response_)[kModemStatusKeyName] = kNotAvailable;
107 void DebugDaemonLogSource::OnGetWiMaxStatus(bool succeeded,
108 const std::string& status) {
109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
112 (*response_)[kWiMaxStatusKeyName] = status;
114 (*response_)[kWiMaxStatusKeyName] = kNotAvailable;
118 void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */,
119 const KeyValueMap& logs) {
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
122 // We ignore 'succeeded' for this callback - we want to display as much of the
123 // debug info as we can even if we failed partway through parsing, and if we
124 // couldn't fetch any of it, none of the fields will even appear.
125 response_->insert(logs.begin(), logs.end());
129 void DebugDaemonLogSource::OnGetUserLogFiles(
131 const KeyValueMap& user_log_files) {
132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
134 SystemLogsResponse* response = new SystemLogsResponse;
135 std::vector<Profile*> last_used = ProfileManager::GetLastOpenedProfiles();
136 content::BrowserThread::PostBlockingPoolTaskAndReply(
138 base::Bind(&DebugDaemonLogSource::ReadUserLogFiles,
139 user_log_files, last_used, response),
140 base::Bind(&DebugDaemonLogSource::MergeResponse,
141 weak_ptr_factory_.GetWeakPtr(),
142 base::Owned(response)));
144 (*response_)[kUserLogFileKeyName] = kNotAvailable;
150 void DebugDaemonLogSource::ReadUserLogFiles(
151 const KeyValueMap& user_log_files,
152 const std::vector<Profile*>& last_used_profiles,
153 SystemLogsResponse* response) {
154 for (size_t i = 0; i < last_used_profiles.size(); ++i) {
155 std::string profile_prefix = "Profile[" + base::UintToString(i) + "] ";
156 for (KeyValueMap::const_iterator it = user_log_files.begin();
157 it != user_log_files.end();
159 std::string key = it->first;
161 std::string filename = it->second;
162 base::FilePath profile_dir = last_used_profiles[i]->GetPath();
163 bool read_success = base::ReadFileToString(
164 profile_dir.Append(filename), &value);
166 if (read_success && !value.empty())
167 (*response)[profile_prefix + key] = value;
169 (*response)[profile_prefix + filename] = kNotAvailable;
174 void DebugDaemonLogSource::MergeResponse(SystemLogsResponse* response) {
175 for (SystemLogsResponse::const_iterator it = response->begin();
176 it != response->end(); ++it)
177 response_->insert(*it);
181 void DebugDaemonLogSource::RequestCompleted() {
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
183 DCHECK(!callback_.is_null());
185 --num_pending_requests_;
186 if (num_pending_requests_ > 0)
188 callback_.Run(response_.get());
191 } // namespace chromeos