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/syslogs/debugd_log_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.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" | 9 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 15 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 16 #include "base/synchronization/waitable_event.h" | 13 #include "chrome/browser/chromeos/syslogs/syslogs_fetcher.h" |
| 17 #include "base/threading/thread.h" | |
| 18 #include "chrome/browser/memory_details.h" | |
| 19 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 21 #include "chromeos/dbus/debug_daemon_client.h" | 16 #include "chromeos/dbus/debug_daemon_client.h" |
| 22 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 23 | 18 |
| 24 using content::BrowserThread; | |
| 25 | |
| 26 namespace chromeos { | 19 namespace chromeos { |
| 27 namespace system { | |
| 28 | 20 |
| 29 // Fetches logs from the debug daemon over DBus. When all the logs have been | 21 // 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: | 22 // fetched, forwards the results to the supplied Request. Used like: |
| 31 // DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request); | 23 // DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request); |
| 32 // fetcher->Fetch(); | 24 // fetcher->Fetch(); |
| 33 // Note that you do not need to delete the fetcher; it will delete itself after | 25 // 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. | 26 // 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 | 27 |
| 41 explicit DebugDaemonLogFetcher(Request request); | |
| 42 ~DebugDaemonLogFetcher(); | |
| 43 | 28 |
| 44 // Fetches logs from the daemon over dbus. After the fetch is complete, the | 29 DebugDaemonLogFetcher::DebugDaemonLogFetcher() |
| 45 // results will be forwarded to the request supplied to the constructor and | 30 : response_(new SysLogsResponse()), |
| 46 // this instance will free itself. | 31 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| 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 | 32 |
| 56 SysInfoResponse* response_; | 33 DebugDaemonLogFetcher::~DebugDaemonLogFetcher() {} |
| 57 Request request_; | |
| 58 int pending_requests_; | |
| 59 base::WeakPtrFactory<DebugDaemonLogFetcher> weak_ptr_factory_; | |
| 60 }; | |
| 61 | 34 |
| 62 DebugDaemonLogFetcher::DebugDaemonLogFetcher(Request request) | 35 void DebugDaemonLogFetcher::Fetch(const SysLogsFetcherCallback& request) { |
| 63 : response_(new SysInfoResponse()), request_(request), | 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 64 weak_ptr_factory_(this) { } | |
| 65 | 37 |
| 66 DebugDaemonLogFetcher::~DebugDaemonLogFetcher() { } | 38 request_ = request; |
| 67 | |
| 68 void DebugDaemonLogFetcher::Fetch() { | |
| 69 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); | 39 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 70 pending_requests_ = 4; | 40 pending_requests_ = 4; |
| 71 // Note that this use of base::Unretained(this) is safe, since |this| is | 41 |
| 72 // deleted in RequestCompleted if and only if all outstanding callbacks are | |
| 73 // done. | |
| 74 client->GetRoutes(true, false, | 42 client->GetRoutes(true, false, |
|
satorux1
2012/08/09 21:15:07
nit: when parameters cannot fit in one line, it's
tudalex(Chromium)
2012/08/10 00:30:31
Done.
| |
| 75 base::Bind(&DebugDaemonLogFetcher::GotRoutes, | 43 base::Bind(&DebugDaemonLogFetcher::GotRoutes, |
| 76 weak_ptr_factory_.GetWeakPtr())); | 44 weak_ptr_factory_.GetWeakPtr())); |
| 77 client->GetNetworkStatus( | 45 client->GetNetworkStatus( |
| 78 base::Bind(&DebugDaemonLogFetcher::GotNetworkStatus, | 46 base::Bind(&DebugDaemonLogFetcher::GotNetworkStatus, |
| 79 weak_ptr_factory_.GetWeakPtr())); | 47 weak_ptr_factory_.GetWeakPtr())); |
| 80 client->GetModemStatus( | 48 client->GetModemStatus( |
| 81 base::Bind(&DebugDaemonLogFetcher::GotModemStatus, | 49 base::Bind(&DebugDaemonLogFetcher::GotModemStatus, |
| 82 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
| 83 client->GetAllLogs( | 51 client->GetAllLogs( |
| 84 base::Bind(&DebugDaemonLogFetcher::GotLogs, | 52 base::Bind(&DebugDaemonLogFetcher::GotLogs, |
| 85 weak_ptr_factory_.GetWeakPtr())); | 53 weak_ptr_factory_.GetWeakPtr())); |
| 86 } | 54 } |
| 87 | 55 |
| 88 const char *kNotAvailable = "<not available>"; | 56 const char* kNotAvailable = "<not available>"; |
|
satorux1
2012/08/09 21:15:07
please put this in anonymous namespace. also pleas
tudalex(Chromium)
2012/08/10 00:30:31
Done.
| |
| 89 | 57 |
| 90 void DebugDaemonLogFetcher::GotRoutes(bool succeeded, | 58 void DebugDaemonLogFetcher::GotRoutes(bool succeeded, |
| 91 const std::vector<std::string>& routes) { | 59 const std::vector<std::string>& routes) { |
| 60 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
satorux1
2012/08/09 21:15:07
nit: add a blank line after DCHECK. Please fix els
tudalex(Chromium)
2012/08/10 00:30:31
Done.
| |
| 92 if (succeeded) | 61 if (succeeded) |
| 93 (*response_)["routes"] = JoinString(routes, '\n'); | 62 (*response_)["routes"] = JoinString(routes, '\n'); |
| 94 else | 63 else |
| 95 (*response_)["routes"] = kNotAvailable; | 64 (*response_)["routes"] = kNotAvailable; |
| 96 RequestCompleted(); | 65 RequestCompleted(); |
| 97 } | 66 } |
| 98 | 67 |
| 99 void DebugDaemonLogFetcher::GotNetworkStatus(bool succeeded, | 68 void DebugDaemonLogFetcher::GotNetworkStatus(bool succeeded, |
| 100 const std::string& status) { | 69 const std::string& status) { |
| 70 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 101 if (succeeded) | 71 if (succeeded) |
| 102 (*response_)["network-status"] = status; | 72 (*response_)["network-status"] = status; |
| 103 else | 73 else |
| 104 (*response_)["network-status"] = kNotAvailable; | 74 (*response_)["network-status"] = kNotAvailable; |
| 105 RequestCompleted(); | 75 RequestCompleted(); |
| 106 } | 76 } |
| 107 | 77 |
| 108 void DebugDaemonLogFetcher::GotModemStatus(bool succeeded, | 78 void DebugDaemonLogFetcher::GotModemStatus(bool succeeded, |
| 109 const std::string& status) { | 79 const std::string& status) { |
| 80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 110 if (succeeded) | 81 if (succeeded) |
| 111 (*response_)["modem-status"] = status; | 82 (*response_)["modem-status"] = status; |
| 112 else | 83 else |
| 113 (*response_)["modem-status"] = kNotAvailable; | 84 (*response_)["modem-status"] = kNotAvailable; |
| 114 RequestCompleted(); | 85 RequestCompleted(); |
| 115 } | 86 } |
| 116 | 87 |
| 117 void DebugDaemonLogFetcher::GotLogs(bool /* succeeded */, | 88 void DebugDaemonLogFetcher::GotLogs(bool /* succeeded */, |
| 118 const std::map<std::string, | 89 const std::map<std::string, |
| 119 std::string>& logs) { | 90 std::string>& logs) { |
| 120 // We ignore 'succeeded' for this callback - we want to display as much of the | 91 // 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 | 92 // 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. | 93 // couldn't fetch any of it, none of the fields will even appear. |
| 94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
satorux1
2012/08/09 21:15:07
move this to the beginning of the function followe
tudalex(Chromium)
2012/08/10 00:30:31
Done.
| |
| 123 for (std::map<std::string, std::string>::const_iterator it = logs.begin(); | 95 for (std::map<std::string, std::string>::const_iterator it = logs.begin(); |
| 124 it != logs.end(); ++it) | 96 it != logs.end(); ++it) |
| 125 (*response_)[it->first] = it->second; | 97 (*response_)[it->first] = it->second; |
| 126 RequestCompleted(); | 98 RequestCompleted(); |
| 127 } | 99 } |
| 128 | 100 |
| 129 void DebugDaemonLogFetcher::RequestCompleted() { | 101 void DebugDaemonLogFetcher::RequestCompleted() { |
| 102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 130 if (--pending_requests_) | 103 if (--pending_requests_) |
| 131 return; | 104 return; |
| 132 request_->ForwardResult(response_); | 105 request_.Run(response_); |
| 133 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | 106 |
| 134 } | 107 } |
| 135 | 108 |
| 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 | 109 } // namespace chromeos |
| OLD | NEW |