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/debugd_log_fetcher.h" |
| 6 | 6 |
| 7 #include "vector" | |
| 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" |
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 16 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "chrome/browser/chromeos/system/syslogs_fetcher.h" | |
| 18 #include "chrome/browser/memory_details.h" | 20 #include "chrome/browser/memory_details.h" |
| 19 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 21 #include "chromeos/dbus/debug_daemon_client.h" | 23 #include "chromeos/dbus/debug_daemon_client.h" |
| 22 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 23 | 25 |
| 26 | |
| 27 | |
| 24 using content::BrowserThread; | 28 using content::BrowserThread; |
| 25 | 29 |
| 26 namespace chromeos { | 30 namespace chromeos { |
| 27 namespace system { | 31 namespace system { |
| 28 | 32 |
| 29 // Fetches logs from the debug daemon over DBus. When all the logs have been | 33 // 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: | 34 // fetched, forwards the results to the supplied Request. Used like: |
| 31 // DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request); | 35 // DebugDaemonLogFetcher* fetcher = new DebugDaemonLogFetcher(request); |
| 32 // fetcher->Fetch(); | 36 // fetcher->Fetch(); |
| 33 // Note that you do not need to delete the fetcher; it will delete itself after | 37 // 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. | 38 // 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 | 39 |
| 41 explicit DebugDaemonLogFetcher(Request request); | |
| 42 ~DebugDaemonLogFetcher(); | |
| 43 | 40 |
| 44 // Fetches logs from the daemon over dbus. After the fetch is complete, the | 41 DebugDaemonLogFetcher::DebugDaemonLogFetcher() |
| 45 // results will be forwarded to the request supplied to the constructor and | 42 : response_(new SysInfoResponse()), weak_ptr_factory_(this) { } |
|
rkc1
2012/08/04 01:15:05
{}
(check other places also in the CL)
tudalex(Chromium)
2012/08/05 01:15:12
Done.
| |
| 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 | 43 |
| 66 DebugDaemonLogFetcher::~DebugDaemonLogFetcher() { } | 44 DebugDaemonLogFetcher::~DebugDaemonLogFetcher() { } |
| 67 | 45 |
| 68 void DebugDaemonLogFetcher::Fetch() { | 46 void DebugDaemonLogFetcher::Fetch(SysLogsFetcherCallback request) { |
| 47 request_ = request; | |
| 69 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); | 48 DebugDaemonClient* client = DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 70 pending_requests_ = 4; | 49 pending_requests_ = 4; |
| 71 // Note that this use of base::Unretained(this) is safe, since |this| is | 50 // 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 | 51 // deleted in RequestCompleted if and only if all outstanding callbacks are |
| 73 // done. | 52 // done. |
| 53 base::Unretained(this); | |
|
rkc1
2012/08/04 01:15:05
Get rid of this; not sure if it's needed.
tudalex(Chromium)
2012/08/05 01:15:12
Done.
| |
| 74 client->GetRoutes(true, false, | 54 client->GetRoutes(true, false, |
| 75 base::Bind(&DebugDaemonLogFetcher::GotRoutes, | 55 base::Bind(&DebugDaemonLogFetcher::GotRoutes, |
| 76 weak_ptr_factory_.GetWeakPtr())); | 56 weak_ptr_factory_.GetWeakPtr())); |
| 77 client->GetNetworkStatus( | 57 client->GetNetworkStatus( |
| 78 base::Bind(&DebugDaemonLogFetcher::GotNetworkStatus, | 58 base::Bind(&DebugDaemonLogFetcher::GotNetworkStatus, |
| 79 weak_ptr_factory_.GetWeakPtr())); | 59 weak_ptr_factory_.GetWeakPtr())); |
| 80 client->GetModemStatus( | 60 client->GetModemStatus( |
| 81 base::Bind(&DebugDaemonLogFetcher::GotModemStatus, | 61 base::Bind(&DebugDaemonLogFetcher::GotModemStatus, |
| 82 weak_ptr_factory_.GetWeakPtr())); | 62 weak_ptr_factory_.GetWeakPtr())); |
| 83 client->GetAllLogs( | 63 client->GetAllLogs( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 // couldn't fetch any of it, none of the fields will even appear. | 102 // 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(); | 103 for (std::map<std::string, std::string>::const_iterator it = logs.begin(); |
| 124 it != logs.end(); ++it) | 104 it != logs.end(); ++it) |
| 125 (*response_)[it->first] = it->second; | 105 (*response_)[it->first] = it->second; |
| 126 RequestCompleted(); | 106 RequestCompleted(); |
| 127 } | 107 } |
| 128 | 108 |
| 129 void DebugDaemonLogFetcher::RequestCompleted() { | 109 void DebugDaemonLogFetcher::RequestCompleted() { |
| 130 if (--pending_requests_) | 110 if (--pending_requests_) |
| 131 return; | 111 return; |
| 132 request_->ForwardResult(response_); | 112 request_.Run(response_); |
| 133 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | 113 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); |
| 134 } | 114 } |
| 135 | 115 |
| 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 | 116 } // namespace system |
| 161 } // namespace chromeos | 117 } // namespace chromeos |
| OLD | NEW |