- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / system_logs / debug_daemon_log_source.cc
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/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"
20
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";
27
28 namespace chromeos {
29
30 DebugDaemonLogSource::DebugDaemonLogSource(bool scrub)
31     : response_(new SystemLogsResponse()),
32       num_pending_requests_(0),
33       scrub_(scrub),
34       weak_ptr_factory_(this) {}
35
36 DebugDaemonLogSource::~DebugDaemonLogSource() {}
37
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());
42
43   callback_ = callback;
44   DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient();
45
46   client->GetRoutes(true,   // Numeric
47                     false,  // No IPv6
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_;
63
64   if (scrub_) {
65     client->GetScrubbedLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs,
66                                        weak_ptr_factory_.GetWeakPtr()));
67   } else {
68     client->GetAllLogs(base::Bind(&DebugDaemonLogSource::OnGetLogs,
69                                   weak_ptr_factory_.GetWeakPtr()));
70   }
71   ++num_pending_requests_;
72 }
73
74 void DebugDaemonLogSource::OnGetRoutes(bool succeeded,
75                                        const std::vector<std::string>& routes) {
76   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
77
78   if (succeeded)
79     (*response_)[kRoutesKeyName] = JoinString(routes, '\n');
80   else
81     (*response_)[kRoutesKeyName] = kNotAvailable;
82   RequestCompleted();
83 }
84
85 void DebugDaemonLogSource::OnGetNetworkStatus(bool succeeded,
86                                               const std::string& status) {
87   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
88
89   if (succeeded)
90     (*response_)[kNetworkStatusKeyName] = status;
91   else
92     (*response_)[kNetworkStatusKeyName] = kNotAvailable;
93   RequestCompleted();
94 }
95
96 void DebugDaemonLogSource::OnGetModemStatus(bool succeeded,
97                                             const std::string& status) {
98   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
99
100   if (succeeded)
101     (*response_)[kModemStatusKeyName] = status;
102   else
103     (*response_)[kModemStatusKeyName] = kNotAvailable;
104   RequestCompleted();
105 }
106
107 void DebugDaemonLogSource::OnGetWiMaxStatus(bool succeeded,
108                                             const std::string& status) {
109   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
110
111   if (succeeded)
112     (*response_)[kWiMaxStatusKeyName] = status;
113   else
114     (*response_)[kWiMaxStatusKeyName] = kNotAvailable;
115   RequestCompleted();
116 }
117
118 void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */,
119                                      const KeyValueMap& logs) {
120   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
121
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());
126   RequestCompleted();
127 }
128
129 void DebugDaemonLogSource::OnGetUserLogFiles(
130     bool succeeded,
131     const KeyValueMap& user_log_files) {
132   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133   if (succeeded) {
134     SystemLogsResponse* response = new SystemLogsResponse;
135     std::vector<Profile*> last_used = ProfileManager::GetLastOpenedProfiles();
136     content::BrowserThread::PostBlockingPoolTaskAndReply(
137         FROM_HERE,
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)));
143   } else {
144     (*response_)[kUserLogFileKeyName] = kNotAvailable;
145     RequestCompleted();
146   }
147 }
148
149 // static
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();
158          ++it) {
159       std::string key = it->first;
160       std::string value;
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);
165
166       if (read_success && !value.empty())
167         (*response)[profile_prefix + key] = value;
168       else
169         (*response)[profile_prefix + filename] = kNotAvailable;
170     }
171   }
172 }
173
174 void DebugDaemonLogSource::MergeResponse(SystemLogsResponse* response) {
175   for (SystemLogsResponse::const_iterator it = response->begin();
176        it != response->end(); ++it)
177     response_->insert(*it);
178   RequestCompleted();
179 }
180
181 void DebugDaemonLogSource::RequestCompleted() {
182   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
183   DCHECK(!callback_.is_null());
184
185   --num_pending_requests_;
186   if (num_pending_requests_ > 0)
187     return;
188   callback_.Run(response_.get());
189 }
190
191 }  // namespace chromeos