Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: chrome/browser/chromeos/system/sysinfo_provider.cc

Issue 10827130: Refactoring the SysInfoProvider. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/sysinfo_provider.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/string_util.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/threading/thread.h"
18 #include "chrome/browser/memory_details.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chromeos/dbus/dbus_thread_manager.h"
21 #include "chromeos/dbus/debug_daemon_client.h"
22 #include "content/public/browser/browser_thread.h"
23
24 using content::BrowserThread;
25
26 namespace chromeos {
27 namespace system {
28
29 // Fetches logs from the debug daemon over DBus. When all the logs have been
30 // fetched, forwards the results to the supplied Request. Used like:
31 // DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request);
32 // fetcher->Fetch();
33 // Note that you do not need to delete the fetcher; it will delete itself after
34 // Fetch() has forwarded the result to the request handler.
35 class DebugDaemonLogFetcher {
36 public:
37 typedef
38 scoped_refptr<CancelableRequest<SysInfoProvider::FetchCompleteCallback> >
39 Request;
40
41 explicit DebugDaemonLogFetcher(Request request);
42 ~DebugDaemonLogFetcher();
43
44 // Fetches logs from the daemon over dbus. After the fetch is complete, the
45 // results will be forwarded to the request supplied to the constructor and
46 // this instance will free itself.
47 void Fetch();
48 private:
49 // Callbacks
50 void GotRoutes(bool succeeded, const std::vector<std::string>& routes);
51 void GotNetworkStatus(bool succeeded, const std::string& status);
52 void GotModemStatus(bool succeeded, const std::string& status);
53 void GotLogs(bool succeeded, const std::map<std::string, std::string>& logs);
54 void RequestCompleted();
55
56 SysInfoResponse* response_;
57 Request request_;
58 int pending_requests_;
59 base::WeakPtrFactory<DebugDaemonLogFetcher> weak_ptr_factory_;
60 };
61
62 DebugDaemonLogFetcher::DebugDaemonLogFetcher(Request request)
63 : response_(new SysInfoResponse()), request_(request),
64 weak_ptr_factory_(this) { }
65
66 DebugDaemonLogFetcher::~DebugDaemonLogFetcher() { }
67
68 void DebugDaemonLogFetcher::Fetch() {
69 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient();
70 pending_requests_ = 4;
71 // Note that this use of base::Unretained(this) is safe, since |this| is
72 // deleted in RequestCompleted if and only if all outstanding callbacks are
73 // done.
74 client->GetRoutes(true, false,
75 base::Bind(&DebugDaemonLogFetcher::GotRoutes,
76 weak_ptr_factory_.GetWeakPtr()));
77 client->GetNetworkStatus(
78 base::Bind(&DebugDaemonLogFetcher::GotNetworkStatus,
79 weak_ptr_factory_.GetWeakPtr()));
80 client->GetModemStatus(
81 base::Bind(&DebugDaemonLogFetcher::GotModemStatus,
82 weak_ptr_factory_.GetWeakPtr()));
83 client->GetAllLogs(
84 base::Bind(&DebugDaemonLogFetcher::GotLogs,
85 weak_ptr_factory_.GetWeakPtr()));
86 }
87
88 const char *kNotAvailable = "<not available>";
89
90 void DebugDaemonLogFetcher::GotRoutes(bool succeeded,
91 const std::vector<std::string>& routes) {
92 if (succeeded)
93 (*response_)["routes"] = JoinString(routes, '\n');
94 else
95 (*response_)["routes"] = kNotAvailable;
96 RequestCompleted();
97 }
98
99 void DebugDaemonLogFetcher::GotNetworkStatus(bool succeeded,
100 const std::string& status) {
101 if (succeeded)
102 (*response_)["network-status"] = status;
103 else
104 (*response_)["network-status"] = kNotAvailable;
105 RequestCompleted();
106 }
107
108 void DebugDaemonLogFetcher::GotModemStatus(bool succeeded,
109 const std::string& status) {
110 if (succeeded)
111 (*response_)["modem-status"] = status;
112 else
113 (*response_)["modem-status"] = kNotAvailable;
114 RequestCompleted();
115 }
116
117 void DebugDaemonLogFetcher::GotLogs(bool /* succeeded */,
118 const std::map<std::string,
119 std::string>& logs) {
120 // We ignore 'succeeded' for this callback - we want to display as much of the
121 // debug info as we can even if we failed partway through parsing, and if we
122 // couldn't fetch any of it, none of the fields will even appear.
123 for (std::map<std::string, std::string>::const_iterator it = logs.begin();
124 it != logs.end(); ++it)
125 (*response_)[it->first] = it->second;
126 RequestCompleted();
127 }
128
129 void DebugDaemonLogFetcher::RequestCompleted() {
130 if (--pending_requests_)
131 return;
132 request_->ForwardResult(response_);
133 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
134 }
135
136 class SysInfoProviderImpl : public SysInfoProvider {
137 public:
138 virtual Handle Fetch(CancelableRequestConsumerBase* consumer,
139 const FetchCompleteCallback& callback);
140 };
141
142 CancelableRequestProvider::Handle SysInfoProviderImpl::Fetch(
143 CancelableRequestConsumerBase* consumer,
144 const FetchCompleteCallback& callback) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 scoped_refptr<CancelableRequest<FetchCompleteCallback> >
147 request(new CancelableRequest<FetchCompleteCallback>(callback));
148 AddRequest(request, consumer);
149 // Deleted by DebugDaemonLogFetcher::RequestCompleted()
150 DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request);
151 fetcher->Fetch();
152
153 return request->handle();
154 }
155
156 SysInfoProvider* SysInfoProvider::Create() {
157 return new SysInfoProviderImpl();
158 }
159
160 } // namespace system
161 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/system/sysinfo_provider.h ('k') | chrome/browser/chromeos/system_logs/command_line_log_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698