Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/system/sysinfo_provider.h" | 5 #include "chrome/browser/chromeos/system/sysinfo_provider.h" |
| 6 | 6 |
| 7 #include "vector" | |
|
rkc
2012/08/02 18:40:54
Already included in the header + should be "#inclu
| |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" | |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/synchronization/lock.h" | |
| 16 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "chrome/browser/chromeos/system/syslogs_fetcher.h" | |
| 18 #include "chrome/browser/memory_details.h" | 22 #include "chrome/browser/memory_details.h" |
| 19 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
| 21 #include "chromeos/dbus/debug_daemon_client.h" | 25 #include "chromeos/dbus/debug_daemon_client.h" |
| 22 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 23 | 27 |
| 28 | |
|
rkc
2012/08/02 18:40:54
Too many LF, need just 1 between the includes endi
| |
| 29 | |
| 30 | |
| 24 using content::BrowserThread; | 31 using content::BrowserThread; |
| 25 | 32 |
| 26 namespace chromeos { | 33 namespace chromeos { |
| 27 namespace system { | 34 namespace system { |
| 28 | 35 |
| 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 { | 36 class SysInfoProviderImpl : public SysInfoProvider { |
| 137 public: | 37 public: |
| 138 virtual Handle Fetch(CancelableRequestConsumerBase* consumer, | 38 virtual Handle Fetch(CancelableRequestConsumerBase* consumer, |
| 139 const FetchCompleteCallback& callback); | 39 const FetchCompleteCallback& callback); |
| 140 }; | 40 }; |
| 141 | 41 |
| 142 CancelableRequestProvider::Handle SysInfoProviderImpl::Fetch( | 42 CancelableRequestProvider::Handle SysInfoProviderImpl::Fetch( |
| 143 CancelableRequestConsumerBase* consumer, | 43 CancelableRequestConsumerBase* consumer, |
| 144 const FetchCompleteCallback& callback) { | 44 const FetchCompleteCallback& callback) { |
| 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 146 scoped_refptr<CancelableRequest<FetchCompleteCallback> > | 46 scoped_refptr<CancelableRequest<FetchCompleteCallback> > |
| 147 request(new CancelableRequest<FetchCompleteCallback>(callback)); | 47 request(new CancelableRequest<FetchCompleteCallback>(callback)); |
| 148 AddRequest(request, consumer); | 48 AddRequest(request, consumer); |
| 149 // Deleted by DebugDaemonLogFetcher::RequestCompleted() | |
| 150 DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request); | |
| 151 fetcher->Fetch(); | |
| 152 | 49 |
| 50 SysLogsFetcher * fetcher = SysLogsFetcherFactory::GetInstance()->GetFetcher(); | |
|
rkc
2012/08/02 18:40:54
When is this freed?
tudalex(Chromium)
2012/08/04 00:24:20
After it responds to the request it calls BrowserT
| |
| 51 fetcher->Fetch(request); | |
| 153 return request->handle(); | 52 return request->handle(); |
| 154 } | 53 } |
| 155 | 54 |
| 156 SysInfoProvider* SysInfoProvider::Create() { | 55 SysInfoProvider* SysInfoProvider::Create() { |
| 157 return new SysInfoProviderImpl(); | 56 return new SysInfoProviderImpl(); |
| 158 } | 57 } |
| 159 | 58 |
| 59 | |
| 160 } // namespace system | 60 } // namespace system |
| 161 } // namespace chromeos | 61 } // namespace chromeos |
| OLD | NEW |